Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/usb Split the u3g driver into two parts: u3ginit att...



details:   https://anonhg.NetBSD.org/src/rev/550c9a6a463d
branches:  trunk
changeset: 750564:550c9a6a463d
user:      martin <martin%NetBSD.org@localhost>
date:      Thu Jan 07 00:15:20 2010 +0000

description:
Split the u3g driver into two parts: u3ginit attaches to those devices
that only come as a umass device in the default configuration and
forces them to reinitialize in 3D mode and detach.
The u3g part attaches to individual interfaces for the 3G functionality,
leaving the umass interface(s) for that driver.
With this change I can use the MMC card in my Huawey stick (as well as
the integrated windows driver CD, which of course is pretty useless) and
the 3G modem at the same time.
Fixes PR 42577.
Code contributed anonymously, minor tweaks (and all bugs) by me.

diffstat:

 sys/dev/usb/files.usb |   10 +-
 sys/dev/usb/u3g.c     |  727 +++++++++++++++++++++++++++++++++++++------------
 2 files changed, 547 insertions(+), 190 deletions(-)

diffs (truncated from 927 to 300 lines):

diff -r 6fdecf8e2209 -r 550c9a6a463d sys/dev/usb/files.usb
--- a/sys/dev/usb/files.usb     Wed Jan 06 20:37:56 2010 +0000
+++ b/sys/dev/usb/files.usb     Thu Jan 07 00:15:20 2010 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.usb,v 1.93 2009/12/23 01:04:45 pooka Exp $
+#      $NetBSD: files.usb,v 1.94 2010/01/07 00:15:20 martin Exp $
 #
 # Config file and device description for machine-independent USB code.
 # Included by ports that need it.  Ports that use it must provide
@@ -169,10 +169,12 @@
 attach ugensa at usbdevif
 file   dev/usb/ugensa.c                ugensa
 
-# Generic Serial Adapter
+# 3G Modem
 device u3g: ucombus
-attach u3g at usbdevif
-file   dev/usb/u3g.c                   u3g
+attach u3g at usbifif
+device u3ginit
+attach u3ginit at usbdevif
+file   dev/usb/u3g.c                   u3g | u3ginit
 
 # YAP phone firmware loader
 device uyap: ezload
diff -r 6fdecf8e2209 -r 550c9a6a463d sys/dev/usb/u3g.c
--- a/sys/dev/usb/u3g.c Wed Jan 06 20:37:56 2010 +0000
+++ b/sys/dev/usb/u3g.c Thu Jan 07 00:15:20 2010 +0000
@@ -1,4 +1,32 @@
-/*     $NetBSD: u3g.c,v 1.8 2009/11/12 19:52:14 dyoung Exp $   */
+/*     $NetBSD: u3g.c,v 1.9 2010/01/07 00:15:20 martin Exp $   */
+
+/*-
+ * Copyright (c) 2009 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
 
 /*
  * Copyright (c) 2008 AnyWi Technologies
@@ -22,20 +50,15 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: u3g.c,v 1.8 2009/11/12 19:52:14 dyoung Exp $");
+__KERNEL_RCSID(0, "$NetBSD: u3g.c,v 1.9 2010/01/07 00:15:20 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/kernel.h>
 #include <sys/malloc.h>
-#include <sys/module.h>
 #include <sys/bus.h>
-#include <sys/ioccom.h>
-#include <sys/fcntl.h>
 #include <sys/conf.h>
 #include <sys/tty.h>
-#include <sys/file.h>
-#include <sys/selinfo.h>
 
 #include <dev/usb/usb.h>
 #include <dev/usb/usbdi.h>
@@ -46,38 +69,116 @@
 
 #include "usbdevs.h"
 
-#define U3GBUFSZ        1024
-#define U3G_MAXPORTS           4
+/*
+ * We read/write data from/to the device in 4KB chunks to maximise
+ * performance.
+ */
+#define U3G_BUFF_SIZE  4096
+
+/*
+ * Some 3G devices (the Huawei E160/E220 springs to mind here) buffer up
+ * data internally even when the USB pipes are closed. So on first open,
+ * we can receive a large chunk of stale data.
+ *
+ * This causes a real problem because the default TTYDEF_LFLAG (applied
+ * on first open) has the ECHO flag set, resulting in all the stale data
+ * being echoed straight back to the device by the tty(4) layer. Some
+ * devices (again, the Huawei E160/E220 for example) react to this spew
+ * by going catatonic.
+ *
+ * All this happens before the application gets a chance to disable ECHO.
+ *
+ * We work around this by ignoring all data received from the device for
+ * a period of two seconds, or until the application starts sending data -
+ * whichever comes first.
+ */
+#define        U3G_PURGE_SECS  2
+
+/*
+ * Define bits for the virtual modem control pins.
+ * The input pin states are reported via the interrupt pipe on some devices.
+ */
+#define        U3G_OUTPIN_DTR  (1u << 0)
+#define        U3G_OUTPIN_RTS  (1u << 1)
+#define        U3G_INPIN_DCD   (1u << 0)
+#define        U3G_INPIN_DSR   (1u << 1)
+#define        U3G_INPIN_RI    (1u << 3)
+
+/*
+ * USB request to set the output pin status
+ */
+#define        U3G_SET_PIN     0x22
 
 struct u3g_softc {
-       device_t                    sc_ucom[U3G_MAXPORTS];
-       device_t                    sc_dev;
-       usbd_device_handle          sc_udev;
-       u_char                      sc_msr;
-       u_char                      sc_lsr;
-       u_char                      numports;
+       device_t                sc_dev;
+       usbd_device_handle      sc_udev;
+       bool                    sc_dying;       /* We're going away */
+
+       device_t                sc_ucom;        /* Child ucom(4) handle */
+       int                     sc_ifaceno;     /* Device interface number */
 
-       usbd_interface_handle       sc_intr_iface;   /* interrupt interface */
-#ifdef U3G_DEBUG
-       int                         sc_intr_number;  /* interrupt number */
-       usbd_pipe_handle            sc_intr_pipe;    /* interrupt pipe */
-       u_char                      *sc_intr_buf;    /* interrupt buffer */
-#endif
-       int                         sc_isize;
-       bool                        sc_pseudodev;
+       bool                    sc_open;        /* Device is in use */
+       bool                    sc_purging;     /* Purging stale data */
+       struct timeval          sc_purge_start; /* Control duration of purge */
+
+       u_char                  sc_msr;         /* Emulated 'msr' */
+       uint16_t                sc_outpins;     /* Output pin state */
+
+       usbd_pipe_handle        sc_intr_pipe;   /* Interrupt pipe */
+       u_char                  *sc_intr_buff;  /* Interrupt buffer */
 };
 
+/*
+ * The device driver has two personalities. The first uses the 'usbdevif'
+ * interface attribute so that a match will claim the entire USB device
+ * for itself. This is used for when a device needs to be mode-switched
+ * and ensures any other interfaces present cannot be claimed by other
+ * drivers while the mode-switch is in progress.
+ *
+ * The second personality uses the 'usbifif' interface attribute so that
+ * it can claim the 3G modem interfaces for itself, leaving others (such
+ * as the mass storage interfaces on some devices) for other drivers.
+ */
+static int u3ginit_match(device_t, cfdata_t, void *);
+static void u3ginit_attach(device_t, device_t, void *);
+static int u3ginit_detach(device_t, int);
+
+CFATTACH_DECL2_NEW(u3ginit, 0, u3ginit_match,
+    u3ginit_attach, u3ginit_detach, NULL, NULL, NULL);
+
+
+static int u3g_match(device_t, cfdata_t, void *);
+static void u3g_attach(device_t, device_t, void *);
+static int u3g_detach(device_t, int);
+static int u3g_activate(device_t, enum devact);
+static void u3g_childdet(device_t, device_t);
+
+CFATTACH_DECL2_NEW(u3g, sizeof(struct u3g_softc), u3g_match,
+    u3g_attach, u3g_detach, u3g_activate, NULL, u3g_childdet);
+
+
+static void u3g_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
+static void u3g_get_status(void *, int, u_char *, u_char *);
+static void u3g_set(void *, int, int, int);
+static int  u3g_open(void *, int);
+static void u3g_close(void *, int);
+static void u3g_read(void *, int, u_char **, uint32_t *);
+static void u3g_write(void *, int, u_char *, u_char *, u_int32_t *);
+
 struct ucom_methods u3g_methods = {
-       NULL,
-       NULL,
-       NULL,
+       u3g_get_status,
+       u3g_set,
        NULL,
        NULL,
-       NULL,
-       NULL,
-       NULL,
+       u3g_open,
+       u3g_close,
+       u3g_read,
+       u3g_write,
 };
 
+/*
+ * Allegedly supported devices
+ */
 static const struct usb_devno u3g_devs[] = {
        /* OEM: Option N.V. */
        { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADPLUSUMTS },
@@ -137,17 +238,8 @@
        { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8781 },
 };
 
-#ifdef U3G_DEBUG
-static void
-u3g_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
-{
-       struct u3g_softc *sc = (struct u3g_softc *)priv;
-       aprint_normal_dev(sc->sc_dev, "INTERRUPT CALLBACK\n");
-}
-#endif
-
 static int
-u3g_novatel_reinit(struct usb_attach_arg *uaa)
+u3g_novatel_reinit(usbd_device_handle dev)
 {
        unsigned char cmd[31];
        usbd_interface_handle iface;
@@ -180,13 +272,13 @@
 
 
        /* Move the device into the configured state. */
-       err = usbd_set_config_index(uaa->device, 0, 0);
+       err = usbd_set_config_index(dev, 0, 0);
        if (err) {
                aprint_error("u3g: failed to set configuration index\n");
                return UMATCH_NONE;
        }
 
-       err = usbd_device2interface_handle(uaa->device, 0, &iface);
+       err = usbd_device2interface_handle(dev, 0, &iface);
        if (err != 0) {
                aprint_error("u3g: failed to get interface\n");
                return UMATCH_NONE;
@@ -215,7 +307,7 @@
                return UMATCH_NONE;
        }
 
-       xfer = usbd_alloc_xfer(uaa->device);
+       xfer = usbd_alloc_xfer(dev);
        if (xfer != NULL) {
                usbd_setup_xfer(xfer, pipe, NULL, cmd, sizeof(cmd),
                    USBD_SYNCHRONOUS, USBD_DEFAULT_TIMEOUT, NULL);
@@ -238,7 +330,8 @@
 static int
 u3g_huawei_reinit(usbd_device_handle dev)
 {
-       /* The Huawei device presents itself as a umass device with Windows
+       /*
+        * The Huawei device presents itself as a umass device with Windows
         * drivers on it. After installation of the driver, it reinits into a
         * 3G serial device.
         */
@@ -247,12 +340,33 @@
 
        /* Get the config descriptor */
        cdesc = usbd_get_config_descriptor(dev);
-       if (cdesc == NULL)
-               return (UMATCH_NONE);
+       if (cdesc == NULL) {
+               usb_device_descriptor_t dd;
+
+               if (usbd_get_device_desc(dev, &dd) != 0)
+                       return (UMATCH_NONE);
+
+               if (dd.bNumConfigurations != 1)
+                       return (UMATCH_NONE);
+
+               if (usbd_set_config_index(dev, 0, 1) != 0)
+                       return (UMATCH_NONE);
+
+               cdesc = usbd_get_config_descriptor(dev);
 
-       /* One iface means umass mode, more than 1 (4 usually) means 3G mode */
+               if (cdesc == NULL)
+                       return (UMATCH_NONE);



Home | Main Index | Thread Index | Old Index