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