The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/usb/u3g.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 23864e4802459167374119b06ceb87e6


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.