]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/usb/u3g.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / usb / 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  * Notes:
24  * - The detour through the tty layer is ridiculously expensive wrt buffering
25  *   due to the high speeds.
26  *   We should consider adding a simple r/w device which allows attaching of PPP
27  *   in a more efficient way.
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/ioccom.h>
37 #include <sys/fcntl.h>
38 #include <sys/conf.h>
39 #include <sys/tty.h>
40 #include <sys/file.h>
41 #include <sys/selinfo.h>
42
43 #include <dev/usb/usb.h>
44 #include <dev/usb/usbdi.h>
45 #include <dev/usb/usbdi_util.h>
46
47 #include <dev/usb/ucomvar.h>
48
49 #if __FreeBSD_version >= 800000
50 #include "opt_u3g.h"
51 #endif
52 #include "usbdevs.h"
53
54 //#define U3G_DEBUG
55 #ifdef U3G_DEBUG
56 #define DPRINTF(x...)           do { if (u3gdebug) device_printf(sc->sc_dev, ##x); } while (0)
57 int     u3gdebug = 1;
58 #else
59 #define DPRINTF(x...)           /* nop */
60 #endif
61
62 #define U3G_MAXPORTS            4
63 #define U3G_CONFIG_INDEX        0
64
65 struct u3g_softc {
66         struct ucom_softc       sc_ucom[U3G_MAXPORTS];
67         device_t                sc_dev;
68         usbd_device_handle      sc_udev;
69         u_int8_t                sc_speed;
70         u_int8_t                sc_flags;
71         u_char                  sc_numports;
72 };
73
74 static int u3g_open(void *addr, int portno);
75 static void u3g_close(void *addr, int portno);
76
77 struct ucom_callback u3g_callback = {
78         NULL,
79         NULL,
80         NULL,
81         NULL,
82         u3g_open,
83         u3g_close,
84         NULL,
85         NULL,
86 };
87
88
89 struct u3g_speeds_s {
90         u_int32_t               ispeed;
91         u_int32_t               ospeed;
92 };
93
94 static const struct u3g_speeds_s u3g_speeds[] = {
95 #define U3GSP_GPRS      0
96         {64000,         64000},
97 #define U3GSP_EDGE      1
98         {384000,        64000},
99 #define U3GSP_CDMA      2
100         {384000,        64000},
101 #define U3GSP_UMTS      3
102         {384000,        64000},
103 #define U3GSP_HSDPA     4
104         {1200000,       384000},
105 #define U3GSP_HSUPA     5
106         {1200000,       384000},
107 #define U3GSP_HSPA      6
108         {7200000,       384000},
109 };
110
111 #define U3GIBUFSIZE     1024
112 #define U3GOBUFSIZE     1024
113
114 /*
115  * Various supported device vendors/products.
116  */
117 struct u3g_dev_type_s {
118         struct usb_devno        devno;
119         u_int8_t                speed;
120         u_int8_t                flags;
121 #define U3GFL_NONE              0x00
122 #define U3GFL_HUAWEI_INIT       0x01            // Requires init command (Huawei cards)
123 #define U3GFL_SCSI_EJECT        0x02            // Requires SCSI eject command (Novatel)
124 #define U3GFL_SIERRA_INIT       0x04            // Requires init command (Sierra cards)
125 #define U3GFL_CMOTECH_INIT      0x08            // Requires init command (CMOTECH cards)
126 #define U3GFL_STUB_WAIT         0x80            // Device reappears after a short delay
127 };
128
129 // Note: The entries marked with XXX should be checked for the correct speed
130 // indication to set the buffer sizes.
131 static const struct u3g_dev_type_s u3g_devs[] = {
132         /* OEM: Option */
133         {{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3G },                U3GSP_UMTS,     U3GFL_NONE },
134         {{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GQUAD },            U3GSP_UMTS,     U3GFL_NONE },
135         {{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GPLUS },            U3GSP_UMTS,     U3GFL_NONE },
136         {{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GTMAX36 },             U3GSP_HSDPA,    U3GFL_NONE },
137         {{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GTMAXHSUPA },          U3GSP_HSDPA,    U3GFL_NONE },
138         {{ USB_VENDOR_OPTION, USB_PRODUCT_OPTION_VODAFONEMC3G },        U3GSP_UMTS,     U3GFL_NONE },
139         /* OEM: Qualcomm, Inc. */
140         {{ USB_VENDOR_QUALCOMMINC, USB_PRODUCT_QUALCOMMINC_ZTE_STOR },  U3GSP_CDMA,     U3GFL_SCSI_EJECT },
141         {{ USB_VENDOR_QUALCOMMINC, USB_PRODUCT_QUALCOMMINC_CDMA_MSM },  U3GSP_CDMA,     U3GFL_SCSI_EJECT },
142         /* OEM: Huawei */
143         {{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE },              U3GSP_HSDPA,    U3GFL_HUAWEI_INIT },
144         {{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 },                U3GSP_HSPA,     U3GFL_HUAWEI_INIT },
145         /* OEM: Novatel */
146         {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_CDMA_MODEM },        U3GSP_CDMA,     U3GFL_SCSI_EJECT },
147         {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_ES620 },             U3GSP_UMTS,     U3GFL_SCSI_EJECT },     // XXX
148         {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_MC950D },            U3GSP_HSUPA,    U3GFL_SCSI_EJECT },
149         {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U720 },              U3GSP_UMTS,     U3GFL_SCSI_EJECT },     // XXX
150         {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U727 },              U3GSP_UMTS,     U3GFL_SCSI_EJECT },     // XXX
151         {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U740 },              U3GSP_HSDPA,    U3GFL_SCSI_EJECT },
152         {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U740_2 },            U3GSP_HSDPA,    U3GFL_SCSI_EJECT },
153         {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U870 },              U3GSP_UMTS,     U3GFL_SCSI_EJECT },     // XXX
154         {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V620 },              U3GSP_UMTS,     U3GFL_SCSI_EJECT },     // XXX
155         {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V640 },              U3GSP_UMTS,     U3GFL_SCSI_EJECT },     // XXX
156         {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V720 },              U3GSP_UMTS,     U3GFL_SCSI_EJECT },     // XXX
157         {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V740 },              U3GSP_HSDPA,    U3GFL_SCSI_EJECT },
158         {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_X950D },             U3GSP_HSUPA,    U3GFL_SCSI_EJECT },
159         {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_XU870 },             U3GSP_HSDPA,    U3GFL_SCSI_EJECT },
160         {{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_ZEROCD },            U3GSP_HSUPA,    U3GFL_SCSI_EJECT },
161         {{ USB_VENDOR_DELL,    USB_PRODUCT_DELL_U740 },                 U3GSP_HSDPA,    U3GFL_SCSI_EJECT },
162         /* OEM: Merlin */
163         {{ USB_VENDOR_MERLIN, USB_PRODUCT_MERLIN_V620 },                U3GSP_UMTS,     U3GFL_NONE },           // XXX
164         /* OEM: Sierra Wireless: */
165         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD580 },          U3GSP_UMTS,     U3GFL_NONE },           // XXX
166         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD595 },          U3GSP_UMTS,     U3GFL_NONE },           // XXX
167         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC595U },              U3GSP_UMTS,     U3GFL_NONE },           // XXX
168         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC597E },              U3GSP_UMTS,     U3GFL_NONE },           // XXX
169         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_C597 },                U3GSP_UMTS,     U3GFL_NONE },           // XXX
170         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880 },               U3GSP_UMTS,     U3GFL_NONE },           // XXX
171         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880E },              U3GSP_UMTS,     U3GFL_NONE },           // XXX
172         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880U },              U3GSP_UMTS,     U3GFL_NONE },           // XXX
173         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881 },               U3GSP_UMTS,     U3GFL_NONE },           // XXX
174         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881E },              U3GSP_UMTS,     U3GFL_NONE },           // XXX
175         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881U },              U3GSP_UMTS,     U3GFL_NONE },           // XXX
176         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 },              U3GSP_UMTS,     U3GFL_NONE },           // XXX
177         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720 },              U3GSP_UMTS,     U3GFL_NONE },           // XXX
178         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720_2 },            U3GSP_UMTS,     U3GFL_NONE },           // XXX
179         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5725 },              U3GSP_UMTS,     U3GFL_NONE },           // XXX
180         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MINI5725 },            U3GSP_UMTS,     U3GFL_NONE },           // XXX
181         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875 },          U3GSP_UMTS,     U3GFL_NONE },           // XXX
182         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755 },              U3GSP_UMTS,     U3GFL_NONE },           // XXX
183         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_2 },            U3GSP_UMTS,     U3GFL_NONE },           // XXX
184         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_3 },            U3GSP_UMTS,     U3GFL_NONE },           // XXX
185         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8765 },              U3GSP_UMTS,     U3GFL_NONE },           // XXX
186         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC875U },              U3GSP_UMTS,     U3GFL_NONE },           // XXX
187         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8775_2 },            U3GSP_HSDPA,    U3GFL_NONE },           // XXX
188         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8780 },              U3GSP_UMTS,     U3GFL_NONE },           // XXX
189         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8781 },              U3GSP_UMTS,     U3GFL_NONE },           // XXX
190         {{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_TRUINSTALL },          U3GSP_UMTS,     U3GFL_SIERRA_INIT },
191         {{ USB_VENDOR_HP, USB_PRODUCT_HP_HS2300 },                      U3GSP_HSDPA,    U3GFL_NONE },
192         /* OEM: CMOTECH */
193         {{ USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CGU628 },            U3GSP_HSDPA,    U3GFL_CMOTECH_INIT },
194         {{ USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_DISK },              U3GSP_HSDPA,    U3GFL_NONE },
195 };
196 #define u3g_lookup(v, p) ((const struct u3g_dev_type_s *)usb_lookup(u3g_devs, v, p))
197
198 static int
199 u3g_match(device_t self)
200 {
201         struct usb_attach_arg *uaa = device_get_ivars(self);
202         const struct u3g_dev_type_s *u3g_dev_type;
203
204         if (!uaa->iface)
205                 return UMATCH_NONE;
206
207         u3g_dev_type = u3g_lookup(uaa->vendor, uaa->product);
208         if (!u3g_dev_type)
209                 return UMATCH_NONE;
210
211         if (u3g_dev_type->flags&U3GFL_HUAWEI_INIT) {
212                 /* If the interface class of the first interface is no longer
213                  * mass storage the card has changed to modem (see u3g_attach()
214                  * below).
215                  */
216                 usb_interface_descriptor_t *id;
217                 id = usbd_get_interface_descriptor(uaa->iface);
218                 if (!id || id->bInterfaceClass == UICLASS_MASS)
219                         return UMATCH_NONE;
220         }
221
222         return UMATCH_VENDOR_PRODUCT_CONF_IFACE;
223 }
224
225 static int
226 u3g_attach(device_t self)
227 {
228         struct u3g_softc *sc = device_get_softc(self);
229         struct usb_attach_arg *uaa = device_get_ivars(self);
230         const struct u3g_dev_type_s *u3g_dev_type;
231         usbd_device_handle dev = uaa->device;
232         usb_interface_descriptor_t *id;
233         usb_endpoint_descriptor_t *ed;
234         int i, n; 
235         usb_config_descriptor_t *cd;
236         char devnamefmt[32];
237
238 #if __FreeBSD_version < 700000
239         char *devinfo = malloc(1024, M_USBDEV, M_WAITOK);
240         usbd_devinfo(dev, 0, devinfo);
241         device_printf(self, "%s\n", devinfo);
242         free(devinfo, M_USBDEV);
243 #endif
244
245         /* get the config descriptor */
246         cd = usbd_get_config_descriptor(dev);
247         if (cd == NULL) {
248                 device_printf(self, "failed to get configuration descriptor\n");
249                 return ENXIO;
250         }
251
252         sc->sc_dev = self;
253         sc->sc_udev = dev;
254
255         u3g_dev_type = u3g_lookup(uaa->vendor, uaa->product);
256         sc->sc_flags = u3g_dev_type->flags;
257         sc->sc_speed = u3g_dev_type->speed;
258
259         sprintf(devnamefmt,"U%d.%%d", device_get_unit(self));
260         int portno = 0;
261         for (i = 0; i < uaa->nifaces && portno < U3G_MAXPORTS; i++) {
262                 if (uaa->ifaces[i] == NULL)
263                         continue;
264
265                 id = usbd_get_interface_descriptor(uaa->ifaces[i]);
266                 if (id && id->bInterfaceClass == UICLASS_MASS) {
267                         /* We attach to the interface instead of the device as
268                          * some devices have a built-in SD card reader.
269                          * Claim the first umass device (cdX) as it contains
270                          * only Windows drivers anyway (CD-ROM), hiding it.
271                          */
272 #ifndef U3G_DEBUG
273                         if (!bootverbose)
274                                 if (uaa->vendor == USB_VENDOR_HUAWEI)
275                                         if (id->bInterfaceNumber == 2)
276                                                 uaa->ifaces[i] = NULL;
277 #endif
278                         continue;
279                 }
280
281                 int bulkin_no = -1, bulkout_no = -1;
282                 int claim_iface = 0;
283                 for (n = 0; n < id->bNumEndpoints && portno < U3G_MAXPORTS; n++) {
284                         ed = usbd_interface2endpoint_descriptor(uaa->ifaces[i], n);
285                         if (ed == NULL)
286                                 continue;
287                         if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
288                             && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
289                                 bulkin_no = ed->bEndpointAddress;
290                         else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
291                                  && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
292                                 bulkout_no = ed->bEndpointAddress;
293
294                         /* If we have found a pair of bulk-in/-out endpoints
295                          * create a serial port for it. Note: We assume that
296                          * the bulk-in and bulk-out endpoints appear in pairs.
297                          */
298                         if (bulkin_no != -1 && bulkout_no != -1) {
299                                 struct ucom_softc *ucom = &sc->sc_ucom[portno];
300
301                                 ucom->sc_dev = self;
302                                 ucom->sc_udev = dev;
303                                 ucom->sc_iface = uaa->ifaces[i];
304                                 ucom->sc_bulkin_no = bulkin_no;
305                                 ucom->sc_bulkout_no = bulkout_no;
306                                 ucom->sc_ibufsize = U3GIBUFSIZE;
307                                 ucom->sc_ibufsizepad = U3GIBUFSIZE;
308                                 ucom->sc_obufsize = U3GOBUFSIZE;
309                                 ucom->sc_opkthdrlen = 0;
310
311                                 ucom->sc_callback = &u3g_callback;
312                                 ucom->sc_parent = sc;
313                                 ucom->sc_portno = portno;
314
315                                 DPRINTF("port=%d iface=%d in=0x%x out=0x%x\n",
316                                          portno, i,
317                                          ucom->sc_bulkin_no,
318                                          ucom->sc_bulkout_no);
319 #if __FreeBSD_version < 700000
320                                 ucom_attach_tty(ucom, MINOR_CALLOUT, devnamefmt, portno);
321 #elif __FreeBSD_version < 800000
322                                 ucom_attach_tty(ucom, TS_CALLOUT, devnamefmt, portno);
323 #else
324                                 ucom_attach_tty(ucom, devnamefmt, portno);
325 #endif
326
327                                 claim_iface = 1;
328                                 portno++;
329                                 bulkin_no = bulkout_no = -1;
330                         }
331                 }
332                 if (claim_iface)
333                         uaa->ifaces[i] = NULL;          // claim the interface
334         }
335         sc->sc_numports = portno;
336
337         device_printf(self, "configured %d serial ports (%s)\n",
338                       sc->sc_numports, devnamefmt);
339         return 0;
340 }
341
342 static int
343 u3g_detach(device_t self)
344 {
345         struct u3g_softc *sc = device_get_softc(self);
346         int rv = 0;
347         int i;
348
349         for (i = 0; i < sc->sc_numports; i++) {
350                 sc->sc_ucom[i].sc_dying = 1;
351                 rv = ucom_detach(&sc->sc_ucom[i]);
352                 if (rv != 0) {
353                         device_printf(self, "ucom_detach(U%d.%d\n", device_get_unit(self), i);
354                         return rv;
355                 }
356         }
357
358         return 0;
359 }
360
361 static int
362 u3g_open(void *addr, int portno)
363 {
364 #if __FreeBSD_version < 800000
365         /* Supply generous buffering for these cards to avoid disappointments
366          * when setting the speed incorrectly. Only do this for the first port
367          * assuming that the rest of the ports are used for diagnostics only
368          * anyway.
369          * Note: We abuse the fact that ucom sets the speed through
370          * ispeed/ospeed, not through ispeedwat/ospeedwat.
371          */
372         if (portno == 0) {
373                 struct u3g_softc *sc = addr;
374                 struct ucom_softc *ucom = &sc->sc_ucom[portno];
375                 struct tty *tp = ucom->sc_tty;
376
377                 tp->t_ispeedwat = u3g_speeds[sc->sc_speed].ispeed;
378                 tp->t_ospeedwat = u3g_speeds[sc->sc_speed].ospeed;
379
380                 /* Avoid excessive buffer sizes.
381                  * XXX The values here should be checked. Lower them and see
382                  * whether 'lost chars' messages appear.
383                  */
384                 if (tp->t_ispeedwat > 384000)
385                     tp->t_ispeedwat = 384000;
386                 if (tp->t_ospeedwat > 384000)
387                     tp->t_ospeedwat = 384000;
388
389                 ttsetwater(tp);
390         }
391 #endif
392
393         return 0;
394 }
395
396 static void
397 u3g_close(void *addr, int portno)
398 {
399 #if __FreeBSD_version < 800000
400         if (portno == 0) {      /* see u3g_open() */
401                 /* Reduce the buffers allocated above again */
402                 struct u3g_softc *sc = addr;
403                 struct ucom_softc *ucom = &sc->sc_ucom[portno];
404                 struct tty *tp = ucom->sc_tty;
405 #ifdef U3G_DEBUG
406                 device_t self = sc->sc_dev;
407 #endif
408
409                 tp->t_ispeedwat = (speed_t)-1;
410                 tp->t_ospeedwat = (speed_t)-1;
411
412                 ttsetwater(tp);
413         }
414 #endif
415 }       
416
417 static device_method_t u3g_methods[] = {
418         /* Device interface */
419         DEVMETHOD(device_probe, u3g_match),
420         DEVMETHOD(device_attach, u3g_attach),
421         DEVMETHOD(device_detach, u3g_detach),
422
423         { 0, 0 }
424 };
425
426 static driver_t u3g_driver = {
427         "ucom",
428         u3g_methods,
429         sizeof (struct u3g_softc)
430 };
431
432 DRIVER_MODULE(u3g, uhub, u3g_driver, ucom_devclass, usbd_driver_load, 0);
433 MODULE_DEPEND(u3g, usb, 1, 1, 1);
434 MODULE_DEPEND(u3g, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
435 MODULE_VERSION(u3g, 1);
436
437 /*******************************************************************
438  ****** Stub driver to hide devices that need to reinitialise ******
439  *******************************************************************/
440
441 struct u3gstub_softc {
442         device_t                sc_dev;
443         usbd_device_handle      sc_udev;
444         usbd_pipe_handle        sc_pipe_out, sc_pipe_in;
445         usbd_xfer_handle        sc_xfer;
446 };
447
448 static int
449 u3gstub_huawei_init(struct u3gstub_softc *sc, struct usb_attach_arg *uaa)
450 {
451         usb_device_request_t req;
452
453         req.bmRequestType = UT_WRITE_DEVICE;
454         req.bRequest = UR_SET_FEATURE;
455         USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
456         USETW(req.wIndex, UHF_PORT_SUSPEND);
457         USETW(req.wLength, 0);
458
459         (void) usbd_do_request(uaa->device, &req, 0);           /* ignore any error */
460
461         return 1;
462 }
463
464 static void
465 u3gstub_BBB_cb(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status err)
466 {
467         struct u3gstub_softc *sc = (struct u3gstub_softc *) priv;
468         unsigned char cmd[13];
469
470         if (err) {
471                 device_printf(sc->sc_dev,
472                               "Failed to send CD eject command to "
473                               "change to modem mode\n");
474         } else {
475                 usbd_setup_xfer(sc->sc_xfer, sc->sc_pipe_in, NULL, cmd, sizeof(cmd),
476                                 0, USBD_DEFAULT_TIMEOUT, NULL);
477                 int err = usbd_transfer(sc->sc_xfer) != USBD_NORMAL_COMPLETION;
478                 if (err != USBD_NORMAL_COMPLETION && err != USBD_IN_PROGRESS)
479                         DPRINTF("failed to start transfer (CSW)\n");
480         }
481 }
482
483 static int
484 u3gstub_scsi_eject(struct u3gstub_softc *sc, struct usb_attach_arg *uaa)
485 {
486         /* See definition of umass_bbb_cbw_t in sys/dev/usb/umass.c and struct
487          * scsi_start_stop_unit in sys/cam/scsi/scsi_all.h .
488          */
489         unsigned char cmd[31] = {
490             0x55, 0x53, 0x42, 0x43,     /* 0..3: Command Block Wrapper (CBW) signature */
491             0x01, 0x00, 0x00, 0x00,     /* 4..7: CBW Tag, unique 32-bit number */
492             0x00, 0x00, 0x00, 0x00,     /* 8..11: CBW Transfer Length, no data here */
493             0x00,                       /* 12: CBW Flag: output */
494             0x00,                       /* 13: CBW Lun */
495             0x06,                       /* 14: CBW Length */
496
497             0x1b,                       /* 15+0: opcode: SCSI START/STOP */
498             0x00,                       /* 15+1: byte2: Not immediate */
499             0x00, 0x00,                 /* 15+2..3: reserved */
500             0x02,                       /* 15+4: Load/Eject command */
501             0x00,                       /* 15+5: control */
502             0x00, 0x00, 0x00, 0x00,     /* 15+6..15: unused */
503             0x00, 0x00, 0x00, 0x00,
504             0x00, 0x00
505         };
506
507         usb_interface_descriptor_t *id;
508         usb_endpoint_descriptor_t *ed = NULL;
509         int i;
510
511
512         /* Find the bulk-out endpoints */
513         id = usbd_get_interface_descriptor(uaa->iface);
514         for (i = 0 ; i < id->bNumEndpoints; i++) {
515                 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
516                 if (ed != NULL
517                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
518                         if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT) {
519                                 if (usbd_open_pipe(uaa->iface,
520                                                    ed->bEndpointAddress,
521                                                    USBD_EXCLUSIVE_USE,
522                                                    &sc->sc_pipe_out)
523                                     != USBD_NORMAL_COMPLETION) {
524                                         DPRINTF("failed to open bulk-out pipe on endpoint %d\n",
525                                                 ed->bEndpointAddress);
526                                         return 0;
527                                 }
528                         } else {
529                                 if (usbd_open_pipe(uaa->iface,
530                                                    ed->bEndpointAddress,
531                                                    USBD_EXCLUSIVE_USE,
532                                                    &sc->sc_pipe_in)
533                                     != USBD_NORMAL_COMPLETION) {
534                                         DPRINTF("failed to open bulk-in pipe on endpoint %d\n",
535                                                 ed->bEndpointAddress);
536                                         return 0;
537                                 }
538                         }
539                 }
540                 if (sc->sc_pipe_out && sc->sc_pipe_in)
541                         break;
542         }
543
544         if (i == id->bNumEndpoints) {
545                 DPRINTF("failed to find bulk-out pipe\n");
546                 return 0;
547         }
548
549         sc->sc_xfer = usbd_alloc_xfer(uaa->device);
550         if (sc->sc_xfer == NULL) {
551                 DPRINTF("failed to allocate xfer\n");
552                 return 0;
553         }
554
555         usbd_setup_xfer(sc->sc_xfer, sc->sc_pipe_out, NULL, cmd, sizeof(cmd),
556                         0, USBD_DEFAULT_TIMEOUT, u3gstub_BBB_cb);
557         int err = usbd_transfer(sc->sc_xfer) != USBD_NORMAL_COMPLETION;
558         if (err != USBD_NORMAL_COMPLETION && err != USBD_IN_PROGRESS) {
559                 DPRINTF("failed to start transfer (CBW)\n");
560                 return 0;
561         }
562
563         return 1;
564 }
565
566 static int
567 u3gstub_cmotech_init(struct u3gstub_softc *sc, struct usb_attach_arg *uaa)
568 {
569         /* See definition of umass_bbb_cbw_t in sys/dev/usb/umass.c
570          * in sys/cam/scsi/scsi_all.h .
571          */
572         unsigned char cmd[31] = {
573             0x55, 0x53, 0x42, 0x43,     /* 0..3: Command Block Wrapper (CBW) signature */
574             0x01, 0x00, 0x00, 0x00,     /* 4..7: CBW Tag, unique 32-bit number */
575             0x00, 0x00, 0x00, 0x00,     /* 8..11: CBW Transfer Length, no data here */
576             0x80,                       /* 12: CBW Flag: output, so 0 */
577             0x00,                       /* 13: CBW Lun */
578             0x08,                       /* 14: CBW Length */
579
580             0xff,                       /* 15+0 */
581             0x52,                       /* 15+1 */
582             0x44,                       /* 15+2 */
583             0x45,                       /* 15+2 */
584             0x56,                       /* 15+4 */
585             0x43,                       /* 15+5 */
586             0x48,                       /* 15+5 */
587             0x47,                       /* 15+5 */
588             0x00, 0x00, 0x00, 0x00,     /* 15+8..15: unused */
589             0x00, 0x00, 0x00, 0x00
590         };
591
592         usb_interface_descriptor_t *id;
593         usb_endpoint_descriptor_t *ed = NULL;
594         int i;
595
596
597         /* Find the bulk-out endpoints */
598         id = usbd_get_interface_descriptor(uaa->iface);
599         for (i = 0 ; i < id->bNumEndpoints ; i++) {
600                 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
601                 if (ed != NULL
602                     && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
603                         if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT) {
604                                 if (usbd_open_pipe(uaa->iface,
605                                                    ed->bEndpointAddress,
606                                                    USBD_EXCLUSIVE_USE,
607                                                    &sc->sc_pipe_out)
608                                     != USBD_NORMAL_COMPLETION) {
609                                         DPRINTF("failed to open bulk-out pipe on endpoint %d\n",
610                                                 ed->bEndpointAddress);
611                                         return 0;
612                                 }
613                         } else {
614                                 if (usbd_open_pipe(uaa->iface,
615                                                    ed->bEndpointAddress,
616                                                    USBD_EXCLUSIVE_USE,
617                                                    &sc->sc_pipe_in)
618                                     != USBD_NORMAL_COMPLETION) {
619                                         DPRINTF("failed to open bulk-in pipe on endpoint %d\n",
620                                                 ed->bEndpointAddress);
621                                         return 0;
622                                 }
623                         }
624                 }
625                 if (sc->sc_pipe_out && sc->sc_pipe_in)
626                         break;
627         }
628
629         if (i == id->bNumEndpoints) {
630                 DPRINTF("failed to find bulk-out pipe\n");
631                 return 0;
632         }
633
634         sc->sc_xfer = usbd_alloc_xfer(uaa->device);
635         if (sc->sc_xfer == NULL) {
636                 DPRINTF("failed to allocate xfer\n");
637                 return 0;
638         }
639
640         usbd_setup_xfer(sc->sc_xfer, sc->sc_pipe_out, NULL, cmd, sizeof(cmd),
641                         0, USBD_DEFAULT_TIMEOUT, u3gstub_BBB_cb);
642         int err = usbd_transfer(sc->sc_xfer) != USBD_NORMAL_COMPLETION;
643         if (err != USBD_NORMAL_COMPLETION && err != USBD_IN_PROGRESS) {
644                 DPRINTF("failed to start transfer (CBW)\n");
645                 return 0;
646         }
647
648         return 1;
649 }
650
651 static int
652 u3gstub_sierra_init(struct u3gstub_softc *sc, struct usb_attach_arg *uaa)
653 {
654       usb_device_request_t req;
655
656       req.bmRequestType = UT_VENDOR;
657       req.bRequest = UR_SET_INTERFACE;
658       USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
659       USETW(req.wIndex, UHF_PORT_CONNECTION);
660       USETW(req.wLength, 0);
661
662       (void) usbd_do_request(uaa->device, &req, 0);             /* ignore any error */
663
664       return 1;
665 }
666
667 static int
668 u3gstub_match(device_t self)
669 {
670         struct usb_attach_arg *uaa = device_get_ivars(self);
671         const struct u3g_dev_type_s *u3g_dev_type;
672         usb_interface_descriptor_t *id;
673
674         /* This stub handles 3G modem devices (E220, Mobile, etc.) with
675          * auto-install flash disks for Windows/MacOSX on the first interface.
676          * After some command or some delay they change appearance to a modem.
677          */
678
679         if (!uaa->iface)
680                 return UMATCH_NONE;
681
682         u3g_dev_type = u3g_lookup(uaa->vendor, uaa->product);
683         if (!u3g_dev_type)
684                 return UMATCH_NONE;
685
686         if (u3g_dev_type->flags&U3GFL_HUAWEI_INIT
687             || u3g_dev_type->flags&U3GFL_SCSI_EJECT
688             || u3g_dev_type->flags&U3GFL_SIERRA_INIT
689             || u3g_dev_type->flags&U3GFL_CMOTECH_INIT
690             || u3g_dev_type->flags&U3GFL_STUB_WAIT) {
691                 /* We assume that if the first interface is still a mass
692                  * storage device the device has not yet changed appearance.
693                  */
694                 id = usbd_get_interface_descriptor(uaa->iface);
695                 if (id && id->bInterfaceNumber == 0
696                     && id->bInterfaceClass == UICLASS_MASS) {
697 #ifndef U3G_DEBUG
698                         if (!bootverbose)
699                                 device_quiet(self);
700 #endif
701
702                         return UMATCH_VENDOR_PRODUCT;
703                 }
704         }
705
706         return UMATCH_NONE;
707 }
708
709 static int
710 u3gstub_attach(device_t self)
711 {
712         struct u3gstub_softc *sc = device_get_softc(self);
713         struct usb_attach_arg *uaa = device_get_ivars(self);
714         const struct u3g_dev_type_s *u3g_dev_type;
715         int i;
716
717 #ifndef U3G_DEBUG
718         if (!bootverbose)
719                 device_quiet(self);
720 #endif
721
722         sc->sc_dev = self;
723         sc->sc_udev = uaa->device;
724
725         for (i = 0; i < uaa->nifaces; i++)
726                 uaa->ifaces[i] = NULL;          // claim all interfaces
727
728         u3g_dev_type = u3g_lookup(uaa->vendor, uaa->product);
729         if (u3g_dev_type->flags&U3GFL_HUAWEI_INIT) {
730                 if (bootverbose)
731                         device_printf(sc->sc_dev,
732                                       "changing Huawei modem to modem mode\n");
733                 if (!u3gstub_huawei_init(sc, uaa))
734                         return ENXIO;
735         } else if (u3g_dev_type->flags&U3GFL_SCSI_EJECT) {
736                 if (bootverbose)
737                         device_printf(sc->sc_dev, "sending CD eject command to "
738                                       "change to modem mode\n");
739                 if (!u3gstub_scsi_eject(sc, uaa))
740                         return ENXIO;
741         } else if (u3g_dev_type->flags&U3GFL_SIERRA_INIT) {
742                 if (bootverbose)
743                         device_printf(sc->sc_dev,
744                                       "changing Sierra modem to modem mode\n");
745                 if (!u3gstub_sierra_init(sc, uaa))
746                         return ENXIO;
747         } else if (u3g_dev_type->flags&U3GFL_CMOTECH_INIT) {
748                 if (bootverbose)
749                         device_printf(sc->sc_dev,
750                                       "changing CMOTECH modem to modem mode\n");
751                 if (!u3gstub_cmotech_init(sc, uaa))
752                         return ENXIO;
753         } else if (u3g_dev_type->flags&U3GFL_STUB_WAIT) {
754                 if (bootverbose)
755                         device_printf(sc->sc_dev, "waiting for modem to change "
756                                       "to modem mode\n");
757                 /* nop  */
758         }
759
760         return 0;
761 }
762
763 static int
764 u3gstub_detach(device_t self)
765 {
766         struct u3gstub_softc *sc = device_get_softc(self);
767         
768         if (sc->sc_xfer)
769                 usbd_free_xfer(sc->sc_xfer);
770
771         if (sc->sc_pipe_in) {
772                 usbd_abort_pipe(sc->sc_pipe_in);
773                 usbd_close_pipe(sc->sc_pipe_in);
774         }
775         if (sc->sc_pipe_out) {
776                 usbd_abort_pipe(sc->sc_pipe_out);
777                 usbd_close_pipe(sc->sc_pipe_out);
778         }
779
780         return 0;
781 }
782
783 static device_method_t u3gstub_methods[] = {
784         /* Device interface */
785         DEVMETHOD(device_probe, u3gstub_match),
786         DEVMETHOD(device_attach, u3gstub_attach),
787         DEVMETHOD(device_detach, u3gstub_detach),
788
789         { 0, 0 }
790 };
791
792 static driver_t u3gstub_driver = {
793         "u3g",
794         u3gstub_methods,
795         sizeof (struct u3gstub_softc)
796 };
797
798 DRIVER_MODULE(u3gstub, uhub, u3gstub_driver, ucom_devclass, usbd_driver_load, 0);
799 MODULE_DEPEND(u3gstub, usb, 1, 1, 1);