1 /*****************************************************************************
5 * Description: IrDA-USB Driver
7 * Author: Dag Brattli <dag@brattli.net>
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>
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.
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.
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.
30 *****************************************************************************/
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...
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)
44 * This driver has NOT been tested with the following drivers :
45 * o ehci-hcd (USB 2.0 controllers)
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...
55 /*------------------------------------------------------------------*/
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>
71 /*------------------------------------------------------------------*/
73 static int qos_mtt_bits = 0;
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, },
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.
106 MODULE_DEVICE_TABLE(usb, dongles);
108 /*------------------------------------------------------------------*/
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);
127 /************************ TRANSMIT ROUTINES ************************/
129 * Receive packets from the IrDA stack and send them on the USB pipe.
130 * Handle speed change, timeout and lot's of ugliness...
133 /*------------------------------------------------------------------*/
135 * Function irda_usb_build_header(self, skb, header)
137 * Builds USB-IrDA outbound header
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.
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...
149 static void irda_usb_build_header(struct irda_usb_cb *self,
153 /* Here we check if we have an STIR421x chip,
154 * and if either speed or xbofs (or both) needs
157 if (self->capability & IUC_STIR421X &&
158 ((self->new_speed != -1) || (self->new_xbofs != -1))) {
160 /* With STIR421x, speed and xBOFs must be set at the same
161 * time, even if only one of them changes.
163 if (self->new_speed == -1)
164 self->new_speed = self->speed ;
166 if (self->new_xbofs == -1)
167 self->new_xbofs = self->xbofs ;
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
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__);
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 */
190 switch (self->speed) {
192 *header = SPEED_2400;
196 *header = SPEED_9600;
199 *header = SPEED_19200;
202 *header = SPEED_38400;
205 *header = SPEED_57600;
208 *header = SPEED_115200;
211 *header = SPEED_576000;
214 *header = SPEED_1152000;
217 *header = SPEED_4000000;
221 *header = SPEED_16000000;
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 */
236 switch (self->xbofs) {
241 case 24: /* USB spec 1.0 says 24 */
248 case 5: /* Bug in IrLAP spec? (should be 6) */
269 * calculate turnaround time for SigmaTel header
271 static __u8 get_turnaround_time(struct sk_buff *skb)
273 int turnaround_time = irda_get_mtt(skb);
275 if ( turnaround_time == 0 )
277 else if ( turnaround_time <= 10 )
279 else if ( turnaround_time <= 50 )
281 else if ( turnaround_time <= 100 )
283 else if ( turnaround_time <= 500 )
285 else if ( turnaround_time <= 1000 )
287 else if ( turnaround_time <= 5000 )
294 /*------------------------------------------------------------------*/
296 * Send a command to change the speed of the dongle
297 * Need to be called with spinlock on.
299 static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self)
305 IRDA_DEBUG(2, "%s(), speed=%d, xbofs=%d\n", __FUNCTION__,
306 self->new_speed, self->new_xbofs);
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__);
315 /* Allocate the fake frame */
316 frame = self->speed_buff;
318 /* Set the new speed and xbofs in this fake frame */
319 irda_usb_build_header(self, frame, 1);
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
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;
335 /* Irq disabled -> GFP_ATOMIC */
336 if ((ret = usb_submit_urb(urb, GFP_ATOMIC))) {
337 IRDA_WARNING("%s(), failed Speed URB\n", __FUNCTION__);
341 /*------------------------------------------------------------------*/
344 * Now, we can only get called for the speed URB.
346 static void speed_bulk_callback(struct urb *urb)
348 struct irda_usb_cb *self = urb->context;
350 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
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;);
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);
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.
366 /* A reset of the dongle might be welcomed here - Jean II */
370 /* urb is now available */
371 //urb->status = 0; -> tested above
373 /* New speed and xbof is now commited in hardware */
374 self->new_speed = -1;
375 self->new_xbofs = -1;
377 /* Allow the stack to send more packets */
378 netif_wake_queue(self->netdev);
381 /*------------------------------------------------------------------*/
383 * Send an IrDA frame to the USB dongle (for transmission)
385 static int irda_usb_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
387 struct irda_usb_cb *self = netdev->priv;
388 struct urb *urb = self->tx_urb;
393 int err = 1; /* Failed */
395 IRDA_DEBUG(4, "%s() on %s\n", __FUNCTION__, netdev->name);
397 netif_stop_queue(netdev);
399 /* Protect us from USB callbacks, net watchdog and else. */
400 spin_lock_irqsave(&self->lock, flags);
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__);
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;
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;
422 /* Check for empty frame */
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...
431 irda_usb_change_speed_xbofs(self);
432 netdev->trans_start = jiffies;
433 /* Will netif_wake_queue() in callback */
434 err = 0; /* No error */
439 if (urb->status != 0) {
440 IRDA_WARNING("%s(), URB still in use!\n", __FUNCTION__);
444 /* Make sure there is room for IrDA-USB header. The actual
445 * allocation will be done lower in skb_push().
446 * Also, we don't use directly skb_cow(), because it require
447 * headroom >= 16, which force unnecessary copies - Jean II */
448 if (skb_headroom(skb) < self->header_length) {
449 IRDA_DEBUG(0, "%s(), Insuficient skb headroom.\n", __FUNCTION__);
450 if (skb_cow(skb, self->header_length)) {
451 IRDA_WARNING("%s(), failed skb_cow() !!!\n", __FUNCTION__);
456 /* Change setting for next frame */
458 if (self->capability & IUC_STIR421X) {
459 __u8 turnaround_time;
461 turnaround_time = get_turnaround_time( skb );
462 frame= skb_push(skb, self->header_length);
463 irda_usb_build_header(self, frame, 0);
464 frame[2] = turnaround_time;
465 if ((skb->len != 0) &&
466 ((skb->len % 128) == 0) &&
467 ((skb->len % 512) != 0)) {
468 /* add extra byte for special SigmaTel feature */
475 irda_usb_build_header(self, skb_push(skb, self->header_length), 0);
478 /* FIXME: Make macro out of this one */
479 ((struct irda_skb_cb *)skb->cb)->context = self;
481 usb_fill_bulk_urb(urb, self->usbdev,
482 usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
483 skb->data, IRDA_SKB_MAX_MTU,
484 write_bulk_callback, skb);
485 urb->transfer_buffer_length = skb->len;
486 /* This flag (URB_ZERO_PACKET) indicates that what we send is not
487 * a continuous stream of data but separate packets.
488 * In this case, the USB layer will insert an empty USB frame (TD)
489 * after each of our packets that is exact multiple of the frame size.
490 * This is how the dongle will detect the end of packet - Jean II */
491 urb->transfer_flags = URB_ZERO_PACKET;
493 /* Generate min turn time. FIXME: can we do better than this? */
494 /* Trying to a turnaround time at this level is trying to measure
495 * processor clock cycle with a wrist-watch, approximate at best...
497 * What we know is the last time we received a frame over USB.
498 * Due to latency over USB that depend on the USB load, we don't
499 * know when this frame was received over IrDA (a few ms before ?)
500 * Then, same story for our outgoing frame...
502 * In theory, the USB dongle is supposed to handle the turnaround
503 * by itself (spec 1.0, chater 4, page 6). Who knows ??? That's
504 * why this code is enabled only for dongles that doesn't meet
507 if (self->capability & IUC_NO_TURN) {
508 mtt = irda_get_mtt(skb);
511 do_gettimeofday(&self->now);
512 diff = self->now.tv_usec - self->stamp.tv_usec;
513 #ifdef IU_USB_MIN_RTT
514 /* Factor in USB delays -> Get rid of udelay() that
515 * would be lost in the noise - Jean II */
516 diff += IU_USB_MIN_RTT;
517 #endif /* IU_USB_MIN_RTT */
518 /* If the usec counter did wraparound, the diff will
519 * go negative (tv_usec is a long), so we need to
520 * correct it by one second. Jean II */
524 /* Check if the mtt is larger than the time we have
525 * already used by all the protocol processing
537 /* Ask USB to send the packet - Irq disabled -> GFP_ATOMIC */
538 if ((res = usb_submit_urb(urb, GFP_ATOMIC))) {
539 IRDA_WARNING("%s(), failed Tx URB\n", __FUNCTION__);
540 self->stats.tx_errors++;
541 /* Let USB recover : We will catch that in the watchdog */
542 /*netif_start_queue(netdev);*/
544 /* Increment packet stats */
545 self->stats.tx_packets++;
546 self->stats.tx_bytes += skb->len;
548 netdev->trans_start = jiffies;
550 spin_unlock_irqrestore(&self->lock, flags);
555 /* Drop silently the skb and exit */
557 spin_unlock_irqrestore(&self->lock, flags);
558 return err; /* Usually 1 */
561 /*------------------------------------------------------------------*/
563 * Note : this function will be called only for tx_urb...
565 static void write_bulk_callback(struct urb *urb)
568 struct sk_buff *skb = urb->context;
569 struct irda_usb_cb *self = ((struct irda_skb_cb *) skb->cb)->context;
571 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
573 /* We should always have a context */
574 IRDA_ASSERT(self != NULL, return;);
575 /* We should always be called for the speed URB */
576 IRDA_ASSERT(urb == self->tx_urb, return;);
578 /* Free up the skb */
579 dev_kfree_skb_any(skb);
582 /* Check for timeout and other USB nasties */
583 if (urb->status != 0) {
584 /* I get a lot of -ECONNABORTED = -103 here - Jean II */
585 IRDA_DEBUG(0, "%s(), URB complete status %d, transfer_flags 0x%04X\n", __FUNCTION__, urb->status, urb->transfer_flags);
587 /* Don't do anything here, that might confuse the USB layer,
588 * and we could go in recursion and blow the kernel stack...
589 * Instead, we will wait for irda_usb_net_timeout(), the
590 * network layer watchdog, to fix the situation.
592 /* A reset of the dongle might be welcomed here - Jean II */
596 /* urb is now available */
597 //urb->status = 0; -> tested above
599 /* Make sure we read self->present properly */
600 spin_lock_irqsave(&self->lock, flags);
602 /* If the network is closed, stop everything */
603 if ((!self->netopen) || (!self->present)) {
604 IRDA_DEBUG(0, "%s(), Network is gone...\n", __FUNCTION__);
605 spin_unlock_irqrestore(&self->lock, flags);
609 /* If changes to speed or xbofs is pending... */
610 if ((self->new_speed != -1) || (self->new_xbofs != -1)) {
611 if ((self->new_speed != self->speed) ||
612 (self->new_xbofs != self->xbofs)) {
613 /* We haven't changed speed yet (because of
614 * IUC_SPEED_BUG), so do it now - Jean II */
615 IRDA_DEBUG(1, "%s(), Changing speed now...\n", __FUNCTION__);
616 irda_usb_change_speed_xbofs(self);
618 /* New speed and xbof is now commited in hardware */
619 self->new_speed = -1;
620 self->new_xbofs = -1;
621 /* Done, waiting for next packet */
622 netif_wake_queue(self->netdev);
625 /* Otherwise, allow the stack to send more packets */
626 netif_wake_queue(self->netdev);
628 spin_unlock_irqrestore(&self->lock, flags);
631 /*------------------------------------------------------------------*/
633 * Watchdog timer from the network layer.
634 * After a predetermined timeout, if we don't give confirmation that
635 * the packet has been sent (i.e. no call to netif_wake_queue()),
636 * the network layer will call this function.
637 * Note that URB that we submit have also a timeout. When the URB timeout
638 * expire, the normal URB callback is called (write_bulk_callback()).
640 static void irda_usb_net_timeout(struct net_device *netdev)
643 struct irda_usb_cb *self = netdev->priv;
645 int done = 0; /* If we have made any progress */
647 IRDA_DEBUG(0, "%s(), Network layer thinks we timed out!\n", __FUNCTION__);
648 IRDA_ASSERT(self != NULL, return;);
650 /* Protect us from USB callbacks, net Tx and else. */
651 spin_lock_irqsave(&self->lock, flags);
653 /* self->present *MUST* be read under spinlock */
654 if (!self->present) {
655 IRDA_WARNING("%s(), device not present!\n", __FUNCTION__);
656 netif_stop_queue(netdev);
657 spin_unlock_irqrestore(&self->lock, flags);
661 /* Check speed URB */
662 urb = self->speed_urb;
663 if (urb->status != 0) {
664 IRDA_DEBUG(0, "%s: Speed change timed out, urb->status=%d, urb->transfer_flags=0x%04X\n", netdev->name, urb->status, urb->transfer_flags);
666 switch (urb->status) {
669 /* Note : above will *NOT* call netif_wake_queue()
670 * in completion handler, we will come back here.
675 case -ENOENT: /* urb unlinked by us */
676 default: /* ??? - Play safe */
678 netif_wake_queue(self->netdev);
686 if (urb->status != 0) {
687 struct sk_buff *skb = urb->context;
689 IRDA_DEBUG(0, "%s: Tx timed out, urb->status=%d, urb->transfer_flags=0x%04X\n", netdev->name, urb->status, urb->transfer_flags);
691 /* Increase error count */
692 self->stats.tx_errors++;
694 #ifdef IU_BUG_KICK_TIMEOUT
695 /* Can't be a bad idea to reset the speed ;-) - Jean II */
696 if(self->new_speed == -1)
697 self->new_speed = self->speed;
698 if(self->new_xbofs == -1)
699 self->new_xbofs = self->xbofs;
700 irda_usb_change_speed_xbofs(self);
701 #endif /* IU_BUG_KICK_TIMEOUT */
703 switch (urb->status) {
706 /* Note : above will *NOT* call netif_wake_queue()
707 * in completion handler, because urb->status will
708 * be -ENOENT. We will fix that at the next watchdog,
709 * leaving more time to USB to recover...
714 case -ENOENT: /* urb unlinked by us */
715 default: /* ??? - Play safe */
717 dev_kfree_skb_any(skb);
721 netif_wake_queue(self->netdev);
726 spin_unlock_irqrestore(&self->lock, flags);
728 /* Maybe we need a reset */
729 /* Note : Some drivers seem to use a usb_set_interface() when they
730 * need to reset the hardware. Hum...
736 /************************* RECEIVE ROUTINES *************************/
738 * Receive packets from the USB layer stack and pass them to the IrDA stack.
739 * Try to work around USB failures...
744 * Some of you may have noticed that most dongle have an interrupt in pipe
745 * that we don't use. Here is the little secret...
746 * When we hang a Rx URB on the bulk in pipe, it generates some USB traffic
747 * in every USB frame. This is unnecessary overhead.
748 * The interrupt in pipe will generate an event every time a packet is
749 * received. Reading an interrupt pipe adds minimal overhead, but has some
751 * If we are connected (speed != 9600), we want to minimise latency, so
752 * we just always hang the Rx URB and ignore the interrupt.
753 * If we are not connected (speed == 9600), there is usually no Rx traffic,
754 * and we want to minimise the USB overhead. In this case we should wait
755 * on the interrupt pipe and hang the Rx URB only when an interrupt is
759 * Note : don't read the above as what we are currently doing, but as
760 * something we could do with KC dongle. Also don't forget that the
761 * interrupt pipe is not part of the original standard, so this would
762 * need to be optional...
766 /*------------------------------------------------------------------*/
768 * Submit a Rx URB to the USB layer to handle reception of a frame
769 * Mostly called by the completion callback of the previous URB.
773 static void irda_usb_submit(struct irda_usb_cb *self, struct sk_buff *skb, struct urb *urb)
775 struct irda_skb_cb *cb;
778 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
780 /* This should never happen */
781 IRDA_ASSERT(skb != NULL, return;);
782 IRDA_ASSERT(urb != NULL, return;);
784 /* Save ourselves in the skb */
785 cb = (struct irda_skb_cb *) skb->cb;
788 /* Reinitialize URB */
789 usb_fill_bulk_urb(urb, self->usbdev,
790 usb_rcvbulkpipe(self->usbdev, self->bulk_in_ep),
791 skb->data, IRDA_SKB_MAX_MTU,
792 irda_usb_receive, skb);
795 /* Can be called from irda_usb_receive (irq handler) -> GFP_ATOMIC */
796 ret = usb_submit_urb(urb, GFP_ATOMIC);
798 /* If this ever happen, we are in deep s***.
799 * Basically, the Rx path will stop... */
800 IRDA_WARNING("%s(), Failed to submit Rx URB %d\n",
805 /*------------------------------------------------------------------*/
807 * Function irda_usb_receive(urb)
809 * Called by the USB subsystem when a frame has been received
812 static void irda_usb_receive(struct urb *urb)
814 struct sk_buff *skb = (struct sk_buff *) urb->context;
815 struct irda_usb_cb *self;
816 struct irda_skb_cb *cb;
817 struct sk_buff *newskb;
818 struct sk_buff *dataskb;
819 struct urb *next_urb;
820 unsigned int len, docopy;
822 IRDA_DEBUG(2, "%s(), len=%d\n", __FUNCTION__, urb->actual_length);
825 cb = (struct irda_skb_cb *) skb->cb;
826 IRDA_ASSERT(cb != NULL, return;);
827 self = (struct irda_usb_cb *) cb->context;
828 IRDA_ASSERT(self != NULL, return;);
830 /* If the network is closed or the device gone, stop everything */
831 if ((!self->netopen) || (!self->present)) {
832 IRDA_DEBUG(0, "%s(), Network is gone!\n", __FUNCTION__);
833 /* Don't re-submit the URB : will stall the Rx path */
837 /* Check the status */
838 if (urb->status != 0) {
839 switch (urb->status) {
841 self->stats.rx_crc_errors++;
842 /* Also precursor to a hot-unplug on UHCI. */
845 /* Random error, if I remember correctly */
846 /* uhci_cleanup_unlink() is going to kill the Rx
847 * URB just after we return. No problem, at this
848 * point the URB will be idle ;-) - Jean II */
850 /* That's usually a hot-unplug. Submit will fail... */
852 /* Usually precursor to a hot-unplug on OHCI. */
854 self->stats.rx_errors++;
855 IRDA_DEBUG(0, "%s(), RX status %d, transfer_flags 0x%04X \n", __FUNCTION__, urb->status, urb->transfer_flags);
858 /* If we received an error, we don't want to resubmit the
859 * Rx URB straight away but to give the USB layer a little
860 * bit of breathing room.
861 * We are in the USB thread context, therefore there is a
862 * danger of recursion (new URB we submit fails, we come
864 * With recent USB stack (2.6.15+), I'm seeing that on
865 * hot unplug of the dongle...
866 * Lowest effective timer is 10ms...
868 self->rx_defer_timer.function = &irda_usb_rx_defer_expired;
869 self->rx_defer_timer.data = (unsigned long) urb;
870 mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
874 /* Check for empty frames */
875 if (urb->actual_length <= self->header_length) {
876 IRDA_WARNING("%s(), empty frame!\n", __FUNCTION__);
881 * Remember the time we received this frame, so we can
882 * reduce the min turn time a bit since we will know
883 * how much time we have used for protocol processing
885 do_gettimeofday(&self->stamp);
887 /* Check if we need to copy the data to a new skb or not.
888 * For most frames, we use ZeroCopy and pass the already
889 * allocated skb up the stack.
890 * If the frame is small, it is more efficient to copy it
891 * to save memory (copy will be fast anyway - that's
892 * called Rx-copy-break). Jean II */
893 docopy = (urb->actual_length < IRDA_RX_COPY_THRESHOLD);
895 /* Allocate a new skb */
896 if (self->capability & IUC_STIR421X)
897 newskb = dev_alloc_skb(docopy ? urb->actual_length :
899 USB_IRDA_STIR421X_HEADER);
901 newskb = dev_alloc_skb(docopy ? urb->actual_length :
905 self->stats.rx_dropped++;
906 /* We could deliver the current skb, but this would stall
907 * the Rx path. Better drop the packet... Jean II */
911 /* Make sure IP header get aligned (IrDA header is 5 bytes) */
912 /* But IrDA-USB header is 1 byte. Jean II */
913 //skb_reserve(newskb, USB_IRDA_HEADER - 1);
916 /* Copy packet, so we can recycle the original */
917 memcpy(newskb->data, skb->data, urb->actual_length);
918 /* Deliver this new skb */
920 /* And hook the old skb to the URB
921 * Note : we don't need to "clean up" the old skb,
922 * as we never touched it. Jean II */
924 /* We are using ZeroCopy. Deliver old skb */
926 /* And hook the new skb to the URB */
930 /* Set proper length on skb & remove USB-IrDA header */
931 skb_put(dataskb, urb->actual_length);
932 skb_pull(dataskb, self->header_length);
934 /* Ask the networking layer to queue the packet for the IrDA stack */
935 dataskb->dev = self->netdev;
936 dataskb->mac.raw = dataskb->data;
937 dataskb->protocol = htons(ETH_P_IRDA);
941 /* Keep stats up to date */
942 self->stats.rx_bytes += len;
943 self->stats.rx_packets++;
944 self->netdev->last_rx = jiffies;
947 /* Note : at this point, the URB we've just received (urb)
948 * is still referenced by the USB layer. For example, if we
949 * have received a -ECONNRESET, uhci_cleanup_unlink() will
950 * continue to process it (in fact, cleaning it up).
951 * If we were to submit this URB, disaster would ensue.
952 * Therefore, we submit our idle URB, and put this URB in our
955 /* Note : with this scheme, we could submit the idle URB before
956 * processing the Rx URB. I don't think it would buy us anything as
957 * we are running in the USB thread context. Jean II */
958 next_urb = self->idle_rx_urb;
960 /* Recycle Rx URB : Now, the idle URB is the present one */
962 self->idle_rx_urb = urb;
964 /* Submit the idle URB to replace the URB we've just received.
965 * Do it last to avoid race conditions... Jean II */
966 irda_usb_submit(self, skb, next_urb);
969 /*------------------------------------------------------------------*/
971 * In case of errors, we want the USB layer to have time to recover.
972 * Now, it is time to resubmit ouur Rx URB...
974 static void irda_usb_rx_defer_expired(unsigned long data)
976 struct urb *urb = (struct urb *) data;
977 struct sk_buff *skb = (struct sk_buff *) urb->context;
978 struct irda_usb_cb *self;
979 struct irda_skb_cb *cb;
980 struct urb *next_urb;
982 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
985 cb = (struct irda_skb_cb *) skb->cb;
986 IRDA_ASSERT(cb != NULL, return;);
987 self = (struct irda_usb_cb *) cb->context;
988 IRDA_ASSERT(self != NULL, return;);
990 /* Same stuff as when Rx is done, see above... */
991 next_urb = self->idle_rx_urb;
993 self->idle_rx_urb = urb;
994 irda_usb_submit(self, skb, next_urb);
997 /*------------------------------------------------------------------*/
999 * Callbak from IrDA layer. IrDA wants to know if we have
1000 * started receiving anything.
1002 static int irda_usb_is_receiving(struct irda_usb_cb *self)
1004 /* Note : because of the way UHCI works, it's almost impossible
1005 * to get this info. The Controller DMA directly to memory and
1006 * signal only when the whole frame is finished. To know if the
1007 * first TD of the URB has been filled or not seems hard work...
1009 * The other solution would be to use the "receiving" command
1010 * on the default decriptor with a usb_control_msg(), but that
1011 * would add USB traffic and would return result only in the
1012 * next USB frame (~1ms).
1014 * I've been told that current dongles send status info on their
1015 * interrupt endpoint, and that's what the Windows driver uses
1016 * to know this info. Unfortunately, this is not yet in the spec...
1021 return 0; /* For now */
1024 #define STIR421X_PATCH_PRODUCT_VER "Product Version: "
1025 #define STIR421X_PATCH_STMP_TAG "STMP"
1026 #define STIR421X_PATCH_CODE_OFFSET 512 /* patch image starts before here */
1027 /* marks end of patch file header (PC DOS text file EOF character) */
1028 #define STIR421X_PATCH_END_OF_HDR_TAG 0x1A
1029 #define STIR421X_PATCH_BLOCK_SIZE 1023
1032 * Function stir421x_fwupload (struct irda_usb_cb *self,
1033 * unsigned char *patch,
1034 * const unsigned int patch_len)
1036 * Upload firmware code to SigmaTel 421X IRDA-USB dongle
1038 static int stir421x_fw_upload(struct irda_usb_cb *self,
1039 unsigned char *patch,
1040 const unsigned int patch_len)
1045 unsigned int block_size = 0;
1046 unsigned char *patch_block;
1048 patch_block = kzalloc(STIR421X_PATCH_BLOCK_SIZE, GFP_KERNEL);
1049 if (patch_block == NULL)
1052 /* break up patch into 1023-byte sections */
1053 for (i = 0; i < patch_len; i += block_size) {
1054 block_size = patch_len - i;
1056 if (block_size > STIR421X_PATCH_BLOCK_SIZE)
1057 block_size = STIR421X_PATCH_BLOCK_SIZE;
1059 /* upload the patch section */
1060 memcpy(patch_block, patch + i, block_size);
1062 ret = usb_bulk_msg(self->usbdev,
1063 usb_sndbulkpipe(self->usbdev,
1065 patch_block, block_size,
1066 &actual_len, msecs_to_jiffies(500));
1067 IRDA_DEBUG(3,"%s(): Bulk send %u bytes, ret=%d\n",
1068 __FUNCTION__, actual_len, ret);
1080 * Function stir421x_patch_device(struct irda_usb_cb *self)
1082 * Get a firmware code from userspase using hotplug request_firmware() call
1084 static int stir421x_patch_device(struct irda_usb_cb *self)
1088 char stir421x_fw_name[11];
1089 const struct firmware *fw;
1090 unsigned char *fw_version_ptr; /* pointer to version string */
1091 unsigned long fw_version = 0;
1094 * Known firmware patch file names for STIR421x dongles
1095 * are "42101001.sb" or "42101002.sb"
1097 sprintf(stir421x_fw_name, "4210%4X.sb",
1098 self->usbdev->descriptor.bcdDevice);
1099 ret = request_firmware(&fw, stir421x_fw_name, &self->usbdev->dev);
1103 /* We get a patch from userspace */
1104 IRDA_MESSAGE("%s(): Received firmware %s (%zu bytes)\n",
1105 __FUNCTION__, stir421x_fw_name, fw->size);
1109 /* Get the bcd product version */
1110 if (!memcmp(fw->data, STIR421X_PATCH_PRODUCT_VER,
1111 sizeof(STIR421X_PATCH_PRODUCT_VER) - 1)) {
1112 fw_version_ptr = fw->data +
1113 sizeof(STIR421X_PATCH_PRODUCT_VER) - 1;
1115 /* Let's check if the product version is dotted */
1116 if (fw_version_ptr[3] == '.' &&
1117 fw_version_ptr[7] == '.') {
1118 unsigned long major, minor, build;
1119 major = simple_strtoul(fw_version_ptr, NULL, 10);
1120 minor = simple_strtoul(fw_version_ptr + 4, NULL, 10);
1121 build = simple_strtoul(fw_version_ptr + 8, NULL, 10);
1123 fw_version = (major << 12)
1125 + ((build / 10) << 4)
1128 IRDA_DEBUG(3, "%s(): Firmware Product version %ld\n",
1129 __FUNCTION__, fw_version);
1133 if (self->usbdev->descriptor.bcdDevice == fw_version) {
1135 * If we're here, we've found a correct patch
1136 * The actual image starts after the "STMP" keyword
1137 * so forward to the firmware header tag
1139 for (i = 0; (fw->data[i] != STIR421X_PATCH_END_OF_HDR_TAG)
1140 && (i < fw->size); i++) ;
1141 /* here we check for the out of buffer case */
1142 if ((STIR421X_PATCH_END_OF_HDR_TAG == fw->data[i])
1143 && (i < STIR421X_PATCH_CODE_OFFSET)) {
1144 if (!memcmp(fw->data + i + 1, STIR421X_PATCH_STMP_TAG,
1145 sizeof(STIR421X_PATCH_STMP_TAG) - 1)) {
1147 /* We can upload the patch to the target */
1148 i += sizeof(STIR421X_PATCH_STMP_TAG);
1149 ret = stir421x_fw_upload(self, &fw->data[i],
1155 release_firmware(fw);
1161 /********************** IRDA DEVICE CALLBACKS **********************/
1163 * Main calls from the IrDA/Network subsystem.
1164 * Mostly registering a new irda-usb device and removing it....
1165 * We only deal with the IrDA side of the business, the USB side will
1166 * be dealt with below...
1170 /*------------------------------------------------------------------*/
1172 * Function irda_usb_net_open (dev)
1174 * Network device is taken up. Usually this is done by "ifconfig irda0 up"
1176 * Note : don't mess with self->netopen - Jean II
1178 static int irda_usb_net_open(struct net_device *netdev)
1180 struct irda_usb_cb *self;
1184 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1186 IRDA_ASSERT(netdev != NULL, return -1;);
1187 self = (struct irda_usb_cb *) netdev->priv;
1188 IRDA_ASSERT(self != NULL, return -1;);
1190 /* Can only open the device if it's there */
1191 if(!self->present) {
1192 IRDA_WARNING("%s(), device not present!\n", __FUNCTION__);
1196 if(self->needspatch) {
1197 IRDA_WARNING("%s(), device needs patch\n", __FUNCTION__) ;
1201 /* Initialise default speed and xbofs value
1202 * (IrLAP will change that soon) */
1205 self->new_speed = -1;
1206 self->new_xbofs = -1;
1208 /* To do *before* submitting Rx urbs and starting net Tx queue
1213 * Now that everything should be initialized properly,
1214 * Open new IrLAP layer instance to take care of us...
1215 * Note : will send immediately a speed change...
1217 sprintf(hwname, "usb#%d", self->usbdev->devnum);
1218 self->irlap = irlap_open(netdev, &self->qos, hwname);
1219 IRDA_ASSERT(self->irlap != NULL, return -1;);
1221 /* Allow IrLAP to send data to us */
1222 netif_start_queue(netdev);
1224 /* We submit all the Rx URB except for one that we keep idle.
1225 * Need to be initialised before submitting other USBs, because
1226 * in some cases as soon as we submit the URBs the USB layer
1227 * will trigger a dummy receive - Jean II */
1228 self->idle_rx_urb = self->rx_urb[IU_MAX_ACTIVE_RX_URBS];
1229 self->idle_rx_urb->context = NULL;
1231 /* Now that we can pass data to IrLAP, allow the USB layer
1232 * to send us some data... */
1233 for (i = 0; i < IU_MAX_ACTIVE_RX_URBS; i++) {
1234 struct sk_buff *skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
1236 /* If this ever happen, we are in deep s***.
1237 * Basically, we can't start the Rx path... */
1238 IRDA_WARNING("%s(), Failed to allocate Rx skb\n",
1242 //skb_reserve(newskb, USB_IRDA_HEADER - 1);
1243 irda_usb_submit(self, skb, self->rx_urb[i]);
1246 /* Ready to play !!! */
1250 /*------------------------------------------------------------------*/
1252 * Function irda_usb_net_close (self)
1254 * Network device is taken down. Usually this is done by
1255 * "ifconfig irda0 down"
1257 static int irda_usb_net_close(struct net_device *netdev)
1259 struct irda_usb_cb *self;
1262 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1264 IRDA_ASSERT(netdev != NULL, return -1;);
1265 self = (struct irda_usb_cb *) netdev->priv;
1266 IRDA_ASSERT(self != NULL, return -1;);
1268 /* Clear this flag *before* unlinking the urbs and *before*
1269 * stopping the network Tx queue - Jean II */
1272 /* Stop network Tx queue */
1273 netif_stop_queue(netdev);
1275 /* Kill defered Rx URB */
1276 del_timer(&self->rx_defer_timer);
1278 /* Deallocate all the Rx path buffers (URBs and skb) */
1279 for (i = 0; i < self->max_rx_urb; i++) {
1280 struct urb *urb = self->rx_urb[i];
1281 struct sk_buff *skb = (struct sk_buff *) urb->context;
1282 /* Cancel the receive command */
1284 /* The skb is ours, free it */
1287 urb->context = NULL;
1290 /* Cancel Tx and speed URB - need to be synchronous to avoid races */
1291 usb_kill_urb(self->tx_urb);
1292 usb_kill_urb(self->speed_urb);
1294 /* Stop and remove instance of IrLAP */
1296 irlap_close(self->irlap);
1302 /*------------------------------------------------------------------*/
1304 * IOCTLs : Extra out-of-band network commands...
1306 static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1308 unsigned long flags;
1309 struct if_irda_req *irq = (struct if_irda_req *) rq;
1310 struct irda_usb_cb *self;
1313 IRDA_ASSERT(dev != NULL, return -1;);
1315 IRDA_ASSERT(self != NULL, return -1;);
1317 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
1320 case SIOCSBANDWIDTH: /* Set bandwidth */
1321 if (!capable(CAP_NET_ADMIN))
1323 /* Protect us from USB callbacks, net watchdog and else. */
1324 spin_lock_irqsave(&self->lock, flags);
1325 /* Check if the device is still there */
1327 /* Set the desired speed */
1328 self->new_speed = irq->ifr_baudrate;
1329 irda_usb_change_speed_xbofs(self);
1331 spin_unlock_irqrestore(&self->lock, flags);
1333 case SIOCSMEDIABUSY: /* Set media busy */
1334 if (!capable(CAP_NET_ADMIN))
1336 /* Check if the IrDA stack is still there */
1338 irda_device_set_media_busy(self->netdev, TRUE);
1340 case SIOCGRECEIVING: /* Check if we are receiving right now */
1341 irq->ifr_receiving = irda_usb_is_receiving(self);
1350 /*------------------------------------------------------------------*/
1352 * Get device stats (for /proc/net/dev and ifconfig)
1354 static struct net_device_stats *irda_usb_net_get_stats(struct net_device *dev)
1356 struct irda_usb_cb *self = dev->priv;
1357 return &self->stats;
1360 /********************* IRDA CONFIG SUBROUTINES *********************/
1362 * Various subroutines dealing with IrDA and network stuff we use to
1363 * configure and initialise each irda-usb instance.
1364 * These functions are used below in the main calls of the driver...
1367 /*------------------------------------------------------------------*/
1369 * Set proper values in the IrDA QOS structure
1371 static inline void irda_usb_init_qos(struct irda_usb_cb *self)
1373 struct irda_class_desc *desc;
1375 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
1377 desc = self->irda_desc;
1379 /* Initialize QoS for this device */
1380 irda_init_max_qos_capabilies(&self->qos);
1382 /* See spec section 7.2 for meaning.
1383 * Values are little endian (as most USB stuff), the IrDA stack
1384 * use it in native order (see parameters.c). - Jean II */
1385 self->qos.baud_rate.bits = le16_to_cpu(desc->wBaudRate);
1386 self->qos.min_turn_time.bits = desc->bmMinTurnaroundTime;
1387 self->qos.additional_bofs.bits = desc->bmAdditionalBOFs;
1388 self->qos.window_size.bits = desc->bmWindowSize;
1389 self->qos.data_size.bits = desc->bmDataSize;
1391 IRDA_DEBUG(0, "%s(), dongle says speed=0x%X, size=0x%X, window=0x%X, bofs=0x%X, turn=0x%X\n",
1392 __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);
1394 /* Don't always trust what the dongle tell us */
1395 if(self->capability & IUC_SIR_ONLY)
1396 self->qos.baud_rate.bits &= 0x00ff;
1397 if(self->capability & IUC_SMALL_PKT)
1398 self->qos.data_size.bits = 0x07;
1399 if(self->capability & IUC_NO_WINDOW)
1400 self->qos.window_size.bits = 0x01;
1401 if(self->capability & IUC_MAX_WINDOW)
1402 self->qos.window_size.bits = 0x7f;
1403 if(self->capability & IUC_MAX_XBOFS)
1404 self->qos.additional_bofs.bits = 0x01;
1407 /* Module parameter can override the rx window size */
1409 self->qos.min_turn_time.bits = qos_mtt_bits;
1412 * Note : most of those values apply only for the receive path,
1413 * the transmit path will be set differently - Jean II
1415 irda_qos_bits_to_value(&self->qos);
1418 /*------------------------------------------------------------------*/
1420 * Initialise the network side of the irda-usb instance
1421 * Called when a new USB instance is registered in irda_usb_probe()
1423 static inline int irda_usb_open(struct irda_usb_cb *self)
1425 struct net_device *netdev = self->netdev;
1427 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1429 irda_usb_init_qos(self);
1431 /* Override the network functions we need to use */
1432 netdev->hard_start_xmit = irda_usb_hard_xmit;
1433 netdev->tx_timeout = irda_usb_net_timeout;
1434 netdev->watchdog_timeo = 250*HZ/1000; /* 250 ms > USB timeout */
1435 netdev->open = irda_usb_net_open;
1436 netdev->stop = irda_usb_net_close;
1437 netdev->get_stats = irda_usb_net_get_stats;
1438 netdev->do_ioctl = irda_usb_net_ioctl;
1440 return register_netdev(netdev);
1443 /*------------------------------------------------------------------*/
1445 * Cleanup the network side of the irda-usb instance
1446 * Called when a USB instance is removed in irda_usb_disconnect()
1448 static inline void irda_usb_close(struct irda_usb_cb *self)
1450 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1452 /* Remove netdevice */
1453 unregister_netdev(self->netdev);
1455 /* Remove the speed buffer */
1456 kfree(self->speed_buff);
1457 self->speed_buff = NULL;
1460 /********************** USB CONFIG SUBROUTINES **********************/
1462 * Various subroutines dealing with USB stuff we use to configure and
1463 * initialise each irda-usb instance.
1464 * These functions are used below in the main calls of the driver...
1467 /*------------------------------------------------------------------*/
1469 * Function irda_usb_parse_endpoints(dev, ifnum)
1471 * Parse the various endpoints and find the one we need.
1473 * The endpoint are the pipes used to communicate with the USB device.
1474 * The spec defines 2 endpoints of type bulk transfer, one in, and one out.
1475 * These are used to pass frames back and forth with the dongle.
1476 * Most dongle have also an interrupt endpoint, that will be probably
1477 * documented in the next spec...
1479 static inline int irda_usb_parse_endpoints(struct irda_usb_cb *self, struct usb_host_endpoint *endpoint, int ennum)
1481 int i; /* Endpoint index in table */
1483 /* Init : no endpoints */
1484 self->bulk_in_ep = 0;
1485 self->bulk_out_ep = 0;
1486 self->bulk_int_ep = 0;
1488 /* Let's look at all those endpoints */
1489 for(i = 0; i < ennum; i++) {
1490 /* All those variables will get optimised by the compiler,
1491 * so let's aim for clarity... - Jean II */
1492 __u8 ep; /* Endpoint address */
1493 __u8 dir; /* Endpoint direction */
1494 __u8 attr; /* Endpoint attribute */
1495 __u16 psize; /* Endpoint max packet size in bytes */
1497 /* Get endpoint address, direction and attribute */
1498 ep = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1499 dir = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK;
1500 attr = endpoint[i].desc.bmAttributes;
1501 psize = le16_to_cpu(endpoint[i].desc.wMaxPacketSize);
1503 /* Is it a bulk endpoint ??? */
1504 if(attr == USB_ENDPOINT_XFER_BULK) {
1505 /* We need to find an IN and an OUT */
1506 if(dir == USB_DIR_IN) {
1507 /* This is our Rx endpoint */
1508 self->bulk_in_ep = ep;
1510 /* This is our Tx endpoint */
1511 self->bulk_out_ep = ep;
1512 self->bulk_out_mtu = psize;
1515 if((attr == USB_ENDPOINT_XFER_INT) &&
1516 (dir == USB_DIR_IN)) {
1517 /* This is our interrupt endpoint */
1518 self->bulk_int_ep = ep;
1520 IRDA_ERROR("%s(), Unrecognised endpoint %02X.\n", __FUNCTION__, ep);
1525 IRDA_DEBUG(0, "%s(), And our endpoints are : in=%02X, out=%02X (%d), int=%02X\n",
1526 __FUNCTION__, self->bulk_in_ep, self->bulk_out_ep, self->bulk_out_mtu, self->bulk_int_ep);
1527 /* Should be 8, 16, 32 or 64 bytes */
1528 IRDA_ASSERT(self->bulk_out_mtu == 64, ;);
1530 return((self->bulk_in_ep != 0) && (self->bulk_out_ep != 0));
1533 #ifdef IU_DUMP_CLASS_DESC
1534 /*------------------------------------------------------------------*/
1536 * Function usb_irda_dump_class_desc(desc)
1538 * Prints out the contents of the IrDA class descriptor
1541 static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
1543 /* Values are little endian */
1544 printk("bLength=%x\n", desc->bLength);
1545 printk("bDescriptorType=%x\n", desc->bDescriptorType);
1546 printk("bcdSpecRevision=%x\n", le16_to_cpu(desc->bcdSpecRevision));
1547 printk("bmDataSize=%x\n", desc->bmDataSize);
1548 printk("bmWindowSize=%x\n", desc->bmWindowSize);
1549 printk("bmMinTurnaroundTime=%d\n", desc->bmMinTurnaroundTime);
1550 printk("wBaudRate=%x\n", le16_to_cpu(desc->wBaudRate));
1551 printk("bmAdditionalBOFs=%x\n", desc->bmAdditionalBOFs);
1552 printk("bIrdaRateSniff=%x\n", desc->bIrdaRateSniff);
1553 printk("bMaxUnicastList=%x\n", desc->bMaxUnicastList);
1555 #endif /* IU_DUMP_CLASS_DESC */
1557 /*------------------------------------------------------------------*/
1559 * Function irda_usb_find_class_desc(intf)
1561 * Returns instance of IrDA class descriptor, or NULL if not found
1563 * The class descriptor is some extra info that IrDA USB devices will
1564 * offer to us, describing their IrDA characteristics. We will use that in
1565 * irda_usb_init_qos()
1567 static inline struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf)
1569 struct usb_device *dev = interface_to_usbdev (intf);
1570 struct irda_class_desc *desc;
1573 desc = kmalloc(sizeof (*desc), GFP_KERNEL);
1576 memset(desc, 0, sizeof(*desc));
1578 /* USB-IrDA class spec 1.0:
1579 * 6.1.3: Standard "Get Descriptor" Device Request is not
1580 * appropriate to retrieve class-specific descriptor
1581 * 6.2.5: Class Specific "Get Class Descriptor" Interface Request
1582 * is mandatory and returns the USB-IrDA class descriptor
1585 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
1586 IU_REQ_GET_CLASS_DESC,
1587 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1588 0, intf->altsetting->desc.bInterfaceNumber, desc,
1589 sizeof(*desc), 500);
1591 IRDA_DEBUG(1, "%s(), ret=%d\n", __FUNCTION__, ret);
1592 if (ret < sizeof(*desc)) {
1593 IRDA_WARNING("usb-irda: class_descriptor read %s (%d)\n",
1594 (ret<0) ? "failed" : "too short", ret);
1596 else if (desc->bDescriptorType != USB_DT_IRDA) {
1597 IRDA_WARNING("usb-irda: bad class_descriptor type\n");
1600 #ifdef IU_DUMP_CLASS_DESC
1601 irda_usb_dump_class_desc(desc);
1602 #endif /* IU_DUMP_CLASS_DESC */
1610 /*********************** USB DEVICE CALLBACKS ***********************/
1612 * Main calls from the USB subsystem.
1613 * Mostly registering a new irda-usb device and removing it....
1616 /*------------------------------------------------------------------*/
1618 * This routine is called by the USB subsystem for each new device
1619 * in the system. We need to check if the device is ours, and in
1620 * this case start handling it.
1621 * The USB layer protect us from reentrancy (via BKL), so we don't need
1622 * to spinlock in there... Jean II
1624 static int irda_usb_probe(struct usb_interface *intf,
1625 const struct usb_device_id *id)
1627 struct net_device *net;
1628 struct usb_device *dev = interface_to_usbdev(intf);
1629 struct irda_usb_cb *self = NULL;
1630 struct usb_host_interface *interface;
1631 struct irda_class_desc *irda_desc;
1633 int i; /* Driver instance index / Rx URB index */
1635 /* Note : the probe make sure to call us only for devices that
1636 * matches the list of dongle (top of the file). So, we
1637 * don't need to check if the dongle is really ours.
1640 IRDA_MESSAGE("IRDA-USB found at address %d, Vendor: %x, Product: %x\n",
1641 dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
1642 le16_to_cpu(dev->descriptor.idProduct));
1644 net = alloc_irdadev(sizeof(*self));
1648 SET_MODULE_OWNER(net);
1649 SET_NETDEV_DEV(net, &intf->dev);
1652 spin_lock_init(&self->lock);
1653 init_timer(&self->rx_defer_timer);
1655 self->capability = id->driver_info;
1656 self->needspatch = ((self->capability & IUC_STIR421X) != 0);
1658 /* Create all of the needed urbs */
1659 if (self->capability & IUC_STIR421X) {
1660 self->max_rx_urb = IU_SIGMATEL_MAX_RX_URBS;
1661 self->header_length = USB_IRDA_STIR421X_HEADER;
1663 self->max_rx_urb = IU_MAX_RX_URBS;
1664 self->header_length = USB_IRDA_HEADER;
1667 self->rx_urb = kzalloc(self->max_rx_urb * sizeof(struct urb *),
1670 for (i = 0; i < self->max_rx_urb; i++) {
1671 self->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
1672 if (!self->rx_urb[i]) {
1676 self->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1677 if (!self->tx_urb) {
1680 self->speed_urb = usb_alloc_urb(0, GFP_KERNEL);
1681 if (!self->speed_urb) {
1685 /* Is this really necessary? (no, except maybe for broken devices) */
1686 if (usb_reset_configuration (dev) < 0) {
1687 err("reset_configuration failed");
1692 /* Is this really necessary? */
1693 /* Note : some driver do hardcode the interface number, some others
1694 * specify an alternate, but very few driver do like this.
1696 ret = usb_set_interface(dev, intf->altsetting->desc.bInterfaceNumber, 0);
1697 IRDA_DEBUG(1, "usb-irda: set interface %d result %d\n", intf->altsetting->desc.bInterfaceNumber, ret);
1701 case -EPIPE: /* -EPIPE = -32 */
1702 /* Martin Diehl says if we get a -EPIPE we should
1703 * be fine and we don't need to do a usb_clear_halt().
1705 IRDA_DEBUG(0, "%s(), Received -EPIPE, ignoring...\n", __FUNCTION__);
1708 IRDA_DEBUG(0, "%s(), Unknown error %d\n", __FUNCTION__, ret);
1713 /* Find our endpoints */
1714 interface = intf->cur_altsetting;
1715 if(!irda_usb_parse_endpoints(self, interface->endpoint,
1716 interface->desc.bNumEndpoints)) {
1717 IRDA_ERROR("%s(), Bogus endpoints...\n", __FUNCTION__);
1724 /* Find IrDA class descriptor */
1725 irda_desc = irda_usb_find_class_desc(intf);
1727 if (irda_desc == NULL)
1730 if (self->needspatch) {
1731 ret = usb_control_msg (self->usbdev, usb_sndctrlpipe (self->usbdev, 0),
1732 0x02, 0x40, 0, 0, NULL, 0, 500);
1734 IRDA_DEBUG (0, "usb_control_msg failed %d\n", ret);
1741 self->irda_desc = irda_desc;
1744 self->usbintf = intf;
1746 /* Allocate the buffer for speed changes */
1747 /* Don't change this buffer size and allocation without doing
1748 * some heavy and complete testing. Don't ask why :-(
1750 self->speed_buff = (char *) kmalloc(IRDA_USB_SPEED_MTU, GFP_KERNEL);
1751 if (self->speed_buff == NULL)
1754 memset(self->speed_buff, 0, IRDA_USB_SPEED_MTU);
1756 ret = irda_usb_open(self);
1760 IRDA_MESSAGE("IrDA: Registered device %s\n", net->name);
1761 usb_set_intfdata(intf, self);
1763 if (self->needspatch) {
1764 /* Now we fetch and upload the firmware patch */
1765 ret = stir421x_patch_device(self);
1766 self->needspatch = (ret < 0);
1767 if (self->needspatch) {
1768 IRDA_ERROR("STIR421X: Couldn't upload patch\n");
1772 /* replace IrDA class descriptor with what patched device is now reporting */
1773 irda_desc = irda_usb_find_class_desc (self->usbintf);
1774 if (irda_desc == NULL) {
1778 if (self->irda_desc)
1779 kfree (self->irda_desc);
1780 self->irda_desc = irda_desc;
1781 irda_usb_init_qos(self);
1787 unregister_netdev(self->netdev);
1789 kfree(self->speed_buff);
1791 /* Free all urbs that we may have created */
1792 usb_free_urb(self->speed_urb);
1794 usb_free_urb(self->tx_urb);
1796 for (i = 0; i < self->max_rx_urb; i++)
1797 usb_free_urb(self->rx_urb[i]);
1803 /*------------------------------------------------------------------*/
1805 * The current irda-usb device is removed, the USB layer tell us
1806 * to shut it down...
1807 * One of the constraints is that when we exit this function,
1808 * we cannot use the usb_device no more. Gone. Destroyed. kfree().
1809 * Most other subsystem allow you to destroy the instance at a time
1810 * when it's convenient to you, to postpone it to a later date, but
1811 * not the USB subsystem.
1812 * So, we must make bloody sure that everything gets deactivated.
1815 static void irda_usb_disconnect(struct usb_interface *intf)
1817 unsigned long flags;
1818 struct irda_usb_cb *self = usb_get_intfdata(intf);
1821 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1823 usb_set_intfdata(intf, NULL);
1827 /* Make sure that the Tx path is not executing. - Jean II */
1828 spin_lock_irqsave(&self->lock, flags);
1830 /* Oups ! We are not there any more.
1831 * This will stop/desactivate the Tx path. - Jean II */
1834 /* Kill defered Rx URB */
1835 del_timer(&self->rx_defer_timer);
1837 /* We need to have irq enabled to unlink the URBs. That's OK,
1838 * at this point the Tx path is gone - Jean II */
1839 spin_unlock_irqrestore(&self->lock, flags);
1841 /* Hum... Check if networking is still active (avoid races) */
1842 if((self->netopen) || (self->irlap)) {
1843 /* Accept no more transmissions */
1844 /*netif_device_detach(self->netdev);*/
1845 netif_stop_queue(self->netdev);
1846 /* Stop all the receive URBs. Must be synchronous. */
1847 for (i = 0; i < self->max_rx_urb; i++)
1848 usb_kill_urb(self->rx_urb[i]);
1849 /* Cancel Tx and speed URB.
1850 * Make sure it's synchronous to avoid races. */
1851 usb_kill_urb(self->tx_urb);
1852 usb_kill_urb(self->speed_urb);
1855 /* Cleanup the device stuff */
1856 irda_usb_close(self);
1857 /* No longer attached to USB bus */
1858 self->usbdev = NULL;
1859 self->usbintf = NULL;
1861 /* Clean up our urbs */
1862 for (i = 0; i < self->max_rx_urb; i++)
1863 usb_free_urb(self->rx_urb[i]);
1864 kfree(self->rx_urb);
1865 /* Clean up Tx and speed URB */
1866 usb_free_urb(self->tx_urb);
1867 usb_free_urb(self->speed_urb);
1869 /* Free self and network device */
1870 free_netdev(self->netdev);
1871 IRDA_DEBUG(0, "%s(), USB IrDA Disconnected\n", __FUNCTION__);
1874 /*------------------------------------------------------------------*/
1876 * USB device callbacks
1878 static struct usb_driver irda_driver = {
1880 .probe = irda_usb_probe,
1881 .disconnect = irda_usb_disconnect,
1882 .id_table = dongles,
1885 /************************* MODULE CALLBACKS *************************/
1887 * Deal with module insertion/removal
1888 * Mostly tell USB about our existence
1891 /*------------------------------------------------------------------*/
1895 static int __init usb_irda_init(void)
1899 ret = usb_register(&irda_driver);
1903 IRDA_MESSAGE("USB IrDA support registered\n");
1906 module_init(usb_irda_init);
1908 /*------------------------------------------------------------------*/
1912 static void __exit usb_irda_cleanup(void)
1914 /* Deregister the driver and remove all pending instances */
1915 usb_deregister(&irda_driver);
1917 module_exit(usb_irda_cleanup);
1919 /*------------------------------------------------------------------*/
1923 module_param(qos_mtt_bits, int, 0);
1924 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
1925 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>");
1926 MODULE_DESCRIPTION("IrDA-USB Dongle Driver");
1927 MODULE_LICENSE("GPL");