Merge branch 'master' into upstream
[linux-2.6] / drivers / net / irda / irda-usb.c
1 /*****************************************************************************
2  *
3  * Filename:      irda-usb.c
4  * Version:       0.10
5  * Description:   IrDA-USB Driver
6  * Status:        Experimental 
7  * Author:        Dag Brattli <dag@brattli.net>
8  *
9  *      Copyright (C) 2000, Roman Weissgaerber <weissg@vienna.at>
10  *      Copyright (C) 2001, Dag Brattli <dag@brattli.net>
11  *      Copyright (C) 2001, Jean Tourrilhes <jt@hpl.hp.com>
12  *      Copyright (C) 2004, SigmaTel, Inc. <irquality@sigmatel.com>
13  *      Copyright (C) 2005, Milan Beno <beno@pobox.sk>
14  *      Copyright (C) 2006, Nick Fedchik <nick@fedchik.org.ua>
15  *          
16  *      This program is free software; you can redistribute it and/or modify
17  *      it under the terms of the GNU General Public License as published by
18  *      the Free Software Foundation; either version 2 of the License, or
19  *      (at your option) any later version.
20  *
21  *      This program is distributed in the hope that it will be useful,
22  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
23  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *      GNU General Public License for more details.
25  *
26  *      You should have received a copy of the GNU General Public License
27  *      along with this program; if not, write to the Free Software
28  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29  *
30  *****************************************************************************/
31
32 /*
33  *                          IMPORTANT NOTE
34  *                          --------------
35  *
36  * As of kernel 2.5.20, this is the state of compliance and testing of
37  * this driver (irda-usb) with regards to the USB low level drivers...
38  *
39  * This driver has been tested SUCCESSFULLY with the following drivers :
40  *      o usb-uhci-hcd  (For Intel/Via USB controllers)
41  *      o uhci-hcd      (Alternate/JE driver for Intel/Via USB controllers)
42  *      o ohci-hcd      (For other USB controllers)
43  *
44  * This driver has NOT been tested with the following drivers :
45  *      o ehci-hcd      (USB 2.0 controllers)
46  *
47  * Note that all HCD drivers do URB_ZERO_PACKET and timeout properly,
48  * so we don't have to worry about that anymore.
49  * One common problem is the failure to set the address on the dongle,
50  * but this happens before the driver gets loaded...
51  *
52  * Jean II
53  */
54
55 /*------------------------------------------------------------------*/
56
57 #include <linux/module.h>
58 #include <linux/moduleparam.h>
59 #include <linux/kernel.h>
60 #include <linux/types.h>
61 #include <linux/init.h>
62 #include <linux/skbuff.h>
63 #include <linux/netdevice.h>
64 #include <linux/slab.h>
65 #include <linux/rtnetlink.h>
66 #include <linux/usb.h>
67 #include <linux/firmware.h>
68
69 #include "irda-usb.h"
70
71 /*------------------------------------------------------------------*/
72
73 static int qos_mtt_bits = 0;
74
75 /* These are the currently known IrDA USB dongles. Add new dongles here */
76 static struct usb_device_id dongles[] = {
77         /* ACTiSYS Corp.,  ACT-IR2000U FIR-USB Adapter */
78         { USB_DEVICE(0x9c4, 0x011), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
79         /* Look like ACTiSYS, Report : IBM Corp., IBM UltraPort IrDA */
80         { USB_DEVICE(0x4428, 0x012), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
81         /* KC Technology Inc.,  KC-180 USB IrDA Device */
82         { USB_DEVICE(0x50f, 0x180), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
83         /* Extended Systems, Inc.,  XTNDAccess IrDA USB (ESI-9685) */
84         { USB_DEVICE(0x8e9, 0x100), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
85         /* SigmaTel STIR4210/4220/4116 USB IrDA (VFIR) Bridge */
86         { USB_DEVICE(0x66f, 0x4210), .driver_info = IUC_STIR_4210 | IUC_SPEED_BUG },
87         { USB_DEVICE(0x66f, 0x4220), .driver_info = IUC_STIR_4210 | IUC_SPEED_BUG },
88         { USB_DEVICE(0x66f, 0x4116), .driver_info = IUC_STIR_4210 | IUC_SPEED_BUG },
89         { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
90           USB_DEVICE_ID_MATCH_INT_SUBCLASS,
91           .bInterfaceClass = USB_CLASS_APP_SPEC,
92           .bInterfaceSubClass = USB_CLASS_IRDA,
93           .driver_info = IUC_DEFAULT, },
94         { }, /* The end */
95 };
96
97 /*
98  * Important note :
99  * Devices based on the SigmaTel chipset (0x66f, 0x4200) are not designed
100  * using the "USB-IrDA specification" (yes, there exist such a thing), and
101  * therefore not supported by this driver (don't add them above).
102  * There is a Linux driver, stir4200, that support those USB devices.
103  * Jean II
104  */
105
106 MODULE_DEVICE_TABLE(usb, dongles);
107
108 /*------------------------------------------------------------------*/
109
110 static void irda_usb_init_qos(struct irda_usb_cb *self) ;
111 static struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf);
112 static void irda_usb_disconnect(struct usb_interface *intf);
113 static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self);
114 static int irda_usb_hard_xmit(struct sk_buff *skb, struct net_device *dev);
115 static int irda_usb_open(struct irda_usb_cb *self);
116 static void irda_usb_close(struct irda_usb_cb *self);
117 static void speed_bulk_callback(struct urb *urb, struct pt_regs *regs);
118 static void write_bulk_callback(struct urb *urb, struct pt_regs *regs);
119 static void irda_usb_receive(struct urb *urb, struct pt_regs *regs);
120 static void irda_usb_rx_defer_expired(unsigned long data);
121 static int irda_usb_net_open(struct net_device *dev);
122 static int irda_usb_net_close(struct net_device *dev);
123 static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
124 static void irda_usb_net_timeout(struct net_device *dev);
125 static struct net_device_stats *irda_usb_net_get_stats(struct net_device *dev);
126
127 /************************ TRANSMIT ROUTINES ************************/
128 /*
129  * Receive packets from the IrDA stack and send them on the USB pipe.
130  * Handle speed change, timeout and lot's of ugliness...
131  */
132
133 /*------------------------------------------------------------------*/
134 /*
135  * Function irda_usb_build_header(self, skb, header)
136  *
137  *   Builds USB-IrDA outbound header
138  *
139  * When we send an IrDA frame over an USB pipe, we add to it a 1 byte
140  * header. This function create this header with the proper values.
141  *
142  * Important note : the USB-IrDA spec 1.0 say very clearly in chapter 5.4.2.2
143  * that the setting of the link speed and xbof number in this outbound header
144  * should be applied *AFTER* the frame has been sent.
145  * Unfortunately, some devices are not compliant with that... It seems that
146  * reading the spec is far too difficult...
147  * Jean II
148  */
149 static void irda_usb_build_header(struct irda_usb_cb *self,
150                                   __u8 *header,
151                                   int   force)
152 {
153         /* Here we check if we have an STIR421x chip,
154          * and if either speed or xbofs (or both) needs
155          * to be changed.
156          */
157         if (self->capability & IUC_STIR_4210 &&
158             ((self->new_speed != -1) || (self->new_xbofs != -1))) {
159
160                 /* With STIR421x, speed and xBOFs must be set at the same
161                  * time, even if only one of them changes.
162                  */
163                 if (self->new_speed == -1)
164                         self->new_speed = self->speed ;
165
166                 if (self->new_xbofs == -1)
167                         self->new_xbofs = self->xbofs ;
168         }
169
170         /* Set the link speed */
171         if (self->new_speed != -1) {
172                 /* Hum... Ugly hack :-(
173                  * Some device are not compliant with the spec and change
174                  * parameters *before* sending the frame. - Jean II
175                  */
176                 if ((self->capability & IUC_SPEED_BUG) &&
177                     (!force) && (self->speed != -1)) {
178                         /* No speed and xbofs change here
179                          * (we'll do it later in the write callback) */
180                         IRDA_DEBUG(2, "%s(), not changing speed yet\n", __FUNCTION__);
181                         *header = 0;
182                         return;
183                 }
184
185                 IRDA_DEBUG(2, "%s(), changing speed to %d\n", __FUNCTION__, self->new_speed);
186                 self->speed = self->new_speed;
187                 /* We will do ` self->new_speed = -1; ' in the completion
188                  * handler just in case the current URB fail - Jean II */
189
190                 switch (self->speed) {
191                 case 2400:
192                         *header = SPEED_2400;
193                         break;
194                 default:
195                 case 9600:
196                         *header = SPEED_9600;
197                         break;
198                 case 19200:
199                         *header = SPEED_19200;
200                         break;
201                 case 38400:
202                         *header = SPEED_38400;
203                         break;
204                 case 57600:
205                         *header = SPEED_57600;
206                         break;
207                 case 115200:
208                         *header = SPEED_115200;
209                         break;
210                 case 576000:
211                         *header = SPEED_576000;
212                         break;
213                 case 1152000:
214                         *header = SPEED_1152000;
215                         break;
216                 case 4000000:
217                         *header = SPEED_4000000;
218                         self->new_xbofs = 0;
219                         break;
220                 case 16000000:
221                         *header = SPEED_16000000;
222                         self->new_xbofs = 0;
223                         break;
224                 }
225         } else
226                 /* No change */
227                 *header = 0;
228         
229         /* Set the negotiated additional XBOFS */
230         if (self->new_xbofs != -1) {
231                 IRDA_DEBUG(2, "%s(), changing xbofs to %d\n", __FUNCTION__, self->new_xbofs);
232                 self->xbofs = self->new_xbofs;
233                 /* We will do ` self->new_xbofs = -1; ' in the completion
234                  * handler just in case the current URB fail - Jean II */
235
236                 switch (self->xbofs) {
237                 case 48:
238                         *header |= 0x10;
239                         break;
240                 case 28:
241                 case 24:        /* USB spec 1.0 says 24 */
242                         *header |= 0x20;
243                         break;
244                 default:
245                 case 12:
246                         *header |= 0x30;
247                         break;
248                 case 5: /* Bug in IrLAP spec? (should be 6) */
249                 case 6:
250                         *header |= 0x40;
251                         break;
252                 case 3:
253                         *header |= 0x50;
254                         break;
255                 case 2:
256                         *header |= 0x60;
257                         break;
258                 case 1:
259                         *header |= 0x70;
260                         break;
261                 case 0:
262                         *header |= 0x80;
263                         break;
264                 }
265         }
266 }
267
268 /*
269 *   calculate turnaround time for SigmaTel header
270 */
271 static __u8 get_turnaround_time(struct sk_buff *skb)
272 {
273         int turnaround_time = irda_get_mtt(skb);
274
275         if ( turnaround_time == 0 )
276                 return 0;
277         else if ( turnaround_time <= 10 )
278                 return 1;
279         else if ( turnaround_time <= 50 )
280                 return 2;
281         else if ( turnaround_time <= 100 )
282                 return 3;
283         else if ( turnaround_time <= 500 )
284                 return 4;
285         else if ( turnaround_time <= 1000 )
286                 return 5;
287         else if ( turnaround_time <= 5000 )
288                 return 6;
289         else
290                 return 7;
291 }
292
293
294 /*------------------------------------------------------------------*/
295 /*
296  * Send a command to change the speed of the dongle
297  * Need to be called with spinlock on.
298  */
299 static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self)
300 {
301         __u8 *frame;
302         struct urb *urb;
303         int ret;
304
305         IRDA_DEBUG(2, "%s(), speed=%d, xbofs=%d\n", __FUNCTION__,
306                    self->new_speed, self->new_xbofs);
307
308         /* Grab the speed URB */
309         urb = self->speed_urb;
310         if (urb->status != 0) {
311                 IRDA_WARNING("%s(), URB still in use!\n", __FUNCTION__);
312                 return;
313         }
314
315         /* Allocate the fake frame */
316         frame = self->speed_buff;
317
318         /* Set the new speed and xbofs in this fake frame */
319         irda_usb_build_header(self, frame, 1);
320
321         if ( self->capability & IUC_STIR_4210 ) {
322                 if (frame[0] == 0) return ; // do nothing if no change
323                 frame[1] = 0; // other parameters don't change here
324                 frame[2] = 0;
325         }
326
327         /* Submit the 0 length IrDA frame to trigger new speed settings */
328         usb_fill_bulk_urb(urb, self->usbdev,
329                       usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
330                       frame, IRDA_USB_SPEED_MTU,
331                       speed_bulk_callback, self);
332         urb->transfer_buffer_length = self->header_length;
333         urb->transfer_flags = 0;
334
335         /* Irq disabled -> GFP_ATOMIC */
336         if ((ret = usb_submit_urb(urb, GFP_ATOMIC))) {
337                 IRDA_WARNING("%s(), failed Speed URB\n", __FUNCTION__);
338         }
339 }
340
341 /*------------------------------------------------------------------*/
342 /*
343  * Speed URB callback
344  * Now, we can only get called for the speed URB.
345  */
346 static void speed_bulk_callback(struct urb *urb, struct pt_regs *regs)
347 {
348         struct irda_usb_cb *self = urb->context;
349         
350         IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
351
352         /* We should always have a context */
353         IRDA_ASSERT(self != NULL, return;);
354         /* We should always be called for the speed URB */
355         IRDA_ASSERT(urb == self->speed_urb, return;);
356
357         /* Check for timeout and other USB nasties */
358         if (urb->status != 0) {
359                 /* I get a lot of -ECONNABORTED = -103 here - Jean II */
360                 IRDA_DEBUG(0, "%s(), URB complete status %d, transfer_flags 0x%04X\n", __FUNCTION__, urb->status, urb->transfer_flags);
361
362                 /* Don't do anything here, that might confuse the USB layer.
363                  * Instead, we will wait for irda_usb_net_timeout(), the
364                  * network layer watchdog, to fix the situation.
365                  * Jean II */
366                 /* A reset of the dongle might be welcomed here - Jean II */
367                 return;
368         }
369
370         /* urb is now available */
371         //urb->status = 0; -> tested above
372
373         /* New speed and xbof is now commited in hardware */
374         self->new_speed = -1;
375         self->new_xbofs = -1;
376
377         /* Allow the stack to send more packets */
378         netif_wake_queue(self->netdev);
379 }
380
381 /*------------------------------------------------------------------*/
382 /*
383  * Send an IrDA frame to the USB dongle (for transmission)
384  */
385 static int irda_usb_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
386 {
387         struct irda_usb_cb *self = netdev->priv;
388         struct urb *urb = self->tx_urb;
389         unsigned long flags;
390         s32 speed;
391         s16 xbofs;
392         int res, mtt;
393         int     err = 1;        /* Failed */
394
395         IRDA_DEBUG(4, "%s() on %s\n", __FUNCTION__, netdev->name);
396
397         netif_stop_queue(netdev);
398
399         /* Protect us from USB callbacks, net watchdog and else. */
400         spin_lock_irqsave(&self->lock, flags);
401
402         /* Check if the device is still there.
403          * We need to check self->present under the spinlock because
404          * of irda_usb_disconnect() is synchronous - Jean II */
405         if (!self->present) {
406                 IRDA_DEBUG(0, "%s(), Device is gone...\n", __FUNCTION__);
407                 goto drop;
408         }
409
410         /* Check if we need to change the number of xbofs */
411         xbofs = irda_get_next_xbofs(skb);
412         if ((xbofs != self->xbofs) && (xbofs != -1)) {
413                 self->new_xbofs = xbofs;
414         }
415
416         /* Check if we need to change the speed */
417         speed = irda_get_next_speed(skb);
418         if ((speed != self->speed) && (speed != -1)) {
419                 /* Set the desired speed */
420                 self->new_speed = speed;
421
422                 /* Check for empty frame */
423                 if (!skb->len) {
424                         /* IrLAP send us an empty frame to make us change the
425                          * speed. Changing speed with the USB adapter is in
426                          * fact sending an empty frame to the adapter, so we
427                          * could just let the present function do its job.
428                          * However, we would wait for min turn time,
429                          * do an extra memcpy and increment packet counters...
430                          * Jean II */
431                         irda_usb_change_speed_xbofs(self);
432                         netdev->trans_start = jiffies;
433                         /* Will netif_wake_queue() in callback */
434                         err = 0;        /* No error */
435                         goto drop;
436                 }
437         }
438
439         if (urb->status != 0) {
440                 IRDA_WARNING("%s(), URB still in use!\n", __FUNCTION__);
441                 goto drop;
442         }
443
444         /* Make sure there is room for IrDA-USB header. The actual
445          * allocation will be done lower in skb_push().
446          * Also, we don't use directly skb_cow(), because it require
447          * headroom >= 16, which force unnecessary copies - Jean II */
448         if (skb_headroom(skb) < self->header_length) {
449                 IRDA_DEBUG(0, "%s(), Insuficient skb headroom.\n", __FUNCTION__);
450                 if (skb_cow(skb, self->header_length)) {
451                         IRDA_WARNING("%s(), failed skb_cow() !!!\n", __FUNCTION__);
452                         goto drop;
453                 }
454         }
455
456         /* Change setting for next frame */
457
458         if ( self->capability & IUC_STIR_4210 ) {
459                 __u8 turnaround_time;
460                 __u8* frame;
461                 turnaround_time = get_turnaround_time( skb );
462                 frame= skb_push(skb, self->header_length);
463                 irda_usb_build_header(self, frame, 0);
464                 frame[2] = turnaround_time;
465                 if ((skb->len != 0) &&
466                     ((skb->len % 128) == 0) &&
467                     ((skb->len % 512) != 0)) {
468                         /* add extra byte for special SigmaTel feature */
469                         frame[1] = 1;
470                         skb_put(skb, 1);
471                 } else {
472                         frame[1] = 0;
473                 }
474         } else {
475                 irda_usb_build_header(self, skb_push(skb, self->header_length), 0);
476         }
477
478         /* FIXME: Make macro out of this one */
479         ((struct irda_skb_cb *)skb->cb)->context = self;
480
481         usb_fill_bulk_urb(urb, self->usbdev, 
482                       usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
483                       skb->data, IRDA_SKB_MAX_MTU,
484                       write_bulk_callback, skb);
485         urb->transfer_buffer_length = skb->len;
486         /* This flag (URB_ZERO_PACKET) indicates that what we send is not
487          * a continuous stream of data but separate packets.
488          * In this case, the USB layer will insert an empty USB frame (TD)
489          * after each of our packets that is exact multiple of the frame size.
490          * This is how the dongle will detect the end of packet - Jean II */
491         urb->transfer_flags = URB_ZERO_PACKET;
492
493         /* Generate min turn time. FIXME: can we do better than this? */
494         /* Trying to a turnaround time at this level is trying to measure
495          * processor clock cycle with a wrist-watch, approximate at best...
496          *
497          * What we know is the last time we received a frame over USB.
498          * Due to latency over USB that depend on the USB load, we don't
499          * know when this frame was received over IrDA (a few ms before ?)
500          * Then, same story for our outgoing frame...
501          *
502          * In theory, the USB dongle is supposed to handle the turnaround
503          * by itself (spec 1.0, chater 4, page 6). Who knows ??? That's
504          * why this code is enabled only for dongles that doesn't meet
505          * the spec.
506          * Jean II */
507         if (self->capability & IUC_NO_TURN) {
508                 mtt = irda_get_mtt(skb);
509                 if (mtt) {
510                         int diff;
511                         do_gettimeofday(&self->now);
512                         diff = self->now.tv_usec - self->stamp.tv_usec;
513 #ifdef IU_USB_MIN_RTT
514                         /* Factor in USB delays -> Get rid of udelay() that
515                          * would be lost in the noise - Jean II */
516                         diff += IU_USB_MIN_RTT;
517 #endif /* IU_USB_MIN_RTT */
518                         /* If the usec counter did wraparound, the diff will
519                          * go negative (tv_usec is a long), so we need to
520                          * correct it by one second. Jean II */
521                         if (diff < 0)
522                                 diff += 1000000;
523
524                         /* Check if the mtt is larger than the time we have
525                          * already used by all the protocol processing
526                          */
527                         if (mtt > diff) {
528                                 mtt -= diff;
529                                 if (mtt > 1000)
530                                         mdelay(mtt/1000);
531                                 else
532                                         udelay(mtt);
533                         }
534                 }
535         }
536         
537         /* Ask USB to send the packet - Irq disabled -> GFP_ATOMIC */
538         if ((res = usb_submit_urb(urb, GFP_ATOMIC))) {
539                 IRDA_WARNING("%s(), failed Tx URB\n", __FUNCTION__);
540                 self->stats.tx_errors++;
541                 /* Let USB recover : We will catch that in the watchdog */
542                 /*netif_start_queue(netdev);*/
543         } else {
544                 /* Increment packet stats */
545                 self->stats.tx_packets++;
546                 self->stats.tx_bytes += skb->len;
547                 
548                 netdev->trans_start = jiffies;
549         }
550         spin_unlock_irqrestore(&self->lock, flags);
551         
552         return 0;
553
554 drop:
555         /* Drop silently the skb and exit */
556         dev_kfree_skb(skb);
557         spin_unlock_irqrestore(&self->lock, flags);
558         return err;             /* Usually 1 */
559 }
560
561 /*------------------------------------------------------------------*/
562 /*
563  * Note : this function will be called only for tx_urb...
564  */
565 static void write_bulk_callback(struct urb *urb, struct pt_regs *regs)
566 {
567         unsigned long flags;
568         struct sk_buff *skb = urb->context;
569         struct irda_usb_cb *self = ((struct irda_skb_cb *) skb->cb)->context;
570         
571         IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
572
573         /* We should always have a context */
574         IRDA_ASSERT(self != NULL, return;);
575         /* We should always be called for the speed URB */
576         IRDA_ASSERT(urb == self->tx_urb, return;);
577
578         /* Free up the skb */
579         dev_kfree_skb_any(skb);
580         urb->context = NULL;
581
582         /* Check for timeout and other USB nasties */
583         if (urb->status != 0) {
584                 /* I get a lot of -ECONNABORTED = -103 here - Jean II */
585                 IRDA_DEBUG(0, "%s(), URB complete status %d, transfer_flags 0x%04X\n", __FUNCTION__, urb->status, urb->transfer_flags);
586
587                 /* Don't do anything here, that might confuse the USB layer,
588                  * and we could go in recursion and blow the kernel stack...
589                  * Instead, we will wait for irda_usb_net_timeout(), the
590                  * network layer watchdog, to fix the situation.
591                  * Jean II */
592                 /* A reset of the dongle might be welcomed here - Jean II */
593                 return;
594         }
595
596         /* urb is now available */
597         //urb->status = 0; -> tested above
598
599         /* Make sure we read self->present properly */
600         spin_lock_irqsave(&self->lock, flags);
601
602         /* If the network is closed, stop everything */
603         if ((!self->netopen) || (!self->present)) {
604                 IRDA_DEBUG(0, "%s(), Network is gone...\n", __FUNCTION__);
605                 spin_unlock_irqrestore(&self->lock, flags);
606                 return;
607         }
608
609         /* If changes to speed or xbofs is pending... */
610         if ((self->new_speed != -1) || (self->new_xbofs != -1)) {
611                 if ((self->new_speed != self->speed) ||
612                     (self->new_xbofs != self->xbofs)) {
613                         /* We haven't changed speed yet (because of
614                          * IUC_SPEED_BUG), so do it now - Jean II */
615                         IRDA_DEBUG(1, "%s(), Changing speed now...\n", __FUNCTION__);
616                         irda_usb_change_speed_xbofs(self);
617                 } else {
618                         /* New speed and xbof is now commited in hardware */
619                         self->new_speed = -1;
620                         self->new_xbofs = -1;
621                         /* Done, waiting for next packet */
622                         netif_wake_queue(self->netdev);
623                 }
624         } else {
625                 /* Otherwise, allow the stack to send more packets */
626                 netif_wake_queue(self->netdev);
627         }
628         spin_unlock_irqrestore(&self->lock, flags);
629 }
630
631 /*------------------------------------------------------------------*/
632 /*
633  * Watchdog timer from the network layer.
634  * After a predetermined timeout, if we don't give confirmation that
635  * the packet has been sent (i.e. no call to netif_wake_queue()),
636  * the network layer will call this function.
637  * Note that URB that we submit have also a timeout. When the URB timeout
638  * expire, the normal URB callback is called (write_bulk_callback()).
639  */
640 static void irda_usb_net_timeout(struct net_device *netdev)
641 {
642         unsigned long flags;
643         struct irda_usb_cb *self = netdev->priv;
644         struct urb *urb;
645         int     done = 0;       /* If we have made any progress */
646
647         IRDA_DEBUG(0, "%s(), Network layer thinks we timed out!\n", __FUNCTION__);
648         IRDA_ASSERT(self != NULL, return;);
649
650         /* Protect us from USB callbacks, net Tx and else. */
651         spin_lock_irqsave(&self->lock, flags);
652
653         /* self->present *MUST* be read under spinlock */
654         if (!self->present) {
655                 IRDA_WARNING("%s(), device not present!\n", __FUNCTION__);
656                 netif_stop_queue(netdev);
657                 spin_unlock_irqrestore(&self->lock, flags);
658                 return;
659         }
660
661         /* Check speed URB */
662         urb = self->speed_urb;
663         if (urb->status != 0) {
664                 IRDA_DEBUG(0, "%s: Speed change timed out, urb->status=%d, urb->transfer_flags=0x%04X\n", netdev->name, urb->status, urb->transfer_flags);
665
666                 switch (urb->status) {
667                 case -EINPROGRESS:
668                         usb_unlink_urb(urb);
669                         /* Note : above will  *NOT* call netif_wake_queue()
670                          * in completion handler, we will come back here.
671                          * Jean II */
672                         done = 1;
673                         break;
674                 case -ECONNABORTED:             /* -103 */
675                 case -ECONNRESET:               /* -104 */
676                 case -ETIMEDOUT:                /* -110 */
677                 case -ENOENT:                   /* -2 (urb unlinked by us)  */
678                 default:                        /* ??? - Play safe */
679                         urb->status = 0;
680                         netif_wake_queue(self->netdev);
681                         done = 1;
682                         break;
683                 }
684         }
685
686         /* Check Tx URB */
687         urb = self->tx_urb;
688         if (urb->status != 0) {
689                 struct sk_buff *skb = urb->context;
690
691                 IRDA_DEBUG(0, "%s: Tx timed out, urb->status=%d, urb->transfer_flags=0x%04X\n", netdev->name, urb->status, urb->transfer_flags);
692
693                 /* Increase error count */
694                 self->stats.tx_errors++;
695
696 #ifdef IU_BUG_KICK_TIMEOUT
697                 /* Can't be a bad idea to reset the speed ;-) - Jean II */
698                 if(self->new_speed == -1)
699                         self->new_speed = self->speed;
700                 if(self->new_xbofs == -1)
701                         self->new_xbofs = self->xbofs;
702                 irda_usb_change_speed_xbofs(self);
703 #endif /* IU_BUG_KICK_TIMEOUT */
704
705                 switch (urb->status) {
706                 case -EINPROGRESS:
707                         usb_unlink_urb(urb);
708                         /* Note : above will  *NOT* call netif_wake_queue()
709                          * in completion handler, because urb->status will
710                          * be -ENOENT. We will fix that at the next watchdog,
711                          * leaving more time to USB to recover...
712                          * Jean II */
713                         done = 1;
714                         break;
715                 case -ECONNABORTED:             /* -103 */
716                 case -ECONNRESET:               /* -104 */
717                 case -ETIMEDOUT:                /* -110 */
718                 case -ENOENT:                   /* -2 (urb unlinked by us)  */
719                 default:                        /* ??? - Play safe */
720                         if(skb != NULL) {
721                                 dev_kfree_skb_any(skb);
722                                 urb->context = NULL;
723                         }
724                         urb->status = 0;
725                         netif_wake_queue(self->netdev);
726                         done = 1;
727                         break;
728                 }
729         }
730         spin_unlock_irqrestore(&self->lock, flags);
731
732         /* Maybe we need a reset */
733         /* Note : Some drivers seem to use a usb_set_interface() when they
734          * need to reset the hardware. Hum...
735          */
736
737         /* if(done == 0) */
738 }
739
740 /************************* RECEIVE ROUTINES *************************/
741 /*
742  * Receive packets from the USB layer stack and pass them to the IrDA stack.
743  * Try to work around USB failures...
744  */
745
746 /*
747  * Note :
748  * Some of you may have noticed that most dongle have an interrupt in pipe
749  * that we don't use. Here is the little secret...
750  * When we hang a Rx URB on the bulk in pipe, it generates some USB traffic
751  * in every USB frame. This is unnecessary overhead.
752  * The interrupt in pipe will generate an event every time a packet is
753  * received. Reading an interrupt pipe adds minimal overhead, but has some
754  * latency (~1ms).
755  * If we are connected (speed != 9600), we want to minimise latency, so
756  * we just always hang the Rx URB and ignore the interrupt.
757  * If we are not connected (speed == 9600), there is usually no Rx traffic,
758  * and we want to minimise the USB overhead. In this case we should wait
759  * on the interrupt pipe and hang the Rx URB only when an interrupt is
760  * received.
761  * Jean II
762  *
763  * Note : don't read the above as what we are currently doing, but as
764  * something we could do with KC dongle. Also don't forget that the
765  * interrupt pipe is not part of the original standard, so this would
766  * need to be optional...
767  * Jean II
768  */
769
770 /*------------------------------------------------------------------*/
771 /*
772  * Submit a Rx URB to the USB layer to handle reception of a frame
773  * Mostly called by the completion callback of the previous URB.
774  *
775  * Jean II
776  */
777 static void irda_usb_submit(struct irda_usb_cb *self, struct sk_buff *skb, struct urb *urb)
778 {
779         struct irda_skb_cb *cb;
780         int ret;
781
782         IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
783
784         /* This should never happen */
785         IRDA_ASSERT(skb != NULL, return;);
786         IRDA_ASSERT(urb != NULL, return;);
787
788         /* Save ourselves in the skb */
789         cb = (struct irda_skb_cb *) skb->cb;
790         cb->context = self;
791
792         /* Reinitialize URB */
793         usb_fill_bulk_urb(urb, self->usbdev, 
794                       usb_rcvbulkpipe(self->usbdev, self->bulk_in_ep), 
795                       skb->data, IRDA_SKB_MAX_MTU,
796                       irda_usb_receive, skb);
797         urb->status = 0;
798
799         /* Can be called from irda_usb_receive (irq handler) -> GFP_ATOMIC */
800         ret = usb_submit_urb(urb, GFP_ATOMIC);
801         if (ret) {
802                 /* If this ever happen, we are in deep s***.
803                  * Basically, the Rx path will stop... */
804                 IRDA_WARNING("%s(), Failed to submit Rx URB %d\n",
805                              __FUNCTION__, ret);
806         }
807 }
808
809 /*------------------------------------------------------------------*/
810 /*
811  * Function irda_usb_receive(urb)
812  *
813  *     Called by the USB subsystem when a frame has been received
814  *
815  */
816 static void irda_usb_receive(struct urb *urb, struct pt_regs *regs)
817 {
818         struct sk_buff *skb = (struct sk_buff *) urb->context;
819         struct irda_usb_cb *self; 
820         struct irda_skb_cb *cb;
821         struct sk_buff *newskb;
822         struct sk_buff *dataskb;
823         struct urb *next_urb;
824         unsigned int len, docopy;
825
826         IRDA_DEBUG(2, "%s(), len=%d\n", __FUNCTION__, urb->actual_length);
827         
828         /* Find ourselves */
829         cb = (struct irda_skb_cb *) skb->cb;
830         IRDA_ASSERT(cb != NULL, return;);
831         self = (struct irda_usb_cb *) cb->context;
832         IRDA_ASSERT(self != NULL, return;);
833
834         /* If the network is closed or the device gone, stop everything */
835         if ((!self->netopen) || (!self->present)) {
836                 IRDA_DEBUG(0, "%s(), Network is gone!\n", __FUNCTION__);
837                 /* Don't re-submit the URB : will stall the Rx path */
838                 return;
839         }
840         
841         /* Check the status */
842         if (urb->status != 0) {
843                 switch (urb->status) {
844                 case -EILSEQ:
845                         self->stats.rx_crc_errors++;    
846                         /* Also precursor to a hot-unplug on UHCI. */
847                         /* Fallthrough... */
848                 case -ECONNRESET:               /* -104 */
849                         /* Random error, if I remember correctly */
850                         /* uhci_cleanup_unlink() is going to kill the Rx
851                          * URB just after we return. No problem, at this
852                          * point the URB will be idle ;-) - Jean II */
853                 case -ESHUTDOWN:                /* -108 */
854                         /* That's usually a hot-unplug. Submit will fail... */
855                 case -ETIMEDOUT:                /* -110 */
856                         /* Usually precursor to a hot-unplug on OHCI. */
857                 default:
858                         self->stats.rx_errors++;
859                         IRDA_DEBUG(0, "%s(), RX status %d, transfer_flags 0x%04X \n", __FUNCTION__, urb->status, urb->transfer_flags);
860                         break;
861                 }
862                 /* If we received an error, we don't want to resubmit the
863                  * Rx URB straight away but to give the USB layer a little
864                  * bit of breathing room.
865                  * We are in the USB thread context, therefore there is a
866                  * danger of recursion (new URB we submit fails, we come
867                  * back here).
868                  * With recent USB stack (2.6.15+), I'm seeing that on
869                  * hot unplug of the dongle...
870                  * Lowest effective timer is 10ms...
871                  * Jean II */
872                 self->rx_defer_timer.function = &irda_usb_rx_defer_expired;
873                 self->rx_defer_timer.data = (unsigned long) urb;
874                 mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
875                 return;
876         }
877         
878         /* Check for empty frames */
879         if (urb->actual_length <= self->header_length) {
880                 IRDA_WARNING("%s(), empty frame!\n", __FUNCTION__);
881                 goto done;
882         }
883
884         /*  
885          * Remember the time we received this frame, so we can
886          * reduce the min turn time a bit since we will know
887          * how much time we have used for protocol processing
888          */
889         do_gettimeofday(&self->stamp);
890
891         /* Check if we need to copy the data to a new skb or not.
892          * For most frames, we use ZeroCopy and pass the already
893          * allocated skb up the stack.
894          * If the frame is small, it is more efficient to copy it
895          * to save memory (copy will be fast anyway - that's
896          * called Rx-copy-break). Jean II */
897         docopy = (urb->actual_length < IRDA_RX_COPY_THRESHOLD);
898
899         /* Allocate a new skb */
900         if ( self->capability & IUC_STIR_4210 )
901                 newskb = dev_alloc_skb(docopy ? urb->actual_length : IRDA_SKB_MAX_MTU + USB_IRDA_SIGMATEL_HEADER);
902         else
903                 newskb = dev_alloc_skb(docopy ? urb->actual_length : IRDA_SKB_MAX_MTU);
904
905         if (!newskb)  {
906                 self->stats.rx_dropped++;
907                 /* We could deliver the current skb, but this would stall
908                  * the Rx path. Better drop the packet... Jean II */
909                 goto done;  
910         }
911
912         /* Make sure IP header get aligned (IrDA header is 5 bytes) */
913         /* But IrDA-USB header is 1 byte. Jean II */
914         //skb_reserve(newskb, USB_IRDA_HEADER - 1);
915
916         if(docopy) {
917                 /* Copy packet, so we can recycle the original */
918                 memcpy(newskb->data, skb->data, urb->actual_length);
919                 /* Deliver this new skb */
920                 dataskb = newskb;
921                 /* And hook the old skb to the URB
922                  * Note : we don't need to "clean up" the old skb,
923                  * as we never touched it. Jean II */
924         } else {
925                 /* We are using ZeroCopy. Deliver old skb */
926                 dataskb = skb;
927                 /* And hook the new skb to the URB */
928                 skb = newskb;
929         }
930
931         /* Set proper length on skb & remove USB-IrDA header */
932         skb_put(dataskb, urb->actual_length);
933         skb_pull(dataskb, self->header_length);
934
935         /* Ask the networking layer to queue the packet for the IrDA stack */
936         dataskb->dev = self->netdev;
937         dataskb->mac.raw  = dataskb->data;
938         dataskb->protocol = htons(ETH_P_IRDA);
939         len = dataskb->len;
940         netif_rx(dataskb);
941
942         /* Keep stats up to date */
943         self->stats.rx_bytes += len;
944         self->stats.rx_packets++;
945         self->netdev->last_rx = jiffies;
946
947 done:
948         /* Note : at this point, the URB we've just received (urb)
949          * is still referenced by the USB layer. For example, if we
950          * have received a -ECONNRESET, uhci_cleanup_unlink() will
951          * continue to process it (in fact, cleaning it up).
952          * If we were to submit this URB, disaster would ensue.
953          * Therefore, we submit our idle URB, and put this URB in our
954          * idle slot....
955          * Jean II */
956         /* Note : with this scheme, we could submit the idle URB before
957          * processing the Rx URB. I don't think it would buy us anything as
958          * we are running in the USB thread context. Jean II */
959         next_urb = self->idle_rx_urb;
960
961         /* Recycle Rx URB : Now, the idle URB is the present one */
962         urb->context = NULL;
963         self->idle_rx_urb = urb;
964
965         /* Submit the idle URB to replace the URB we've just received.
966          * Do it last to avoid race conditions... Jean II */
967         irda_usb_submit(self, skb, next_urb);
968 }
969
970 /*------------------------------------------------------------------*/
971 /*
972  * In case of errors, we want the USB layer to have time to recover.
973  * Now, it is time to resubmit ouur Rx URB...
974  */
975 static void irda_usb_rx_defer_expired(unsigned long data)
976 {
977         struct urb *urb = (struct urb *) data;
978         struct sk_buff *skb = (struct sk_buff *) urb->context;
979         struct irda_usb_cb *self; 
980         struct irda_skb_cb *cb;
981         struct urb *next_urb;
982
983         IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
984
985         /* Find ourselves */
986         cb = (struct irda_skb_cb *) skb->cb;
987         IRDA_ASSERT(cb != NULL, return;);
988         self = (struct irda_usb_cb *) cb->context;
989         IRDA_ASSERT(self != NULL, return;);
990
991         /* Same stuff as when Rx is done, see above... */
992         next_urb = self->idle_rx_urb;
993         urb->context = NULL;
994         self->idle_rx_urb = urb;
995         irda_usb_submit(self, skb, next_urb);
996 }
997
998 /*------------------------------------------------------------------*/
999 /*
1000  * Callbak from IrDA layer. IrDA wants to know if we have
1001  * started receiving anything.
1002  */
1003 static int irda_usb_is_receiving(struct irda_usb_cb *self)
1004 {
1005         /* Note : because of the way UHCI works, it's almost impossible
1006          * to get this info. The Controller DMA directly to memory and
1007          * signal only when the whole frame is finished. To know if the
1008          * first TD of the URB has been filled or not seems hard work...
1009          *
1010          * The other solution would be to use the "receiving" command
1011          * on the default decriptor with a usb_control_msg(), but that
1012          * would add USB traffic and would return result only in the
1013          * next USB frame (~1ms).
1014          *
1015          * I've been told that current dongles send status info on their
1016          * interrupt endpoint, and that's what the Windows driver uses
1017          * to know this info. Unfortunately, this is not yet in the spec...
1018          *
1019          * Jean II
1020          */
1021
1022         return 0; /* For now */
1023 }
1024
1025
1026 #define STIR421X_PATCH_PRODUCT_VERSION_STR       "Product Version: "
1027 #define STIR421X_PATCH_COMPONENT_VERSION_STR     "Component Version: "
1028 #define STIR421X_PATCH_DATA_TAG_STR              "STMP"
1029 #define STIR421X_PATCH_FILE_VERSION_MAX_OFFSET   512     /* version info is before here */
1030 #define STIR421X_PATCH_FILE_IMAGE_MAX_OFFSET     512     /* patch image starts before here */
1031 #define STIR421X_PATCH_FILE_END_OF_HEADER_TAG    0x1A    /* marks end of patch file header (PC DOS text file EOF character) */
1032
1033 /*
1034  * Known firmware patches for STIR421x dongles
1035  */
1036 static char * stir421x_patches[] = {
1037         "42101001.sb",
1038         "42101002.sb",
1039 };
1040
1041 static int stir421x_get_patch_version(unsigned char * patch, const unsigned long patch_len)
1042 {
1043         unsigned int version_offset;
1044         unsigned long version_major, version_minor, version_build;
1045         unsigned char * version_start;
1046         int version_found = 0;
1047
1048         for (version_offset = 0;
1049              version_offset < STIR421X_PATCH_FILE_END_OF_HEADER_TAG;
1050              version_offset++) {
1051                 if (!memcmp(patch + version_offset,
1052                             STIR421X_PATCH_PRODUCT_VERSION_STR,
1053                             sizeof(STIR421X_PATCH_PRODUCT_VERSION_STR) - 1)) {
1054                                     version_found = 1;
1055                                     version_start = patch +
1056                                             version_offset +
1057                                             sizeof(STIR421X_PATCH_PRODUCT_VERSION_STR) - 1;
1058                                     break;
1059                 }
1060         }
1061
1062         /* We couldn't find a product version on this patch */
1063         if (!version_found)
1064                 return -EINVAL;
1065
1066         /* Let's check if the product version is dotted */
1067         if (version_start[3] != '.' ||
1068             version_start[7] != '.')
1069                 return -EINVAL;
1070
1071         version_major = simple_strtoul(version_start, NULL, 10);
1072         version_minor = simple_strtoul(version_start + 4, NULL, 10);
1073         version_build = simple_strtoul(version_start + 8, NULL, 10);
1074
1075         IRDA_DEBUG(2, "%s(), Major: %ld Minor: %ld Build: %ld\n",
1076                    __FUNCTION__,
1077                    version_major, version_minor, version_build);
1078
1079         return (((version_major) << 12) +
1080                 ((version_minor) << 8) +
1081                 ((version_build / 10) << 4) +
1082                 (version_build % 10));
1083
1084 }
1085
1086
1087 static int stir421x_upload_patch (struct irda_usb_cb *self,
1088                                   unsigned char * patch,
1089                                   const unsigned int patch_len)
1090 {
1091     int retval = 0;
1092     int actual_len;
1093     unsigned int i = 0, download_amount = 0;
1094     unsigned char * patch_chunk;
1095
1096     IRDA_DEBUG (2, "%s(), Uploading STIR421x Patch\n", __FUNCTION__);
1097
1098     patch_chunk = kzalloc(STIR421X_MAX_PATCH_DOWNLOAD_SIZE, GFP_KERNEL);
1099     if (patch_chunk == NULL)
1100             return -ENOMEM;
1101
1102     /* break up patch into 1023-byte sections */
1103     for (i = 0; retval >= 0 && i < patch_len; i += download_amount) {
1104             download_amount = patch_len - i;
1105             if (download_amount > STIR421X_MAX_PATCH_DOWNLOAD_SIZE)
1106                     download_amount = STIR421X_MAX_PATCH_DOWNLOAD_SIZE;
1107
1108             /* download the patch section */
1109             memcpy(patch_chunk, patch + i, download_amount);
1110
1111             retval = usb_bulk_msg (self->usbdev,
1112                                    usb_sndbulkpipe (self->usbdev,
1113                                                     self->bulk_out_ep),
1114                                    patch_chunk, download_amount,
1115                                    &actual_len, msecs_to_jiffies (500));
1116             IRDA_DEBUG (2, "%s(), Sent %u bytes\n", __FUNCTION__,
1117                         actual_len);
1118             if (retval == 0)
1119                     mdelay(10);
1120     }
1121
1122     kfree(patch_chunk);
1123
1124     if (i != patch_len) {
1125             IRDA_ERROR ("%s(), Pushed %d bytes (!= patch_len (%d))\n",
1126                        __FUNCTION__, i, patch_len);
1127             retval = -EIO;
1128     }
1129
1130     if (retval < 0)
1131             /* todo - mark device as not ready */
1132             IRDA_ERROR ("%s(), STIR421x patch upload failed (%d)\n",
1133                         __FUNCTION__, retval);
1134
1135     return retval;
1136 }
1137
1138
1139 static int stir421x_patch_device(struct irda_usb_cb *self)
1140 {
1141         unsigned int i, patch_found = 0, data_found = 0, data_offset;
1142         int patch_version, ret = 0;
1143         const struct firmware *fw_entry;
1144
1145         for (i = 0; i < ARRAY_SIZE(stir421x_patches); i++) {
1146                 if(request_firmware(&fw_entry, stir421x_patches[i], &self->usbdev->dev) != 0) {
1147                         IRDA_ERROR( "%s(), Patch %s is not available\n", __FUNCTION__, stir421x_patches[i]);
1148                         continue;
1149                 }
1150
1151                 /* We found a patch from userspace */
1152                 patch_version = stir421x_get_patch_version (fw_entry->data, fw_entry->size);
1153
1154                 if (patch_version < 0) {
1155                         /* Couldn't fetch a version, let's move on to the next file */
1156                         IRDA_ERROR("%s(), version parsing failed\n", __FUNCTION__);
1157                         ret = patch_version;
1158                         release_firmware(fw_entry);
1159                         continue;
1160                 }
1161
1162                 if (patch_version != self->usbdev->descriptor.bcdDevice) {
1163                         /* Patch version and device don't match */
1164                         IRDA_ERROR ("%s(), wrong patch version (%d <-> %d)\n",
1165                                     __FUNCTION__,
1166                                     patch_version, self->usbdev->descriptor.bcdDevice);
1167                         ret = -EINVAL;
1168                         release_firmware(fw_entry);
1169                         continue;
1170                 }
1171
1172                 /* If we're here, we've found a correct patch */
1173                 patch_found = 1;
1174                 break;
1175
1176         }
1177
1178         /* We couldn't find a valid firmware, let's leave */
1179         if (!patch_found)
1180                 return ret;
1181
1182         /* The actual image starts after the "STMP" keyword */
1183         for (data_offset = 0; data_offset < STIR421X_PATCH_FILE_IMAGE_MAX_OFFSET; data_offset++) {
1184                 if (!memcmp(fw_entry->data + data_offset,
1185                             STIR421X_PATCH_DATA_TAG_STR,
1186                             sizeof(STIR421X_PATCH_FILE_IMAGE_MAX_OFFSET))) {
1187                         IRDA_DEBUG(2, "%s(), found patch data for STIR421x at offset %d\n",
1188                                    __FUNCTION__, data_offset);
1189                         data_found = 1;
1190                         break;
1191                 }
1192         }
1193
1194         /* We couldn't find "STMP" from the header */
1195         if (!data_found)
1196                 return -EINVAL;
1197
1198         /* Let's upload the patch to the target */
1199         ret = stir421x_upload_patch(self,
1200                                     &fw_entry->data[data_offset + sizeof(STIR421X_PATCH_FILE_IMAGE_MAX_OFFSET)],
1201                                     fw_entry->size - (data_offset + sizeof(STIR421X_PATCH_FILE_IMAGE_MAX_OFFSET)));
1202
1203         release_firmware(fw_entry);
1204
1205         return ret;
1206
1207 }
1208
1209
1210 /********************** IRDA DEVICE CALLBACKS **********************/
1211 /*
1212  * Main calls from the IrDA/Network subsystem.
1213  * Mostly registering a new irda-usb device and removing it....
1214  * We only deal with the IrDA side of the business, the USB side will
1215  * be dealt with below...
1216  */
1217
1218
1219 /*------------------------------------------------------------------*/
1220 /*
1221  * Function irda_usb_net_open (dev)
1222  *
1223  *    Network device is taken up. Usually this is done by "ifconfig irda0 up" 
1224  *   
1225  * Note : don't mess with self->netopen - Jean II
1226  */
1227 static int irda_usb_net_open(struct net_device *netdev)
1228 {
1229         struct irda_usb_cb *self;
1230         char    hwname[16];
1231         int i;
1232         
1233         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1234
1235         IRDA_ASSERT(netdev != NULL, return -1;);
1236         self = (struct irda_usb_cb *) netdev->priv;
1237         IRDA_ASSERT(self != NULL, return -1;);
1238
1239         /* Can only open the device if it's there */
1240         if(!self->present) {
1241                 IRDA_WARNING("%s(), device not present!\n", __FUNCTION__);
1242                 return -1;
1243         }
1244
1245         if(self->needspatch) {
1246                 IRDA_WARNING("%s(), device needs patch\n", __FUNCTION__) ;
1247                 return -EIO ;
1248         }
1249
1250         /* Initialise default speed and xbofs value
1251          * (IrLAP will change that soon) */
1252         self->speed = -1;
1253         self->xbofs = -1;
1254         self->new_speed = -1;
1255         self->new_xbofs = -1;
1256
1257         /* To do *before* submitting Rx urbs and starting net Tx queue
1258          * Jean II */
1259         self->netopen = 1;
1260
1261         /* 
1262          * Now that everything should be initialized properly,
1263          * Open new IrLAP layer instance to take care of us...
1264          * Note : will send immediately a speed change...
1265          */
1266         sprintf(hwname, "usb#%d", self->usbdev->devnum);
1267         self->irlap = irlap_open(netdev, &self->qos, hwname);
1268         IRDA_ASSERT(self->irlap != NULL, return -1;);
1269
1270         /* Allow IrLAP to send data to us */
1271         netif_start_queue(netdev);
1272
1273         /* We submit all the Rx URB except for one that we keep idle.
1274          * Need to be initialised before submitting other USBs, because
1275          * in some cases as soon as we submit the URBs the USB layer
1276          * will trigger a dummy receive - Jean II */
1277         self->idle_rx_urb = self->rx_urb[IU_MAX_ACTIVE_RX_URBS];
1278         self->idle_rx_urb->context = NULL;
1279
1280         /* Now that we can pass data to IrLAP, allow the USB layer
1281          * to send us some data... */
1282         for (i = 0; i < IU_MAX_ACTIVE_RX_URBS; i++) {
1283                 struct sk_buff *skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
1284                 if (!skb) {
1285                         /* If this ever happen, we are in deep s***.
1286                          * Basically, we can't start the Rx path... */
1287                         IRDA_WARNING("%s(), Failed to allocate Rx skb\n",
1288                                      __FUNCTION__);
1289                         return -1;
1290                 }
1291                 //skb_reserve(newskb, USB_IRDA_HEADER - 1);
1292                 irda_usb_submit(self, skb, self->rx_urb[i]);
1293         }
1294
1295         /* Ready to play !!! */
1296         return 0;
1297 }
1298
1299 /*------------------------------------------------------------------*/
1300 /*
1301  * Function irda_usb_net_close (self)
1302  *
1303  *    Network device is taken down. Usually this is done by 
1304  *    "ifconfig irda0 down" 
1305  */
1306 static int irda_usb_net_close(struct net_device *netdev)
1307 {
1308         struct irda_usb_cb *self;
1309         int     i;
1310
1311         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1312
1313         IRDA_ASSERT(netdev != NULL, return -1;);
1314         self = (struct irda_usb_cb *) netdev->priv;
1315         IRDA_ASSERT(self != NULL, return -1;);
1316
1317         /* Clear this flag *before* unlinking the urbs and *before*
1318          * stopping the network Tx queue - Jean II */
1319         self->netopen = 0;
1320
1321         /* Stop network Tx queue */
1322         netif_stop_queue(netdev);
1323
1324         /* Kill defered Rx URB */
1325         del_timer(&self->rx_defer_timer);
1326
1327         /* Deallocate all the Rx path buffers (URBs and skb) */
1328         for (i = 0; i < self->max_rx_urb; i++) {
1329                 struct urb *urb = self->rx_urb[i];
1330                 struct sk_buff *skb = (struct sk_buff *) urb->context;
1331                 /* Cancel the receive command */
1332                 usb_kill_urb(urb);
1333                 /* The skb is ours, free it */
1334                 if(skb) {
1335                         dev_kfree_skb(skb);
1336                         urb->context = NULL;
1337                 }
1338         }
1339         /* Cancel Tx and speed URB - need to be synchronous to avoid races */
1340         usb_kill_urb(self->tx_urb);
1341         usb_kill_urb(self->speed_urb);
1342
1343         /* Stop and remove instance of IrLAP */
1344         if (self->irlap)
1345                 irlap_close(self->irlap);
1346         self->irlap = NULL;
1347
1348         return 0;
1349 }
1350
1351 /*------------------------------------------------------------------*/
1352 /*
1353  * IOCTLs : Extra out-of-band network commands...
1354  */
1355 static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1356 {
1357         unsigned long flags;
1358         struct if_irda_req *irq = (struct if_irda_req *) rq;
1359         struct irda_usb_cb *self;
1360         int ret = 0;
1361
1362         IRDA_ASSERT(dev != NULL, return -1;);
1363         self = dev->priv;
1364         IRDA_ASSERT(self != NULL, return -1;);
1365
1366         IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
1367
1368         switch (cmd) {
1369         case SIOCSBANDWIDTH: /* Set bandwidth */
1370                 if (!capable(CAP_NET_ADMIN))
1371                         return -EPERM;
1372                 /* Protect us from USB callbacks, net watchdog and else. */
1373                 spin_lock_irqsave(&self->lock, flags);
1374                 /* Check if the device is still there */
1375                 if(self->present) {
1376                         /* Set the desired speed */
1377                         self->new_speed = irq->ifr_baudrate;
1378                         irda_usb_change_speed_xbofs(self);
1379                 }
1380                 spin_unlock_irqrestore(&self->lock, flags);
1381                 break;
1382         case SIOCSMEDIABUSY: /* Set media busy */
1383                 if (!capable(CAP_NET_ADMIN))
1384                         return -EPERM;
1385                 /* Check if the IrDA stack is still there */
1386                 if(self->netopen)
1387                         irda_device_set_media_busy(self->netdev, TRUE);
1388                 break;
1389         case SIOCGRECEIVING: /* Check if we are receiving right now */
1390                 irq->ifr_receiving = irda_usb_is_receiving(self);
1391                 break;
1392         default:
1393                 ret = -EOPNOTSUPP;
1394         }
1395         
1396         return ret;
1397 }
1398
1399 /*------------------------------------------------------------------*/
1400 /*
1401  * Get device stats (for /proc/net/dev and ifconfig)
1402  */
1403 static struct net_device_stats *irda_usb_net_get_stats(struct net_device *dev)
1404 {
1405         struct irda_usb_cb *self = dev->priv;
1406         return &self->stats;
1407 }
1408
1409 /********************* IRDA CONFIG SUBROUTINES *********************/
1410 /*
1411  * Various subroutines dealing with IrDA and network stuff we use to
1412  * configure and initialise each irda-usb instance.
1413  * These functions are used below in the main calls of the driver...
1414  */
1415
1416 /*------------------------------------------------------------------*/
1417 /*
1418  * Set proper values in the IrDA QOS structure
1419  */
1420 static inline void irda_usb_init_qos(struct irda_usb_cb *self)
1421 {
1422         struct irda_class_desc *desc;
1423
1424         IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
1425         
1426         desc = self->irda_desc;
1427         
1428         /* Initialize QoS for this device */
1429         irda_init_max_qos_capabilies(&self->qos);
1430
1431         /* See spec section 7.2 for meaning.
1432          * Values are little endian (as most USB stuff), the IrDA stack
1433          * use it in native order (see parameters.c). - Jean II */
1434         self->qos.baud_rate.bits       = le16_to_cpu(desc->wBaudRate);
1435         self->qos.min_turn_time.bits   = desc->bmMinTurnaroundTime;
1436         self->qos.additional_bofs.bits = desc->bmAdditionalBOFs;
1437         self->qos.window_size.bits     = desc->bmWindowSize;
1438         self->qos.data_size.bits       = desc->bmDataSize;
1439
1440         IRDA_DEBUG(0, "%s(), dongle says speed=0x%X, size=0x%X, window=0x%X, bofs=0x%X, turn=0x%X\n", 
1441                 __FUNCTION__, self->qos.baud_rate.bits, self->qos.data_size.bits, self->qos.window_size.bits, self->qos.additional_bofs.bits, self->qos.min_turn_time.bits);
1442
1443         /* Don't always trust what the dongle tell us */
1444         if(self->capability & IUC_SIR_ONLY)
1445                 self->qos.baud_rate.bits        &= 0x00ff;
1446         if(self->capability & IUC_SMALL_PKT)
1447                 self->qos.data_size.bits         = 0x07;
1448         if(self->capability & IUC_NO_WINDOW)
1449                 self->qos.window_size.bits       = 0x01;
1450         if(self->capability & IUC_MAX_WINDOW)
1451                 self->qos.window_size.bits       = 0x7f;
1452         if(self->capability & IUC_MAX_XBOFS)
1453                 self->qos.additional_bofs.bits   = 0x01;
1454
1455 #if 1
1456         /* Module parameter can override the rx window size */
1457         if (qos_mtt_bits)
1458                 self->qos.min_turn_time.bits = qos_mtt_bits;
1459 #endif      
1460         /* 
1461          * Note : most of those values apply only for the receive path,
1462          * the transmit path will be set differently - Jean II 
1463          */
1464         irda_qos_bits_to_value(&self->qos);
1465 }
1466
1467 /*------------------------------------------------------------------*/
1468 /*
1469  * Initialise the network side of the irda-usb instance
1470  * Called when a new USB instance is registered in irda_usb_probe()
1471  */
1472 static inline int irda_usb_open(struct irda_usb_cb *self)
1473 {
1474         struct net_device *netdev = self->netdev;
1475
1476         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1477
1478         irda_usb_init_qos(self);
1479
1480         /* Override the network functions we need to use */
1481         netdev->hard_start_xmit = irda_usb_hard_xmit;
1482         netdev->tx_timeout      = irda_usb_net_timeout;
1483         netdev->watchdog_timeo  = 250*HZ/1000;  /* 250 ms > USB timeout */
1484         netdev->open            = irda_usb_net_open;
1485         netdev->stop            = irda_usb_net_close;
1486         netdev->get_stats       = irda_usb_net_get_stats;
1487         netdev->do_ioctl        = irda_usb_net_ioctl;
1488
1489         return register_netdev(netdev);
1490 }
1491
1492 /*------------------------------------------------------------------*/
1493 /*
1494  * Cleanup the network side of the irda-usb instance
1495  * Called when a USB instance is removed in irda_usb_disconnect()
1496  */
1497 static inline void irda_usb_close(struct irda_usb_cb *self)
1498 {
1499         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1500
1501         /* Remove netdevice */
1502         unregister_netdev(self->netdev);
1503
1504         /* Remove the speed buffer */
1505         kfree(self->speed_buff);
1506         self->speed_buff = NULL;
1507 }
1508
1509 /********************** USB CONFIG SUBROUTINES **********************/
1510 /*
1511  * Various subroutines dealing with USB stuff we use to configure and
1512  * initialise each irda-usb instance.
1513  * These functions are used below in the main calls of the driver...
1514  */
1515
1516 /*------------------------------------------------------------------*/
1517 /*
1518  * Function irda_usb_parse_endpoints(dev, ifnum)
1519  *
1520  *    Parse the various endpoints and find the one we need.
1521  *
1522  * The endpoint are the pipes used to communicate with the USB device.
1523  * The spec defines 2 endpoints of type bulk transfer, one in, and one out.
1524  * These are used to pass frames back and forth with the dongle.
1525  * Most dongle have also an interrupt endpoint, that will be probably
1526  * documented in the next spec...
1527  */
1528 static inline int irda_usb_parse_endpoints(struct irda_usb_cb *self, struct usb_host_endpoint *endpoint, int ennum)
1529 {
1530         int i;          /* Endpoint index in table */
1531                 
1532         /* Init : no endpoints */
1533         self->bulk_in_ep = 0;
1534         self->bulk_out_ep = 0;
1535         self->bulk_int_ep = 0;
1536
1537         /* Let's look at all those endpoints */
1538         for(i = 0; i < ennum; i++) {
1539                 /* All those variables will get optimised by the compiler,
1540                  * so let's aim for clarity... - Jean II */
1541                 __u8 ep;        /* Endpoint address */
1542                 __u8 dir;       /* Endpoint direction */
1543                 __u8 attr;      /* Endpoint attribute */
1544                 __u16 psize;    /* Endpoint max packet size in bytes */
1545
1546                 /* Get endpoint address, direction and attribute */
1547                 ep = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1548                 dir = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK;
1549                 attr = endpoint[i].desc.bmAttributes;
1550                 psize = le16_to_cpu(endpoint[i].desc.wMaxPacketSize);
1551
1552                 /* Is it a bulk endpoint ??? */
1553                 if(attr == USB_ENDPOINT_XFER_BULK) {
1554                         /* We need to find an IN and an OUT */
1555                         if(dir == USB_DIR_IN) {
1556                                 /* This is our Rx endpoint */
1557                                 self->bulk_in_ep = ep;
1558                         } else {
1559                                 /* This is our Tx endpoint */
1560                                 self->bulk_out_ep = ep;
1561                                 self->bulk_out_mtu = psize;
1562                         }
1563                 } else {
1564                         if((attr == USB_ENDPOINT_XFER_INT) &&
1565                            (dir == USB_DIR_IN)) {
1566                                 /* This is our interrupt endpoint */
1567                                 self->bulk_int_ep = ep;
1568                         } else {
1569                                 IRDA_ERROR("%s(), Unrecognised endpoint %02X.\n", __FUNCTION__, ep);
1570                         }
1571                 }
1572         }
1573
1574         IRDA_DEBUG(0, "%s(), And our endpoints are : in=%02X, out=%02X (%d), int=%02X\n",
1575                 __FUNCTION__, self->bulk_in_ep, self->bulk_out_ep, self->bulk_out_mtu, self->bulk_int_ep);
1576         /* Should be 8, 16, 32 or 64 bytes */
1577         IRDA_ASSERT(self->bulk_out_mtu == 64, ;);
1578
1579         return((self->bulk_in_ep != 0) && (self->bulk_out_ep != 0));
1580 }
1581
1582 #ifdef IU_DUMP_CLASS_DESC
1583 /*------------------------------------------------------------------*/
1584 /*
1585  * Function usb_irda_dump_class_desc(desc)
1586  *
1587  *    Prints out the contents of the IrDA class descriptor
1588  *
1589  */
1590 static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
1591 {
1592         /* Values are little endian */
1593         printk("bLength=%x\n", desc->bLength);
1594         printk("bDescriptorType=%x\n", desc->bDescriptorType);
1595         printk("bcdSpecRevision=%x\n", le16_to_cpu(desc->bcdSpecRevision)); 
1596         printk("bmDataSize=%x\n", desc->bmDataSize);
1597         printk("bmWindowSize=%x\n", desc->bmWindowSize);
1598         printk("bmMinTurnaroundTime=%d\n", desc->bmMinTurnaroundTime);
1599         printk("wBaudRate=%x\n", le16_to_cpu(desc->wBaudRate));
1600         printk("bmAdditionalBOFs=%x\n", desc->bmAdditionalBOFs);
1601         printk("bIrdaRateSniff=%x\n", desc->bIrdaRateSniff);
1602         printk("bMaxUnicastList=%x\n", desc->bMaxUnicastList);
1603 }
1604 #endif /* IU_DUMP_CLASS_DESC */
1605
1606 /*------------------------------------------------------------------*/
1607 /*
1608  * Function irda_usb_find_class_desc(intf)
1609  *
1610  *    Returns instance of IrDA class descriptor, or NULL if not found
1611  *
1612  * The class descriptor is some extra info that IrDA USB devices will
1613  * offer to us, describing their IrDA characteristics. We will use that in
1614  * irda_usb_init_qos()
1615  */
1616 static inline struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf)
1617 {
1618         struct usb_device *dev = interface_to_usbdev (intf);
1619         struct irda_class_desc *desc;
1620         int ret;
1621
1622         desc = kmalloc(sizeof (*desc), GFP_KERNEL);
1623         if (desc == NULL) 
1624                 return NULL;
1625         memset(desc, 0, sizeof(*desc));
1626
1627         /* USB-IrDA class spec 1.0:
1628          *      6.1.3: Standard "Get Descriptor" Device Request is not
1629          *             appropriate to retrieve class-specific descriptor
1630          *      6.2.5: Class Specific "Get Class Descriptor" Interface Request
1631          *             is mandatory and returns the USB-IrDA class descriptor
1632          */
1633
1634         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
1635                 IU_REQ_GET_CLASS_DESC,
1636                 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1637                 0, intf->altsetting->desc.bInterfaceNumber, desc,
1638                 sizeof(*desc), 500);
1639         
1640         IRDA_DEBUG(1, "%s(), ret=%d\n", __FUNCTION__, ret);
1641         if (ret < sizeof(*desc)) {
1642                 IRDA_WARNING("usb-irda: class_descriptor read %s (%d)\n",
1643                              (ret<0) ? "failed" : "too short", ret);
1644         }
1645         else if (desc->bDescriptorType != USB_DT_IRDA) {
1646                 IRDA_WARNING("usb-irda: bad class_descriptor type\n");
1647         }
1648         else {
1649 #ifdef IU_DUMP_CLASS_DESC
1650                 irda_usb_dump_class_desc(desc);
1651 #endif  /* IU_DUMP_CLASS_DESC */
1652
1653                 return desc;
1654         }
1655         kfree(desc);
1656         return NULL;
1657 }
1658
1659 /*********************** USB DEVICE CALLBACKS ***********************/
1660 /*
1661  * Main calls from the USB subsystem.
1662  * Mostly registering a new irda-usb device and removing it....
1663  */
1664
1665 /*------------------------------------------------------------------*/
1666 /*
1667  * This routine is called by the USB subsystem for each new device
1668  * in the system. We need to check if the device is ours, and in
1669  * this case start handling it.
1670  * The USB layer protect us from reentrancy (via BKL), so we don't need
1671  * to spinlock in there... Jean II
1672  */
1673 static int irda_usb_probe(struct usb_interface *intf,
1674                           const struct usb_device_id *id)
1675 {
1676         struct net_device *net;
1677         struct usb_device *dev = interface_to_usbdev(intf);
1678         struct irda_usb_cb *self = NULL;
1679         struct usb_host_interface *interface;
1680         struct irda_class_desc *irda_desc;
1681         int ret = -ENOMEM;
1682         int i;          /* Driver instance index / Rx URB index */
1683
1684         /* Note : the probe make sure to call us only for devices that
1685          * matches the list of dongle (top of the file). So, we
1686          * don't need to check if the dongle is really ours.
1687          * Jean II */
1688
1689         IRDA_MESSAGE("IRDA-USB found at address %d, Vendor: %x, Product: %x\n",
1690                      dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
1691                      le16_to_cpu(dev->descriptor.idProduct));
1692
1693         net = alloc_irdadev(sizeof(*self));
1694         if (!net) 
1695                 goto err_out;
1696
1697         SET_MODULE_OWNER(net);
1698         SET_NETDEV_DEV(net, &intf->dev);
1699         self = net->priv;
1700         self->netdev = net;
1701         spin_lock_init(&self->lock);
1702         init_timer(&self->rx_defer_timer);
1703
1704         self->capability = id->driver_info;
1705         self->needspatch = ((self->capability & IUC_STIR_4210) != 0) ;
1706
1707         /* Create all of the needed urbs */
1708         if (self->capability & IUC_STIR_4210) {
1709                 self->max_rx_urb = IU_SIGMATEL_MAX_RX_URBS;
1710                 self->header_length = USB_IRDA_SIGMATEL_HEADER;
1711         } else {
1712                 self->max_rx_urb = IU_MAX_RX_URBS;
1713                 self->header_length = USB_IRDA_HEADER;
1714         }
1715
1716         self->rx_urb = kzalloc(self->max_rx_urb * sizeof(struct urb *),
1717                                 GFP_KERNEL);
1718
1719         for (i = 0; i < self->max_rx_urb; i++) {
1720                 self->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
1721                 if (!self->rx_urb[i]) {
1722                         goto err_out_1;
1723                 }
1724         }
1725         self->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1726         if (!self->tx_urb) {
1727                 goto err_out_1;
1728         }
1729         self->speed_urb = usb_alloc_urb(0, GFP_KERNEL);
1730         if (!self->speed_urb) {
1731                 goto err_out_2;
1732         }
1733
1734         /* Is this really necessary? (no, except maybe for broken devices) */
1735         if (usb_reset_configuration (dev) < 0) {
1736                 err("reset_configuration failed");
1737                 ret = -EIO;
1738                 goto err_out_3;
1739         }
1740
1741         /* Is this really necessary? */
1742         /* Note : some driver do hardcode the interface number, some others
1743          * specify an alternate, but very few driver do like this.
1744          * Jean II */
1745         ret = usb_set_interface(dev, intf->altsetting->desc.bInterfaceNumber, 0);
1746         IRDA_DEBUG(1, "usb-irda: set interface %d result %d\n", intf->altsetting->desc.bInterfaceNumber, ret);
1747         switch (ret) {
1748                 case 0:
1749                         break;
1750                 case -EPIPE:            /* -EPIPE = -32 */
1751                         /* Martin Diehl says if we get a -EPIPE we should
1752                          * be fine and we don't need to do a usb_clear_halt().
1753                          * - Jean II */
1754                         IRDA_DEBUG(0, "%s(), Received -EPIPE, ignoring...\n", __FUNCTION__);
1755                         break;
1756                 default:
1757                         IRDA_DEBUG(0, "%s(), Unknown error %d\n", __FUNCTION__, ret);
1758                         ret = -EIO;
1759                         goto err_out_3;
1760         }
1761
1762         /* Find our endpoints */
1763         interface = intf->cur_altsetting;
1764         if(!irda_usb_parse_endpoints(self, interface->endpoint,
1765                                      interface->desc.bNumEndpoints)) {
1766                 IRDA_ERROR("%s(), Bogus endpoints...\n", __FUNCTION__);
1767                 ret = -EIO;
1768                 goto err_out_3;
1769         }
1770
1771         self->usbdev = dev;
1772
1773         /* Find IrDA class descriptor */
1774         irda_desc = irda_usb_find_class_desc(intf);
1775         ret = -ENODEV;
1776         if (irda_desc == NULL)
1777                 goto err_out_3;
1778
1779         if (self->needspatch) {
1780                 ret = usb_control_msg (self->usbdev, usb_sndctrlpipe (self->usbdev, 0),
1781                                        0x02, 0x40, 0, 0, NULL, 0, 500);
1782                 if (ret < 0) {
1783                         IRDA_DEBUG (0, "usb_control_msg failed %d\n", ret);
1784                         goto err_out_3;
1785                 } else {
1786                         mdelay(10);
1787                 }
1788         }
1789
1790         self->irda_desc =  irda_desc;
1791         self->present = 1;
1792         self->netopen = 0;
1793         self->usbintf = intf;
1794
1795         /* Allocate the buffer for speed changes */
1796         /* Don't change this buffer size and allocation without doing
1797          * some heavy and complete testing. Don't ask why :-(
1798          * Jean II */
1799         self->speed_buff = (char *) kmalloc(IRDA_USB_SPEED_MTU, GFP_KERNEL);
1800         if (self->speed_buff == NULL) 
1801                 goto err_out_3;
1802
1803         memset(self->speed_buff, 0, IRDA_USB_SPEED_MTU);
1804
1805         ret = irda_usb_open(self);
1806         if (ret) 
1807                 goto err_out_4;
1808
1809         IRDA_MESSAGE("IrDA: Registered device %s\n", net->name);
1810         usb_set_intfdata(intf, self);
1811
1812         if (self->needspatch) {
1813                 /* Now we fetch and upload the firmware patch */
1814                 ret = stir421x_patch_device(self);
1815                 self->needspatch = (ret < 0);
1816                 if (ret < 0) {
1817                         printk("patch_device failed\n");
1818                         goto err_out_5;
1819                 }
1820
1821                 /* replace IrDA class descriptor with what patched device is now reporting */
1822                 irda_desc = irda_usb_find_class_desc (self->usbintf);
1823                 if (irda_desc == NULL) {
1824                         ret = -ENODEV;
1825                         goto err_out_5;
1826                 }
1827                 if (self->irda_desc)
1828                         kfree (self->irda_desc);
1829                 self->irda_desc = irda_desc;
1830                 irda_usb_init_qos(self);
1831         }
1832
1833         return 0;
1834
1835 err_out_5:
1836         unregister_netdev(self->netdev);
1837 err_out_4:
1838         kfree(self->speed_buff);
1839 err_out_3:
1840         /* Free all urbs that we may have created */
1841         usb_free_urb(self->speed_urb);
1842 err_out_2:
1843         usb_free_urb(self->tx_urb);
1844 err_out_1:
1845         for (i = 0; i < self->max_rx_urb; i++) {
1846                 if (self->rx_urb[i])
1847                         usb_free_urb(self->rx_urb[i]);
1848         }
1849         free_netdev(net);
1850 err_out:
1851         return ret;
1852 }
1853
1854 /*------------------------------------------------------------------*/
1855 /*
1856  * The current irda-usb device is removed, the USB layer tell us
1857  * to shut it down...
1858  * One of the constraints is that when we exit this function,
1859  * we cannot use the usb_device no more. Gone. Destroyed. kfree().
1860  * Most other subsystem allow you to destroy the instance at a time
1861  * when it's convenient to you, to postpone it to a later date, but
1862  * not the USB subsystem.
1863  * So, we must make bloody sure that everything gets deactivated.
1864  * Jean II
1865  */
1866 static void irda_usb_disconnect(struct usb_interface *intf)
1867 {
1868         unsigned long flags;
1869         struct irda_usb_cb *self = usb_get_intfdata(intf);
1870         int i;
1871
1872         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1873
1874         usb_set_intfdata(intf, NULL);
1875         if (!self)
1876                 return;
1877
1878         /* Make sure that the Tx path is not executing. - Jean II */
1879         spin_lock_irqsave(&self->lock, flags);
1880
1881         /* Oups ! We are not there any more.
1882          * This will stop/desactivate the Tx path. - Jean II */
1883         self->present = 0;
1884
1885         /* Kill defered Rx URB */
1886         del_timer(&self->rx_defer_timer);
1887
1888         /* We need to have irq enabled to unlink the URBs. That's OK,
1889          * at this point the Tx path is gone - Jean II */
1890         spin_unlock_irqrestore(&self->lock, flags);
1891
1892         /* Hum... Check if networking is still active (avoid races) */
1893         if((self->netopen) || (self->irlap)) {
1894                 /* Accept no more transmissions */
1895                 /*netif_device_detach(self->netdev);*/
1896                 netif_stop_queue(self->netdev);
1897                 /* Stop all the receive URBs. Must be synchronous. */
1898                 for (i = 0; i < self->max_rx_urb; i++)
1899                         usb_kill_urb(self->rx_urb[i]);
1900                 /* Cancel Tx and speed URB.
1901                  * Make sure it's synchronous to avoid races. */
1902                 usb_kill_urb(self->tx_urb);
1903                 usb_kill_urb(self->speed_urb);
1904         }
1905
1906         /* Cleanup the device stuff */
1907         irda_usb_close(self);
1908         /* No longer attached to USB bus */
1909         self->usbdev = NULL;
1910         self->usbintf = NULL;
1911
1912         /* Clean up our urbs */
1913         for (i = 0; i < self->max_rx_urb; i++)
1914                 usb_free_urb(self->rx_urb[i]);
1915         kfree(self->rx_urb);
1916         /* Clean up Tx and speed URB */
1917         usb_free_urb(self->tx_urb);
1918         usb_free_urb(self->speed_urb);
1919
1920         /* Free self and network device */
1921         free_netdev(self->netdev);
1922         IRDA_DEBUG(0, "%s(), USB IrDA Disconnected\n", __FUNCTION__);
1923 }
1924
1925 /*------------------------------------------------------------------*/
1926 /*
1927  * USB device callbacks
1928  */
1929 static struct usb_driver irda_driver = {
1930         .name           = "irda-usb",
1931         .probe          = irda_usb_probe,
1932         .disconnect     = irda_usb_disconnect,
1933         .id_table       = dongles,
1934 };
1935
1936 /************************* MODULE CALLBACKS *************************/
1937 /*
1938  * Deal with module insertion/removal
1939  * Mostly tell USB about our existence
1940  */
1941
1942 /*------------------------------------------------------------------*/
1943 /*
1944  * Module insertion
1945  */
1946 static int __init usb_irda_init(void)
1947 {
1948         int     ret;
1949
1950         ret = usb_register(&irda_driver);
1951         if (ret < 0)
1952                 return ret;
1953
1954         IRDA_MESSAGE("USB IrDA support registered\n");
1955         return 0;
1956 }
1957 module_init(usb_irda_init);
1958
1959 /*------------------------------------------------------------------*/
1960 /*
1961  * Module removal
1962  */
1963 static void __exit usb_irda_cleanup(void)
1964 {
1965         /* Deregister the driver and remove all pending instances */
1966         usb_deregister(&irda_driver);
1967 }
1968 module_exit(usb_irda_cleanup);
1969
1970 /*------------------------------------------------------------------*/
1971 /*
1972  * Module parameters
1973  */
1974 module_param(qos_mtt_bits, int, 0);
1975 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
1976 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>");
1977 MODULE_DESCRIPTION("IrDA-USB Dongle Driver");
1978 MODULE_LICENSE("GPL");