Bluetooth: hci_uart: Use generic functionality from Broadcom module
[linux/fpc-iii.git] / drivers / net / irda / irda-usb.c
blobf6c916312577193cd32773cf20798f6509509770
1 /*****************************************************************************
3 * Filename: irda-usb.c
4 * Version: 0.10
5 * Description: IrDA-USB Driver
6 * Status: Experimental
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 *****************************************************************************/
33 * IMPORTANT NOTE
34 * --------------
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...
52 * Jean II
55 /*------------------------------------------------------------------*/
57 #include <linux/module.h>
58 #include <linux/moduleparam.h>
59 #include <linux/kernel.h>
60 #include <linux/types.h>
61 #include <linux/skbuff.h>
62 #include <linux/netdevice.h>
63 #include <linux/slab.h>
64 #include <linux/rtnetlink.h>
65 #include <linux/usb.h>
66 #include <linux/firmware.h>
68 #include "irda-usb.h"
70 /*------------------------------------------------------------------*/
72 static int qos_mtt_bits = 0;
74 /* These are the currently known IrDA USB dongles. Add new dongles here */
75 static struct usb_device_id dongles[] = {
76 /* ACTiSYS Corp., ACT-IR2000U FIR-USB Adapter */
77 { USB_DEVICE(0x9c4, 0x011), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
78 /* Look like ACTiSYS, Report : IBM Corp., IBM UltraPort IrDA */
79 { USB_DEVICE(0x4428, 0x012), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
80 /* KC Technology Inc., KC-180 USB IrDA Device */
81 { USB_DEVICE(0x50f, 0x180), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
82 /* Extended Systems, Inc., XTNDAccess IrDA USB (ESI-9685) */
83 { USB_DEVICE(0x8e9, 0x100), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
84 /* SigmaTel STIR4210/4220/4116 USB IrDA (VFIR) Bridge */
85 { USB_DEVICE(0x66f, 0x4210), .driver_info = IUC_STIR421X | IUC_SPEED_BUG },
86 { USB_DEVICE(0x66f, 0x4220), .driver_info = IUC_STIR421X | IUC_SPEED_BUG },
87 { USB_DEVICE(0x66f, 0x4116), .driver_info = IUC_STIR421X | IUC_SPEED_BUG },
88 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
89 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
90 .bInterfaceClass = USB_CLASS_APP_SPEC,
91 .bInterfaceSubClass = USB_CLASS_IRDA,
92 .driver_info = IUC_DEFAULT, },
93 { }, /* The end */
97 * Important note :
98 * Devices based on the SigmaTel chipset (0x66f, 0x4200) are not designed
99 * using the "USB-IrDA specification" (yes, there exist such a thing), and
100 * therefore not supported by this driver (don't add them above).
101 * There is a Linux driver, stir4200, that support those USB devices.
102 * Jean II
105 MODULE_DEVICE_TABLE(usb, dongles);
107 /*------------------------------------------------------------------*/
109 static void irda_usb_init_qos(struct irda_usb_cb *self) ;
110 static struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf);
111 static void irda_usb_disconnect(struct usb_interface *intf);
112 static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self);
113 static netdev_tx_t irda_usb_hard_xmit(struct sk_buff *skb,
114 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);
126 /************************ TRANSMIT ROUTINES ************************/
128 * Receive packets from the IrDA stack and send them on the USB pipe.
129 * Handle speed change, timeout and lot's of ugliness...
132 /*------------------------------------------------------------------*/
134 * Function irda_usb_build_header(self, skb, header)
136 * Builds USB-IrDA outbound header
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.
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
148 static void irda_usb_build_header(struct irda_usb_cb *self,
149 __u8 *header,
150 int force)
152 /* Here we check if we have an STIR421x chip,
153 * and if either speed or xbofs (or both) needs
154 * to be changed.
156 if (self->capability & IUC_STIR421X &&
157 ((self->new_speed != -1) || (self->new_xbofs != -1))) {
159 /* With STIR421x, speed and xBOFs must be set at the same
160 * time, even if only one of them changes.
162 if (self->new_speed == -1)
163 self->new_speed = self->speed ;
165 if (self->new_xbofs == -1)
166 self->new_xbofs = self->xbofs ;
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
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 pr_debug("%s(), not changing speed yet\n", __func__);
180 *header = 0;
181 return;
184 pr_debug("%s(), changing speed to %d\n",
185 __func__, 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) {
191 case 2400:
192 *header = SPEED_2400;
193 break;
194 default:
195 case 9600:
196 *header = SPEED_9600;
197 break;
198 case 19200:
199 *header = SPEED_19200;
200 break;
201 case 38400:
202 *header = SPEED_38400;
203 break;
204 case 57600:
205 *header = SPEED_57600;
206 break;
207 case 115200:
208 *header = SPEED_115200;
209 break;
210 case 576000:
211 *header = SPEED_576000;
212 break;
213 case 1152000:
214 *header = SPEED_1152000;
215 break;
216 case 4000000:
217 *header = SPEED_4000000;
218 self->new_xbofs = 0;
219 break;
220 case 16000000:
221 *header = SPEED_16000000;
222 self->new_xbofs = 0;
223 break;
225 } else
226 /* No change */
227 *header = 0;
229 /* Set the negotiated additional XBOFS */
230 if (self->new_xbofs != -1) {
231 pr_debug("%s(), changing xbofs to %d\n",
232 __func__, self->new_xbofs);
233 self->xbofs = self->new_xbofs;
234 /* We will do ` self->new_xbofs = -1; ' in the completion
235 * handler just in case the current URB fail - Jean II */
237 switch (self->xbofs) {
238 case 48:
239 *header |= 0x10;
240 break;
241 case 28:
242 case 24: /* USB spec 1.0 says 24 */
243 *header |= 0x20;
244 break;
245 default:
246 case 12:
247 *header |= 0x30;
248 break;
249 case 5: /* Bug in IrLAP spec? (should be 6) */
250 case 6:
251 *header |= 0x40;
252 break;
253 case 3:
254 *header |= 0x50;
255 break;
256 case 2:
257 *header |= 0x60;
258 break;
259 case 1:
260 *header |= 0x70;
261 break;
262 case 0:
263 *header |= 0x80;
264 break;
270 * calculate turnaround time for SigmaTel header
272 static __u8 get_turnaround_time(struct sk_buff *skb)
274 int turnaround_time = irda_get_mtt(skb);
276 if ( turnaround_time == 0 )
277 return 0;
278 else if ( turnaround_time <= 10 )
279 return 1;
280 else if ( turnaround_time <= 50 )
281 return 2;
282 else if ( turnaround_time <= 100 )
283 return 3;
284 else if ( turnaround_time <= 500 )
285 return 4;
286 else if ( turnaround_time <= 1000 )
287 return 5;
288 else if ( turnaround_time <= 5000 )
289 return 6;
290 else
291 return 7;
295 /*------------------------------------------------------------------*/
297 * Send a command to change the speed of the dongle
298 * Need to be called with spinlock on.
300 static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self)
302 __u8 *frame;
303 struct urb *urb;
304 int ret;
306 pr_debug("%s(), speed=%d, xbofs=%d\n", __func__,
307 self->new_speed, self->new_xbofs);
309 /* Grab the speed URB */
310 urb = self->speed_urb;
311 if (urb->status != 0) {
312 net_warn_ratelimited("%s(), URB still in use!\n", __func__);
313 return;
316 /* Allocate the fake frame */
317 frame = self->speed_buff;
319 /* Set the new speed and xbofs in this fake frame */
320 irda_usb_build_header(self, frame, 1);
322 if (self->capability & IUC_STIR421X) {
323 if (frame[0] == 0) return ; // do nothing if no change
324 frame[1] = 0; // other parameters don't change here
325 frame[2] = 0;
328 /* Submit the 0 length IrDA frame to trigger new speed settings */
329 usb_fill_bulk_urb(urb, self->usbdev,
330 usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
331 frame, IRDA_USB_SPEED_MTU,
332 speed_bulk_callback, self);
333 urb->transfer_buffer_length = self->header_length;
334 urb->transfer_flags = 0;
336 /* Irq disabled -> GFP_ATOMIC */
337 if ((ret = usb_submit_urb(urb, GFP_ATOMIC))) {
338 net_warn_ratelimited("%s(), failed Speed URB\n", __func__);
342 /*------------------------------------------------------------------*/
344 * Speed URB callback
345 * Now, we can only get called for the speed URB.
347 static void speed_bulk_callback(struct urb *urb)
349 struct irda_usb_cb *self = urb->context;
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;);
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 pr_debug("%s(), URB complete status %d, transfer_flags 0x%04X\n",
360 __func__, 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.
365 * Jean II */
366 /* A reset of the dongle might be welcomed here - Jean II */
367 return;
370 /* urb is now available */
371 //urb->status = 0; -> tested above
373 /* New speed and xbof is now committed 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 netdev_tx_t irda_usb_hard_xmit(struct sk_buff *skb,
386 struct net_device *netdev)
388 struct irda_usb_cb *self = netdev_priv(netdev);
389 struct urb *urb = self->tx_urb;
390 unsigned long flags;
391 s32 speed;
392 s16 xbofs;
393 int res, mtt;
395 pr_debug("%s() on %s\n", __func__, 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 pr_debug("%s(), Device is gone...\n", __func__);
407 goto drop;
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 */
423 if (!skb->len) {
424 /* IrLAP send us an empty frame to make us change the
425 * speed. Changing speed with the USB adapter is in
426 * fact sending an empty frame to the adapter, so we
427 * could just let the present function do its job.
428 * However, we would wait for min turn time,
429 * do an extra memcpy and increment packet counters...
430 * Jean II */
431 irda_usb_change_speed_xbofs(self);
432 netdev->trans_start = jiffies;
433 /* Will netif_wake_queue() in callback */
434 goto drop;
438 if (urb->status != 0) {
439 net_warn_ratelimited("%s(), URB still in use!\n", __func__);
440 goto drop;
443 skb_copy_from_linear_data(skb, self->tx_buff + self->header_length, skb->len);
445 /* Change setting for next frame */
446 if (self->capability & IUC_STIR421X) {
447 __u8 turnaround_time;
448 __u8* frame = self->tx_buff;
449 turnaround_time = get_turnaround_time( skb );
450 irda_usb_build_header(self, frame, 0);
451 frame[2] = turnaround_time;
452 if ((skb->len != 0) &&
453 ((skb->len % 128) == 0) &&
454 ((skb->len % 512) != 0)) {
455 /* add extra byte for special SigmaTel feature */
456 frame[1] = 1;
457 skb_put(skb, 1);
458 } else {
459 frame[1] = 0;
461 } else {
462 irda_usb_build_header(self, self->tx_buff, 0);
465 /* FIXME: Make macro out of this one */
466 ((struct irda_skb_cb *)skb->cb)->context = self;
468 usb_fill_bulk_urb(urb, self->usbdev,
469 usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
470 self->tx_buff, skb->len + self->header_length,
471 write_bulk_callback, skb);
473 /* This flag (URB_ZERO_PACKET) indicates that what we send is not
474 * a continuous stream of data but separate packets.
475 * In this case, the USB layer will insert an empty USB frame (TD)
476 * after each of our packets that is exact multiple of the frame size.
477 * This is how the dongle will detect the end of packet - Jean II */
478 urb->transfer_flags = URB_ZERO_PACKET;
480 /* Generate min turn time. FIXME: can we do better than this? */
481 /* Trying to a turnaround time at this level is trying to measure
482 * processor clock cycle with a wrist-watch, approximate at best...
484 * What we know is the last time we received a frame over USB.
485 * Due to latency over USB that depend on the USB load, we don't
486 * know when this frame was received over IrDA (a few ms before ?)
487 * Then, same story for our outgoing frame...
489 * In theory, the USB dongle is supposed to handle the turnaround
490 * by itself (spec 1.0, chater 4, page 6). Who knows ??? That's
491 * why this code is enabled only for dongles that doesn't meet
492 * the spec.
493 * Jean II */
494 if (self->capability & IUC_NO_TURN) {
495 mtt = irda_get_mtt(skb);
496 if (mtt) {
497 int diff;
498 diff = ktime_us_delta(ktime_get(), self->stamp);
499 #ifdef IU_USB_MIN_RTT
500 /* Factor in USB delays -> Get rid of udelay() that
501 * would be lost in the noise - Jean II */
502 diff += IU_USB_MIN_RTT;
503 #endif /* IU_USB_MIN_RTT */
505 /* Check if the mtt is larger than the time we have
506 * already used by all the protocol processing
508 if (mtt > diff) {
509 mtt -= diff;
510 if (mtt > 1000)
511 mdelay(mtt/1000);
512 else
513 udelay(mtt);
518 /* Ask USB to send the packet - Irq disabled -> GFP_ATOMIC */
519 if ((res = usb_submit_urb(urb, GFP_ATOMIC))) {
520 net_warn_ratelimited("%s(), failed Tx URB\n", __func__);
521 netdev->stats.tx_errors++;
522 /* Let USB recover : We will catch that in the watchdog */
523 /*netif_start_queue(netdev);*/
524 } else {
525 /* Increment packet stats */
526 netdev->stats.tx_packets++;
527 netdev->stats.tx_bytes += skb->len;
529 netdev->trans_start = jiffies;
531 spin_unlock_irqrestore(&self->lock, flags);
533 return NETDEV_TX_OK;
535 drop:
536 /* Drop silently the skb and exit */
537 dev_kfree_skb(skb);
538 spin_unlock_irqrestore(&self->lock, flags);
539 return NETDEV_TX_OK;
542 /*------------------------------------------------------------------*/
544 * Note : this function will be called only for tx_urb...
546 static void write_bulk_callback(struct urb *urb)
548 unsigned long flags;
549 struct sk_buff *skb = urb->context;
550 struct irda_usb_cb *self = ((struct irda_skb_cb *) skb->cb)->context;
552 /* We should always have a context */
553 IRDA_ASSERT(self != NULL, return;);
554 /* We should always be called for the speed URB */
555 IRDA_ASSERT(urb == self->tx_urb, return;);
557 /* Free up the skb */
558 dev_kfree_skb_any(skb);
559 urb->context = NULL;
561 /* Check for timeout and other USB nasties */
562 if (urb->status != 0) {
563 /* I get a lot of -ECONNABORTED = -103 here - Jean II */
564 pr_debug("%s(), URB complete status %d, transfer_flags 0x%04X\n",
565 __func__, urb->status, urb->transfer_flags);
567 /* Don't do anything here, that might confuse the USB layer,
568 * and we could go in recursion and blow the kernel stack...
569 * Instead, we will wait for irda_usb_net_timeout(), the
570 * network layer watchdog, to fix the situation.
571 * Jean II */
572 /* A reset of the dongle might be welcomed here - Jean II */
573 return;
576 /* urb is now available */
577 //urb->status = 0; -> tested above
579 /* Make sure we read self->present properly */
580 spin_lock_irqsave(&self->lock, flags);
582 /* If the network is closed, stop everything */
583 if ((!self->netopen) || (!self->present)) {
584 pr_debug("%s(), Network is gone...\n", __func__);
585 spin_unlock_irqrestore(&self->lock, flags);
586 return;
589 /* If changes to speed or xbofs is pending... */
590 if ((self->new_speed != -1) || (self->new_xbofs != -1)) {
591 if ((self->new_speed != self->speed) ||
592 (self->new_xbofs != self->xbofs)) {
593 /* We haven't changed speed yet (because of
594 * IUC_SPEED_BUG), so do it now - Jean II */
595 pr_debug("%s(), Changing speed now...\n", __func__);
596 irda_usb_change_speed_xbofs(self);
597 } else {
598 /* New speed and xbof is now committed in hardware */
599 self->new_speed = -1;
600 self->new_xbofs = -1;
601 /* Done, waiting for next packet */
602 netif_wake_queue(self->netdev);
604 } else {
605 /* Otherwise, allow the stack to send more packets */
606 netif_wake_queue(self->netdev);
608 spin_unlock_irqrestore(&self->lock, flags);
611 /*------------------------------------------------------------------*/
613 * Watchdog timer from the network layer.
614 * After a predetermined timeout, if we don't give confirmation that
615 * the packet has been sent (i.e. no call to netif_wake_queue()),
616 * the network layer will call this function.
617 * Note that URB that we submit have also a timeout. When the URB timeout
618 * expire, the normal URB callback is called (write_bulk_callback()).
620 static void irda_usb_net_timeout(struct net_device *netdev)
622 unsigned long flags;
623 struct irda_usb_cb *self = netdev_priv(netdev);
624 struct urb *urb;
625 int done = 0; /* If we have made any progress */
627 pr_debug("%s(), Network layer thinks we timed out!\n", __func__);
628 IRDA_ASSERT(self != NULL, return;);
630 /* Protect us from USB callbacks, net Tx and else. */
631 spin_lock_irqsave(&self->lock, flags);
633 /* self->present *MUST* be read under spinlock */
634 if (!self->present) {
635 net_warn_ratelimited("%s(), device not present!\n", __func__);
636 netif_stop_queue(netdev);
637 spin_unlock_irqrestore(&self->lock, flags);
638 return;
641 /* Check speed URB */
642 urb = self->speed_urb;
643 if (urb->status != 0) {
644 pr_debug("%s: Speed change timed out, urb->status=%d, urb->transfer_flags=0x%04X\n",
645 netdev->name, urb->status, urb->transfer_flags);
647 switch (urb->status) {
648 case -EINPROGRESS:
649 usb_unlink_urb(urb);
650 /* Note : above will *NOT* call netif_wake_queue()
651 * in completion handler, we will come back here.
652 * Jean II */
653 done = 1;
654 break;
655 case -ECONNRESET:
656 case -ENOENT: /* urb unlinked by us */
657 default: /* ??? - Play safe */
658 urb->status = 0;
659 netif_wake_queue(self->netdev);
660 done = 1;
661 break;
665 /* Check Tx URB */
666 urb = self->tx_urb;
667 if (urb->status != 0) {
668 struct sk_buff *skb = urb->context;
670 pr_debug("%s: Tx timed out, urb->status=%d, urb->transfer_flags=0x%04X\n",
671 netdev->name, urb->status, urb->transfer_flags);
673 /* Increase error count */
674 netdev->stats.tx_errors++;
676 #ifdef IU_BUG_KICK_TIMEOUT
677 /* Can't be a bad idea to reset the speed ;-) - Jean II */
678 if(self->new_speed == -1)
679 self->new_speed = self->speed;
680 if(self->new_xbofs == -1)
681 self->new_xbofs = self->xbofs;
682 irda_usb_change_speed_xbofs(self);
683 #endif /* IU_BUG_KICK_TIMEOUT */
685 switch (urb->status) {
686 case -EINPROGRESS:
687 usb_unlink_urb(urb);
688 /* Note : above will *NOT* call netif_wake_queue()
689 * in completion handler, because urb->status will
690 * be -ENOENT. We will fix that at the next watchdog,
691 * leaving more time to USB to recover...
692 * Jean II */
693 done = 1;
694 break;
695 case -ECONNRESET:
696 case -ENOENT: /* urb unlinked by us */
697 default: /* ??? - Play safe */
698 if(skb != NULL) {
699 dev_kfree_skb_any(skb);
700 urb->context = NULL;
702 urb->status = 0;
703 netif_wake_queue(self->netdev);
704 done = 1;
705 break;
708 spin_unlock_irqrestore(&self->lock, flags);
710 /* Maybe we need a reset */
711 /* Note : Some drivers seem to use a usb_set_interface() when they
712 * need to reset the hardware. Hum...
715 /* if(done == 0) */
718 /************************* RECEIVE ROUTINES *************************/
720 * Receive packets from the USB layer stack and pass them to the IrDA stack.
721 * Try to work around USB failures...
725 * Note :
726 * Some of you may have noticed that most dongle have an interrupt in pipe
727 * that we don't use. Here is the little secret...
728 * When we hang a Rx URB on the bulk in pipe, it generates some USB traffic
729 * in every USB frame. This is unnecessary overhead.
730 * The interrupt in pipe will generate an event every time a packet is
731 * received. Reading an interrupt pipe adds minimal overhead, but has some
732 * latency (~1ms).
733 * If we are connected (speed != 9600), we want to minimise latency, so
734 * we just always hang the Rx URB and ignore the interrupt.
735 * If we are not connected (speed == 9600), there is usually no Rx traffic,
736 * and we want to minimise the USB overhead. In this case we should wait
737 * on the interrupt pipe and hang the Rx URB only when an interrupt is
738 * received.
739 * Jean II
741 * Note : don't read the above as what we are currently doing, but as
742 * something we could do with KC dongle. Also don't forget that the
743 * interrupt pipe is not part of the original standard, so this would
744 * need to be optional...
745 * Jean II
748 /*------------------------------------------------------------------*/
750 * Submit a Rx URB to the USB layer to handle reception of a frame
751 * Mostly called by the completion callback of the previous URB.
753 * Jean II
755 static void irda_usb_submit(struct irda_usb_cb *self, struct sk_buff *skb, struct urb *urb)
757 struct irda_skb_cb *cb;
758 int ret;
760 /* This should never happen */
761 IRDA_ASSERT(skb != NULL, return;);
762 IRDA_ASSERT(urb != NULL, return;);
764 /* Save ourselves in the skb */
765 cb = (struct irda_skb_cb *) skb->cb;
766 cb->context = self;
768 /* Reinitialize URB */
769 usb_fill_bulk_urb(urb, self->usbdev,
770 usb_rcvbulkpipe(self->usbdev, self->bulk_in_ep),
771 skb->data, IRDA_SKB_MAX_MTU,
772 irda_usb_receive, skb);
773 urb->status = 0;
775 /* Can be called from irda_usb_receive (irq handler) -> GFP_ATOMIC */
776 ret = usb_submit_urb(urb, GFP_ATOMIC);
777 if (ret) {
778 /* If this ever happen, we are in deep s***.
779 * Basically, the Rx path will stop... */
780 net_warn_ratelimited("%s(), Failed to submit Rx URB %d\n",
781 __func__, ret);
785 /*------------------------------------------------------------------*/
787 * Function irda_usb_receive(urb)
789 * Called by the USB subsystem when a frame has been received
792 static void irda_usb_receive(struct urb *urb)
794 struct sk_buff *skb = (struct sk_buff *) urb->context;
795 struct irda_usb_cb *self;
796 struct irda_skb_cb *cb;
797 struct sk_buff *newskb;
798 struct sk_buff *dataskb;
799 struct urb *next_urb;
800 unsigned int len, docopy;
802 pr_debug("%s(), len=%d\n", __func__, urb->actual_length);
804 /* Find ourselves */
805 cb = (struct irda_skb_cb *) skb->cb;
806 IRDA_ASSERT(cb != NULL, return;);
807 self = (struct irda_usb_cb *) cb->context;
808 IRDA_ASSERT(self != NULL, return;);
810 /* If the network is closed or the device gone, stop everything */
811 if ((!self->netopen) || (!self->present)) {
812 pr_debug("%s(), Network is gone!\n", __func__);
813 /* Don't re-submit the URB : will stall the Rx path */
814 return;
817 /* Check the status */
818 if (urb->status != 0) {
819 switch (urb->status) {
820 case -EILSEQ:
821 self->netdev->stats.rx_crc_errors++;
822 /* Also precursor to a hot-unplug on UHCI. */
823 /* Fallthrough... */
824 case -ECONNRESET:
825 /* Random error, if I remember correctly */
826 /* uhci_cleanup_unlink() is going to kill the Rx
827 * URB just after we return. No problem, at this
828 * point the URB will be idle ;-) - Jean II */
829 case -ESHUTDOWN:
830 /* That's usually a hot-unplug. Submit will fail... */
831 case -ETIME:
832 /* Usually precursor to a hot-unplug on OHCI. */
833 default:
834 self->netdev->stats.rx_errors++;
835 pr_debug("%s(), RX status %d, transfer_flags 0x%04X\n",
836 __func__, urb->status, urb->transfer_flags);
837 break;
839 /* If we received an error, we don't want to resubmit the
840 * Rx URB straight away but to give the USB layer a little
841 * bit of breathing room.
842 * We are in the USB thread context, therefore there is a
843 * danger of recursion (new URB we submit fails, we come
844 * back here).
845 * With recent USB stack (2.6.15+), I'm seeing that on
846 * hot unplug of the dongle...
847 * Lowest effective timer is 10ms...
848 * Jean II */
849 self->rx_defer_timer.function = irda_usb_rx_defer_expired;
850 self->rx_defer_timer.data = (unsigned long) urb;
851 mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
852 return;
855 /* Check for empty frames */
856 if (urb->actual_length <= self->header_length) {
857 net_warn_ratelimited("%s(), empty frame!\n", __func__);
858 goto done;
862 * Remember the time we received this frame, so we can
863 * reduce the min turn time a bit since we will know
864 * how much time we have used for protocol processing
866 self->stamp = ktime_get();
868 /* Check if we need to copy the data to a new skb or not.
869 * For most frames, we use ZeroCopy and pass the already
870 * allocated skb up the stack.
871 * If the frame is small, it is more efficient to copy it
872 * to save memory (copy will be fast anyway - that's
873 * called Rx-copy-break). Jean II */
874 docopy = (urb->actual_length < IRDA_RX_COPY_THRESHOLD);
876 /* Allocate a new skb */
877 if (self->capability & IUC_STIR421X)
878 newskb = dev_alloc_skb(docopy ? urb->actual_length :
879 IRDA_SKB_MAX_MTU +
880 USB_IRDA_STIR421X_HEADER);
881 else
882 newskb = dev_alloc_skb(docopy ? urb->actual_length :
883 IRDA_SKB_MAX_MTU);
885 if (!newskb) {
886 self->netdev->stats.rx_dropped++;
887 /* We could deliver the current skb, but this would stall
888 * the Rx path. Better drop the packet... Jean II */
889 goto done;
892 /* Make sure IP header get aligned (IrDA header is 5 bytes) */
893 /* But IrDA-USB header is 1 byte. Jean II */
894 //skb_reserve(newskb, USB_IRDA_HEADER - 1);
896 if(docopy) {
897 /* Copy packet, so we can recycle the original */
898 skb_copy_from_linear_data(skb, newskb->data, urb->actual_length);
899 /* Deliver this new skb */
900 dataskb = newskb;
901 /* And hook the old skb to the URB
902 * Note : we don't need to "clean up" the old skb,
903 * as we never touched it. Jean II */
904 } else {
905 /* We are using ZeroCopy. Deliver old skb */
906 dataskb = skb;
907 /* And hook the new skb to the URB */
908 skb = newskb;
911 /* Set proper length on skb & remove USB-IrDA header */
912 skb_put(dataskb, urb->actual_length);
913 skb_pull(dataskb, self->header_length);
915 /* Ask the networking layer to queue the packet for the IrDA stack */
916 dataskb->dev = self->netdev;
917 skb_reset_mac_header(dataskb);
918 dataskb->protocol = htons(ETH_P_IRDA);
919 len = dataskb->len;
920 netif_rx(dataskb);
922 /* Keep stats up to date */
923 self->netdev->stats.rx_bytes += len;
924 self->netdev->stats.rx_packets++;
926 done:
927 /* Note : at this point, the URB we've just received (urb)
928 * is still referenced by the USB layer. For example, if we
929 * have received a -ECONNRESET, uhci_cleanup_unlink() will
930 * continue to process it (in fact, cleaning it up).
931 * If we were to submit this URB, disaster would ensue.
932 * Therefore, we submit our idle URB, and put this URB in our
933 * idle slot....
934 * Jean II */
935 /* Note : with this scheme, we could submit the idle URB before
936 * processing the Rx URB. I don't think it would buy us anything as
937 * we are running in the USB thread context. Jean II */
938 next_urb = self->idle_rx_urb;
940 /* Recycle Rx URB : Now, the idle URB is the present one */
941 urb->context = NULL;
942 self->idle_rx_urb = urb;
944 /* Submit the idle URB to replace the URB we've just received.
945 * Do it last to avoid race conditions... Jean II */
946 irda_usb_submit(self, skb, next_urb);
949 /*------------------------------------------------------------------*/
951 * In case of errors, we want the USB layer to have time to recover.
952 * Now, it is time to resubmit ouur Rx URB...
954 static void irda_usb_rx_defer_expired(unsigned long data)
956 struct urb *urb = (struct urb *) data;
957 struct sk_buff *skb = (struct sk_buff *) urb->context;
958 struct irda_usb_cb *self;
959 struct irda_skb_cb *cb;
960 struct urb *next_urb;
962 /* Find ourselves */
963 cb = (struct irda_skb_cb *) skb->cb;
964 IRDA_ASSERT(cb != NULL, return;);
965 self = (struct irda_usb_cb *) cb->context;
966 IRDA_ASSERT(self != NULL, return;);
968 /* Same stuff as when Rx is done, see above... */
969 next_urb = self->idle_rx_urb;
970 urb->context = NULL;
971 self->idle_rx_urb = urb;
972 irda_usb_submit(self, skb, next_urb);
975 /*------------------------------------------------------------------*/
977 * Callbak from IrDA layer. IrDA wants to know if we have
978 * started receiving anything.
980 static int irda_usb_is_receiving(struct irda_usb_cb *self)
982 /* Note : because of the way UHCI works, it's almost impossible
983 * to get this info. The Controller DMA directly to memory and
984 * signal only when the whole frame is finished. To know if the
985 * first TD of the URB has been filled or not seems hard work...
987 * The other solution would be to use the "receiving" command
988 * on the default decriptor with a usb_control_msg(), but that
989 * would add USB traffic and would return result only in the
990 * next USB frame (~1ms).
992 * I've been told that current dongles send status info on their
993 * interrupt endpoint, and that's what the Windows driver uses
994 * to know this info. Unfortunately, this is not yet in the spec...
996 * Jean II
999 return 0; /* For now */
1002 #define STIR421X_PATCH_PRODUCT_VER "Product Version: "
1003 #define STIR421X_PATCH_STMP_TAG "STMP"
1004 #define STIR421X_PATCH_CODE_OFFSET 512 /* patch image starts before here */
1005 /* marks end of patch file header (PC DOS text file EOF character) */
1006 #define STIR421X_PATCH_END_OF_HDR_TAG 0x1A
1007 #define STIR421X_PATCH_BLOCK_SIZE 1023
1010 * Function stir421x_fwupload (struct irda_usb_cb *self,
1011 * unsigned char *patch,
1012 * const unsigned int patch_len)
1014 * Upload firmware code to SigmaTel 421X IRDA-USB dongle
1016 static int stir421x_fw_upload(struct irda_usb_cb *self,
1017 const unsigned char *patch,
1018 const unsigned int patch_len)
1020 int ret = -ENOMEM;
1021 int actual_len = 0;
1022 unsigned int i;
1023 unsigned int block_size = 0;
1024 unsigned char *patch_block;
1026 patch_block = kzalloc(STIR421X_PATCH_BLOCK_SIZE, GFP_KERNEL);
1027 if (patch_block == NULL)
1028 return -ENOMEM;
1030 /* break up patch into 1023-byte sections */
1031 for (i = 0; i < patch_len; i += block_size) {
1032 block_size = patch_len - i;
1034 if (block_size > STIR421X_PATCH_BLOCK_SIZE)
1035 block_size = STIR421X_PATCH_BLOCK_SIZE;
1037 /* upload the patch section */
1038 memcpy(patch_block, patch + i, block_size);
1040 ret = usb_bulk_msg(self->usbdev,
1041 usb_sndbulkpipe(self->usbdev,
1042 self->bulk_out_ep),
1043 patch_block, block_size,
1044 &actual_len, msecs_to_jiffies(500));
1045 pr_debug("%s(): Bulk send %u bytes, ret=%d\n",
1046 __func__, actual_len, ret);
1048 if (ret < 0)
1049 break;
1051 mdelay(10);
1054 kfree(patch_block);
1056 return ret;
1060 * Function stir421x_patch_device(struct irda_usb_cb *self)
1062 * Get a firmware code from userspase using hotplug request_firmware() call
1064 static int stir421x_patch_device(struct irda_usb_cb *self)
1066 unsigned int i;
1067 int ret;
1068 char stir421x_fw_name[12];
1069 const struct firmware *fw;
1070 const unsigned char *fw_version_ptr; /* pointer to version string */
1071 unsigned long fw_version = 0;
1074 * Known firmware patch file names for STIR421x dongles
1075 * are "42101001.sb" or "42101002.sb"
1077 sprintf(stir421x_fw_name, "4210%4X.sb",
1078 self->usbdev->descriptor.bcdDevice);
1079 ret = request_firmware(&fw, stir421x_fw_name, &self->usbdev->dev);
1080 if (ret < 0)
1081 return ret;
1083 /* We get a patch from userspace */
1084 net_info_ratelimited("%s(): Received firmware %s (%zu bytes)\n",
1085 __func__, stir421x_fw_name, fw->size);
1087 ret = -EINVAL;
1089 /* Get the bcd product version */
1090 if (!memcmp(fw->data, STIR421X_PATCH_PRODUCT_VER,
1091 sizeof(STIR421X_PATCH_PRODUCT_VER) - 1)) {
1092 fw_version_ptr = fw->data +
1093 sizeof(STIR421X_PATCH_PRODUCT_VER) - 1;
1095 /* Let's check if the product version is dotted */
1096 if (fw_version_ptr[3] == '.' &&
1097 fw_version_ptr[7] == '.') {
1098 unsigned long major, minor, build;
1099 major = simple_strtoul(fw_version_ptr, NULL, 10);
1100 minor = simple_strtoul(fw_version_ptr + 4, NULL, 10);
1101 build = simple_strtoul(fw_version_ptr + 8, NULL, 10);
1103 fw_version = (major << 12)
1104 + (minor << 8)
1105 + ((build / 10) << 4)
1106 + (build % 10);
1108 pr_debug("%s(): Firmware Product version %ld\n",
1109 __func__, fw_version);
1113 if (self->usbdev->descriptor.bcdDevice == cpu_to_le16(fw_version)) {
1115 * If we're here, we've found a correct patch
1116 * The actual image starts after the "STMP" keyword
1117 * so forward to the firmware header tag
1119 for (i = 0; i < fw->size && fw->data[i] !=
1120 STIR421X_PATCH_END_OF_HDR_TAG; i++) ;
1121 /* here we check for the out of buffer case */
1122 if (i < STIR421X_PATCH_CODE_OFFSET && i < fw->size &&
1123 STIR421X_PATCH_END_OF_HDR_TAG == fw->data[i]) {
1124 if (!memcmp(fw->data + i + 1, STIR421X_PATCH_STMP_TAG,
1125 sizeof(STIR421X_PATCH_STMP_TAG) - 1)) {
1127 /* We can upload the patch to the target */
1128 i += sizeof(STIR421X_PATCH_STMP_TAG);
1129 ret = stir421x_fw_upload(self, &fw->data[i],
1130 fw->size - i);
1135 release_firmware(fw);
1137 return ret;
1141 /********************** IRDA DEVICE CALLBACKS **********************/
1143 * Main calls from the IrDA/Network subsystem.
1144 * Mostly registering a new irda-usb device and removing it....
1145 * We only deal with the IrDA side of the business, the USB side will
1146 * be dealt with below...
1150 /*------------------------------------------------------------------*/
1152 * Function irda_usb_net_open (dev)
1154 * Network device is taken up. Usually this is done by "ifconfig irda0 up"
1156 * Note : don't mess with self->netopen - Jean II
1158 static int irda_usb_net_open(struct net_device *netdev)
1160 struct irda_usb_cb *self;
1161 unsigned long flags;
1162 char hwname[16];
1163 int i;
1165 IRDA_ASSERT(netdev != NULL, return -1;);
1166 self = netdev_priv(netdev);
1167 IRDA_ASSERT(self != NULL, return -1;);
1169 spin_lock_irqsave(&self->lock, flags);
1170 /* Can only open the device if it's there */
1171 if(!self->present) {
1172 spin_unlock_irqrestore(&self->lock, flags);
1173 net_warn_ratelimited("%s(), device not present!\n", __func__);
1174 return -1;
1177 if(self->needspatch) {
1178 spin_unlock_irqrestore(&self->lock, flags);
1179 net_warn_ratelimited("%s(), device needs patch\n", __func__);
1180 return -EIO ;
1183 /* Initialise default speed and xbofs value
1184 * (IrLAP will change that soon) */
1185 self->speed = -1;
1186 self->xbofs = -1;
1187 self->new_speed = -1;
1188 self->new_xbofs = -1;
1190 /* To do *before* submitting Rx urbs and starting net Tx queue
1191 * Jean II */
1192 self->netopen = 1;
1193 spin_unlock_irqrestore(&self->lock, flags);
1196 * Now that everything should be initialized properly,
1197 * Open new IrLAP layer instance to take care of us...
1198 * Note : will send immediately a speed change...
1200 sprintf(hwname, "usb#%d", self->usbdev->devnum);
1201 self->irlap = irlap_open(netdev, &self->qos, hwname);
1202 IRDA_ASSERT(self->irlap != NULL, return -1;);
1204 /* Allow IrLAP to send data to us */
1205 netif_start_queue(netdev);
1207 /* We submit all the Rx URB except for one that we keep idle.
1208 * Need to be initialised before submitting other USBs, because
1209 * in some cases as soon as we submit the URBs the USB layer
1210 * will trigger a dummy receive - Jean II */
1211 self->idle_rx_urb = self->rx_urb[IU_MAX_ACTIVE_RX_URBS];
1212 self->idle_rx_urb->context = NULL;
1214 /* Now that we can pass data to IrLAP, allow the USB layer
1215 * to send us some data... */
1216 for (i = 0; i < IU_MAX_ACTIVE_RX_URBS; i++) {
1217 struct sk_buff *skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
1218 if (!skb) {
1219 /* If this ever happen, we are in deep s***.
1220 * Basically, we can't start the Rx path... */
1221 return -1;
1223 //skb_reserve(newskb, USB_IRDA_HEADER - 1);
1224 irda_usb_submit(self, skb, self->rx_urb[i]);
1227 /* Ready to play !!! */
1228 return 0;
1231 /*------------------------------------------------------------------*/
1233 * Function irda_usb_net_close (self)
1235 * Network device is taken down. Usually this is done by
1236 * "ifconfig irda0 down"
1238 static int irda_usb_net_close(struct net_device *netdev)
1240 struct irda_usb_cb *self;
1241 int i;
1243 IRDA_ASSERT(netdev != NULL, return -1;);
1244 self = netdev_priv(netdev);
1245 IRDA_ASSERT(self != NULL, return -1;);
1247 /* Clear this flag *before* unlinking the urbs and *before*
1248 * stopping the network Tx queue - Jean II */
1249 self->netopen = 0;
1251 /* Stop network Tx queue */
1252 netif_stop_queue(netdev);
1254 /* Kill defered Rx URB */
1255 del_timer(&self->rx_defer_timer);
1257 /* Deallocate all the Rx path buffers (URBs and skb) */
1258 for (i = 0; i < self->max_rx_urb; i++) {
1259 struct urb *urb = self->rx_urb[i];
1260 struct sk_buff *skb = (struct sk_buff *) urb->context;
1261 /* Cancel the receive command */
1262 usb_kill_urb(urb);
1263 /* The skb is ours, free it */
1264 if(skb) {
1265 dev_kfree_skb(skb);
1266 urb->context = NULL;
1269 /* Cancel Tx and speed URB - need to be synchronous to avoid races */
1270 usb_kill_urb(self->tx_urb);
1271 usb_kill_urb(self->speed_urb);
1273 /* Stop and remove instance of IrLAP */
1274 if (self->irlap)
1275 irlap_close(self->irlap);
1276 self->irlap = NULL;
1278 return 0;
1281 /*------------------------------------------------------------------*/
1283 * IOCTLs : Extra out-of-band network commands...
1285 static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1287 unsigned long flags;
1288 struct if_irda_req *irq = (struct if_irda_req *) rq;
1289 struct irda_usb_cb *self;
1290 int ret = 0;
1292 IRDA_ASSERT(dev != NULL, return -1;);
1293 self = netdev_priv(dev);
1294 IRDA_ASSERT(self != NULL, return -1;);
1296 pr_debug("%s(), %s, (cmd=0x%X)\n", __func__, dev->name, cmd);
1298 switch (cmd) {
1299 case SIOCSBANDWIDTH: /* Set bandwidth */
1300 if (!capable(CAP_NET_ADMIN))
1301 return -EPERM;
1302 /* Protect us from USB callbacks, net watchdog and else. */
1303 spin_lock_irqsave(&self->lock, flags);
1304 /* Check if the device is still there */
1305 if(self->present) {
1306 /* Set the desired speed */
1307 self->new_speed = irq->ifr_baudrate;
1308 irda_usb_change_speed_xbofs(self);
1310 spin_unlock_irqrestore(&self->lock, flags);
1311 break;
1312 case SIOCSMEDIABUSY: /* Set media busy */
1313 if (!capable(CAP_NET_ADMIN))
1314 return -EPERM;
1315 /* Check if the IrDA stack is still there */
1316 if(self->netopen)
1317 irda_device_set_media_busy(self->netdev, TRUE);
1318 break;
1319 case SIOCGRECEIVING: /* Check if we are receiving right now */
1320 irq->ifr_receiving = irda_usb_is_receiving(self);
1321 break;
1322 default:
1323 ret = -EOPNOTSUPP;
1326 return ret;
1329 /*------------------------------------------------------------------*/
1331 /********************* IRDA CONFIG SUBROUTINES *********************/
1333 * Various subroutines dealing with IrDA and network stuff we use to
1334 * configure and initialise each irda-usb instance.
1335 * These functions are used below in the main calls of the driver...
1338 /*------------------------------------------------------------------*/
1340 * Set proper values in the IrDA QOS structure
1342 static inline void irda_usb_init_qos(struct irda_usb_cb *self)
1344 struct irda_class_desc *desc;
1347 desc = self->irda_desc;
1349 /* Initialize QoS for this device */
1350 irda_init_max_qos_capabilies(&self->qos);
1352 /* See spec section 7.2 for meaning.
1353 * Values are little endian (as most USB stuff), the IrDA stack
1354 * use it in native order (see parameters.c). - Jean II */
1355 self->qos.baud_rate.bits = le16_to_cpu(desc->wBaudRate);
1356 self->qos.min_turn_time.bits = desc->bmMinTurnaroundTime;
1357 self->qos.additional_bofs.bits = desc->bmAdditionalBOFs;
1358 self->qos.window_size.bits = desc->bmWindowSize;
1359 self->qos.data_size.bits = desc->bmDataSize;
1361 pr_debug("%s(), dongle says speed=0x%X, size=0x%X, window=0x%X, bofs=0x%X, turn=0x%X\n",
1362 __func__, self->qos.baud_rate.bits, self->qos.data_size.bits,
1363 self->qos.window_size.bits, self->qos.additional_bofs.bits,
1364 self->qos.min_turn_time.bits);
1366 /* Don't always trust what the dongle tell us */
1367 if(self->capability & IUC_SIR_ONLY)
1368 self->qos.baud_rate.bits &= 0x00ff;
1369 if(self->capability & IUC_SMALL_PKT)
1370 self->qos.data_size.bits = 0x07;
1371 if(self->capability & IUC_NO_WINDOW)
1372 self->qos.window_size.bits = 0x01;
1373 if(self->capability & IUC_MAX_WINDOW)
1374 self->qos.window_size.bits = 0x7f;
1375 if(self->capability & IUC_MAX_XBOFS)
1376 self->qos.additional_bofs.bits = 0x01;
1378 #if 1
1379 /* Module parameter can override the rx window size */
1380 if (qos_mtt_bits)
1381 self->qos.min_turn_time.bits = qos_mtt_bits;
1382 #endif
1384 * Note : most of those values apply only for the receive path,
1385 * the transmit path will be set differently - Jean II
1387 irda_qos_bits_to_value(&self->qos);
1390 /*------------------------------------------------------------------*/
1391 static const struct net_device_ops irda_usb_netdev_ops = {
1392 .ndo_open = irda_usb_net_open,
1393 .ndo_stop = irda_usb_net_close,
1394 .ndo_do_ioctl = irda_usb_net_ioctl,
1395 .ndo_start_xmit = irda_usb_hard_xmit,
1396 .ndo_tx_timeout = irda_usb_net_timeout,
1400 * Initialise the network side of the irda-usb instance
1401 * Called when a new USB instance is registered in irda_usb_probe()
1403 static inline int irda_usb_open(struct irda_usb_cb *self)
1405 struct net_device *netdev = self->netdev;
1407 netdev->netdev_ops = &irda_usb_netdev_ops;
1409 irda_usb_init_qos(self);
1411 return register_netdev(netdev);
1414 /*------------------------------------------------------------------*/
1416 * Cleanup the network side of the irda-usb instance
1417 * Called when a USB instance is removed in irda_usb_disconnect()
1419 static inline void irda_usb_close(struct irda_usb_cb *self)
1421 /* Remove netdevice */
1422 unregister_netdev(self->netdev);
1424 /* Remove the speed buffer */
1425 kfree(self->speed_buff);
1426 self->speed_buff = NULL;
1428 kfree(self->tx_buff);
1429 self->tx_buff = NULL;
1432 /********************** USB CONFIG SUBROUTINES **********************/
1434 * Various subroutines dealing with USB stuff we use to configure and
1435 * initialise each irda-usb instance.
1436 * These functions are used below in the main calls of the driver...
1439 /*------------------------------------------------------------------*/
1441 * Function irda_usb_parse_endpoints(dev, ifnum)
1443 * Parse the various endpoints and find the one we need.
1445 * The endpoint are the pipes used to communicate with the USB device.
1446 * The spec defines 2 endpoints of type bulk transfer, one in, and one out.
1447 * These are used to pass frames back and forth with the dongle.
1448 * Most dongle have also an interrupt endpoint, that will be probably
1449 * documented in the next spec...
1451 static inline int irda_usb_parse_endpoints(struct irda_usb_cb *self, struct usb_host_endpoint *endpoint, int ennum)
1453 int i; /* Endpoint index in table */
1455 /* Init : no endpoints */
1456 self->bulk_in_ep = 0;
1457 self->bulk_out_ep = 0;
1458 self->bulk_int_ep = 0;
1460 /* Let's look at all those endpoints */
1461 for(i = 0; i < ennum; i++) {
1462 /* All those variables will get optimised by the compiler,
1463 * so let's aim for clarity... - Jean II */
1464 __u8 ep; /* Endpoint address */
1465 __u8 dir; /* Endpoint direction */
1466 __u8 attr; /* Endpoint attribute */
1467 __u16 psize; /* Endpoint max packet size in bytes */
1469 /* Get endpoint address, direction and attribute */
1470 ep = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1471 dir = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK;
1472 attr = endpoint[i].desc.bmAttributes;
1473 psize = le16_to_cpu(endpoint[i].desc.wMaxPacketSize);
1475 /* Is it a bulk endpoint ??? */
1476 if(attr == USB_ENDPOINT_XFER_BULK) {
1477 /* We need to find an IN and an OUT */
1478 if(dir == USB_DIR_IN) {
1479 /* This is our Rx endpoint */
1480 self->bulk_in_ep = ep;
1481 } else {
1482 /* This is our Tx endpoint */
1483 self->bulk_out_ep = ep;
1484 self->bulk_out_mtu = psize;
1486 } else {
1487 if((attr == USB_ENDPOINT_XFER_INT) &&
1488 (dir == USB_DIR_IN)) {
1489 /* This is our interrupt endpoint */
1490 self->bulk_int_ep = ep;
1491 } else {
1492 net_err_ratelimited("%s(), Unrecognised endpoint %02X\n",
1493 __func__, ep);
1498 pr_debug("%s(), And our endpoints are : in=%02X, out=%02X (%d), int=%02X\n",
1499 __func__, self->bulk_in_ep, self->bulk_out_ep,
1500 self->bulk_out_mtu, self->bulk_int_ep);
1502 return (self->bulk_in_ep != 0) && (self->bulk_out_ep != 0);
1505 #ifdef IU_DUMP_CLASS_DESC
1506 /*------------------------------------------------------------------*/
1508 * Function usb_irda_dump_class_desc(desc)
1510 * Prints out the contents of the IrDA class descriptor
1513 static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
1515 /* Values are little endian */
1516 printk("bLength=%x\n", desc->bLength);
1517 printk("bDescriptorType=%x\n", desc->bDescriptorType);
1518 printk("bcdSpecRevision=%x\n", le16_to_cpu(desc->bcdSpecRevision));
1519 printk("bmDataSize=%x\n", desc->bmDataSize);
1520 printk("bmWindowSize=%x\n", desc->bmWindowSize);
1521 printk("bmMinTurnaroundTime=%d\n", desc->bmMinTurnaroundTime);
1522 printk("wBaudRate=%x\n", le16_to_cpu(desc->wBaudRate));
1523 printk("bmAdditionalBOFs=%x\n", desc->bmAdditionalBOFs);
1524 printk("bIrdaRateSniff=%x\n", desc->bIrdaRateSniff);
1525 printk("bMaxUnicastList=%x\n", desc->bMaxUnicastList);
1527 #endif /* IU_DUMP_CLASS_DESC */
1529 /*------------------------------------------------------------------*/
1531 * Function irda_usb_find_class_desc(intf)
1533 * Returns instance of IrDA class descriptor, or NULL if not found
1535 * The class descriptor is some extra info that IrDA USB devices will
1536 * offer to us, describing their IrDA characteristics. We will use that in
1537 * irda_usb_init_qos()
1539 static inline struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf)
1541 struct usb_device *dev = interface_to_usbdev (intf);
1542 struct irda_class_desc *desc;
1543 int ret;
1545 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
1546 if (!desc)
1547 return NULL;
1549 /* USB-IrDA class spec 1.0:
1550 * 6.1.3: Standard "Get Descriptor" Device Request is not
1551 * appropriate to retrieve class-specific descriptor
1552 * 6.2.5: Class Specific "Get Class Descriptor" Interface Request
1553 * is mandatory and returns the USB-IrDA class descriptor
1556 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
1557 IU_REQ_GET_CLASS_DESC,
1558 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1559 0, intf->altsetting->desc.bInterfaceNumber, desc,
1560 sizeof(*desc), 500);
1562 pr_debug("%s(), ret=%d\n", __func__, ret);
1563 if (ret < sizeof(*desc)) {
1564 net_warn_ratelimited("usb-irda: class_descriptor read %s (%d)\n",
1565 ret < 0 ? "failed" : "too short", ret);
1567 else if (desc->bDescriptorType != USB_DT_IRDA) {
1568 net_warn_ratelimited("usb-irda: bad class_descriptor type\n");
1570 else {
1571 #ifdef IU_DUMP_CLASS_DESC
1572 irda_usb_dump_class_desc(desc);
1573 #endif /* IU_DUMP_CLASS_DESC */
1575 return desc;
1577 kfree(desc);
1578 return NULL;
1581 /*********************** USB DEVICE CALLBACKS ***********************/
1583 * Main calls from the USB subsystem.
1584 * Mostly registering a new irda-usb device and removing it....
1587 /*------------------------------------------------------------------*/
1589 * This routine is called by the USB subsystem for each new device
1590 * in the system. We need to check if the device is ours, and in
1591 * this case start handling it.
1592 * The USB layer protect us from reentrancy (via BKL), so we don't need
1593 * to spinlock in there... Jean II
1595 static int irda_usb_probe(struct usb_interface *intf,
1596 const struct usb_device_id *id)
1598 struct net_device *net;
1599 struct usb_device *dev = interface_to_usbdev(intf);
1600 struct irda_usb_cb *self;
1601 struct usb_host_interface *interface;
1602 struct irda_class_desc *irda_desc;
1603 int ret = -ENOMEM;
1604 int i; /* Driver instance index / Rx URB index */
1606 /* Note : the probe make sure to call us only for devices that
1607 * matches the list of dongle (top of the file). So, we
1608 * don't need to check if the dongle is really ours.
1609 * Jean II */
1611 net_info_ratelimited("IRDA-USB found at address %d, Vendor: %x, Product: %x\n",
1612 dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
1613 le16_to_cpu(dev->descriptor.idProduct));
1615 net = alloc_irdadev(sizeof(*self));
1616 if (!net)
1617 goto err_out;
1619 SET_NETDEV_DEV(net, &intf->dev);
1620 self = netdev_priv(net);
1621 self->netdev = net;
1622 spin_lock_init(&self->lock);
1623 init_timer(&self->rx_defer_timer);
1625 self->capability = id->driver_info;
1626 self->needspatch = ((self->capability & IUC_STIR421X) != 0);
1628 /* Create all of the needed urbs */
1629 if (self->capability & IUC_STIR421X) {
1630 self->max_rx_urb = IU_SIGMATEL_MAX_RX_URBS;
1631 self->header_length = USB_IRDA_STIR421X_HEADER;
1632 } else {
1633 self->max_rx_urb = IU_MAX_RX_URBS;
1634 self->header_length = USB_IRDA_HEADER;
1637 self->rx_urb = kcalloc(self->max_rx_urb, sizeof(struct urb *),
1638 GFP_KERNEL);
1639 if (!self->rx_urb)
1640 goto err_free_net;
1642 for (i = 0; i < self->max_rx_urb; i++) {
1643 self->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
1644 if (!self->rx_urb[i]) {
1645 goto err_out_1;
1648 self->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1649 if (!self->tx_urb) {
1650 goto err_out_1;
1652 self->speed_urb = usb_alloc_urb(0, GFP_KERNEL);
1653 if (!self->speed_urb) {
1654 goto err_out_2;
1657 /* Is this really necessary? (no, except maybe for broken devices) */
1658 if (usb_reset_configuration (dev) < 0) {
1659 dev_err(&intf->dev, "reset_configuration failed\n");
1660 ret = -EIO;
1661 goto err_out_3;
1664 /* Is this really necessary? */
1665 /* Note : some driver do hardcode the interface number, some others
1666 * specify an alternate, but very few driver do like this.
1667 * Jean II */
1668 ret = usb_set_interface(dev, intf->altsetting->desc.bInterfaceNumber, 0);
1669 pr_debug("usb-irda: set interface %d result %d\n",
1670 intf->altsetting->desc.bInterfaceNumber, ret);
1671 switch (ret) {
1672 case 0:
1673 break;
1674 case -EPIPE: /* -EPIPE = -32 */
1675 /* Martin Diehl says if we get a -EPIPE we should
1676 * be fine and we don't need to do a usb_clear_halt().
1677 * - Jean II */
1678 pr_debug("%s(), Received -EPIPE, ignoring...\n",
1679 __func__);
1680 break;
1681 default:
1682 pr_debug("%s(), Unknown error %d\n", __func__, ret);
1683 ret = -EIO;
1684 goto err_out_3;
1687 /* Find our endpoints */
1688 interface = intf->cur_altsetting;
1689 if(!irda_usb_parse_endpoints(self, interface->endpoint,
1690 interface->desc.bNumEndpoints)) {
1691 net_err_ratelimited("%s(), Bogus endpoints...\n", __func__);
1692 ret = -EIO;
1693 goto err_out_3;
1696 self->usbdev = dev;
1698 /* Find IrDA class descriptor */
1699 irda_desc = irda_usb_find_class_desc(intf);
1700 ret = -ENODEV;
1701 if (!irda_desc)
1702 goto err_out_3;
1704 if (self->needspatch) {
1705 ret = usb_control_msg (self->usbdev, usb_sndctrlpipe (self->usbdev, 0),
1706 0x02, 0x40, 0, 0, NULL, 0, 500);
1707 if (ret < 0) {
1708 pr_debug("usb_control_msg failed %d\n", ret);
1709 goto err_out_3;
1710 } else {
1711 mdelay(10);
1715 self->irda_desc = irda_desc;
1716 self->present = 1;
1717 self->netopen = 0;
1718 self->usbintf = intf;
1720 /* Allocate the buffer for speed changes */
1721 /* Don't change this buffer size and allocation without doing
1722 * some heavy and complete testing. Don't ask why :-(
1723 * Jean II */
1724 self->speed_buff = kzalloc(IRDA_USB_SPEED_MTU, GFP_KERNEL);
1725 if (!self->speed_buff)
1726 goto err_out_3;
1728 self->tx_buff = kzalloc(IRDA_SKB_MAX_MTU + self->header_length,
1729 GFP_KERNEL);
1730 if (!self->tx_buff)
1731 goto err_out_4;
1733 ret = irda_usb_open(self);
1734 if (ret)
1735 goto err_out_5;
1737 net_info_ratelimited("IrDA: Registered device %s\n", net->name);
1738 usb_set_intfdata(intf, self);
1740 if (self->needspatch) {
1741 /* Now we fetch and upload the firmware patch */
1742 ret = stir421x_patch_device(self);
1743 self->needspatch = (ret < 0);
1744 if (self->needspatch) {
1745 net_err_ratelimited("STIR421X: Couldn't upload patch\n");
1746 goto err_out_6;
1749 /* replace IrDA class descriptor with what patched device is now reporting */
1750 irda_desc = irda_usb_find_class_desc (self->usbintf);
1751 if (!irda_desc) {
1752 ret = -ENODEV;
1753 goto err_out_6;
1755 kfree(self->irda_desc);
1756 self->irda_desc = irda_desc;
1757 irda_usb_init_qos(self);
1760 return 0;
1761 err_out_6:
1762 unregister_netdev(self->netdev);
1763 err_out_5:
1764 kfree(self->tx_buff);
1765 err_out_4:
1766 kfree(self->speed_buff);
1767 err_out_3:
1768 /* Free all urbs that we may have created */
1769 usb_free_urb(self->speed_urb);
1770 err_out_2:
1771 usb_free_urb(self->tx_urb);
1772 err_out_1:
1773 for (i = 0; i < self->max_rx_urb; i++)
1774 usb_free_urb(self->rx_urb[i]);
1775 kfree(self->rx_urb);
1776 err_free_net:
1777 free_netdev(net);
1778 err_out:
1779 return ret;
1782 /*------------------------------------------------------------------*/
1784 * The current irda-usb device is removed, the USB layer tell us
1785 * to shut it down...
1786 * One of the constraints is that when we exit this function,
1787 * we cannot use the usb_device no more. Gone. Destroyed. kfree().
1788 * Most other subsystem allow you to destroy the instance at a time
1789 * when it's convenient to you, to postpone it to a later date, but
1790 * not the USB subsystem.
1791 * So, we must make bloody sure that everything gets deactivated.
1792 * Jean II
1794 static void irda_usb_disconnect(struct usb_interface *intf)
1796 unsigned long flags;
1797 struct irda_usb_cb *self = usb_get_intfdata(intf);
1798 int i;
1800 usb_set_intfdata(intf, NULL);
1801 if (!self)
1802 return;
1804 /* Make sure that the Tx path is not executing. - Jean II */
1805 spin_lock_irqsave(&self->lock, flags);
1807 /* Oups ! We are not there any more.
1808 * This will stop/desactivate the Tx path. - Jean II */
1809 self->present = 0;
1811 /* Kill defered Rx URB */
1812 del_timer(&self->rx_defer_timer);
1814 /* We need to have irq enabled to unlink the URBs. That's OK,
1815 * at this point the Tx path is gone - Jean II */
1816 spin_unlock_irqrestore(&self->lock, flags);
1818 /* Hum... Check if networking is still active (avoid races) */
1819 if((self->netopen) || (self->irlap)) {
1820 /* Accept no more transmissions */
1821 /*netif_device_detach(self->netdev);*/
1822 netif_stop_queue(self->netdev);
1823 /* Stop all the receive URBs. Must be synchronous. */
1824 for (i = 0; i < self->max_rx_urb; i++)
1825 usb_kill_urb(self->rx_urb[i]);
1826 /* Cancel Tx and speed URB.
1827 * Make sure it's synchronous to avoid races. */
1828 usb_kill_urb(self->tx_urb);
1829 usb_kill_urb(self->speed_urb);
1832 /* Cleanup the device stuff */
1833 irda_usb_close(self);
1834 /* No longer attached to USB bus */
1835 self->usbdev = NULL;
1836 self->usbintf = NULL;
1838 /* Clean up our urbs */
1839 for (i = 0; i < self->max_rx_urb; i++)
1840 usb_free_urb(self->rx_urb[i]);
1841 kfree(self->rx_urb);
1842 /* Clean up Tx and speed URB */
1843 usb_free_urb(self->tx_urb);
1844 usb_free_urb(self->speed_urb);
1846 /* Free self and network device */
1847 free_netdev(self->netdev);
1848 pr_debug("%s(), USB IrDA Disconnected\n", __func__);
1851 #ifdef CONFIG_PM
1852 /* USB suspend, so power off the transmitter/receiver */
1853 static int irda_usb_suspend(struct usb_interface *intf, pm_message_t message)
1855 struct irda_usb_cb *self = usb_get_intfdata(intf);
1856 int i;
1858 netif_device_detach(self->netdev);
1860 if (self->tx_urb != NULL)
1861 usb_kill_urb(self->tx_urb);
1862 if (self->speed_urb != NULL)
1863 usb_kill_urb(self->speed_urb);
1864 for (i = 0; i < self->max_rx_urb; i++) {
1865 if (self->rx_urb[i] != NULL)
1866 usb_kill_urb(self->rx_urb[i]);
1868 return 0;
1871 /* Coming out of suspend, so reset hardware */
1872 static int irda_usb_resume(struct usb_interface *intf)
1874 struct irda_usb_cb *self = usb_get_intfdata(intf);
1875 int i;
1877 for (i = 0; i < self->max_rx_urb; i++) {
1878 if (self->rx_urb[i] != NULL)
1879 usb_submit_urb(self->rx_urb[i], GFP_KERNEL);
1882 netif_device_attach(self->netdev);
1883 return 0;
1885 #endif
1887 /*------------------------------------------------------------------*/
1889 * USB device callbacks
1891 static struct usb_driver irda_driver = {
1892 .name = "irda-usb",
1893 .probe = irda_usb_probe,
1894 .disconnect = irda_usb_disconnect,
1895 .id_table = dongles,
1896 #ifdef CONFIG_PM
1897 .suspend = irda_usb_suspend,
1898 .resume = irda_usb_resume,
1899 #endif
1902 module_usb_driver(irda_driver);
1905 * Module parameters
1907 module_param(qos_mtt_bits, int, 0);
1908 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
1909 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>");
1910 MODULE_DESCRIPTION("IrDA-USB Dongle Driver");
1911 MODULE_LICENSE("GPL");