ath5k: use SWBA to detect IBSS HW merges
[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_STIR421X | IUC_SPEED_BUG },
87         { USB_DEVICE(0x66f, 0x4220), .driver_info = IUC_STIR421X | IUC_SPEED_BUG },
88         { USB_DEVICE(0x66f, 0x4116), .driver_info = IUC_STIR421X | 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);
118 static void write_bulk_callback(struct urb *urb);
119 static void irda_usb_receive(struct urb *urb);
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_STIR421X &&
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_STIR421X) {
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)
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         skb_copy_from_linear_data(skb, self->tx_buff + self->header_length, skb->len);
445
446         /* Change setting for next frame */
447         if (self->capability & IUC_STIR421X) {
448                 __u8 turnaround_time;
449                 __u8* frame = self->tx_buff;
450                 turnaround_time = get_turnaround_time( skb );
451                 irda_usb_build_header(self, frame, 0);
452                 frame[2] = turnaround_time;
453                 if ((skb->len != 0) &&
454                     ((skb->len % 128) == 0) &&
455                     ((skb->len % 512) != 0)) {
456                         /* add extra byte for special SigmaTel feature */
457                         frame[1] = 1;
458                         skb_put(skb, 1);
459                 } else {
460                         frame[1] = 0;
461                 }
462         } else {
463                 irda_usb_build_header(self, self->tx_buff, 0);
464         }
465
466         /* FIXME: Make macro out of this one */
467         ((struct irda_skb_cb *)skb->cb)->context = self;
468
469         usb_fill_bulk_urb(urb, self->usbdev,
470                       usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
471                       self->tx_buff, skb->len + self->header_length,
472                       write_bulk_callback, skb);
473
474         /* This flag (URB_ZERO_PACKET) indicates that what we send is not
475          * a continuous stream of data but separate packets.
476          * In this case, the USB layer will insert an empty USB frame (TD)
477          * after each of our packets that is exact multiple of the frame size.
478          * This is how the dongle will detect the end of packet - Jean II */
479         urb->transfer_flags = URB_ZERO_PACKET;
480
481         /* Generate min turn time. FIXME: can we do better than this? */
482         /* Trying to a turnaround time at this level is trying to measure
483          * processor clock cycle with a wrist-watch, approximate at best...
484          *
485          * What we know is the last time we received a frame over USB.
486          * Due to latency over USB that depend on the USB load, we don't
487          * know when this frame was received over IrDA (a few ms before ?)
488          * Then, same story for our outgoing frame...
489          *
490          * In theory, the USB dongle is supposed to handle the turnaround
491          * by itself (spec 1.0, chater 4, page 6). Who knows ??? That's
492          * why this code is enabled only for dongles that doesn't meet
493          * the spec.
494          * Jean II */
495         if (self->capability & IUC_NO_TURN) {
496                 mtt = irda_get_mtt(skb);
497                 if (mtt) {
498                         int diff;
499                         do_gettimeofday(&self->now);
500                         diff = self->now.tv_usec - self->stamp.tv_usec;
501 #ifdef IU_USB_MIN_RTT
502                         /* Factor in USB delays -> Get rid of udelay() that
503                          * would be lost in the noise - Jean II */
504                         diff += IU_USB_MIN_RTT;
505 #endif /* IU_USB_MIN_RTT */
506                         /* If the usec counter did wraparound, the diff will
507                          * go negative (tv_usec is a long), so we need to
508                          * correct it by one second. Jean II */
509                         if (diff < 0)
510                                 diff += 1000000;
511
512                         /* Check if the mtt is larger than the time we have
513                          * already used by all the protocol processing
514                          */
515                         if (mtt > diff) {
516                                 mtt -= diff;
517                                 if (mtt > 1000)
518                                         mdelay(mtt/1000);
519                                 else
520                                         udelay(mtt);
521                         }
522                 }
523         }
524         
525         /* Ask USB to send the packet - Irq disabled -> GFP_ATOMIC */
526         if ((res = usb_submit_urb(urb, GFP_ATOMIC))) {
527                 IRDA_WARNING("%s(), failed Tx URB\n", __FUNCTION__);
528                 self->stats.tx_errors++;
529                 /* Let USB recover : We will catch that in the watchdog */
530                 /*netif_start_queue(netdev);*/
531         } else {
532                 /* Increment packet stats */
533                 self->stats.tx_packets++;
534                 self->stats.tx_bytes += skb->len;
535                 
536                 netdev->trans_start = jiffies;
537         }
538         spin_unlock_irqrestore(&self->lock, flags);
539         
540         return 0;
541
542 drop:
543         /* Drop silently the skb and exit */
544         dev_kfree_skb(skb);
545         spin_unlock_irqrestore(&self->lock, flags);
546         return err;             /* Usually 1 */
547 }
548
549 /*------------------------------------------------------------------*/
550 /*
551  * Note : this function will be called only for tx_urb...
552  */
553 static void write_bulk_callback(struct urb *urb)
554 {
555         unsigned long flags;
556         struct sk_buff *skb = urb->context;
557         struct irda_usb_cb *self = ((struct irda_skb_cb *) skb->cb)->context;
558         
559         IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
560
561         /* We should always have a context */
562         IRDA_ASSERT(self != NULL, return;);
563         /* We should always be called for the speed URB */
564         IRDA_ASSERT(urb == self->tx_urb, return;);
565
566         /* Free up the skb */
567         dev_kfree_skb_any(skb);
568         urb->context = NULL;
569
570         /* Check for timeout and other USB nasties */
571         if (urb->status != 0) {
572                 /* I get a lot of -ECONNABORTED = -103 here - Jean II */
573                 IRDA_DEBUG(0, "%s(), URB complete status %d, transfer_flags 0x%04X\n", __FUNCTION__, urb->status, urb->transfer_flags);
574
575                 /* Don't do anything here, that might confuse the USB layer,
576                  * and we could go in recursion and blow the kernel stack...
577                  * Instead, we will wait for irda_usb_net_timeout(), the
578                  * network layer watchdog, to fix the situation.
579                  * Jean II */
580                 /* A reset of the dongle might be welcomed here - Jean II */
581                 return;
582         }
583
584         /* urb is now available */
585         //urb->status = 0; -> tested above
586
587         /* Make sure we read self->present properly */
588         spin_lock_irqsave(&self->lock, flags);
589
590         /* If the network is closed, stop everything */
591         if ((!self->netopen) || (!self->present)) {
592                 IRDA_DEBUG(0, "%s(), Network is gone...\n", __FUNCTION__);
593                 spin_unlock_irqrestore(&self->lock, flags);
594                 return;
595         }
596
597         /* If changes to speed or xbofs is pending... */
598         if ((self->new_speed != -1) || (self->new_xbofs != -1)) {
599                 if ((self->new_speed != self->speed) ||
600                     (self->new_xbofs != self->xbofs)) {
601                         /* We haven't changed speed yet (because of
602                          * IUC_SPEED_BUG), so do it now - Jean II */
603                         IRDA_DEBUG(1, "%s(), Changing speed now...\n", __FUNCTION__);
604                         irda_usb_change_speed_xbofs(self);
605                 } else {
606                         /* New speed and xbof is now commited in hardware */
607                         self->new_speed = -1;
608                         self->new_xbofs = -1;
609                         /* Done, waiting for next packet */
610                         netif_wake_queue(self->netdev);
611                 }
612         } else {
613                 /* Otherwise, allow the stack to send more packets */
614                 netif_wake_queue(self->netdev);
615         }
616         spin_unlock_irqrestore(&self->lock, flags);
617 }
618
619 /*------------------------------------------------------------------*/
620 /*
621  * Watchdog timer from the network layer.
622  * After a predetermined timeout, if we don't give confirmation that
623  * the packet has been sent (i.e. no call to netif_wake_queue()),
624  * the network layer will call this function.
625  * Note that URB that we submit have also a timeout. When the URB timeout
626  * expire, the normal URB callback is called (write_bulk_callback()).
627  */
628 static void irda_usb_net_timeout(struct net_device *netdev)
629 {
630         unsigned long flags;
631         struct irda_usb_cb *self = netdev->priv;
632         struct urb *urb;
633         int     done = 0;       /* If we have made any progress */
634
635         IRDA_DEBUG(0, "%s(), Network layer thinks we timed out!\n", __FUNCTION__);
636         IRDA_ASSERT(self != NULL, return;);
637
638         /* Protect us from USB callbacks, net Tx and else. */
639         spin_lock_irqsave(&self->lock, flags);
640
641         /* self->present *MUST* be read under spinlock */
642         if (!self->present) {
643                 IRDA_WARNING("%s(), device not present!\n", __FUNCTION__);
644                 netif_stop_queue(netdev);
645                 spin_unlock_irqrestore(&self->lock, flags);
646                 return;
647         }
648
649         /* Check speed URB */
650         urb = self->speed_urb;
651         if (urb->status != 0) {
652                 IRDA_DEBUG(0, "%s: Speed change timed out, urb->status=%d, urb->transfer_flags=0x%04X\n", netdev->name, urb->status, urb->transfer_flags);
653
654                 switch (urb->status) {
655                 case -EINPROGRESS:
656                         usb_unlink_urb(urb);
657                         /* Note : above will  *NOT* call netif_wake_queue()
658                          * in completion handler, we will come back here.
659                          * Jean II */
660                         done = 1;
661                         break;
662                 case -ECONNRESET:
663                 case -ENOENT:                   /* urb unlinked by us */
664                 default:                        /* ??? - Play safe */
665                         urb->status = 0;
666                         netif_wake_queue(self->netdev);
667                         done = 1;
668                         break;
669                 }
670         }
671
672         /* Check Tx URB */
673         urb = self->tx_urb;
674         if (urb->status != 0) {
675                 struct sk_buff *skb = urb->context;
676
677                 IRDA_DEBUG(0, "%s: Tx timed out, urb->status=%d, urb->transfer_flags=0x%04X\n", netdev->name, urb->status, urb->transfer_flags);
678
679                 /* Increase error count */
680                 self->stats.tx_errors++;
681
682 #ifdef IU_BUG_KICK_TIMEOUT
683                 /* Can't be a bad idea to reset the speed ;-) - Jean II */
684                 if(self->new_speed == -1)
685                         self->new_speed = self->speed;
686                 if(self->new_xbofs == -1)
687                         self->new_xbofs = self->xbofs;
688                 irda_usb_change_speed_xbofs(self);
689 #endif /* IU_BUG_KICK_TIMEOUT */
690
691                 switch (urb->status) {
692                 case -EINPROGRESS:
693                         usb_unlink_urb(urb);
694                         /* Note : above will  *NOT* call netif_wake_queue()
695                          * in completion handler, because urb->status will
696                          * be -ENOENT. We will fix that at the next watchdog,
697                          * leaving more time to USB to recover...
698                          * Jean II */
699                         done = 1;
700                         break;
701                 case -ECONNRESET:
702                 case -ENOENT:                   /* urb unlinked by us */
703                 default:                        /* ??? - Play safe */
704                         if(skb != NULL) {
705                                 dev_kfree_skb_any(skb);
706                                 urb->context = NULL;
707                         }
708                         urb->status = 0;
709                         netif_wake_queue(self->netdev);
710                         done = 1;
711                         break;
712                 }
713         }
714         spin_unlock_irqrestore(&self->lock, flags);
715
716         /* Maybe we need a reset */
717         /* Note : Some drivers seem to use a usb_set_interface() when they
718          * need to reset the hardware. Hum...
719          */
720
721         /* if(done == 0) */
722 }
723
724 /************************* RECEIVE ROUTINES *************************/
725 /*
726  * Receive packets from the USB layer stack and pass them to the IrDA stack.
727  * Try to work around USB failures...
728  */
729
730 /*
731  * Note :
732  * Some of you may have noticed that most dongle have an interrupt in pipe
733  * that we don't use. Here is the little secret...
734  * When we hang a Rx URB on the bulk in pipe, it generates some USB traffic
735  * in every USB frame. This is unnecessary overhead.
736  * The interrupt in pipe will generate an event every time a packet is
737  * received. Reading an interrupt pipe adds minimal overhead, but has some
738  * latency (~1ms).
739  * If we are connected (speed != 9600), we want to minimise latency, so
740  * we just always hang the Rx URB and ignore the interrupt.
741  * If we are not connected (speed == 9600), there is usually no Rx traffic,
742  * and we want to minimise the USB overhead. In this case we should wait
743  * on the interrupt pipe and hang the Rx URB only when an interrupt is
744  * received.
745  * Jean II
746  *
747  * Note : don't read the above as what we are currently doing, but as
748  * something we could do with KC dongle. Also don't forget that the
749  * interrupt pipe is not part of the original standard, so this would
750  * need to be optional...
751  * Jean II
752  */
753
754 /*------------------------------------------------------------------*/
755 /*
756  * Submit a Rx URB to the USB layer to handle reception of a frame
757  * Mostly called by the completion callback of the previous URB.
758  *
759  * Jean II
760  */
761 static void irda_usb_submit(struct irda_usb_cb *self, struct sk_buff *skb, struct urb *urb)
762 {
763         struct irda_skb_cb *cb;
764         int ret;
765
766         IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
767
768         /* This should never happen */
769         IRDA_ASSERT(skb != NULL, return;);
770         IRDA_ASSERT(urb != NULL, return;);
771
772         /* Save ourselves in the skb */
773         cb = (struct irda_skb_cb *) skb->cb;
774         cb->context = self;
775
776         /* Reinitialize URB */
777         usb_fill_bulk_urb(urb, self->usbdev, 
778                       usb_rcvbulkpipe(self->usbdev, self->bulk_in_ep), 
779                       skb->data, IRDA_SKB_MAX_MTU,
780                       irda_usb_receive, skb);
781         urb->status = 0;
782
783         /* Can be called from irda_usb_receive (irq handler) -> GFP_ATOMIC */
784         ret = usb_submit_urb(urb, GFP_ATOMIC);
785         if (ret) {
786                 /* If this ever happen, we are in deep s***.
787                  * Basically, the Rx path will stop... */
788                 IRDA_WARNING("%s(), Failed to submit Rx URB %d\n",
789                              __FUNCTION__, ret);
790         }
791 }
792
793 /*------------------------------------------------------------------*/
794 /*
795  * Function irda_usb_receive(urb)
796  *
797  *     Called by the USB subsystem when a frame has been received
798  *
799  */
800 static void irda_usb_receive(struct urb *urb)
801 {
802         struct sk_buff *skb = (struct sk_buff *) urb->context;
803         struct irda_usb_cb *self; 
804         struct irda_skb_cb *cb;
805         struct sk_buff *newskb;
806         struct sk_buff *dataskb;
807         struct urb *next_urb;
808         unsigned int len, docopy;
809
810         IRDA_DEBUG(2, "%s(), len=%d\n", __FUNCTION__, urb->actual_length);
811         
812         /* Find ourselves */
813         cb = (struct irda_skb_cb *) skb->cb;
814         IRDA_ASSERT(cb != NULL, return;);
815         self = (struct irda_usb_cb *) cb->context;
816         IRDA_ASSERT(self != NULL, return;);
817
818         /* If the network is closed or the device gone, stop everything */
819         if ((!self->netopen) || (!self->present)) {
820                 IRDA_DEBUG(0, "%s(), Network is gone!\n", __FUNCTION__);
821                 /* Don't re-submit the URB : will stall the Rx path */
822                 return;
823         }
824         
825         /* Check the status */
826         if (urb->status != 0) {
827                 switch (urb->status) {
828                 case -EILSEQ:
829                         self->stats.rx_crc_errors++;    
830                         /* Also precursor to a hot-unplug on UHCI. */
831                         /* Fallthrough... */
832                 case -ECONNRESET:
833                         /* Random error, if I remember correctly */
834                         /* uhci_cleanup_unlink() is going to kill the Rx
835                          * URB just after we return. No problem, at this
836                          * point the URB will be idle ;-) - Jean II */
837                 case -ESHUTDOWN:
838                         /* That's usually a hot-unplug. Submit will fail... */
839                 case -ETIME:
840                         /* Usually precursor to a hot-unplug on OHCI. */
841                 default:
842                         self->stats.rx_errors++;
843                         IRDA_DEBUG(0, "%s(), RX status %d, transfer_flags 0x%04X \n", __FUNCTION__, urb->status, urb->transfer_flags);
844                         break;
845                 }
846                 /* If we received an error, we don't want to resubmit the
847                  * Rx URB straight away but to give the USB layer a little
848                  * bit of breathing room.
849                  * We are in the USB thread context, therefore there is a
850                  * danger of recursion (new URB we submit fails, we come
851                  * back here).
852                  * With recent USB stack (2.6.15+), I'm seeing that on
853                  * hot unplug of the dongle...
854                  * Lowest effective timer is 10ms...
855                  * Jean II */
856                 self->rx_defer_timer.function = &irda_usb_rx_defer_expired;
857                 self->rx_defer_timer.data = (unsigned long) urb;
858                 mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
859                 return;
860         }
861         
862         /* Check for empty frames */
863         if (urb->actual_length <= self->header_length) {
864                 IRDA_WARNING("%s(), empty frame!\n", __FUNCTION__);
865                 goto done;
866         }
867
868         /*  
869          * Remember the time we received this frame, so we can
870          * reduce the min turn time a bit since we will know
871          * how much time we have used for protocol processing
872          */
873         do_gettimeofday(&self->stamp);
874
875         /* Check if we need to copy the data to a new skb or not.
876          * For most frames, we use ZeroCopy and pass the already
877          * allocated skb up the stack.
878          * If the frame is small, it is more efficient to copy it
879          * to save memory (copy will be fast anyway - that's
880          * called Rx-copy-break). Jean II */
881         docopy = (urb->actual_length < IRDA_RX_COPY_THRESHOLD);
882
883         /* Allocate a new skb */
884         if (self->capability & IUC_STIR421X)
885                 newskb = dev_alloc_skb(docopy ? urb->actual_length :
886                                        IRDA_SKB_MAX_MTU +
887                                        USB_IRDA_STIR421X_HEADER);
888         else
889                 newskb = dev_alloc_skb(docopy ? urb->actual_length :
890                                        IRDA_SKB_MAX_MTU);
891
892         if (!newskb)  {
893                 self->stats.rx_dropped++;
894                 /* We could deliver the current skb, but this would stall
895                  * the Rx path. Better drop the packet... Jean II */
896                 goto done;  
897         }
898
899         /* Make sure IP header get aligned (IrDA header is 5 bytes) */
900         /* But IrDA-USB header is 1 byte. Jean II */
901         //skb_reserve(newskb, USB_IRDA_HEADER - 1);
902
903         if(docopy) {
904                 /* Copy packet, so we can recycle the original */
905                 skb_copy_from_linear_data(skb, newskb->data, urb->actual_length);
906                 /* Deliver this new skb */
907                 dataskb = newskb;
908                 /* And hook the old skb to the URB
909                  * Note : we don't need to "clean up" the old skb,
910                  * as we never touched it. Jean II */
911         } else {
912                 /* We are using ZeroCopy. Deliver old skb */
913                 dataskb = skb;
914                 /* And hook the new skb to the URB */
915                 skb = newskb;
916         }
917
918         /* Set proper length on skb & remove USB-IrDA header */
919         skb_put(dataskb, urb->actual_length);
920         skb_pull(dataskb, self->header_length);
921
922         /* Ask the networking layer to queue the packet for the IrDA stack */
923         dataskb->dev = self->netdev;
924         skb_reset_mac_header(dataskb);
925         dataskb->protocol = htons(ETH_P_IRDA);
926         len = dataskb->len;
927         netif_rx(dataskb);
928
929         /* Keep stats up to date */
930         self->stats.rx_bytes += len;
931         self->stats.rx_packets++;
932         self->netdev->last_rx = jiffies;
933
934 done:
935         /* Note : at this point, the URB we've just received (urb)
936          * is still referenced by the USB layer. For example, if we
937          * have received a -ECONNRESET, uhci_cleanup_unlink() will
938          * continue to process it (in fact, cleaning it up).
939          * If we were to submit this URB, disaster would ensue.
940          * Therefore, we submit our idle URB, and put this URB in our
941          * idle slot....
942          * Jean II */
943         /* Note : with this scheme, we could submit the idle URB before
944          * processing the Rx URB. I don't think it would buy us anything as
945          * we are running in the USB thread context. Jean II */
946         next_urb = self->idle_rx_urb;
947
948         /* Recycle Rx URB : Now, the idle URB is the present one */
949         urb->context = NULL;
950         self->idle_rx_urb = urb;
951
952         /* Submit the idle URB to replace the URB we've just received.
953          * Do it last to avoid race conditions... Jean II */
954         irda_usb_submit(self, skb, next_urb);
955 }
956
957 /*------------------------------------------------------------------*/
958 /*
959  * In case of errors, we want the USB layer to have time to recover.
960  * Now, it is time to resubmit ouur Rx URB...
961  */
962 static void irda_usb_rx_defer_expired(unsigned long data)
963 {
964         struct urb *urb = (struct urb *) data;
965         struct sk_buff *skb = (struct sk_buff *) urb->context;
966         struct irda_usb_cb *self; 
967         struct irda_skb_cb *cb;
968         struct urb *next_urb;
969
970         IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
971
972         /* Find ourselves */
973         cb = (struct irda_skb_cb *) skb->cb;
974         IRDA_ASSERT(cb != NULL, return;);
975         self = (struct irda_usb_cb *) cb->context;
976         IRDA_ASSERT(self != NULL, return;);
977
978         /* Same stuff as when Rx is done, see above... */
979         next_urb = self->idle_rx_urb;
980         urb->context = NULL;
981         self->idle_rx_urb = urb;
982         irda_usb_submit(self, skb, next_urb);
983 }
984
985 /*------------------------------------------------------------------*/
986 /*
987  * Callbak from IrDA layer. IrDA wants to know if we have
988  * started receiving anything.
989  */
990 static int irda_usb_is_receiving(struct irda_usb_cb *self)
991 {
992         /* Note : because of the way UHCI works, it's almost impossible
993          * to get this info. The Controller DMA directly to memory and
994          * signal only when the whole frame is finished. To know if the
995          * first TD of the URB has been filled or not seems hard work...
996          *
997          * The other solution would be to use the "receiving" command
998          * on the default decriptor with a usb_control_msg(), but that
999          * would add USB traffic and would return result only in the
1000          * next USB frame (~1ms).
1001          *
1002          * I've been told that current dongles send status info on their
1003          * interrupt endpoint, and that's what the Windows driver uses
1004          * to know this info. Unfortunately, this is not yet in the spec...
1005          *
1006          * Jean II
1007          */
1008
1009         return 0; /* For now */
1010 }
1011
1012 #define STIR421X_PATCH_PRODUCT_VER     "Product Version: "
1013 #define STIR421X_PATCH_STMP_TAG        "STMP"
1014 #define STIR421X_PATCH_CODE_OFFSET     512 /* patch image starts before here */
1015 /* marks end of patch file header (PC DOS text file EOF character) */
1016 #define STIR421X_PATCH_END_OF_HDR_TAG  0x1A
1017 #define STIR421X_PATCH_BLOCK_SIZE      1023
1018
1019 /*
1020  * Function stir421x_fwupload (struct irda_usb_cb *self,
1021  *                             unsigned char *patch,
1022  *                             const unsigned int patch_len)
1023  *
1024  *   Upload firmware code to SigmaTel 421X IRDA-USB dongle
1025  */
1026 static int stir421x_fw_upload(struct irda_usb_cb *self,
1027                              unsigned char *patch,
1028                              const unsigned int patch_len)
1029 {
1030         int ret = -ENOMEM;
1031         int actual_len = 0;
1032         unsigned int i;
1033         unsigned int block_size = 0;
1034         unsigned char *patch_block;
1035
1036         patch_block = kzalloc(STIR421X_PATCH_BLOCK_SIZE, GFP_KERNEL);
1037         if (patch_block == NULL)
1038                 return -ENOMEM;
1039
1040         /* break up patch into 1023-byte sections */
1041         for (i = 0; i < patch_len; i += block_size) {
1042                 block_size = patch_len - i;
1043
1044                 if (block_size > STIR421X_PATCH_BLOCK_SIZE)
1045                         block_size = STIR421X_PATCH_BLOCK_SIZE;
1046
1047                 /* upload the patch section */
1048                 memcpy(patch_block, patch + i, block_size);
1049
1050                 ret = usb_bulk_msg(self->usbdev,
1051                                    usb_sndbulkpipe(self->usbdev,
1052                                                    self->bulk_out_ep),
1053                                    patch_block, block_size,
1054                                    &actual_len, msecs_to_jiffies(500));
1055                 IRDA_DEBUG(3,"%s(): Bulk send %u bytes, ret=%d\n",
1056                            __FUNCTION__, actual_len, ret);
1057
1058                 if (ret < 0)
1059                         break;
1060
1061                 mdelay(10);
1062         }
1063
1064         kfree(patch_block);
1065
1066         return ret;
1067  }
1068
1069 /*
1070  * Function stir421x_patch_device(struct irda_usb_cb *self)
1071  *
1072  * Get a firmware code from userspase using hotplug request_firmware() call
1073   */
1074 static int stir421x_patch_device(struct irda_usb_cb *self)
1075 {
1076         unsigned int i;
1077         int ret;
1078         char stir421x_fw_name[11];
1079         const struct firmware *fw;
1080         unsigned char *fw_version_ptr; /* pointer to version string */
1081         unsigned long fw_version = 0;
1082
1083         /*
1084          * Known firmware patch file names for STIR421x dongles
1085          * are "42101001.sb" or "42101002.sb"
1086          */
1087         sprintf(stir421x_fw_name, "4210%4X.sb",
1088                 self->usbdev->descriptor.bcdDevice);
1089         ret = request_firmware(&fw, stir421x_fw_name, &self->usbdev->dev);
1090         if (ret < 0)
1091                 return ret;
1092
1093         /* We get a patch from userspace */
1094         IRDA_MESSAGE("%s(): Received firmware %s (%zu bytes)\n",
1095                      __FUNCTION__, stir421x_fw_name, fw->size);
1096
1097         ret = -EINVAL;
1098
1099         /* Get the bcd product version */
1100         if (!memcmp(fw->data, STIR421X_PATCH_PRODUCT_VER,
1101                     sizeof(STIR421X_PATCH_PRODUCT_VER) - 1)) {
1102                 fw_version_ptr = fw->data +
1103                         sizeof(STIR421X_PATCH_PRODUCT_VER) - 1;
1104
1105                 /* Let's check if the product version is dotted */
1106                 if (fw_version_ptr[3] == '.' &&
1107                     fw_version_ptr[7] == '.') {
1108                         unsigned long major, minor, build;
1109                         major = simple_strtoul(fw_version_ptr, NULL, 10);
1110                         minor = simple_strtoul(fw_version_ptr + 4, NULL, 10);
1111                         build = simple_strtoul(fw_version_ptr + 8, NULL, 10);
1112
1113                         fw_version = (major << 12)
1114                                 + (minor << 8)
1115                                 + ((build / 10) << 4)
1116                                 + (build % 10);
1117
1118                         IRDA_DEBUG(3, "%s(): Firmware Product version %ld\n",
1119                                    __FUNCTION__, fw_version);
1120                 }
1121         }
1122
1123         if (self->usbdev->descriptor.bcdDevice == fw_version) {
1124                 /*
1125                  * If we're here, we've found a correct patch
1126                  * The actual image starts after the "STMP" keyword
1127                  * so forward to the firmware header tag
1128                  */
1129                 for (i = 0; (fw->data[i] != STIR421X_PATCH_END_OF_HDR_TAG)
1130                              && (i < fw->size); i++) ;
1131                 /* here we check for the out of buffer case */
1132                 if ((STIR421X_PATCH_END_OF_HDR_TAG == fw->data[i])
1133                     && (i < STIR421X_PATCH_CODE_OFFSET)) {
1134                         if (!memcmp(fw->data + i + 1, STIR421X_PATCH_STMP_TAG,
1135                                     sizeof(STIR421X_PATCH_STMP_TAG) - 1)) {
1136
1137                                 /* We can upload the patch to the target */
1138                                 i += sizeof(STIR421X_PATCH_STMP_TAG);
1139                                 ret = stir421x_fw_upload(self, &fw->data[i],
1140                                                          fw->size - i);
1141                         }
1142                 }
1143         }
1144
1145         release_firmware(fw);
1146
1147         return ret;
1148 }
1149
1150
1151 /********************** IRDA DEVICE CALLBACKS **********************/
1152 /*
1153  * Main calls from the IrDA/Network subsystem.
1154  * Mostly registering a new irda-usb device and removing it....
1155  * We only deal with the IrDA side of the business, the USB side will
1156  * be dealt with below...
1157  */
1158
1159
1160 /*------------------------------------------------------------------*/
1161 /*
1162  * Function irda_usb_net_open (dev)
1163  *
1164  *    Network device is taken up. Usually this is done by "ifconfig irda0 up" 
1165  *   
1166  * Note : don't mess with self->netopen - Jean II
1167  */
1168 static int irda_usb_net_open(struct net_device *netdev)
1169 {
1170         struct irda_usb_cb *self;
1171         unsigned long flags;
1172         char    hwname[16];
1173         int i;
1174         
1175         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1176
1177         IRDA_ASSERT(netdev != NULL, return -1;);
1178         self = (struct irda_usb_cb *) netdev->priv;
1179         IRDA_ASSERT(self != NULL, return -1;);
1180
1181         spin_lock_irqsave(&self->lock, flags);
1182         /* Can only open the device if it's there */
1183         if(!self->present) {
1184                 spin_unlock_irqrestore(&self->lock, flags);
1185                 IRDA_WARNING("%s(), device not present!\n", __FUNCTION__);
1186                 return -1;
1187         }
1188
1189         if(self->needspatch) {
1190                 spin_unlock_irqrestore(&self->lock, flags);
1191                 IRDA_WARNING("%s(), device needs patch\n", __FUNCTION__) ;
1192                 return -EIO ;
1193         }
1194
1195         /* Initialise default speed and xbofs value
1196          * (IrLAP will change that soon) */
1197         self->speed = -1;
1198         self->xbofs = -1;
1199         self->new_speed = -1;
1200         self->new_xbofs = -1;
1201
1202         /* To do *before* submitting Rx urbs and starting net Tx queue
1203          * Jean II */
1204         self->netopen = 1;
1205         spin_unlock_irqrestore(&self->lock, flags);
1206
1207         /* 
1208          * Now that everything should be initialized properly,
1209          * Open new IrLAP layer instance to take care of us...
1210          * Note : will send immediately a speed change...
1211          */
1212         sprintf(hwname, "usb#%d", self->usbdev->devnum);
1213         self->irlap = irlap_open(netdev, &self->qos, hwname);
1214         IRDA_ASSERT(self->irlap != NULL, return -1;);
1215
1216         /* Allow IrLAP to send data to us */
1217         netif_start_queue(netdev);
1218
1219         /* We submit all the Rx URB except for one that we keep idle.
1220          * Need to be initialised before submitting other USBs, because
1221          * in some cases as soon as we submit the URBs the USB layer
1222          * will trigger a dummy receive - Jean II */
1223         self->idle_rx_urb = self->rx_urb[IU_MAX_ACTIVE_RX_URBS];
1224         self->idle_rx_urb->context = NULL;
1225
1226         /* Now that we can pass data to IrLAP, allow the USB layer
1227          * to send us some data... */
1228         for (i = 0; i < IU_MAX_ACTIVE_RX_URBS; i++) {
1229                 struct sk_buff *skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
1230                 if (!skb) {
1231                         /* If this ever happen, we are in deep s***.
1232                          * Basically, we can't start the Rx path... */
1233                         IRDA_WARNING("%s(), Failed to allocate Rx skb\n",
1234                                      __FUNCTION__);
1235                         return -1;
1236                 }
1237                 //skb_reserve(newskb, USB_IRDA_HEADER - 1);
1238                 irda_usb_submit(self, skb, self->rx_urb[i]);
1239         }
1240
1241         /* Ready to play !!! */
1242         return 0;
1243 }
1244
1245 /*------------------------------------------------------------------*/
1246 /*
1247  * Function irda_usb_net_close (self)
1248  *
1249  *    Network device is taken down. Usually this is done by 
1250  *    "ifconfig irda0 down" 
1251  */
1252 static int irda_usb_net_close(struct net_device *netdev)
1253 {
1254         struct irda_usb_cb *self;
1255         int     i;
1256
1257         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1258
1259         IRDA_ASSERT(netdev != NULL, return -1;);
1260         self = (struct irda_usb_cb *) netdev->priv;
1261         IRDA_ASSERT(self != NULL, return -1;);
1262
1263         /* Clear this flag *before* unlinking the urbs and *before*
1264          * stopping the network Tx queue - Jean II */
1265         self->netopen = 0;
1266
1267         /* Stop network Tx queue */
1268         netif_stop_queue(netdev);
1269
1270         /* Kill defered Rx URB */
1271         del_timer(&self->rx_defer_timer);
1272
1273         /* Deallocate all the Rx path buffers (URBs and skb) */
1274         for (i = 0; i < self->max_rx_urb; i++) {
1275                 struct urb *urb = self->rx_urb[i];
1276                 struct sk_buff *skb = (struct sk_buff *) urb->context;
1277                 /* Cancel the receive command */
1278                 usb_kill_urb(urb);
1279                 /* The skb is ours, free it */
1280                 if(skb) {
1281                         dev_kfree_skb(skb);
1282                         urb->context = NULL;
1283                 }
1284         }
1285         /* Cancel Tx and speed URB - need to be synchronous to avoid races */
1286         usb_kill_urb(self->tx_urb);
1287         usb_kill_urb(self->speed_urb);
1288
1289         /* Stop and remove instance of IrLAP */
1290         if (self->irlap)
1291                 irlap_close(self->irlap);
1292         self->irlap = NULL;
1293
1294         return 0;
1295 }
1296
1297 /*------------------------------------------------------------------*/
1298 /*
1299  * IOCTLs : Extra out-of-band network commands...
1300  */
1301 static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1302 {
1303         unsigned long flags;
1304         struct if_irda_req *irq = (struct if_irda_req *) rq;
1305         struct irda_usb_cb *self;
1306         int ret = 0;
1307
1308         IRDA_ASSERT(dev != NULL, return -1;);
1309         self = dev->priv;
1310         IRDA_ASSERT(self != NULL, return -1;);
1311
1312         IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
1313
1314         switch (cmd) {
1315         case SIOCSBANDWIDTH: /* Set bandwidth */
1316                 if (!capable(CAP_NET_ADMIN))
1317                         return -EPERM;
1318                 /* Protect us from USB callbacks, net watchdog and else. */
1319                 spin_lock_irqsave(&self->lock, flags);
1320                 /* Check if the device is still there */
1321                 if(self->present) {
1322                         /* Set the desired speed */
1323                         self->new_speed = irq->ifr_baudrate;
1324                         irda_usb_change_speed_xbofs(self);
1325                 }
1326                 spin_unlock_irqrestore(&self->lock, flags);
1327                 break;
1328         case SIOCSMEDIABUSY: /* Set media busy */
1329                 if (!capable(CAP_NET_ADMIN))
1330                         return -EPERM;
1331                 /* Check if the IrDA stack is still there */
1332                 if(self->netopen)
1333                         irda_device_set_media_busy(self->netdev, TRUE);
1334                 break;
1335         case SIOCGRECEIVING: /* Check if we are receiving right now */
1336                 irq->ifr_receiving = irda_usb_is_receiving(self);
1337                 break;
1338         default:
1339                 ret = -EOPNOTSUPP;
1340         }
1341         
1342         return ret;
1343 }
1344
1345 /*------------------------------------------------------------------*/
1346 /*
1347  * Get device stats (for /proc/net/dev and ifconfig)
1348  */
1349 static struct net_device_stats *irda_usb_net_get_stats(struct net_device *dev)
1350 {
1351         struct irda_usb_cb *self = dev->priv;
1352         return &self->stats;
1353 }
1354
1355 /********************* IRDA CONFIG SUBROUTINES *********************/
1356 /*
1357  * Various subroutines dealing with IrDA and network stuff we use to
1358  * configure and initialise each irda-usb instance.
1359  * These functions are used below in the main calls of the driver...
1360  */
1361
1362 /*------------------------------------------------------------------*/
1363 /*
1364  * Set proper values in the IrDA QOS structure
1365  */
1366 static inline void irda_usb_init_qos(struct irda_usb_cb *self)
1367 {
1368         struct irda_class_desc *desc;
1369
1370         IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
1371         
1372         desc = self->irda_desc;
1373         
1374         /* Initialize QoS for this device */
1375         irda_init_max_qos_capabilies(&self->qos);
1376
1377         /* See spec section 7.2 for meaning.
1378          * Values are little endian (as most USB stuff), the IrDA stack
1379          * use it in native order (see parameters.c). - Jean II */
1380         self->qos.baud_rate.bits       = le16_to_cpu(desc->wBaudRate);
1381         self->qos.min_turn_time.bits   = desc->bmMinTurnaroundTime;
1382         self->qos.additional_bofs.bits = desc->bmAdditionalBOFs;
1383         self->qos.window_size.bits     = desc->bmWindowSize;
1384         self->qos.data_size.bits       = desc->bmDataSize;
1385
1386         IRDA_DEBUG(0, "%s(), dongle says speed=0x%X, size=0x%X, window=0x%X, bofs=0x%X, turn=0x%X\n", 
1387                 __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);
1388
1389         /* Don't always trust what the dongle tell us */
1390         if(self->capability & IUC_SIR_ONLY)
1391                 self->qos.baud_rate.bits        &= 0x00ff;
1392         if(self->capability & IUC_SMALL_PKT)
1393                 self->qos.data_size.bits         = 0x07;
1394         if(self->capability & IUC_NO_WINDOW)
1395                 self->qos.window_size.bits       = 0x01;
1396         if(self->capability & IUC_MAX_WINDOW)
1397                 self->qos.window_size.bits       = 0x7f;
1398         if(self->capability & IUC_MAX_XBOFS)
1399                 self->qos.additional_bofs.bits   = 0x01;
1400
1401 #if 1
1402         /* Module parameter can override the rx window size */
1403         if (qos_mtt_bits)
1404                 self->qos.min_turn_time.bits = qos_mtt_bits;
1405 #endif      
1406         /* 
1407          * Note : most of those values apply only for the receive path,
1408          * the transmit path will be set differently - Jean II 
1409          */
1410         irda_qos_bits_to_value(&self->qos);
1411 }
1412
1413 /*------------------------------------------------------------------*/
1414 /*
1415  * Initialise the network side of the irda-usb instance
1416  * Called when a new USB instance is registered in irda_usb_probe()
1417  */
1418 static inline int irda_usb_open(struct irda_usb_cb *self)
1419 {
1420         struct net_device *netdev = self->netdev;
1421
1422         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1423
1424         irda_usb_init_qos(self);
1425
1426         /* Override the network functions we need to use */
1427         netdev->hard_start_xmit = irda_usb_hard_xmit;
1428         netdev->tx_timeout      = irda_usb_net_timeout;
1429         netdev->watchdog_timeo  = 250*HZ/1000;  /* 250 ms > USB timeout */
1430         netdev->open            = irda_usb_net_open;
1431         netdev->stop            = irda_usb_net_close;
1432         netdev->get_stats       = irda_usb_net_get_stats;
1433         netdev->do_ioctl        = irda_usb_net_ioctl;
1434
1435         return register_netdev(netdev);
1436 }
1437
1438 /*------------------------------------------------------------------*/
1439 /*
1440  * Cleanup the network side of the irda-usb instance
1441  * Called when a USB instance is removed in irda_usb_disconnect()
1442  */
1443 static inline void irda_usb_close(struct irda_usb_cb *self)
1444 {
1445         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1446
1447         /* Remove netdevice */
1448         unregister_netdev(self->netdev);
1449
1450         /* Remove the speed buffer */
1451         kfree(self->speed_buff);
1452         self->speed_buff = NULL;
1453
1454         kfree(self->tx_buff);
1455         self->tx_buff = NULL;
1456 }
1457
1458 /********************** USB CONFIG SUBROUTINES **********************/
1459 /*
1460  * Various subroutines dealing with USB stuff we use to configure and
1461  * initialise each irda-usb instance.
1462  * These functions are used below in the main calls of the driver...
1463  */
1464
1465 /*------------------------------------------------------------------*/
1466 /*
1467  * Function irda_usb_parse_endpoints(dev, ifnum)
1468  *
1469  *    Parse the various endpoints and find the one we need.
1470  *
1471  * The endpoint are the pipes used to communicate with the USB device.
1472  * The spec defines 2 endpoints of type bulk transfer, one in, and one out.
1473  * These are used to pass frames back and forth with the dongle.
1474  * Most dongle have also an interrupt endpoint, that will be probably
1475  * documented in the next spec...
1476  */
1477 static inline int irda_usb_parse_endpoints(struct irda_usb_cb *self, struct usb_host_endpoint *endpoint, int ennum)
1478 {
1479         int i;          /* Endpoint index in table */
1480                 
1481         /* Init : no endpoints */
1482         self->bulk_in_ep = 0;
1483         self->bulk_out_ep = 0;
1484         self->bulk_int_ep = 0;
1485
1486         /* Let's look at all those endpoints */
1487         for(i = 0; i < ennum; i++) {
1488                 /* All those variables will get optimised by the compiler,
1489                  * so let's aim for clarity... - Jean II */
1490                 __u8 ep;        /* Endpoint address */
1491                 __u8 dir;       /* Endpoint direction */
1492                 __u8 attr;      /* Endpoint attribute */
1493                 __u16 psize;    /* Endpoint max packet size in bytes */
1494
1495                 /* Get endpoint address, direction and attribute */
1496                 ep = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1497                 dir = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK;
1498                 attr = endpoint[i].desc.bmAttributes;
1499                 psize = le16_to_cpu(endpoint[i].desc.wMaxPacketSize);
1500
1501                 /* Is it a bulk endpoint ??? */
1502                 if(attr == USB_ENDPOINT_XFER_BULK) {
1503                         /* We need to find an IN and an OUT */
1504                         if(dir == USB_DIR_IN) {
1505                                 /* This is our Rx endpoint */
1506                                 self->bulk_in_ep = ep;
1507                         } else {
1508                                 /* This is our Tx endpoint */
1509                                 self->bulk_out_ep = ep;
1510                                 self->bulk_out_mtu = psize;
1511                         }
1512                 } else {
1513                         if((attr == USB_ENDPOINT_XFER_INT) &&
1514                            (dir == USB_DIR_IN)) {
1515                                 /* This is our interrupt endpoint */
1516                                 self->bulk_int_ep = ep;
1517                         } else {
1518                                 IRDA_ERROR("%s(), Unrecognised endpoint %02X.\n", __FUNCTION__, ep);
1519                         }
1520                 }
1521         }
1522
1523         IRDA_DEBUG(0, "%s(), And our endpoints are : in=%02X, out=%02X (%d), int=%02X\n",
1524                 __FUNCTION__, self->bulk_in_ep, self->bulk_out_ep, self->bulk_out_mtu, self->bulk_int_ep);
1525
1526         return((self->bulk_in_ep != 0) && (self->bulk_out_ep != 0));
1527 }
1528
1529 #ifdef IU_DUMP_CLASS_DESC
1530 /*------------------------------------------------------------------*/
1531 /*
1532  * Function usb_irda_dump_class_desc(desc)
1533  *
1534  *    Prints out the contents of the IrDA class descriptor
1535  *
1536  */
1537 static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
1538 {
1539         /* Values are little endian */
1540         printk("bLength=%x\n", desc->bLength);
1541         printk("bDescriptorType=%x\n", desc->bDescriptorType);
1542         printk("bcdSpecRevision=%x\n", le16_to_cpu(desc->bcdSpecRevision)); 
1543         printk("bmDataSize=%x\n", desc->bmDataSize);
1544         printk("bmWindowSize=%x\n", desc->bmWindowSize);
1545         printk("bmMinTurnaroundTime=%d\n", desc->bmMinTurnaroundTime);
1546         printk("wBaudRate=%x\n", le16_to_cpu(desc->wBaudRate));
1547         printk("bmAdditionalBOFs=%x\n", desc->bmAdditionalBOFs);
1548         printk("bIrdaRateSniff=%x\n", desc->bIrdaRateSniff);
1549         printk("bMaxUnicastList=%x\n", desc->bMaxUnicastList);
1550 }
1551 #endif /* IU_DUMP_CLASS_DESC */
1552
1553 /*------------------------------------------------------------------*/
1554 /*
1555  * Function irda_usb_find_class_desc(intf)
1556  *
1557  *    Returns instance of IrDA class descriptor, or NULL if not found
1558  *
1559  * The class descriptor is some extra info that IrDA USB devices will
1560  * offer to us, describing their IrDA characteristics. We will use that in
1561  * irda_usb_init_qos()
1562  */
1563 static inline struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf)
1564 {
1565         struct usb_device *dev = interface_to_usbdev (intf);
1566         struct irda_class_desc *desc;
1567         int ret;
1568
1569         desc = kzalloc(sizeof(*desc), GFP_KERNEL);
1570         if (!desc)
1571                 return NULL;
1572
1573         /* USB-IrDA class spec 1.0:
1574          *      6.1.3: Standard "Get Descriptor" Device Request is not
1575          *             appropriate to retrieve class-specific descriptor
1576          *      6.2.5: Class Specific "Get Class Descriptor" Interface Request
1577          *             is mandatory and returns the USB-IrDA class descriptor
1578          */
1579
1580         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
1581                 IU_REQ_GET_CLASS_DESC,
1582                 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1583                 0, intf->altsetting->desc.bInterfaceNumber, desc,
1584                 sizeof(*desc), 500);
1585         
1586         IRDA_DEBUG(1, "%s(), ret=%d\n", __FUNCTION__, ret);
1587         if (ret < sizeof(*desc)) {
1588                 IRDA_WARNING("usb-irda: class_descriptor read %s (%d)\n",
1589                              (ret<0) ? "failed" : "too short", ret);
1590         }
1591         else if (desc->bDescriptorType != USB_DT_IRDA) {
1592                 IRDA_WARNING("usb-irda: bad class_descriptor type\n");
1593         }
1594         else {
1595 #ifdef IU_DUMP_CLASS_DESC
1596                 irda_usb_dump_class_desc(desc);
1597 #endif  /* IU_DUMP_CLASS_DESC */
1598
1599                 return desc;
1600         }
1601         kfree(desc);
1602         return NULL;
1603 }
1604
1605 /*********************** USB DEVICE CALLBACKS ***********************/
1606 /*
1607  * Main calls from the USB subsystem.
1608  * Mostly registering a new irda-usb device and removing it....
1609  */
1610
1611 /*------------------------------------------------------------------*/
1612 /*
1613  * This routine is called by the USB subsystem for each new device
1614  * in the system. We need to check if the device is ours, and in
1615  * this case start handling it.
1616  * The USB layer protect us from reentrancy (via BKL), so we don't need
1617  * to spinlock in there... Jean II
1618  */
1619 static int irda_usb_probe(struct usb_interface *intf,
1620                           const struct usb_device_id *id)
1621 {
1622         struct net_device *net;
1623         struct usb_device *dev = interface_to_usbdev(intf);
1624         struct irda_usb_cb *self;
1625         struct usb_host_interface *interface;
1626         struct irda_class_desc *irda_desc;
1627         int ret = -ENOMEM;
1628         int i;          /* Driver instance index / Rx URB index */
1629
1630         /* Note : the probe make sure to call us only for devices that
1631          * matches the list of dongle (top of the file). So, we
1632          * don't need to check if the dongle is really ours.
1633          * Jean II */
1634
1635         IRDA_MESSAGE("IRDA-USB found at address %d, Vendor: %x, Product: %x\n",
1636                      dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
1637                      le16_to_cpu(dev->descriptor.idProduct));
1638
1639         net = alloc_irdadev(sizeof(*self));
1640         if (!net) 
1641                 goto err_out;
1642
1643         SET_NETDEV_DEV(net, &intf->dev);
1644         self = net->priv;
1645         self->netdev = net;
1646         spin_lock_init(&self->lock);
1647         init_timer(&self->rx_defer_timer);
1648
1649         self->capability = id->driver_info;
1650         self->needspatch = ((self->capability & IUC_STIR421X) != 0);
1651
1652         /* Create all of the needed urbs */
1653         if (self->capability & IUC_STIR421X) {
1654                 self->max_rx_urb = IU_SIGMATEL_MAX_RX_URBS;
1655                 self->header_length = USB_IRDA_STIR421X_HEADER;
1656         } else {
1657                 self->max_rx_urb = IU_MAX_RX_URBS;
1658                 self->header_length = USB_IRDA_HEADER;
1659         }
1660
1661         self->rx_urb = kcalloc(self->max_rx_urb, sizeof(struct urb *),
1662                                 GFP_KERNEL);
1663
1664         for (i = 0; i < self->max_rx_urb; i++) {
1665                 self->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
1666                 if (!self->rx_urb[i]) {
1667                         goto err_out_1;
1668                 }
1669         }
1670         self->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1671         if (!self->tx_urb) {
1672                 goto err_out_1;
1673         }
1674         self->speed_urb = usb_alloc_urb(0, GFP_KERNEL);
1675         if (!self->speed_urb) {
1676                 goto err_out_2;
1677         }
1678
1679         /* Is this really necessary? (no, except maybe for broken devices) */
1680         if (usb_reset_configuration (dev) < 0) {
1681                 err("reset_configuration failed");
1682                 ret = -EIO;
1683                 goto err_out_3;
1684         }
1685
1686         /* Is this really necessary? */
1687         /* Note : some driver do hardcode the interface number, some others
1688          * specify an alternate, but very few driver do like this.
1689          * Jean II */
1690         ret = usb_set_interface(dev, intf->altsetting->desc.bInterfaceNumber, 0);
1691         IRDA_DEBUG(1, "usb-irda: set interface %d result %d\n", intf->altsetting->desc.bInterfaceNumber, ret);
1692         switch (ret) {
1693                 case 0:
1694                         break;
1695                 case -EPIPE:            /* -EPIPE = -32 */
1696                         /* Martin Diehl says if we get a -EPIPE we should
1697                          * be fine and we don't need to do a usb_clear_halt().
1698                          * - Jean II */
1699                         IRDA_DEBUG(0, "%s(), Received -EPIPE, ignoring...\n", __FUNCTION__);
1700                         break;
1701                 default:
1702                         IRDA_DEBUG(0, "%s(), Unknown error %d\n", __FUNCTION__, ret);
1703                         ret = -EIO;
1704                         goto err_out_3;
1705         }
1706
1707         /* Find our endpoints */
1708         interface = intf->cur_altsetting;
1709         if(!irda_usb_parse_endpoints(self, interface->endpoint,
1710                                      interface->desc.bNumEndpoints)) {
1711                 IRDA_ERROR("%s(), Bogus endpoints...\n", __FUNCTION__);
1712                 ret = -EIO;
1713                 goto err_out_3;
1714         }
1715
1716         self->usbdev = dev;
1717
1718         /* Find IrDA class descriptor */
1719         irda_desc = irda_usb_find_class_desc(intf);
1720         ret = -ENODEV;
1721         if (!irda_desc)
1722                 goto err_out_3;
1723
1724         if (self->needspatch) {
1725                 ret = usb_control_msg (self->usbdev, usb_sndctrlpipe (self->usbdev, 0),
1726                                        0x02, 0x40, 0, 0, NULL, 0, 500);
1727                 if (ret < 0) {
1728                         IRDA_DEBUG (0, "usb_control_msg failed %d\n", ret);
1729                         goto err_out_3;
1730                 } else {
1731                         mdelay(10);
1732                 }
1733         }
1734
1735         self->irda_desc =  irda_desc;
1736         self->present = 1;
1737         self->netopen = 0;
1738         self->usbintf = intf;
1739
1740         /* Allocate the buffer for speed changes */
1741         /* Don't change this buffer size and allocation without doing
1742          * some heavy and complete testing. Don't ask why :-(
1743          * Jean II */
1744         self->speed_buff = kzalloc(IRDA_USB_SPEED_MTU, GFP_KERNEL);
1745         if (!self->speed_buff)
1746                 goto err_out_3;
1747
1748         self->tx_buff = kzalloc(IRDA_SKB_MAX_MTU + self->header_length,
1749                                 GFP_KERNEL);
1750         if (!self->tx_buff)
1751                 goto err_out_4;
1752
1753         ret = irda_usb_open(self);
1754         if (ret) 
1755                 goto err_out_5;
1756
1757         IRDA_MESSAGE("IrDA: Registered device %s\n", net->name);
1758         usb_set_intfdata(intf, self);
1759
1760         if (self->needspatch) {
1761                 /* Now we fetch and upload the firmware patch */
1762                 ret = stir421x_patch_device(self);
1763                 self->needspatch = (ret < 0);
1764                 if (self->needspatch) {
1765                         IRDA_ERROR("STIR421X: Couldn't upload patch\n");
1766                         goto err_out_6;
1767                 }
1768
1769                 /* replace IrDA class descriptor with what patched device is now reporting */
1770                 irda_desc = irda_usb_find_class_desc (self->usbintf);
1771                 if (!irda_desc) {
1772                         ret = -ENODEV;
1773                         goto err_out_6;
1774                 }
1775                 kfree(self->irda_desc);
1776                 self->irda_desc = irda_desc;
1777                 irda_usb_init_qos(self);
1778         }
1779
1780         return 0;
1781 err_out_6:
1782         unregister_netdev(self->netdev);
1783 err_out_5:
1784         kfree(self->tx_buff);
1785 err_out_4:
1786         kfree(self->speed_buff);
1787 err_out_3:
1788         /* Free all urbs that we may have created */
1789         usb_free_urb(self->speed_urb);
1790 err_out_2:
1791         usb_free_urb(self->tx_urb);
1792 err_out_1:
1793         for (i = 0; i < self->max_rx_urb; i++)
1794                 usb_free_urb(self->rx_urb[i]);
1795         free_netdev(net);
1796 err_out:
1797         return ret;
1798 }
1799
1800 /*------------------------------------------------------------------*/
1801 /*
1802  * The current irda-usb device is removed, the USB layer tell us
1803  * to shut it down...
1804  * One of the constraints is that when we exit this function,
1805  * we cannot use the usb_device no more. Gone. Destroyed. kfree().
1806  * Most other subsystem allow you to destroy the instance at a time
1807  * when it's convenient to you, to postpone it to a later date, but
1808  * not the USB subsystem.
1809  * So, we must make bloody sure that everything gets deactivated.
1810  * Jean II
1811  */
1812 static void irda_usb_disconnect(struct usb_interface *intf)
1813 {
1814         unsigned long flags;
1815         struct irda_usb_cb *self = usb_get_intfdata(intf);
1816         int i;
1817
1818         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1819
1820         usb_set_intfdata(intf, NULL);
1821         if (!self)
1822                 return;
1823
1824         /* Make sure that the Tx path is not executing. - Jean II */
1825         spin_lock_irqsave(&self->lock, flags);
1826
1827         /* Oups ! We are not there any more.
1828          * This will stop/desactivate the Tx path. - Jean II */
1829         self->present = 0;
1830
1831         /* Kill defered Rx URB */
1832         del_timer(&self->rx_defer_timer);
1833
1834         /* We need to have irq enabled to unlink the URBs. That's OK,
1835          * at this point the Tx path is gone - Jean II */
1836         spin_unlock_irqrestore(&self->lock, flags);
1837
1838         /* Hum... Check if networking is still active (avoid races) */
1839         if((self->netopen) || (self->irlap)) {
1840                 /* Accept no more transmissions */
1841                 /*netif_device_detach(self->netdev);*/
1842                 netif_stop_queue(self->netdev);
1843                 /* Stop all the receive URBs. Must be synchronous. */
1844                 for (i = 0; i < self->max_rx_urb; i++)
1845                         usb_kill_urb(self->rx_urb[i]);
1846                 /* Cancel Tx and speed URB.
1847                  * Make sure it's synchronous to avoid races. */
1848                 usb_kill_urb(self->tx_urb);
1849                 usb_kill_urb(self->speed_urb);
1850         }
1851
1852         /* Cleanup the device stuff */
1853         irda_usb_close(self);
1854         /* No longer attached to USB bus */
1855         self->usbdev = NULL;
1856         self->usbintf = NULL;
1857
1858         /* Clean up our urbs */
1859         for (i = 0; i < self->max_rx_urb; i++)
1860                 usb_free_urb(self->rx_urb[i]);
1861         kfree(self->rx_urb);
1862         /* Clean up Tx and speed URB */
1863         usb_free_urb(self->tx_urb);
1864         usb_free_urb(self->speed_urb);
1865
1866         /* Free self and network device */
1867         free_netdev(self->netdev);
1868         IRDA_DEBUG(0, "%s(), USB IrDA Disconnected\n", __FUNCTION__);
1869 }
1870
1871 /*------------------------------------------------------------------*/
1872 /*
1873  * USB device callbacks
1874  */
1875 static struct usb_driver irda_driver = {
1876         .name           = "irda-usb",
1877         .probe          = irda_usb_probe,
1878         .disconnect     = irda_usb_disconnect,
1879         .id_table       = dongles,
1880 };
1881
1882 /************************* MODULE CALLBACKS *************************/
1883 /*
1884  * Deal with module insertion/removal
1885  * Mostly tell USB about our existence
1886  */
1887
1888 /*------------------------------------------------------------------*/
1889 /*
1890  * Module insertion
1891  */
1892 static int __init usb_irda_init(void)
1893 {
1894         int     ret;
1895
1896         ret = usb_register(&irda_driver);
1897         if (ret < 0)
1898                 return ret;
1899
1900         IRDA_MESSAGE("USB IrDA support registered\n");
1901         return 0;
1902 }
1903 module_init(usb_irda_init);
1904
1905 /*------------------------------------------------------------------*/
1906 /*
1907  * Module removal
1908  */
1909 static void __exit usb_irda_cleanup(void)
1910 {
1911         /* Deregister the driver and remove all pending instances */
1912         usb_deregister(&irda_driver);
1913 }
1914 module_exit(usb_irda_cleanup);
1915
1916 /*------------------------------------------------------------------*/
1917 /*
1918  * Module parameters
1919  */
1920 module_param(qos_mtt_bits, int, 0);
1921 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
1922 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>");
1923 MODULE_DESCRIPTION("IrDA-USB Dongle Driver");
1924 MODULE_LICENSE("GPL");