USB-CDC: Fix tx/rx_req memory leaks
[platform/kernel/u-boot.git] / drivers / usb / gadget / ether.c
index 8126f76..8f5eec1 100644 (file)
@@ -89,8 +89,8 @@ unsigned packet_received, packet_sent;
 /* Based on linux 2.6.27 version */
 #define DRIVER_VERSION         "May Day 2005"
 
-static const char shortname [] = "ether";
-static const char driver_desc [] = DRIVER_DESC;
+static const char shortname[] = "ether";
+static const char driver_desc[] = DRIVER_DESC;
 
 #define RX_EXTRA       20              /* guard against rx overflows */
 
@@ -114,7 +114,7 @@ static inline int is_cdc(struct eth_dev *dev)
 {
 #if    !defined(DEV_CONFIG_SUBSET)
        return 1;               /* only cdc possible */
-#elif  !defined (DEV_CONFIG_CDC)
+#elif  !defined(DEV_CONFIG_CDC)
        return 0;               /* only subset possible */
 #else
        return dev->cdc;        /* depends on what hardware we found */
@@ -122,7 +122,7 @@ static inline int is_cdc(struct eth_dev *dev)
 }
 
 #define        subset_active(dev)      (!is_cdc(dev))
-#define        cdc_active(dev)         ( is_cdc(dev))
+#define        cdc_active(dev)         (is_cdc(dev))
 
 #define DEFAULT_QLEN   2       /* double buffering by default */
 
@@ -180,15 +180,16 @@ struct eth_dev {
        unsigned                zlp:1;
        unsigned                cdc:1;
        unsigned                suspended:1;
-       unsigned                network_started:1;
+       unsigned                network_started:1;
        u16                     cdc_filter;
        unsigned long           todo;
-       int                     mtu;
+       int                     mtu;
 #define        WORK_RX_MEMORY          0
-       u8                      host_mac [ETH_ALEN];
+       u8                      host_mac[ETH_ALEN];
 };
 
-/* This version autoconfigures as much as possible at run-time.
+/*
+ * This version autoconfigures as much as possible at run-time.
  *
  * It also ASSUMES a self-powered device, without remote wakeup,
  * although remote wakeup support would make sense.
@@ -196,17 +197,20 @@ struct eth_dev {
 
 /*-------------------------------------------------------------------------*/
 
-/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
+/*
+ * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
  * Instead:  allocate your own, using normal USB-IF procedures.
  */
 
-/* Thanks to NetChip Technologies for donating this product ID.
+/*
+ * Thanks to NetChip Technologies for donating this product ID.
  * It's for devices with only CDC Ethernet configurations.
  */
 #define CDC_VENDOR_NUM         0x0525  /* NetChip */
 #define CDC_PRODUCT_NUM                0xa4a1  /* Linux-USB Ethernet Gadget */
 
-/* For hardware that can't talk CDC, we use the same vendor ID that
+/*
+ * For hardware that can't talk CDC, we use the same vendor ID that
  * ARM Linux has used for ethernet-over-usb, both with sa1100 and
  * with pxa250.  We're protocol-compatible, if the host-side drivers
  * use the endpoint descriptors.  bcdDevice (version) is nonzero, so
@@ -220,7 +224,8 @@ struct eth_dev {
 #define        SIMPLE_VENDOR_NUM       0x049f
 #define        SIMPLE_PRODUCT_NUM      0x505a
 
-/* Some systems will want different product identifers published in the
+/*
+ * Some systems will want different product identifers published in the
  * device descriptor, either numbers or strings or both.  These string
  * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
  */
@@ -238,7 +243,8 @@ static char host_addr[18];
 
 /*-------------------------------------------------------------------------*/
 
-/* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
+/*
+ * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
  * ep0 implementation:  descriptors, config management, setup().
  * also optional class-specific notification interrupt transfer.
  */
@@ -276,14 +282,14 @@ device_desc = {
        .bLength =              sizeof device_desc,
        .bDescriptorType =      USB_DT_DEVICE,
 
-       .bcdUSB =               __constant_cpu_to_le16 (0x0200),
+       .bcdUSB =               __constant_cpu_to_le16(0x0200),
 
        .bDeviceClass =         USB_CLASS_COMM,
        .bDeviceSubClass =      0,
        .bDeviceProtocol =      0,
 
-       .idVendor =             __constant_cpu_to_le16 (CDC_VENDOR_NUM),
-       .idProduct =            __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
+       .idVendor =             __constant_cpu_to_le16(CDC_VENDOR_NUM),
+       .idProduct =            __constant_cpu_to_le16(CDC_PRODUCT_NUM),
        .iManufacturer =        STRING_MANUFACTURER,
        .iProduct =             STRING_PRODUCT,
        .bNumConfigurations =   1,
@@ -338,7 +344,7 @@ static const struct usb_cdc_header_desc header_desc = {
        .bDescriptorType =      USB_DT_CS_INTERFACE,
        .bDescriptorSubType =   USB_CDC_HEADER_TYPE,
 
-       .bcdCDC =               __constant_cpu_to_le16 (0x0110),
+       .bcdCDC =               __constant_cpu_to_le16(0x0110),
 };
 
 #if defined(DEV_CONFIG_CDC)
@@ -356,7 +362,8 @@ static const struct usb_cdc_union_desc union_desc = {
 
 #ifndef DEV_CONFIG_CDC
 
-/* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
+/*
+ * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
  * ways:  data endpoints live in the control interface, there's no data
  * interface, and it's not used to talk to a cell phone radio.
  */
@@ -373,7 +380,8 @@ static const struct usb_cdc_mdlm_desc mdlm_desc = {
        },
 };
 
-/* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
+/*
+ * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
  * can't really use its struct.  All we do here is say that we're using
  * the submode of "SAFE" which directly matches the CDC Subset.
  */
@@ -389,24 +397,23 @@ static const u8 mdlm_detail_desc[] = {
 
 #endif
 
-
 static const struct usb_cdc_ether_desc ether_desc = {
-       .bLength =              sizeof (ether_desc),
+       .bLength =              sizeof(ether_desc),
        .bDescriptorType =      USB_DT_CS_INTERFACE,
        .bDescriptorSubType =   USB_CDC_ETHERNET_TYPE,
 
        /* this descriptor actually adds value, surprise! */
        .iMACAddress =          STRING_ETHADDR,
-       .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
-       .wMaxSegmentSize =      __constant_cpu_to_le16 (ETH_FRAME_LEN),
-       .wNumberMCFilters =     __constant_cpu_to_le16 (0),
+       .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
+       .wMaxSegmentSize =      __constant_cpu_to_le16(ETH_FRAME_LEN),
+       .wNumberMCFilters =     __constant_cpu_to_le16(0),
        .bNumberPowerFilters =  0,
 };
 
-
 #if defined(DEV_CONFIG_CDC)
 
-/* include the status endpoint if we can, even where it's optional.
+/*
+ * include the status endpoint if we can, even where it's optional.
  * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
  * packet, to simplify cancellation; and a big transfer interval, to
  * waste less bandwidth.
@@ -427,7 +434,7 @@ fs_status_desc = {
 
        .bEndpointAddress =     USB_DIR_IN,
        .bmAttributes =         USB_ENDPOINT_XFER_INT,
-       .wMaxPacketSize =       __constant_cpu_to_le16 (STATUS_BYTECOUNT),
+       .wMaxPacketSize =       __constant_cpu_to_le16(STATUS_BYTECOUNT),
        .bInterval =            1 << LOG2_STATUS_INTERVAL_MSEC,
 };
 #endif
@@ -493,7 +500,6 @@ subset_data_intf = {
 
 #endif /* SUBSET */
 
-
 static struct usb_endpoint_descriptor
 fs_source_desc = {
        .bLength =              USB_DT_ENDPOINT_SIZE,
@@ -512,7 +518,7 @@ fs_sink_desc = {
        .bmAttributes =         USB_ENDPOINT_XFER_BULK,
 };
 
-static const struct usb_descriptor_header *fs_eth_function [11] = {
+static const struct usb_descriptor_header *fs_eth_function[11] = {
        (struct usb_descriptor_header *) &otg_descriptor,
 #ifdef DEV_CONFIG_CDC
        /* "cdc" mode descriptors */
@@ -560,7 +566,7 @@ hs_status_desc = {
        .bDescriptorType =      USB_DT_ENDPOINT,
 
        .bmAttributes =         USB_ENDPOINT_XFER_INT,
-       .wMaxPacketSize =       __constant_cpu_to_le16 (STATUS_BYTECOUNT),
+       .wMaxPacketSize =       __constant_cpu_to_le16(STATUS_BYTECOUNT),
        .bInterval =            LOG2_STATUS_INTERVAL_MSEC + 4,
 };
 #endif /* DEV_CONFIG_CDC */
@@ -571,7 +577,7 @@ hs_source_desc = {
        .bDescriptorType =      USB_DT_ENDPOINT,
 
        .bmAttributes =         USB_ENDPOINT_XFER_BULK,
-       .wMaxPacketSize =       __constant_cpu_to_le16 (512),
+       .wMaxPacketSize =       __constant_cpu_to_le16(512),
 };
 
 static struct usb_endpoint_descriptor
@@ -580,7 +586,7 @@ hs_sink_desc = {
        .bDescriptorType =      USB_DT_ENDPOINT,
 
        .bmAttributes =         USB_ENDPOINT_XFER_BULK,
-       .wMaxPacketSize =       __constant_cpu_to_le16 (512),
+       .wMaxPacketSize =       __constant_cpu_to_le16(512),
 };
 
 static struct usb_qualifier_descriptor
@@ -588,13 +594,13 @@ dev_qualifier = {
        .bLength =              sizeof dev_qualifier,
        .bDescriptorType =      USB_DT_DEVICE_QUALIFIER,
 
-       .bcdUSB =               __constant_cpu_to_le16 (0x0200),
+       .bcdUSB =               __constant_cpu_to_le16(0x0200),
        .bDeviceClass =         USB_CLASS_COMM,
 
        .bNumConfigurations =   1,
 };
 
-static const struct usb_descriptor_header *hs_eth_function [11] = {
+static const struct usb_descriptor_header *hs_eth_function[11] = {
        (struct usb_descriptor_header *) &otg_descriptor,
 #ifdef DEV_CONFIG_CDC
        /* "cdc" mode descriptors */
@@ -640,20 +646,19 @@ ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
        return fs;
 }
 
-
 /*-------------------------------------------------------------------------*/
 
 /* descriptors that are built on-demand */
 
-static char manufacturer [50];
-static char product_desc [40] = DRIVER_DESC;
-static char serial_number [20];
+static char manufacturer[50];
+static char product_desc[40] = DRIVER_DESC;
+static char serial_number[20];
 
 /* address that the host will use ... usually assigned at random */
-static char ethaddr [2 * ETH_ALEN + 1];
+static char ethaddr[2 * ETH_ALEN + 1];
 
 /* static strings, in UTF-8 */
-static struct usb_string               strings [] = {
+static struct usb_string               strings[] = {
        { STRING_MANUFACTURER,  manufacturer, },
        { STRING_PRODUCT,       product_desc, },
        { STRING_SERIALNUMBER,  serial_number, },
@@ -674,11 +679,9 @@ static struct usb_gadget_strings   stringtab = {
        .strings        = strings,
 };
 
-
 /*============================================================================*/
 static u8 control_req[USB_BUFSIZ];
-static u8 status_req[STATUS_BYTECOUNT];
-
+static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4)));
 
 
 /**
@@ -704,7 +707,6 @@ size_t strlcpy(char *dest, const char *src, size_t size)
        return ret;
 }
 
-
 /*============================================================================*/
 
 /*
@@ -730,13 +732,13 @@ config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
                return -EINVAL;
 
        config = &eth_config;
-       function = which_fn (eth);
+       function = which_fn(eth);
 
        /* for now, don't advertise srp-only devices */
        if (!is_otg)
                function++;
 
-       len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
+       len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
        if (len < 0)
                return len;
        ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
@@ -745,10 +747,10 @@ config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
 
 /*-------------------------------------------------------------------------*/
 
-static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
+static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
 
 static int
-set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
+set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
 {
        int                                     result = 0;
        struct usb_gadget                       *gadget = dev->gadget;
@@ -756,11 +758,11 @@ set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
 #if defined(DEV_CONFIG_CDC)
        /* status endpoint used for (optionally) CDC */
        if (!subset_active(dev) && dev->status_ep) {
-               dev->status = ep_desc (gadget, &hs_status_desc,
+               dev->status = ep_desc(gadget, &hs_status_desc,
                                                &fs_status_desc);
                dev->status_ep->driver_data = dev;
 
-               result = usb_ep_enable (dev->status_ep, dev->status);
+               result = usb_ep_enable(dev->status_ep, dev->status);
                if (result != 0) {
                        debug("enable %s --> %d\n",
                                dev->status_ep->name, result);
@@ -775,19 +777,20 @@ set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
        dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
        dev->out_ep->driver_data = dev;
 
-       /* With CDC,  the host isn't allowed to use these two data
+       /*
+        * With CDC,  the host isn't allowed to use these two data
         * endpoints in the default altsetting for the interface.
         * so we don't activate them yet.  Reset from SET_INTERFACE.
         */
        if (!cdc_active(dev)) {
-               result = usb_ep_enable (dev->in_ep, dev->in);
+               result = usb_ep_enable(dev->in_ep, dev->in);
                if (result != 0) {
                        debug("enable %s --> %d\n",
                                dev->in_ep->name, result);
                        goto done;
                }
 
-               result = usb_ep_enable (dev->out_ep, dev->out);
+               result = usb_ep_enable(dev->out_ep, dev->out);
                if (result != 0) {
                        debug("enable %s --> %d\n",
                                dev->out_ep->name, result);
@@ -797,15 +800,15 @@ set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
 
 done:
        if (result == 0)
-               result = alloc_requests (dev, qlen (gadget), gfp_flags);
+               result = alloc_requests(dev, qlen(gadget), gfp_flags);
 
        /* on error, disable any endpoints  */
        if (result < 0) {
-               if (!subset_active(dev))
-                       (void) usb_ep_disable (dev->status_ep);
+               if (!subset_active(dev) && dev->status_ep)
+                       (void) usb_ep_disable(dev->status_ep);
                dev->status = NULL;
-               (void) usb_ep_disable (dev->in_ep);
-               (void) usb_ep_disable (dev->out_ep);
+               (void) usb_ep_disable(dev->in_ep);
+               (void) usb_ep_disable(dev->out_ep);
                dev->in = NULL;
                dev->out = NULL;
        }
@@ -814,59 +817,61 @@ done:
        return result;
 }
 
-
-static void eth_reset_config (struct eth_dev *dev)
+static void eth_reset_config(struct eth_dev *dev)
 {
        if (dev->config == 0)
                return;
 
        debug("%s\n", __func__);
 
-       /* disable endpoints, forcing (synchronous) completion of
+       /*
+        * disable endpoints, forcing (synchronous) completion of
         * pending i/o.  then free the requests.
         */
 
        if (dev->in) {
-               usb_ep_disable (dev->in_ep);
+               usb_ep_disable(dev->in_ep);
                if (dev->tx_req) {
-                       usb_ep_free_request (dev->in_ep, dev->tx_req);
-                       dev->tx_req=NULL;
+                       usb_ep_free_request(dev->in_ep, dev->tx_req);
+                       dev->tx_req = NULL;
                }
        }
        if (dev->out) {
-               usb_ep_disable (dev->out_ep);
+               usb_ep_disable(dev->out_ep);
                if (dev->rx_req) {
-                       usb_ep_free_request (dev->out_ep, dev->rx_req);
-                       dev->rx_req=NULL;
+                       usb_ep_free_request(dev->out_ep, dev->rx_req);
+                       dev->rx_req = NULL;
                }
        }
-       if (dev->status) {
-               usb_ep_disable (dev->status_ep);
-       }
+       if (dev->status)
+               usb_ep_disable(dev->status_ep);
+
        dev->cdc_filter = 0;
        dev->config = 0;
 }
 
-/* change our operational config.  must agree with the code
+/*
+ * change our operational config.  must agree with the code
  * that returns config descriptors, and altsetting code.
  */
-static int eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
+static int eth_set_config(struct eth_dev *dev, unsigned number,
+                               gfp_t gfp_flags)
 {
        int                     result = 0;
        struct usb_gadget       *gadget = dev->gadget;
 
-       if (gadget_is_sa1100 (gadget)
+       if (gadget_is_sa1100(gadget)
                        && dev->config
                        && dev->tx_qlen != 0) {
                /* tx fifo is full, but we can't clear it...*/
                error("can't change configurations");
                return -ESPIPE;
        }
-       eth_reset_config (dev);
+       eth_reset_config(dev);
 
        switch (number) {
        case DEV_CONFIG_VALUE:
-               result = set_ether_config (dev, gfp_flags);
+               result = set_ether_config(dev, gfp_flags);
                break;
        default:
                result = -EINVAL;
@@ -877,7 +882,7 @@ static int eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags
 
        if (result) {
                if (number)
-                       eth_reset_config (dev);
+                       eth_reset_config(dev);
                usb_gadget_vbus_draw(dev->gadget,
                                gadget_is_otg(dev->gadget) ? 8 : 100);
        } else {
@@ -888,17 +893,20 @@ static int eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags
                usb_gadget_vbus_draw(dev->gadget, power);
 
                switch (gadget->speed) {
-               case USB_SPEED_FULL:    speed = "full"; break;
+               case USB_SPEED_FULL:
+                       speed = "full"; break;
 #ifdef CONFIG_USB_GADGET_DUALSPEED
-               case USB_SPEED_HIGH:    speed = "high"; break;
+               case USB_SPEED_HIGH:
+                       speed = "high"; break;
 #endif
-               default:                speed = "?"; break;
+               default:
+                       speed = "?"; break;
                }
 
                dev->config = number;
                printf("%s speed config #%d: %d mA, %s, using %s\n",
                                speed, number, power, driver_desc,
-                               (cdc_active(dev)? "CDC Ethernet"
+                               (cdc_active(dev) ? "CDC Ethernet"
                                                : "CDC Ethernet Subset"));
        }
        return result;
@@ -908,13 +916,14 @@ static int eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags
 
 #ifdef DEV_CONFIG_CDC
 
-/* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
+/*
+ * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
  * only to notify the host about link status changes (which we support) or
  * report completion of some encapsulated command.  Since
  * we want this CDC Ethernet code to be vendor-neutral, we don't use that
  * command mechanism; and only one status request is ever queued.
  */
-static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
+static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
 {
        struct usb_cdc_notification     *event = req->buf;
        int                             value = req->status;
@@ -927,38 +936,38 @@ static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
 
                event->bmRequestType = 0xA1;
                event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
-               event->wValue = __constant_cpu_to_le16 (0);
-               event->wIndex = __constant_cpu_to_le16 (1);
-               event->wLength = __constant_cpu_to_le16 (8);
+               event->wValue = __constant_cpu_to_le16(0);
+               event->wIndex = __constant_cpu_to_le16(1);
+               event->wLength = __constant_cpu_to_le16(8);
 
                /* SPEED_CHANGE data is up/down speeds in bits/sec */
-               data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
+               data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
 
                req->length = STATUS_BYTECOUNT;
-               value = usb_ep_queue (ep, req, GFP_ATOMIC);
+               value = usb_ep_queue(ep, req, GFP_ATOMIC);
                debug("send SPEED_CHANGE --> %d\n", value);
                if (value == 0)
                        return;
        } else if (value != -ECONNRESET) {
                debug("event %02x --> %d\n",
                        event->bNotificationType, value);
-               if (event->bNotificationType==
-                               USB_CDC_NOTIFY_SPEED_CHANGE)
-               {
-                       l_ethdev.network_started=1;
+               if (event->bNotificationType ==
+                               USB_CDC_NOTIFY_SPEED_CHANGE) {
+                       l_ethdev.network_started = 1;
                        printf("USB network up!\n");
                }
        }
        req->context = NULL;
 }
 
-static void issue_start_status (struct eth_dev *dev)
+static void issue_start_status(struct eth_dev *dev)
 {
        struct usb_request              *req = dev->stat_req;
        struct usb_cdc_notification     *event;
        int                             value;
 
-       /* flush old status
+       /*
+        * flush old status
         *
         * FIXME ugly idiom, maybe we'd be better with just
         * a "cancel the whole queue" primitive since any
@@ -967,24 +976,25 @@ static void issue_start_status (struct eth_dev *dev)
         *
         * FIXME iff req->context != null just dequeue it
         */
-       usb_ep_disable (dev->status_ep);
-       usb_ep_enable (dev->status_ep, dev->status);
+       usb_ep_disable(dev->status_ep);
+       usb_ep_enable(dev->status_ep, dev->status);
 
-       /* 3.8.1 says to issue first NETWORK_CONNECTION, then
+       /*
+        * 3.8.1 says to issue first NETWORK_CONNECTION, then
         * a SPEED_CHANGE.  could be useful in some configs.
         */
        event = req->buf;
        event->bmRequestType = 0xA1;
        event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
-       event->wValue = __constant_cpu_to_le16 (1);     /* connected */
-       event->wIndex = __constant_cpu_to_le16 (1);
+       event->wValue = __constant_cpu_to_le16(1);      /* connected */
+       event->wIndex = __constant_cpu_to_le16(1);
        event->wLength = 0;
 
        req->length = sizeof *event;
        req->complete = eth_status_complete;
        req->context = dev;
 
-       value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
+       value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
        if (value < 0)
                debug("status buf queue --> %d\n", value);
 }
@@ -993,7 +1003,7 @@ static void issue_start_status (struct eth_dev *dev)
 
 /*-------------------------------------------------------------------------*/
 
-static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
+static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
 {
        if (req->status || req->actual != req->length)
                debug("setup complete --> %d, %d/%d\n",
@@ -1010,16 +1020,17 @@ static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
  *  - class-specific control requests
  */
 static int
-eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
+eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
 {
-       struct eth_dev          *dev = get_gadget_data (gadget);
+       struct eth_dev          *dev = get_gadget_data(gadget);
        struct usb_request      *req = dev->req;
        int                     value = -EOPNOTSUPP;
        u16                     wIndex = le16_to_cpu(ctrl->wIndex);
        u16                     wValue = le16_to_cpu(ctrl->wValue);
        u16                     wLength = le16_to_cpu(ctrl->wLength);
 
-       /* descriptors just go into the pre-allocated ep0 buffer,
+       /*
+        * descriptors just go into the pre-allocated ep0 buffer,
         * while config change events may enable network traffic.
         */
 
@@ -1034,14 +1045,14 @@ eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
                switch (wValue >> 8) {
 
                case USB_DT_DEVICE:
-                       value = min (wLength, (u16) sizeof device_desc);
-                       memcpy (req->buf, &device_desc, value);
+                       value = min(wLength, (u16) sizeof device_desc);
+                       memcpy(req->buf, &device_desc, value);
                        break;
                case USB_DT_DEVICE_QUALIFIER:
                        if (!gadget_is_dualspeed(gadget))
                                break;
-                       value = min (wLength, (u16) sizeof dev_qualifier);
-                       memcpy (req->buf, &dev_qualifier, value);
+                       value = min(wLength, (u16) sizeof dev_qualifier);
+                       memcpy(req->buf, &dev_qualifier, value);
                        break;
 
                case USB_DT_OTHER_SPEED_CONFIG:
@@ -1054,15 +1065,15 @@ eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
                                        wValue & 0xff,
                                        gadget_is_otg(gadget));
                        if (value >= 0)
-                               value = min (wLength, (u16) value);
+                               value = min(wLength, (u16) value);
                        break;
 
                case USB_DT_STRING:
-                       value = usb_gadget_get_string (&stringtab,
+                       value = usb_gadget_get_string(&stringtab,
                                        wValue & 0xff, req->buf);
 
                        if (value >= 0)
-                               value = min (wLength, (u16) value);
+                               value = min(wLength, (u16) value);
 
                        break;
                }
@@ -1075,13 +1086,13 @@ eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
                        debug("HNP available\n");
                else if (gadget->a_alt_hnp_support)
                        debug("HNP needs a different root port\n");
-               value = eth_set_config (dev, wValue, GFP_ATOMIC);
+               value = eth_set_config(dev, wValue, GFP_ATOMIC);
                break;
        case USB_REQ_GET_CONFIGURATION:
                if (ctrl->bRequestType != USB_DIR_IN)
                        break;
                *(u8 *)req->buf = dev->config;
-               value = min (wLength, (u16) 1);
+               value = min(wLength, (u16) 1);
                break;
 
        case USB_REQ_SET_INTERFACE:
@@ -1092,11 +1103,12 @@ eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
                if (!cdc_active(dev) && wIndex != 0)
                        break;
 
-               /* PXA hardware partially handles SET_INTERFACE;
+               /*
+                * PXA hardware partially handles SET_INTERFACE;
                 * we need to kluge around that interference.
                 */
-               if (gadget_is_pxa (gadget)) {
-                       value = eth_set_config (dev, DEV_CONFIG_VALUE,
+               if (gadget_is_pxa(gadget)) {
+                       value = eth_set_config(dev, DEV_CONFIG_VALUE,
                                                GFP_ATOMIC);
                        goto done_set_intf;
                }
@@ -1107,36 +1119,38 @@ eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
                        if (wValue != 0)
                                break;
                        if (dev->status) {
-                               usb_ep_disable (dev->status_ep);
-                               usb_ep_enable (dev->status_ep, dev->status);
+                               usb_ep_disable(dev->status_ep);
+                               usb_ep_enable(dev->status_ep, dev->status);
                        }
                        value = 0;
                        break;
                case 1:         /* data intf */
                        if (wValue > 1)
                                break;
-                       usb_ep_disable (dev->in_ep);
-                       usb_ep_disable (dev->out_ep);
+                       usb_ep_disable(dev->in_ep);
+                       usb_ep_disable(dev->out_ep);
 
-                       /* CDC requires the data transfers not be done from
+                       /*
+                        * CDC requires the data transfers not be done from
                         * the default interface setting ... also, setting
                         * the non-default interface resets filters etc.
                         */
                        if (wValue == 1) {
-                               if (!cdc_active (dev))
+                               if (!cdc_active(dev))
                                        break;
-                               usb_ep_enable (dev->in_ep, dev->in);
-                               usb_ep_enable (dev->out_ep, dev->out);
+                               usb_ep_enable(dev->in_ep, dev->in);
+                               usb_ep_enable(dev->out_ep, dev->out);
                                dev->cdc_filter = DEFAULT_FILTER;
                                if (dev->status)
-                                       issue_start_status (dev);
+                                       issue_start_status(dev);
                        }
 
                        value = 0;
                        break;
                }
 #else
-               /* FIXME this is wrong, as is the assumption that
+               /*
+                * FIXME this is wrong, as is the assumption that
                 * all non-PXA hardware talks real CDC ...
                 */
                debug("set_interface ignored!\n");
@@ -1160,12 +1174,13 @@ done_set_intf:
                        /* carrier always ok ...*/
                        *(u8 *)req->buf = 1 ;
                }
-               value = min (wLength, (u16) 1);
+               value = min(wLength, (u16) 1);
                break;
 
 #ifdef DEV_CONFIG_CDC
        case USB_CDC_SET_ETHERNET_PACKET_FILTER:
-               /* see 6.2.30: no data, wIndex = interface,
+               /*
+                * see 6.2.30: no data, wIndex = interface,
                 * wValue = packet filter bitmap
                 */
                if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
@@ -1178,7 +1193,8 @@ done_set_intf:
                value = 0;
                break;
 
-       /* and potentially:
+       /*
+        * and potentially:
         * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
         * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
         * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
@@ -1199,11 +1215,11 @@ done_set_intf:
                req->length = value;
                req->zero = value < wLength
                                && (value % gadget->ep0->maxpacket) == 0;
-               value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
+               value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
                if (value < 0) {
                        debug("ep_queue --> %d\n", value);
                        req->status = 0;
-                       eth_setup_complete (gadget->ep0, req);
+                       eth_setup_complete(gadget->ep0, req);
                }
        }
 
@@ -1211,18 +1227,18 @@ done_set_intf:
        return value;
 }
 
-
 /*-------------------------------------------------------------------------*/
 
-static void rx_complete (struct usb_ep *ep, struct usb_request *req);
+static void rx_complete(struct usb_ep *ep, struct usb_request *req);
 
-static int rx_submit ( struct eth_dev *dev, struct usb_request *req, \
+static int rx_submit(struct eth_dev *dev, struct usb_request *req,
                                gfp_t gfp_flags)
 {
        int                     retval = -ENOMEM;
        size_t                  size;
 
-       /* Padding up to RX_EXTRA handles minor disagreements with host.
+       /*
+        * Padding up to RX_EXTRA handles minor disagreements with host.
         * Normally we use the USB "terminate on short read" convention;
         * so allow up to (N*maxpacket), since that memory is normally
         * already allocated.  Some hardware doesn't deal well with short
@@ -1236,8 +1252,8 @@ static int rx_submit ( struct eth_dev *dev, struct usb_request *req, \
        size += dev->out_ep->maxpacket - 1;
        size -= size % dev->out_ep->maxpacket;
 
-
-       /* Some platforms perform better when IP packets are aligned,
+       /*
+        * Some platforms perform better when IP packets are aligned,
         * but on at least one, checksumming fails otherwise.
         */
 
@@ -1245,59 +1261,55 @@ static int rx_submit ( struct eth_dev *dev, struct usb_request *req, \
        req->length = size;
        req->complete = rx_complete;
 
-       retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
+       retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
 
-       if (retval) {
+       if (retval)
                error("rx submit --> %d", retval);
-       }
+
        return retval;
 }
 
-
-static void rx_complete (struct usb_ep *ep, struct usb_request *req)
+static void rx_complete(struct usb_ep *ep, struct usb_request *req)
 {
        struct eth_dev  *dev = ep->driver_data;
 
        debug("%s: status %d\n", __func__, req->status);
 
-       packet_received=1;
-
-       if (req)
-               dev->rx_req=req;
+       packet_received = 1;
 }
 
-
-static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
+static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
 {
 
-       dev->tx_req = usb_ep_alloc_request (dev->in_ep, 0);
+       dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
 
        if (!dev->tx_req)
-               goto fail;
+               goto fail1;
 
-       dev->rx_req = usb_ep_alloc_request (dev->out_ep, 0);
+       dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
 
        if (!dev->rx_req)
-               goto fail;
+               goto fail2;
 
        return 0;
 
-fail:
+fail2:
+       usb_ep_free_request(dev->in_ep, dev->tx_req);
+fail1:
        error("can't alloc requests");
        return -1;
 }
 
-
-static void tx_complete (struct usb_ep *ep, struct usb_request *req)
+static void tx_complete(struct usb_ep *ep, struct usb_request *req)
 {
-       debug("%s: status %s\n", __func__, (req->status)?"failed":"ok");
-       packet_sent=1;
+       debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
+       packet_sent = 1;
 }
 
-static inline int eth_is_promisc (struct eth_dev *dev)
+static inline int eth_is_promisc(struct eth_dev *dev)
 {
        /* no filters for the CDC subset; always promisc */
-       if (subset_active (dev))
+       if (subset_active(dev))
                return 1;
        return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
 }
@@ -1415,49 +1427,49 @@ drop:
 /*-------------------------------------------------------------------------*/
 #endif
 
-static void eth_unbind (struct usb_gadget *gadget)
+static void eth_unbind(struct usb_gadget *gadget)
 {
-       struct eth_dev *dev = get_gadget_data (gadget);
+       struct eth_dev *dev = get_gadget_data(gadget);
 
        debug("%s...\n", __func__);
 
        /* we've already been disconnected ... no i/o is active */
        if (dev->req) {
-               usb_ep_free_request (gadget->ep0, dev->req);
+               usb_ep_free_request(gadget->ep0, dev->req);
                dev->req = NULL;
        }
        if (dev->stat_req) {
-               usb_ep_free_request (dev->status_ep, dev->stat_req);
+               usb_ep_free_request(dev->status_ep, dev->stat_req);
                dev->stat_req = NULL;
        }
 
        if (dev->tx_req) {
-               usb_ep_free_request (dev->in_ep, dev->tx_req);
-               dev->tx_req=NULL;
+               usb_ep_free_request(dev->in_ep, dev->tx_req);
+               dev->tx_req = NULL;
        }
 
        if (dev->rx_req) {
-               usb_ep_free_request (dev->out_ep, dev->rx_req);
-               dev->rx_req=NULL;
+               usb_ep_free_request(dev->out_ep, dev->rx_req);
+               dev->rx_req = NULL;
        }
 
 /*     unregister_netdev (dev->net);*/
 /*     free_netdev(dev->net);*/
 
-       set_gadget_data (gadget, NULL);
+       set_gadget_data(gadget, NULL);
 }
 
-static void eth_disconnect (struct usb_gadget *gadget)
+static void eth_disconnect(struct usb_gadget *gadget)
 {
-       eth_reset_config (get_gadget_data (gadget));
+       eth_reset_config(get_gadget_data(gadget));
 }
 
-static void eth_suspend (struct usb_gadget *gadget)
+static void eth_suspend(struct usb_gadget *gadget)
 {
        /* Not used */
 }
 
-static void eth_resume (struct usb_gadget *gadget)
+static void eth_resume(struct usb_gadget *gadget)
 {
        /* Not used */
 }
@@ -1492,12 +1504,12 @@ static int is_eth_addr_valid(char *str)
        return 0;
 }
 
-static u8 nibble (unsigned char c)
+static u8 nibble(unsigned char c)
 {
-       if (likely (isdigit (c)))
+       if (likely(isdigit(c)))
                return c - '0';
-       c = toupper (c);
-       if (likely (isxdigit (c)))
+       c = toupper(c);
+       if (likely(isxdigit(c)))
                return 10 + c - 'A';
        return 0;
 }
@@ -1510,13 +1522,13 @@ static int get_ether_addr(const char *str, u8 *dev_addr)
                for (i = 0; i < 6; i++) {
                        unsigned char num;
 
-                       if((*str == '.') || (*str == ':'))
+                       if ((*str == '.') || (*str == ':'))
                                str++;
                        num = nibble(*str++) << 4;
                        num |= (nibble(*str++));
-                       dev_addr [i] = num;
+                       dev_addr[i] = num;
                }
-               if (is_valid_ether_addr (dev_addr))
+               if (is_valid_ether_addr(dev_addr))
                        return 0;
        }
        return 1;
@@ -1528,17 +1540,18 @@ static int eth_bind(struct usb_gadget *gadget)
        u8                      cdc = 1, zlp = 1;
        struct usb_ep           *in_ep, *out_ep, *status_ep = NULL;
        int                     gcnum;
-       u8                      tmp[7];
+       u8                      tmp[7];
 
        /* these flags are only ever cleared; compiler take note */
 #ifndef        DEV_CONFIG_CDC
        cdc = 0;
 #endif
-       /* Because most host side USB stacks handle CDC Ethernet, that
+       /*
+        * Because most host side USB stacks handle CDC Ethernet, that
         * standard protocol is _strongly_ preferred for interop purposes.
         * (By everyone except Microsoft.)
         */
-       if (gadget_is_pxa (gadget)) {
+       if (gadget_is_pxa(gadget)) {
                /* pxa doesn't support altsettings */
                cdc = 0;
        } else if (gadget_is_musbhdrc(gadget)) {
@@ -1547,20 +1560,22 @@ static int eth_bind(struct usb_gadget *gadget)
        } else if (gadget_is_sh(gadget)) {
                /* sh doesn't support multiple interfaces or configs */
                cdc = 0;
-       } else if (gadget_is_sa1100 (gadget)) {
+       } else if (gadget_is_sa1100(gadget)) {
                /* hardware can't write zlps */
                zlp = 0;
-               /* sa1100 CAN do CDC, without status endpoint ... we use
+               /*
+                * sa1100 CAN do CDC, without status endpoint ... we use
                 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
                 */
                cdc = 0;
        }
 
-       gcnum = usb_gadget_controller_number (gadget);
+       gcnum = usb_gadget_controller_number(gadget);
        if (gcnum >= 0)
-               device_desc.bcdDevice = cpu_to_le16 (0x0300 + gcnum);
+               device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
        else {
-               /* can't assume CDC works.  don't want to default to
+               /*
+                * can't assume CDC works.  don't want to default to
                 * anything less functional on CDC-capable hardware,
                 * so we fail in this case.
                 */
@@ -1569,7 +1584,8 @@ static int eth_bind(struct usb_gadget *gadget)
                return -ENODEV;
        }
 
-       /* CDC subset ... recognized by Linux since 2.4.10, but Windows
+       /*
+        * CDC subset ... recognized by Linux since 2.4.10, but Windows
         * drivers aren't widely available.  (That may be improved by
         * supporting one submode of the "SAFE" variant of MDLM.)
         */
@@ -1588,17 +1604,17 @@ static int eth_bind(struct usb_gadget *gadget)
        if (bcdDevice)
                device_desc.bcdDevice = cpu_to_le16(bcdDevice);
        if (iManufacturer)
-               strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
+               strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
        if (iProduct)
-               strlcpy (product_desc, iProduct, sizeof product_desc);
+               strlcpy(product_desc, iProduct, sizeof product_desc);
        if (iSerialNumber) {
                device_desc.iSerialNumber = STRING_SERIALNUMBER,
                strlcpy(serial_number, iSerialNumber, sizeof serial_number);
        }
 
        /* all we really need is bulk IN/OUT */
-       usb_ep_autoconfig_reset (gadget);
-       in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
+       usb_ep_autoconfig_reset(gadget);
+       in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
        if (!in_ep) {
 autoconf_fail:
                error("can't autoconfigure on %s\n",
@@ -1607,17 +1623,18 @@ autoconf_fail:
        }
        in_ep->driver_data = in_ep;     /* claim */
 
-       out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
+       out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
        if (!out_ep)
                goto autoconf_fail;
        out_ep->driver_data = out_ep;   /* claim */
 
 #if defined(DEV_CONFIG_CDC)
-       /* CDC Ethernet control interface doesn't require a status endpoint.
+       /*
+        * CDC Ethernet control interface doesn't require a status endpoint.
         * Since some hosts expect one, try to allocate one anyway.
         */
        if (cdc) {
-               status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
+               status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
                if (status_ep) {
                        status_ep->driver_data = status_ep;     /* claim */
                } else if (cdc) {
@@ -1632,7 +1649,8 @@ autoconf_fail:
                eth_config.bNumInterfaces = 1;
                eth_config.iConfiguration = STRING_SUBSET;
 
-               /* use functions to set these up, in case we're built to work
+               /*
+                * use functions to set these up, in case we're built to work
                 * with multiple controllers and must override CDC Ethernet.
                 */
                fs_subset_descriptors();
@@ -1640,7 +1658,7 @@ autoconf_fail:
        }
 
        device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
-       usb_gadget_set_selfpowered (gadget);
+       usb_gadget_set_selfpowered(gadget);
 
        if (gadget_is_dualspeed(gadget)) {
                if (!cdc)
@@ -1668,7 +1686,7 @@ autoconf_fail:
        }
 
        dev->net = &l_netdev;
-       strcpy (dev->net->name, USB_NET_NAME);
+       strcpy(dev->net->name, USB_NET_NAME);
 
        dev->cdc = cdc;
        dev->zlp = zlp;
@@ -1677,7 +1695,8 @@ autoconf_fail:
        dev->out_ep = out_ep;
        dev->status_ep = status_ep;
 
-       /* Module params for these addresses should come from ID proms.
+       /*
+        * Module params for these addresses should come from ID proms.
         * The host side address is used with CDC, and commonly
         * ends up in a persistent config database.  It's not clear if
         * host side code for the SAFE thing cares -- its original BLAN
@@ -1690,10 +1709,10 @@ autoconf_fail:
 
        get_ether_addr(host_addr, dev->host_mac);
 
-       sprintf (ethaddr, "%02X%02X%02X%02X%02X%02X",
-               dev->host_mac [0], dev->host_mac [1],
-                       dev->host_mac [2], dev->host_mac [3],
-                       dev->host_mac [4], dev->host_mac [5]);
+       sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
+               dev->host_mac[0], dev->host_mac[1],
+                       dev->host_mac[2], dev->host_mac[3],
+                       dev->host_mac[4], dev->host_mac[5]);
 
        printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
                out_ep->name, in_ep->name,
@@ -1701,23 +1720,25 @@ autoconf_fail:
                status_ep ? status_ep->name : ""
                );
        printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
-               dev->net->enetaddr [0], dev->net->enetaddr [1],
-               dev->net->enetaddr [2], dev->net->enetaddr [3],
-               dev->net->enetaddr [4], dev->net->enetaddr [5]);
+               dev->net->enetaddr[0], dev->net->enetaddr[1],
+               dev->net->enetaddr[2], dev->net->enetaddr[3],
+               dev->net->enetaddr[4], dev->net->enetaddr[5]);
 
        if (cdc) {
                printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
-                       dev->host_mac [0], dev->host_mac [1],
-                       dev->host_mac [2], dev->host_mac [3],
-                       dev->host_mac [4], dev->host_mac [5]);
+                       dev->host_mac[0], dev->host_mac[1],
+                       dev->host_mac[2], dev->host_mac[3],
+                       dev->host_mac[4], dev->host_mac[5]);
        }
 
-       /* use PKTSIZE (or aligned... from u-boot) and set
-        * wMaxSegmentSize accordingly*/
+       /*
+        * use PKTSIZE (or aligned... from u-boot) and set
+        * wMaxSegmentSize accordingly
+        */
        dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
 
        /* preallocate control message data and buffer */
-       dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
+       dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
        if (!dev->req)
                goto fail;
        dev->req->buf = control_req;
@@ -1726,9 +1747,10 @@ autoconf_fail:
        /* ... and maybe likewise for status transfer */
 #if defined(DEV_CONFIG_CDC)
        if (dev->status_ep) {
-               dev->stat_req = usb_ep_alloc_request(dev->status_ep, GFP_KERNEL);
+               dev->stat_req = usb_ep_alloc_request(dev->status_ep,
+                                                       GFP_KERNEL);
                if (!dev->stat_req) {
-                       usb_ep_free_request (dev->status_ep, dev->req);
+                       usb_ep_free_request(dev->status_ep, dev->req);
 
                        goto fail;
                }
@@ -1739,10 +1761,11 @@ autoconf_fail:
 
        /* finish hookup to lower layer ... */
        dev->gadget = gadget;
-       set_gadget_data (gadget, dev);
+       set_gadget_data(gadget, dev);
        gadget->ep0->driver_data = dev;
 
-       /* two kinds of host-initiated state changes:
+       /*
+        * two kinds of host-initiated state changes:
         *  - iff DATA transfer is active, carrier is "on"
         *  - tx queueing enabled if open *and* carrier is "on"
         */
@@ -1750,13 +1773,13 @@ autoconf_fail:
 
 fail:
        error("%s failed", __func__);
-       eth_unbind (gadget);
+       eth_unbind(gadget);
        return -ENOMEM;
 }
 
-static int usb_eth_init(struct eth_device* netdev, bd_t* bd)
+static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
 {
-       struct eth_dev *dev=&l_ethdev;
+       struct eth_dev *dev = &l_ethdev;
        struct usb_gadget *gadget;
        unsigned long ts;
        unsigned long timeout = USB_CONNECT_TIMEOUT;
@@ -1767,8 +1790,6 @@ static int usb_eth_init(struct eth_device* netdev, bd_t* bd)
        }
 
        dev->network_started = 0;
-       dev->tx_req = NULL;
-       dev->rx_req = NULL;
 
        packet_received = 0;
        packet_sent = 0;
@@ -1780,8 +1801,7 @@ static int usb_eth_init(struct eth_device* netdev, bd_t* bd)
                timeout = simple_strtoul(getenv("cdc_connect_timeout"),
                                                NULL, 10) * CONFIG_SYS_HZ;
        ts = get_timer(0);
-       while (!l_ethdev.network_started)
-       {
+       while (!l_ethdev.network_started) {
                /* Handle control-c and timeouts */
                if (ctrlc() || (get_timer(ts) > timeout)) {
                        error("The remote end did not respond in time.");
@@ -1790,27 +1810,29 @@ static int usb_eth_init(struct eth_device* netdev, bd_t* bd)
                usb_gadget_handle_interrupts();
        }
 
-       rx_submit (dev, dev->rx_req, 0);
+       rx_submit(dev, dev->rx_req, 0);
        return 0;
 fail:
        return -1;
 }
 
-static int usb_eth_send(struct eth_device* netdev, volatile void* packet, int length)
+static int usb_eth_send(struct eth_device *netdev,
+                       volatile void *packet, int length)
 {
        int                     retval;
-       struct usb_request      *req = NULL;
        struct eth_dev          *dev = &l_ethdev;
+       struct usb_request      *req = dev->tx_req;
+       unsigned long ts;
+       unsigned long timeout = USB_CONNECT_TIMEOUT;
 
        debug("%s:...\n", __func__);
 
-       req = dev->tx_req;
-
        req->buf = (void *)packet;
        req->context = NULL;
        req->complete = tx_complete;
 
-       /* use zlp framing on tx for strict CDC-Ether conformance,
+       /*
+        * use zlp framing on tx for strict CDC-Ether conformance,
         * though any robust network rx path ignores extra padding.
         * and some hardware doesn't like to write zlps.
         */
@@ -1825,48 +1847,49 @@ static int usb_eth_send(struct eth_device* netdev, volatile void* packet, int le
                req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
                        ? ((dev->tx_qlen % qmult) != 0) : 0;
 #endif
-       dev->tx_qlen=1;
+       dev->tx_qlen = 1;
+       ts = get_timer(0);
+       packet_sent = 0;
 
-       retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
+       retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
 
        if (!retval)
                debug("%s: packet queued\n", __func__);
-       while(!packet_sent)
-       {
-               packet_sent=0;
+       while (!packet_sent) {
+               if (get_timer(ts) > timeout) {
+                       printf("timeout sending packets to usb ethernet\n");
+                       return -1;
+               }
+               usb_gadget_handle_interrupts();
        }
 
        return 0;
 }
 
-static int usb_eth_recv(struct eth_devicenetdev)
+static int usb_eth_recv(struct eth_device *netdev)
 {
        struct eth_dev *dev = &l_ethdev;
 
        usb_gadget_handle_interrupts();
 
-       if (packet_received)
-       {
-               debug("%s: packet received \n", __func__);
-               if (dev->rx_req)
-               {
-                       NetReceive(NetRxPackets[0],dev->rx_req->length);
-                       packet_received=0;
+       if (packet_received) {
+               debug("%s: packet received\n", __func__);
+               if (dev->rx_req) {
+                       NetReceive(NetRxPackets[0], dev->rx_req->length);
+                       packet_received = 0;
 
-                       if (dev->rx_req)
-                               rx_submit (dev, dev->rx_req, 0);
-               }
-               else error("dev->rx_req invalid");
+                       rx_submit(dev, dev->rx_req, 0);
+               } else
+                       error("dev->rx_req invalid");
        }
        return 0;
 }
 
-void usb_eth_halt(struct eth_devicenetdev)
+void usb_eth_halt(struct eth_device *netdev)
 {
-       struct eth_dev *dev =&l_ethdev;
+       struct eth_dev *dev = &l_ethdev;
 
-       if (!netdev)
-       {
+       if (!netdev) {
                error("received NULL ptr");
                return;
        }
@@ -1890,9 +1913,9 @@ static struct usb_gadget_driver eth_driver = {
 int usb_eth_initialize(bd_t *bi)
 {
        int status = 0;
-       struct eth_device *netdev=&l_netdev;
+       struct eth_device *netdev = &l_netdev;
 
-       sprintf(netdev->name,"usb_ether");
+       sprintf(netdev->name, "usb_ether");
 
        netdev->init = usb_eth_init;
        netdev->send = usb_eth_send;