Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / net / irda / irda-usb.c
index 46e0022..cd87593 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  *
  * Filename:      irda-usb.c
- * Version:       0.9b
+ * Version:       0.10
  * Description:   IrDA-USB Driver
  * Status:        Experimental 
  * Author:        Dag Brattli <dag@brattli.net>
@@ -9,6 +9,9 @@
  *     Copyright (C) 2000, Roman Weissgaerber <weissg@vienna.at>
  *      Copyright (C) 2001, Dag Brattli <dag@brattli.net>
  *      Copyright (C) 2001, Jean Tourrilhes <jt@hpl.hp.com>
+ *      Copyright (C) 2004, SigmaTel, Inc. <irquality@sigmatel.com>
+ *      Copyright (C) 2005, Milan Beno <beno@pobox.sk>
+ *      Copyright (C) 2006, Nick Fedchik <nick@fedchik.org.ua>
  *          
  *     This program is free software; you can redistribute it and/or modify
  *     it under the terms of the GNU General Public License as published by
@@ -61,6 +64,7 @@
 #include <linux/slab.h>
 #include <linux/rtnetlink.h>
 #include <linux/usb.h>
+#include <linux/firmware.h>
 
 #include "irda-usb.h"
 
@@ -78,8 +82,12 @@ static struct usb_device_id dongles[] = {
        { USB_DEVICE(0x50f, 0x180), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
        /* Extended Systems, Inc.,  XTNDAccess IrDA USB (ESI-9685) */
        { USB_DEVICE(0x8e9, 0x100), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
+       /* SigmaTel STIR4210/4220/4116 USB IrDA (VFIR) Bridge */
+       { USB_DEVICE(0x66f, 0x4210), .driver_info = IUC_STIR_4210 | IUC_SPEED_BUG },
+       { USB_DEVICE(0x66f, 0x4220), .driver_info = IUC_STIR_4210 | IUC_SPEED_BUG },
+       { USB_DEVICE(0x66f, 0x4116), .driver_info = IUC_STIR_4210 | IUC_SPEED_BUG },
        { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
-                      USB_DEVICE_ID_MATCH_INT_SUBCLASS,
+         USB_DEVICE_ID_MATCH_INT_SUBCLASS,
          .bInterfaceClass = USB_CLASS_APP_SPEC,
          .bInterfaceSubClass = USB_CLASS_IRDA,
          .driver_info = IUC_DEFAULT, },
@@ -99,6 +107,7 @@ MODULE_DEVICE_TABLE(usb, dongles);
 
 /*------------------------------------------------------------------*/
 
+static void irda_usb_init_qos(struct irda_usb_cb *self) ;
 static struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf);
 static void irda_usb_disconnect(struct usb_interface *intf);
 static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self);
@@ -108,6 +117,7 @@ static void irda_usb_close(struct irda_usb_cb *self);
 static void speed_bulk_callback(struct urb *urb, struct pt_regs *regs);
 static void write_bulk_callback(struct urb *urb, struct pt_regs *regs);
 static void irda_usb_receive(struct urb *urb, struct pt_regs *regs);
+static void irda_usb_rx_defer_expired(unsigned long data);
 static int irda_usb_net_open(struct net_device *dev);
 static int irda_usb_net_close(struct net_device *dev);
 static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
@@ -140,7 +150,24 @@ static void irda_usb_build_header(struct irda_usb_cb *self,
                                  __u8 *header,
                                  int   force)
 {
-       /* Set the negotiated link speed */
+       /* Here we check if we have an STIR421x chip,
+        * and if either speed or xbofs (or both) needs
+        * to be changed.
+        */
+       if (self->capability & IUC_STIR_4210 &&
+           ((self->new_speed != -1) || (self->new_xbofs != -1))) {
+
+               /* With STIR421x, speed and xBOFs must be set at the same
+                * time, even if only one of them changes.
+                */
+               if (self->new_speed == -1)
+                       self->new_speed = self->speed ;
+
+               if (self->new_xbofs == -1)
+                       self->new_xbofs = self->xbofs ;
+       }
+
+       /* Set the link speed */
        if (self->new_speed != -1) {
                /* Hum... Ugly hack :-(
                 * Some device are not compliant with the spec and change
@@ -190,7 +217,11 @@ static void irda_usb_build_header(struct irda_usb_cb *self,
                        *header = SPEED_4000000;
                        self->new_xbofs = 0;
                        break;
-               }
+               case 16000000:
+                       *header = SPEED_16000000;
+                       self->new_xbofs = 0;
+                       break;
+               }
        } else
                /* No change */
                *header = 0;
@@ -234,6 +265,32 @@ static void irda_usb_build_header(struct irda_usb_cb *self,
        }
 }
 
+/*
+*   calculate turnaround time for SigmaTel header
+*/
+static __u8 get_turnaround_time(struct sk_buff *skb)
+{
+       int turnaround_time = irda_get_mtt(skb);
+
+       if ( turnaround_time == 0 )
+               return 0;
+       else if ( turnaround_time <= 10 )
+               return 1;
+       else if ( turnaround_time <= 50 )
+               return 2;
+       else if ( turnaround_time <= 100 )
+               return 3;
+       else if ( turnaround_time <= 500 )
+               return 4;
+       else if ( turnaround_time <= 1000 )
+               return 5;
+       else if ( turnaround_time <= 5000 )
+               return 6;
+       else
+               return 7;
+}
+
+
 /*------------------------------------------------------------------*/
 /*
  * Send a command to change the speed of the dongle
@@ -261,13 +318,19 @@ static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self)
        /* Set the new speed and xbofs in this fake frame */
        irda_usb_build_header(self, frame, 1);
 
+       if ( self->capability & IUC_STIR_4210 ) {
+               if (frame[0] == 0) return ; // do nothing if no change
+               frame[1] = 0; // other parameters don't change here
+               frame[2] = 0;
+       }
+
        /* Submit the 0 length IrDA frame to trigger new speed settings */
         usb_fill_bulk_urb(urb, self->usbdev,
                      usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
                       frame, IRDA_USB_SPEED_MTU,
                       speed_bulk_callback, self);
-       urb->transfer_buffer_length = USB_IRDA_HEADER;
-       urb->transfer_flags = URB_ASYNC_UNLINK;
+       urb->transfer_buffer_length = self->header_length;
+       urb->transfer_flags = 0;
 
        /* Irq disabled -> GFP_ATOMIC */
        if ((ret = usb_submit_urb(urb, GFP_ATOMIC))) {
@@ -382,16 +445,35 @@ static int irda_usb_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
         * allocation will be done lower in skb_push().
         * Also, we don't use directly skb_cow(), because it require
         * headroom >= 16, which force unnecessary copies - Jean II */
-       if (skb_headroom(skb) < USB_IRDA_HEADER) {
+       if (skb_headroom(skb) < self->header_length) {
                IRDA_DEBUG(0, "%s(), Insuficient skb headroom.\n", __FUNCTION__);
-               if (skb_cow(skb, USB_IRDA_HEADER)) {
+               if (skb_cow(skb, self->header_length)) {
                        IRDA_WARNING("%s(), failed skb_cow() !!!\n", __FUNCTION__);
                        goto drop;
                }
        }
 
        /* Change setting for next frame */
-       irda_usb_build_header(self, skb_push(skb, USB_IRDA_HEADER), 0);
+
+       if ( self->capability & IUC_STIR_4210 ) {
+               __u8 turnaround_time;
+               __u8* frame;
+               turnaround_time = get_turnaround_time( skb );
+               frame= skb_push(skb, self->header_length);
+               irda_usb_build_header(self, frame, 0);
+               frame[2] = turnaround_time;
+               if ((skb->len != 0) &&
+                   ((skb->len % 128) == 0) &&
+                   ((skb->len % 512) != 0)) {
+                       /* add extra byte for special SigmaTel feature */
+                       frame[1] = 1;
+                       skb_put(skb, 1);
+               } else {
+                       frame[1] = 0;
+               }
+       } else {
+               irda_usb_build_header(self, skb_push(skb, self->header_length), 0);
+       }
 
        /* FIXME: Make macro out of this one */
        ((struct irda_skb_cb *)skb->cb)->context = self;
@@ -401,15 +483,12 @@ static int irda_usb_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
                       skb->data, IRDA_SKB_MAX_MTU,
                       write_bulk_callback, skb);
        urb->transfer_buffer_length = skb->len;
-       /* Note : unlink *must* be Asynchronous because of the code in 
-        * irda_usb_net_timeout() -> call in irq - Jean II */
-       urb->transfer_flags = URB_ASYNC_UNLINK;
        /* This flag (URB_ZERO_PACKET) indicates that what we send is not
         * a continuous stream of data but separate packets.
         * In this case, the USB layer will insert an empty USB frame (TD)
         * after each of our packets that is exact multiple of the frame size.
         * This is how the dongle will detect the end of packet - Jean II */
-       urb->transfer_flags |= URB_ZERO_PACKET;
+       urb->transfer_flags = URB_ZERO_PACKET;
 
        /* Generate min turn time. FIXME: can we do better than this? */
        /* Trying to a turnaround time at this level is trying to measure
@@ -630,8 +709,6 @@ static void irda_usb_net_timeout(struct net_device *netdev)
                         * in completion handler, because urb->status will
                         * be -ENOENT. We will fix that at the next watchdog,
                         * leaving more time to USB to recover...
-                        * Also, we are in interrupt, so we need to have
-                        * URB_ASYNC_UNLINK to work properly...
                         * Jean II */
                        done = 1;
                        break;
@@ -682,6 +759,12 @@ static void irda_usb_net_timeout(struct net_device *netdev)
  * on the interrupt pipe and hang the Rx URB only when an interrupt is
  * received.
  * Jean II
+ *
+ * Note : don't read the above as what we are currently doing, but as
+ * something we could do with KC dongle. Also don't forget that the
+ * interrupt pipe is not part of the original standard, so this would
+ * need to be optional...
+ * Jean II
  */
 
 /*------------------------------------------------------------------*/
@@ -709,10 +792,8 @@ static void irda_usb_submit(struct irda_usb_cb *self, struct sk_buff *skb, struc
        /* Reinitialize URB */
        usb_fill_bulk_urb(urb, self->usbdev, 
                      usb_rcvbulkpipe(self->usbdev, self->bulk_in_ep), 
-                     skb->data, skb->truesize,
+                     skb->data, IRDA_SKB_MAX_MTU,
                       irda_usb_receive, skb);
-       /* Note : unlink *must* be synchronous because of the code in 
-        * irda_usb_net_close() -> free the skb - Jean II */
        urb->status = 0;
 
        /* Can be called from irda_usb_receive (irq handler) -> GFP_ATOMIC */
@@ -739,7 +820,8 @@ static void irda_usb_receive(struct urb *urb, struct pt_regs *regs)
        struct irda_skb_cb *cb;
        struct sk_buff *newskb;
        struct sk_buff *dataskb;
-       int             docopy;
+       struct urb *next_urb;
+       unsigned int len, docopy;
 
        IRDA_DEBUG(2, "%s(), len=%d\n", __FUNCTION__, urb->actual_length);
        
@@ -760,24 +842,41 @@ static void irda_usb_receive(struct urb *urb, struct pt_regs *regs)
        if (urb->status != 0) {
                switch (urb->status) {
                case -EILSEQ:
-                       self->stats.rx_errors++;
                        self->stats.rx_crc_errors++;    
-                       break;
+                       /* Also precursor to a hot-unplug on UHCI. */
+                       /* Fallthrough... */
                case -ECONNRESET:               /* -104 */
-                       IRDA_DEBUG(0, "%s(), Connection Reset (-104), transfer_flags 0x%04X \n", __FUNCTION__, urb->transfer_flags);
+                       /* Random error, if I remember correctly */
                        /* uhci_cleanup_unlink() is going to kill the Rx
                         * URB just after we return. No problem, at this
                         * point the URB will be idle ;-) - Jean II */
-                       break;
+               case -ESHUTDOWN:                /* -108 */
+                       /* That's usually a hot-unplug. Submit will fail... */
+               case -ETIMEDOUT:                /* -110 */
+                       /* Usually precursor to a hot-unplug on OHCI. */
                default:
-                       IRDA_DEBUG(0, "%s(), RX status %d,transfer_flags 0x%04X \n", __FUNCTION__, urb->status, urb->transfer_flags);
+                       self->stats.rx_errors++;
+                       IRDA_DEBUG(0, "%s(), RX status %d, transfer_flags 0x%04X \n", __FUNCTION__, urb->status, urb->transfer_flags);
                        break;
                }
-               goto done;
+               /* If we received an error, we don't want to resubmit the
+                * Rx URB straight away but to give the USB layer a little
+                * bit of breathing room.
+                * We are in the USB thread context, therefore there is a
+                * danger of recursion (new URB we submit fails, we come
+                * back here).
+                * With recent USB stack (2.6.15+), I'm seeing that on
+                * hot unplug of the dongle...
+                * Lowest effective timer is 10ms...
+                * Jean II */
+               self->rx_defer_timer.function = &irda_usb_rx_defer_expired;
+               self->rx_defer_timer.data = (unsigned long) urb;
+               mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
+               return;
        }
        
        /* Check for empty frames */
-       if (urb->actual_length <= USB_IRDA_HEADER) {
+       if (urb->actual_length <= self->header_length) {
                IRDA_WARNING("%s(), empty frame!\n", __FUNCTION__);
                goto done;
        }
@@ -798,7 +897,11 @@ static void irda_usb_receive(struct urb *urb, struct pt_regs *regs)
        docopy = (urb->actual_length < IRDA_RX_COPY_THRESHOLD);
 
        /* Allocate a new skb */
-       newskb = dev_alloc_skb(docopy ? urb->actual_length : IRDA_SKB_MAX_MTU);
+       if ( self->capability & IUC_STIR_4210 )
+               newskb = dev_alloc_skb(docopy ? urb->actual_length : IRDA_SKB_MAX_MTU + USB_IRDA_SIGMATEL_HEADER);
+       else
+               newskb = dev_alloc_skb(docopy ? urb->actual_length : IRDA_SKB_MAX_MTU);
+
        if (!newskb)  {
                self->stats.rx_dropped++;
                /* We could deliver the current skb, but this would stall
@@ -827,16 +930,17 @@ static void irda_usb_receive(struct urb *urb, struct pt_regs *regs)
 
        /* Set proper length on skb & remove USB-IrDA header */
        skb_put(dataskb, urb->actual_length);
-       skb_pull(dataskb, USB_IRDA_HEADER);
+       skb_pull(dataskb, self->header_length);
 
        /* Ask the networking layer to queue the packet for the IrDA stack */
        dataskb->dev = self->netdev;
        dataskb->mac.raw  = dataskb->data;
        dataskb->protocol = htons(ETH_P_IRDA);
+       len = dataskb->len;
        netif_rx(dataskb);
 
        /* Keep stats up to date */
-       self->stats.rx_bytes += dataskb->len;
+       self->stats.rx_bytes += len;
        self->stats.rx_packets++;
        self->netdev->last_rx = jiffies;
 
@@ -850,13 +954,45 @@ done:
         * idle slot....
         * Jean II */
        /* Note : with this scheme, we could submit the idle URB before
-        * processing the Rx URB. Another time... Jean II */
+        * processing the Rx URB. I don't think it would buy us anything as
+        * we are running in the USB thread context. Jean II */
+       next_urb = self->idle_rx_urb;
 
-       /* Submit the idle URB to replace the URB we've just received */
-       irda_usb_submit(self, skb, self->idle_rx_urb);
        /* Recycle Rx URB : Now, the idle URB is the present one */
        urb->context = NULL;
        self->idle_rx_urb = urb;
+
+       /* Submit the idle URB to replace the URB we've just received.
+        * Do it last to avoid race conditions... Jean II */
+       irda_usb_submit(self, skb, next_urb);
+}
+
+/*------------------------------------------------------------------*/
+/*
+ * In case of errors, we want the USB layer to have time to recover.
+ * Now, it is time to resubmit ouur Rx URB...
+ */
+static void irda_usb_rx_defer_expired(unsigned long data)
+{
+       struct urb *urb = (struct urb *) data;
+       struct sk_buff *skb = (struct sk_buff *) urb->context;
+       struct irda_usb_cb *self; 
+       struct irda_skb_cb *cb;
+       struct urb *next_urb;
+
+       IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
+
+       /* Find ourselves */
+       cb = (struct irda_skb_cb *) skb->cb;
+       IRDA_ASSERT(cb != NULL, return;);
+       self = (struct irda_usb_cb *) cb->context;
+       IRDA_ASSERT(self != NULL, return;);
+
+       /* Same stuff as when Rx is done, see above... */
+       next_urb = self->idle_rx_urb;
+       urb->context = NULL;
+       self->idle_rx_urb = urb;
+       irda_usb_submit(self, skb, next_urb);
 }
 
 /*------------------------------------------------------------------*/
@@ -886,6 +1022,191 @@ static int irda_usb_is_receiving(struct irda_usb_cb *self)
        return 0; /* For now */
 }
 
+
+#define STIR421X_PATCH_PRODUCT_VERSION_STR       "Product Version: "
+#define STIR421X_PATCH_COMPONENT_VERSION_STR     "Component Version: "
+#define STIR421X_PATCH_DATA_TAG_STR              "STMP"
+#define STIR421X_PATCH_FILE_VERSION_MAX_OFFSET   512     /* version info is before here */
+#define STIR421X_PATCH_FILE_IMAGE_MAX_OFFSET     512     /* patch image starts before here */
+#define STIR421X_PATCH_FILE_END_OF_HEADER_TAG    0x1A    /* marks end of patch file header (PC DOS text file EOF character) */
+
+/*
+ * Known firmware patches for STIR421x dongles
+ */
+static char * stir421x_patches[] = {
+       "42101001.sb",
+       "42101002.sb",
+};
+
+static int stir421x_get_patch_version(unsigned char * patch, const unsigned long patch_len)
+{
+       unsigned int version_offset;
+       unsigned long version_major, version_minor, version_build;
+       unsigned char * version_start;
+       int version_found = 0;
+
+       for (version_offset = 0;
+            version_offset < STIR421X_PATCH_FILE_END_OF_HEADER_TAG;
+            version_offset++) {
+               if (!memcmp(patch + version_offset,
+                           STIR421X_PATCH_PRODUCT_VERSION_STR,
+                           sizeof(STIR421X_PATCH_PRODUCT_VERSION_STR) - 1)) {
+                                   version_found = 1;
+                                   version_start = patch +
+                                           version_offset +
+                                           sizeof(STIR421X_PATCH_PRODUCT_VERSION_STR) - 1;
+                                   break;
+               }
+       }
+
+       /* We couldn't find a product version on this patch */
+       if (!version_found)
+               return -EINVAL;
+
+       /* Let's check if the product version is dotted */
+       if (version_start[3] != '.' ||
+           version_start[7] != '.')
+               return -EINVAL;
+
+       version_major = simple_strtoul(version_start, NULL, 10);
+       version_minor = simple_strtoul(version_start + 4, NULL, 10);
+       version_build = simple_strtoul(version_start + 8, NULL, 10);
+
+       IRDA_DEBUG(2, "%s(), Major: %ld Minor: %ld Build: %ld\n",
+                  __FUNCTION__,
+                  version_major, version_minor, version_build);
+
+       return (((version_major) << 12) +
+               ((version_minor) << 8) +
+               ((version_build / 10) << 4) +
+               (version_build % 10));
+
+}
+
+
+static int stir421x_upload_patch (struct irda_usb_cb *self,
+                                 unsigned char * patch,
+                                 const unsigned int patch_len)
+{
+    int retval = 0;
+    int actual_len;
+    unsigned int i = 0, download_amount = 0;
+    unsigned char * patch_chunk;
+
+    IRDA_DEBUG (2, "%s(), Uploading STIR421x Patch\n", __FUNCTION__);
+
+    patch_chunk = kzalloc(STIR421X_MAX_PATCH_DOWNLOAD_SIZE, GFP_KERNEL);
+    if (patch_chunk == NULL)
+           return -ENOMEM;
+
+    /* break up patch into 1023-byte sections */
+    for (i = 0; retval >= 0 && i < patch_len; i += download_amount) {
+           download_amount = patch_len - i;
+           if (download_amount > STIR421X_MAX_PATCH_DOWNLOAD_SIZE)
+                   download_amount = STIR421X_MAX_PATCH_DOWNLOAD_SIZE;
+
+           /* download the patch section */
+           memcpy(patch_chunk, patch + i, download_amount);
+
+           retval = usb_bulk_msg (self->usbdev,
+                                  usb_sndbulkpipe (self->usbdev,
+                                                   self->bulk_out_ep),
+                                  patch_chunk, download_amount,
+                                  &actual_len, msecs_to_jiffies (500));
+           IRDA_DEBUG (2, "%s(), Sent %u bytes\n", __FUNCTION__,
+                       actual_len);
+           if (retval == 0)
+                   mdelay(10);
+    }
+
+    kfree(patch_chunk);
+
+    if (i != patch_len) {
+           IRDA_ERROR ("%s(), Pushed %d bytes (!= patch_len (%d))\n",
+                      __FUNCTION__, i, patch_len);
+           retval = -EIO;
+    }
+
+    if (retval < 0)
+           /* todo - mark device as not ready */
+           IRDA_ERROR ("%s(), STIR421x patch upload failed (%d)\n",
+                       __FUNCTION__, retval);
+
+    return retval;
+}
+
+
+static int stir421x_patch_device(struct irda_usb_cb *self)
+{
+       unsigned int i, patch_found = 0, data_found = 0, data_offset;
+       int patch_version, ret = 0;
+       const struct firmware *fw_entry;
+
+       for (i = 0; i < ARRAY_SIZE(stir421x_patches); i++) {
+               if(request_firmware(&fw_entry, stir421x_patches[i], &self->usbdev->dev) != 0) {
+                       IRDA_ERROR( "%s(), Patch %s is not available\n", __FUNCTION__, stir421x_patches[i]);
+                       continue;
+               }
+
+                /* We found a patch from userspace */
+               patch_version = stir421x_get_patch_version (fw_entry->data, fw_entry->size);
+
+               if (patch_version < 0) {
+                       /* Couldn't fetch a version, let's move on to the next file */
+                       IRDA_ERROR("%s(), version parsing failed\n", __FUNCTION__);
+                       ret = patch_version;
+                       release_firmware(fw_entry);
+                       continue;
+               }
+
+               if (patch_version != self->usbdev->descriptor.bcdDevice) {
+                       /* Patch version and device don't match */
+                       IRDA_ERROR ("%s(), wrong patch version (%d <-> %d)\n",
+                                   __FUNCTION__,
+                                   patch_version, self->usbdev->descriptor.bcdDevice);
+                       ret = -EINVAL;
+                       release_firmware(fw_entry);
+                       continue;
+               }
+
+               /* If we're here, we've found a correct patch */
+               patch_found = 1;
+               break;
+
+       }
+
+       /* We couldn't find a valid firmware, let's leave */
+       if (!patch_found)
+               return ret;
+
+       /* The actual image starts after the "STMP" keyword */
+       for (data_offset = 0; data_offset < STIR421X_PATCH_FILE_IMAGE_MAX_OFFSET; data_offset++) {
+               if (!memcmp(fw_entry->data + data_offset,
+                           STIR421X_PATCH_DATA_TAG_STR,
+                           sizeof(STIR421X_PATCH_FILE_IMAGE_MAX_OFFSET))) {
+                       IRDA_DEBUG(2, "%s(), found patch data for STIR421x at offset %d\n",
+                                  __FUNCTION__, data_offset);
+                       data_found = 1;
+                       break;
+               }
+       }
+
+       /* We couldn't find "STMP" from the header */
+       if (!data_found)
+               return -EINVAL;
+
+       /* Let's upload the patch to the target */
+       ret = stir421x_upload_patch(self,
+                                   &fw_entry->data[data_offset + sizeof(STIR421X_PATCH_FILE_IMAGE_MAX_OFFSET)],
+                                   fw_entry->size - (data_offset + sizeof(STIR421X_PATCH_FILE_IMAGE_MAX_OFFSET)));
+
+       release_firmware(fw_entry);
+
+       return ret;
+
+}
+
+
 /********************** IRDA DEVICE CALLBACKS **********************/
 /*
  * Main calls from the IrDA/Network subsystem.
@@ -921,6 +1242,11 @@ static int irda_usb_net_open(struct net_device *netdev)
                return -1;
        }
 
+       if(self->needspatch) {
+               IRDA_WARNING("%s(), device needs patch\n", __FUNCTION__) ;
+               return -EIO ;
+       }
+
        /* Initialise default speed and xbofs value
         * (IrLAP will change that soon) */
        self->speed = -1;
@@ -995,8 +1321,11 @@ static int irda_usb_net_close(struct net_device *netdev)
        /* Stop network Tx queue */
        netif_stop_queue(netdev);
 
+       /* Kill defered Rx URB */
+       del_timer(&self->rx_defer_timer);
+
        /* Deallocate all the Rx path buffers (URBs and skb) */
-       for (i = 0; i < IU_MAX_RX_URBS; i++) {
+       for (i = 0; i < self->max_rx_urb; i++) {
                struct urb *urb = self->rx_urb[i];
                struct sk_buff *skb = (struct sk_buff *) urb->context;
                /* Cancel the receive command */
@@ -1008,9 +1337,7 @@ static int irda_usb_net_close(struct net_device *netdev)
                }
        }
        /* Cancel Tx and speed URB - need to be synchronous to avoid races */
-       self->tx_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
        usb_kill_urb(self->tx_urb);
-       self->speed_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
        usb_kill_urb(self->speed_urb);
 
        /* Stop and remove instance of IrLAP */
@@ -1175,10 +1502,8 @@ static inline void irda_usb_close(struct irda_usb_cb *self)
        unregister_netdev(self->netdev);
 
        /* Remove the speed buffer */
-       if (self->speed_buff != NULL) {
-               kfree(self->speed_buff);
-               self->speed_buff = NULL;
-       }
+       kfree(self->speed_buff);
+       self->speed_buff = NULL;
 }
 
 /********************** USB CONFIG SUBROUTINES **********************/
@@ -1374,9 +1699,24 @@ static int irda_usb_probe(struct usb_interface *intf,
        self = net->priv;
        self->netdev = net;
        spin_lock_init(&self->lock);
+       init_timer(&self->rx_defer_timer);
+
+       self->capability = id->driver_info;
+       self->needspatch = ((self->capability & IUC_STIR_4210) != 0) ;
 
        /* Create all of the needed urbs */
-       for (i = 0; i < IU_MAX_RX_URBS; i++) {
+       if (self->capability & IUC_STIR_4210) {
+               self->max_rx_urb = IU_SIGMATEL_MAX_RX_URBS;
+               self->header_length = USB_IRDA_SIGMATEL_HEADER;
+       } else {
+               self->max_rx_urb = IU_MAX_RX_URBS;
+               self->header_length = USB_IRDA_HEADER;
+       }
+
+       self->rx_urb = kzalloc(self->max_rx_urb * sizeof(struct urb *),
+                               GFP_KERNEL);
+
+       for (i = 0; i < self->max_rx_urb; i++) {
                self->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
                if (!self->rx_urb[i]) {
                        goto err_out_1;
@@ -1428,17 +1768,28 @@ static int irda_usb_probe(struct usb_interface *intf,
                goto err_out_3;
        }
 
+       self->usbdev = dev;
+
        /* Find IrDA class descriptor */
        irda_desc = irda_usb_find_class_desc(intf);
        ret = -ENODEV;
        if (irda_desc == NULL)
                goto err_out_3;
 
+       if (self->needspatch) {
+               ret = usb_control_msg (self->usbdev, usb_sndctrlpipe (self->usbdev, 0),
+                                      0x02, 0x40, 0, 0, NULL, 0, 500);
+               if (ret < 0) {
+                       IRDA_DEBUG (0, "usb_control_msg failed %d\n", ret);
+                       goto err_out_3;
+               } else {
+                       mdelay(10);
+               }
+       }
+
        self->irda_desc =  irda_desc;
        self->present = 1;
        self->netopen = 0;
-       self->capability = id->driver_info;
-       self->usbdev = dev;
        self->usbintf = intf;
 
        /* Allocate the buffer for speed changes */
@@ -1457,8 +1808,32 @@ static int irda_usb_probe(struct usb_interface *intf,
 
        IRDA_MESSAGE("IrDA: Registered device %s\n", net->name);
        usb_set_intfdata(intf, self);
+
+       if (self->needspatch) {
+               /* Now we fetch and upload the firmware patch */
+               ret = stir421x_patch_device(self);
+               self->needspatch = (ret < 0);
+               if (ret < 0) {
+                       printk("patch_device failed\n");
+                       goto err_out_5;
+               }
+
+               /* replace IrDA class descriptor with what patched device is now reporting */
+               irda_desc = irda_usb_find_class_desc (self->usbintf);
+               if (irda_desc == NULL) {
+                       ret = -ENODEV;
+                       goto err_out_5;
+               }
+               if (self->irda_desc)
+                       kfree (self->irda_desc);
+               self->irda_desc = irda_desc;
+               irda_usb_init_qos(self);
+       }
+
        return 0;
 
+err_out_5:
+       unregister_netdev(self->netdev);
 err_out_4:
        kfree(self->speed_buff);
 err_out_3:
@@ -1467,7 +1842,7 @@ err_out_3:
 err_out_2:
        usb_free_urb(self->tx_urb);
 err_out_1:
-       for (i = 0; i < IU_MAX_RX_URBS; i++) {
+       for (i = 0; i < self->max_rx_urb; i++) {
                if (self->rx_urb[i])
                        usb_free_urb(self->rx_urb[i]);
        }
@@ -1507,6 +1882,9 @@ static void irda_usb_disconnect(struct usb_interface *intf)
         * This will stop/desactivate the Tx path. - Jean II */
        self->present = 0;
 
+       /* Kill defered Rx URB */
+       del_timer(&self->rx_defer_timer);
+
        /* We need to have irq enabled to unlink the URBs. That's OK,
         * at this point the Tx path is gone - Jean II */
        spin_unlock_irqrestore(&self->lock, flags);
@@ -1516,14 +1894,12 @@ static void irda_usb_disconnect(struct usb_interface *intf)
                /* Accept no more transmissions */
                /*netif_device_detach(self->netdev);*/
                netif_stop_queue(self->netdev);
-               /* Stop all the receive URBs */
-               for (i = 0; i < IU_MAX_RX_URBS; i++)
+               /* Stop all the receive URBs. Must be synchronous. */
+               for (i = 0; i < self->max_rx_urb; i++)
                        usb_kill_urb(self->rx_urb[i]);
                /* Cancel Tx and speed URB.
-                * Toggle flags to make sure it's synchronous. */
-               self->tx_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
+                * Make sure it's synchronous to avoid races. */
                usb_kill_urb(self->tx_urb);
-               self->speed_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
                usb_kill_urb(self->speed_urb);
        }
 
@@ -1534,8 +1910,9 @@ static void irda_usb_disconnect(struct usb_interface *intf)
        self->usbintf = NULL;
 
        /* Clean up our urbs */
-       for (i = 0; i < IU_MAX_RX_URBS; i++)
+       for (i = 0; i < self->max_rx_urb; i++)
                usb_free_urb(self->rx_urb[i]);
+       kfree(self->rx_urb);
        /* Clean up Tx and speed URB */
        usb_free_urb(self->tx_urb);
        usb_free_urb(self->speed_urb);
@@ -1550,7 +1927,6 @@ static void irda_usb_disconnect(struct usb_interface *intf)
  * USB device callbacks
  */
 static struct usb_driver irda_driver = {
-       .owner          = THIS_MODULE,
        .name           = "irda-usb",
        .probe          = irda_usb_probe,
        .disconnect     = irda_usb_disconnect,
@@ -1597,6 +1973,6 @@ module_exit(usb_irda_cleanup);
  */
 module_param(qos_mtt_bits, int, 0);
 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
-MODULE_AUTHOR("Roman Weissgaerber <weissg@vienna.at>, Dag Brattli <dag@brattli.net> and Jean Tourrilhes <jt@hpl.hp.com>");
-MODULE_DESCRIPTION("IrDA-USB Dongle Driver"); 
+MODULE_AUTHOR("Roman Weissgaerber <weissg@vienna.at>, Dag Brattli <dag@brattli.net>, Jean Tourrilhes <jt@hpl.hp.com> and Nick Fedchik <nick@fedchik.org.ua>");
+MODULE_DESCRIPTION("IrDA-USB Dongle Driver");
 MODULE_LICENSE("GPL");