initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / net / irda / irda-usb.c
blob62f9853893ba7bbf62aa759e3252f768a1959b8a
1 /*****************************************************************************
3 * Filename: irda-usb.c
4 * Version: 0.9b
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>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 *****************************************************************************/
30 * IMPORTANT NOTE
31 * --------------
33 * As of kernel 2.5.20, this is the state of compliance and testing of
34 * this driver (irda-usb) with regards to the USB low level drivers...
36 * This driver has been tested SUCCESSFULLY with the following drivers :
37 * o usb-uhci-hcd (For Intel/Via USB controllers)
38 * o uhci-hcd (Alternate/JE driver for Intel/Via USB controllers)
39 * o ohci-hcd (For other USB controllers)
41 * This driver has NOT been tested with the following drivers :
42 * o ehci-hcd (USB 2.0 controllers)
44 * Note that all HCD drivers do URB_ZERO_PACKET and timeout properly,
45 * so we don't have to worry about that anymore.
46 * One common problem is the failure to set the address on the dongle,
47 * but this happens before the driver gets loaded...
49 * Jean II
52 /*------------------------------------------------------------------*/
54 #include <linux/module.h>
56 #include <linux/kernel.h>
57 #include <linux/types.h>
58 #include <linux/init.h>
59 #include <linux/skbuff.h>
60 #include <linux/netdevice.h>
61 #include <linux/slab.h>
62 #include <linux/rtnetlink.h>
63 #include <linux/usb.h>
65 #include "irda-usb.h"
67 /*------------------------------------------------------------------*/
69 static int qos_mtt_bits = 0;
71 /* These are the currently known IrDA USB dongles. Add new dongles here */
72 static struct usb_device_id dongles[] = {
73 /* ACTiSYS Corp., ACT-IR2000U FIR-USB Adapter */
74 { USB_DEVICE(0x9c4, 0x011), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
75 /* Look like ACTiSYS, Report : IBM Corp., IBM UltraPort IrDA */
76 { USB_DEVICE(0x4428, 0x012), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
77 /* KC Technology Inc., KC-180 USB IrDA Device */
78 { USB_DEVICE(0x50f, 0x180), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
79 /* Extended Systems, Inc., XTNDAccess IrDA USB (ESI-9685) */
80 { USB_DEVICE(0x8e9, 0x100), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
81 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
82 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
83 .bInterfaceClass = USB_CLASS_APP_SPEC,
84 .bInterfaceSubClass = USB_CLASS_IRDA,
85 .driver_info = IUC_DEFAULT, },
86 { }, /* The end */
90 * Important note :
91 * Devices based on the SigmaTel chipset (0x66f, 0x4200) are not compliant
92 * with the USB-IrDA specification (and actually very very different), and
93 * there is no way this driver can support those devices, apart from
94 * a complete rewrite...
95 * Jean II
98 MODULE_DEVICE_TABLE(usb, dongles);
100 /*------------------------------------------------------------------*/
102 static struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf);
103 static void irda_usb_disconnect(struct usb_interface *intf);
104 static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self);
105 static int irda_usb_hard_xmit(struct sk_buff *skb, struct net_device *dev);
106 static int irda_usb_open(struct irda_usb_cb *self);
107 static void irda_usb_close(struct irda_usb_cb *self);
108 static void speed_bulk_callback(struct urb *urb, struct pt_regs *regs);
109 static void write_bulk_callback(struct urb *urb, struct pt_regs *regs);
110 static void irda_usb_receive(struct urb *urb, struct pt_regs *regs);
111 static int irda_usb_net_open(struct net_device *dev);
112 static int irda_usb_net_close(struct net_device *dev);
113 static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
114 static void irda_usb_net_timeout(struct net_device *dev);
115 static struct net_device_stats *irda_usb_net_get_stats(struct net_device *dev);
117 /************************ TRANSMIT ROUTINES ************************/
119 * Receive packets from the IrDA stack and send them on the USB pipe.
120 * Handle speed change, timeout and lot's of ugliness...
123 /*------------------------------------------------------------------*/
125 * Function irda_usb_build_header(self, skb, header)
127 * Builds USB-IrDA outbound header
129 * When we send an IrDA frame over an USB pipe, we add to it a 1 byte
130 * header. This function create this header with the proper values.
132 * Important note : the USB-IrDA spec 1.0 say very clearly in chapter 5.4.2.2
133 * that the setting of the link speed and xbof number in this outbound header
134 * should be applied *AFTER* the frame has been sent.
135 * Unfortunately, some devices are not compliant with that... It seems that
136 * reading the spec is far too difficult...
137 * Jean II
139 static void irda_usb_build_header(struct irda_usb_cb *self,
140 __u8 *header,
141 int force)
143 /* Set the negotiated link speed */
144 if (self->new_speed != -1) {
145 /* Hum... Ugly hack :-(
146 * Some device are not compliant with the spec and change
147 * parameters *before* sending the frame. - Jean II
149 if ((self->capability & IUC_SPEED_BUG) &&
150 (!force) && (self->speed != -1)) {
151 /* No speed and xbofs change here
152 * (we'll do it later in the write callback) */
153 IRDA_DEBUG(2, "%s(), not changing speed yet\n", __FUNCTION__);
154 *header = 0;
155 return;
158 IRDA_DEBUG(2, "%s(), changing speed to %d\n", __FUNCTION__, self->new_speed);
159 self->speed = self->new_speed;
160 /* We will do ` self->new_speed = -1; ' in the completion
161 * handler just in case the current URB fail - Jean II */
163 switch (self->speed) {
164 case 2400:
165 *header = SPEED_2400;
166 break;
167 default:
168 case 9600:
169 *header = SPEED_9600;
170 break;
171 case 19200:
172 *header = SPEED_19200;
173 break;
174 case 38400:
175 *header = SPEED_38400;
176 break;
177 case 57600:
178 *header = SPEED_57600;
179 break;
180 case 115200:
181 *header = SPEED_115200;
182 break;
183 case 576000:
184 *header = SPEED_576000;
185 break;
186 case 1152000:
187 *header = SPEED_1152000;
188 break;
189 case 4000000:
190 *header = SPEED_4000000;
191 self->new_xbofs = 0;
192 break;
194 } else
195 /* No change */
196 *header = 0;
198 /* Set the negotiated additional XBOFS */
199 if (self->new_xbofs != -1) {
200 IRDA_DEBUG(2, "%s(), changing xbofs to %d\n", __FUNCTION__, self->new_xbofs);
201 self->xbofs = self->new_xbofs;
202 /* We will do ` self->new_xbofs = -1; ' in the completion
203 * handler just in case the current URB fail - Jean II */
205 switch (self->xbofs) {
206 case 48:
207 *header |= 0x10;
208 break;
209 case 28:
210 case 24: /* USB spec 1.0 says 24 */
211 *header |= 0x20;
212 break;
213 default:
214 case 12:
215 *header |= 0x30;
216 break;
217 case 5: /* Bug in IrLAP spec? (should be 6) */
218 case 6:
219 *header |= 0x40;
220 break;
221 case 3:
222 *header |= 0x50;
223 break;
224 case 2:
225 *header |= 0x60;
226 break;
227 case 1:
228 *header |= 0x70;
229 break;
230 case 0:
231 *header |= 0x80;
232 break;
237 /*------------------------------------------------------------------*/
239 * Send a command to change the speed of the dongle
240 * Need to be called with spinlock on.
242 static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self)
244 __u8 *frame;
245 struct urb *urb;
246 int ret;
248 IRDA_DEBUG(2, "%s(), speed=%d, xbofs=%d\n", __FUNCTION__,
249 self->new_speed, self->new_xbofs);
251 /* Grab the speed URB */
252 urb = self->speed_urb;
253 if (urb->status != 0) {
254 WARNING("%s(), URB still in use!\n", __FUNCTION__);
255 return;
258 /* Allocate the fake frame */
259 frame = self->speed_buff;
261 /* Set the new speed and xbofs in this fake frame */
262 irda_usb_build_header(self, frame, 1);
264 /* Submit the 0 length IrDA frame to trigger new speed settings */
265 usb_fill_bulk_urb(urb, self->usbdev,
266 usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
267 frame, IRDA_USB_SPEED_MTU,
268 speed_bulk_callback, self);
269 urb->transfer_buffer_length = USB_IRDA_HEADER;
270 urb->transfer_flags = URB_ASYNC_UNLINK;
272 /* Irq disabled -> GFP_ATOMIC */
273 if ((ret = usb_submit_urb(urb, GFP_ATOMIC))) {
274 WARNING("%s(), failed Speed URB\n", __FUNCTION__);
278 /*------------------------------------------------------------------*/
280 * Speed URB callback
281 * Now, we can only get called for the speed URB.
283 static void speed_bulk_callback(struct urb *urb, struct pt_regs *regs)
285 struct irda_usb_cb *self = urb->context;
287 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
289 /* We should always have a context */
290 ASSERT(self != NULL, return;);
291 /* We should always be called for the speed URB */
292 ASSERT(urb == self->speed_urb, return;);
294 /* Check for timeout and other USB nasties */
295 if (urb->status != 0) {
296 /* I get a lot of -ECONNABORTED = -103 here - Jean II */
297 IRDA_DEBUG(0, "%s(), URB complete status %d, transfer_flags 0x%04X\n", __FUNCTION__, urb->status, urb->transfer_flags);
299 /* Don't do anything here, that might confuse the USB layer.
300 * Instead, we will wait for irda_usb_net_timeout(), the
301 * network layer watchdog, to fix the situation.
302 * Jean II */
303 /* A reset of the dongle might be welcomed here - Jean II */
304 return;
307 /* urb is now available */
308 //urb->status = 0; -> tested above
310 /* New speed and xbof is now commited in hardware */
311 self->new_speed = -1;
312 self->new_xbofs = -1;
314 /* Allow the stack to send more packets */
315 netif_wake_queue(self->netdev);
318 /*------------------------------------------------------------------*/
320 * Send an IrDA frame to the USB dongle (for transmission)
322 static int irda_usb_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
324 struct irda_usb_cb *self = netdev->priv;
325 struct urb *urb = self->tx_urb;
326 unsigned long flags;
327 s32 speed;
328 s16 xbofs;
329 int res, mtt;
330 int err = 1; /* Failed */
332 IRDA_DEBUG(4, "%s() on %s\n", __FUNCTION__, netdev->name);
334 netif_stop_queue(netdev);
336 /* Protect us from USB callbacks, net watchdog and else. */
337 spin_lock_irqsave(&self->lock, flags);
339 /* Check if the device is still there.
340 * We need to check self->present under the spinlock because
341 * of irda_usb_disconnect() is synchronous - Jean II */
342 if (!self->present) {
343 IRDA_DEBUG(0, "%s(), Device is gone...\n", __FUNCTION__);
344 goto drop;
347 /* Check if we need to change the number of xbofs */
348 xbofs = irda_get_next_xbofs(skb);
349 if ((xbofs != self->xbofs) && (xbofs != -1)) {
350 self->new_xbofs = xbofs;
353 /* Check if we need to change the speed */
354 speed = irda_get_next_speed(skb);
355 if ((speed != self->speed) && (speed != -1)) {
356 /* Set the desired speed */
357 self->new_speed = speed;
359 /* Check for empty frame */
360 if (!skb->len) {
361 /* IrLAP send us an empty frame to make us change the
362 * speed. Changing speed with the USB adapter is in
363 * fact sending an empty frame to the adapter, so we
364 * could just let the present function do its job.
365 * However, we would wait for min turn time,
366 * do an extra memcpy and increment packet counters...
367 * Jean II */
368 irda_usb_change_speed_xbofs(self);
369 netdev->trans_start = jiffies;
370 /* Will netif_wake_queue() in callback */
371 err = 0; /* No error */
372 goto drop;
376 if (urb->status != 0) {
377 WARNING("%s(), URB still in use!\n", __FUNCTION__);
378 goto drop;
381 /* Make sure there is room for IrDA-USB header. The actual
382 * allocation will be done lower in skb_push().
383 * Also, we don't use directly skb_cow(), because it require
384 * headroom >= 16, which force unnecessary copies - Jean II */
385 if (skb_headroom(skb) < USB_IRDA_HEADER) {
386 IRDA_DEBUG(0, "%s(), Insuficient skb headroom.\n", __FUNCTION__);
387 if (skb_cow(skb, USB_IRDA_HEADER)) {
388 WARNING("%s(), failed skb_cow() !!!\n", __FUNCTION__);
389 goto drop;
393 /* Change setting for next frame */
394 irda_usb_build_header(self, skb_push(skb, USB_IRDA_HEADER), 0);
396 /* FIXME: Make macro out of this one */
397 ((struct irda_skb_cb *)skb->cb)->context = self;
399 usb_fill_bulk_urb(urb, self->usbdev,
400 usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
401 skb->data, IRDA_SKB_MAX_MTU,
402 write_bulk_callback, skb);
403 urb->transfer_buffer_length = skb->len;
404 /* Note : unlink *must* be Asynchronous because of the code in
405 * irda_usb_net_timeout() -> call in irq - Jean II */
406 urb->transfer_flags = URB_ASYNC_UNLINK;
407 /* This flag (URB_ZERO_PACKET) indicates that what we send is not
408 * a continuous stream of data but separate packets.
409 * In this case, the USB layer will insert an empty USB frame (TD)
410 * after each of our packets that is exact multiple of the frame size.
411 * This is how the dongle will detect the end of packet - Jean II */
412 urb->transfer_flags |= URB_ZERO_PACKET;
414 /* Generate min turn time. FIXME: can we do better than this? */
415 /* Trying to a turnaround time at this level is trying to measure
416 * processor clock cycle with a wrist-watch, approximate at best...
418 * What we know is the last time we received a frame over USB.
419 * Due to latency over USB that depend on the USB load, we don't
420 * know when this frame was received over IrDA (a few ms before ?)
421 * Then, same story for our outgoing frame...
423 * In theory, the USB dongle is supposed to handle the turnaround
424 * by itself (spec 1.0, chater 4, page 6). Who knows ??? That's
425 * why this code is enabled only for dongles that doesn't meet
426 * the spec.
427 * Jean II */
428 if (self->capability & IUC_NO_TURN) {
429 mtt = irda_get_mtt(skb);
430 if (mtt) {
431 int diff;
432 do_gettimeofday(&self->now);
433 diff = self->now.tv_usec - self->stamp.tv_usec;
434 #ifdef IU_USB_MIN_RTT
435 /* Factor in USB delays -> Get rid of udelay() that
436 * would be lost in the noise - Jean II */
437 diff += IU_USB_MIN_RTT;
438 #endif /* IU_USB_MIN_RTT */
439 /* If the usec counter did wraparound, the diff will
440 * go negative (tv_usec is a long), so we need to
441 * correct it by one second. Jean II */
442 if (diff < 0)
443 diff += 1000000;
445 /* Check if the mtt is larger than the time we have
446 * already used by all the protocol processing
448 if (mtt > diff) {
449 mtt -= diff;
450 if (mtt > 1000)
451 mdelay(mtt/1000);
452 else
453 udelay(mtt);
458 /* Ask USB to send the packet - Irq disabled -> GFP_ATOMIC */
459 if ((res = usb_submit_urb(urb, GFP_ATOMIC))) {
460 WARNING("%s(), failed Tx URB\n", __FUNCTION__);
461 self->stats.tx_errors++;
462 /* Let USB recover : We will catch that in the watchdog */
463 /*netif_start_queue(netdev);*/
464 } else {
465 /* Increment packet stats */
466 self->stats.tx_packets++;
467 self->stats.tx_bytes += skb->len;
469 netdev->trans_start = jiffies;
471 spin_unlock_irqrestore(&self->lock, flags);
473 return 0;
475 drop:
476 /* Drop silently the skb and exit */
477 dev_kfree_skb(skb);
478 spin_unlock_irqrestore(&self->lock, flags);
479 return err; /* Usually 1 */
482 /*------------------------------------------------------------------*/
484 * Note : this function will be called only for tx_urb...
486 static void write_bulk_callback(struct urb *urb, struct pt_regs *regs)
488 unsigned long flags;
489 struct sk_buff *skb = urb->context;
490 struct irda_usb_cb *self = ((struct irda_skb_cb *) skb->cb)->context;
492 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
494 /* We should always have a context */
495 ASSERT(self != NULL, return;);
496 /* We should always be called for the speed URB */
497 ASSERT(urb == self->tx_urb, return;);
499 /* Free up the skb */
500 dev_kfree_skb_any(skb);
501 urb->context = NULL;
503 /* Check for timeout and other USB nasties */
504 if (urb->status != 0) {
505 /* I get a lot of -ECONNABORTED = -103 here - Jean II */
506 IRDA_DEBUG(0, "%s(), URB complete status %d, transfer_flags 0x%04X\n", __FUNCTION__, urb->status, urb->transfer_flags);
508 /* Don't do anything here, that might confuse the USB layer,
509 * and we could go in recursion and blow the kernel stack...
510 * Instead, we will wait for irda_usb_net_timeout(), the
511 * network layer watchdog, to fix the situation.
512 * Jean II */
513 /* A reset of the dongle might be welcomed here - Jean II */
514 return;
517 /* urb is now available */
518 //urb->status = 0; -> tested above
520 /* Make sure we read self->present properly */
521 spin_lock_irqsave(&self->lock, flags);
523 /* If the network is closed, stop everything */
524 if ((!self->netopen) || (!self->present)) {
525 IRDA_DEBUG(0, "%s(), Network is gone...\n", __FUNCTION__);
526 spin_unlock_irqrestore(&self->lock, flags);
527 return;
530 /* If changes to speed or xbofs is pending... */
531 if ((self->new_speed != -1) || (self->new_xbofs != -1)) {
532 if ((self->new_speed != self->speed) ||
533 (self->new_xbofs != self->xbofs)) {
534 /* We haven't changed speed yet (because of
535 * IUC_SPEED_BUG), so do it now - Jean II */
536 IRDA_DEBUG(1, "%s(), Changing speed now...\n", __FUNCTION__);
537 irda_usb_change_speed_xbofs(self);
538 } else {
539 /* New speed and xbof is now commited in hardware */
540 self->new_speed = -1;
541 self->new_xbofs = -1;
542 /* Done, waiting for next packet */
543 netif_wake_queue(self->netdev);
545 } else {
546 /* Otherwise, allow the stack to send more packets */
547 netif_wake_queue(self->netdev);
549 spin_unlock_irqrestore(&self->lock, flags);
552 /*------------------------------------------------------------------*/
554 * Watchdog timer from the network layer.
555 * After a predetermined timeout, if we don't give confirmation that
556 * the packet has been sent (i.e. no call to netif_wake_queue()),
557 * the network layer will call this function.
558 * Note that URB that we submit have also a timeout. When the URB timeout
559 * expire, the normal URB callback is called (write_bulk_callback()).
561 static void irda_usb_net_timeout(struct net_device *netdev)
563 unsigned long flags;
564 struct irda_usb_cb *self = netdev->priv;
565 struct urb *urb;
566 int done = 0; /* If we have made any progress */
568 IRDA_DEBUG(0, "%s(), Network layer thinks we timed out!\n", __FUNCTION__);
569 ASSERT(self != NULL, return;);
571 /* Protect us from USB callbacks, net Tx and else. */
572 spin_lock_irqsave(&self->lock, flags);
574 /* self->present *MUST* be read under spinlock */
575 if (!self->present) {
576 WARNING("%s(), device not present!\n", __FUNCTION__);
577 netif_stop_queue(netdev);
578 spin_unlock_irqrestore(&self->lock, flags);
579 return;
582 /* Check speed URB */
583 urb = self->speed_urb;
584 if (urb->status != 0) {
585 IRDA_DEBUG(0, "%s: Speed change timed out, urb->status=%d, urb->transfer_flags=0x%04X\n", netdev->name, urb->status, urb->transfer_flags);
587 switch (urb->status) {
588 case -EINPROGRESS:
589 usb_unlink_urb(urb);
590 /* Note : above will *NOT* call netif_wake_queue()
591 * in completion handler, we will come back here.
592 * Jean II */
593 done = 1;
594 break;
595 case -ECONNABORTED: /* -103 */
596 case -ECONNRESET: /* -104 */
597 case -ETIMEDOUT: /* -110 */
598 case -ENOENT: /* -2 (urb unlinked by us) */
599 default: /* ??? - Play safe */
600 urb->status = 0;
601 netif_wake_queue(self->netdev);
602 done = 1;
603 break;
607 /* Check Tx URB */
608 urb = self->tx_urb;
609 if (urb->status != 0) {
610 struct sk_buff *skb = urb->context;
612 IRDA_DEBUG(0, "%s: Tx timed out, urb->status=%d, urb->transfer_flags=0x%04X\n", netdev->name, urb->status, urb->transfer_flags);
614 /* Increase error count */
615 self->stats.tx_errors++;
617 #ifdef IU_BUG_KICK_TIMEOUT
618 /* Can't be a bad idea to reset the speed ;-) - Jean II */
619 if(self->new_speed == -1)
620 self->new_speed = self->speed;
621 if(self->new_xbofs == -1)
622 self->new_xbofs = self->xbofs;
623 irda_usb_change_speed_xbofs(self);
624 #endif /* IU_BUG_KICK_TIMEOUT */
626 switch (urb->status) {
627 case -EINPROGRESS:
628 usb_unlink_urb(urb);
629 /* Note : above will *NOT* call netif_wake_queue()
630 * in completion handler, because urb->status will
631 * be -ENOENT. We will fix that at the next watchdog,
632 * leaving more time to USB to recover...
633 * Also, we are in interrupt, so we need to have
634 * URB_ASYNC_UNLINK to work properly...
635 * Jean II */
636 done = 1;
637 break;
638 case -ECONNABORTED: /* -103 */
639 case -ECONNRESET: /* -104 */
640 case -ETIMEDOUT: /* -110 */
641 case -ENOENT: /* -2 (urb unlinked by us) */
642 default: /* ??? - Play safe */
643 if(skb != NULL) {
644 dev_kfree_skb_any(skb);
645 urb->context = NULL;
647 urb->status = 0;
648 netif_wake_queue(self->netdev);
649 done = 1;
650 break;
653 spin_unlock_irqrestore(&self->lock, flags);
655 /* Maybe we need a reset */
656 /* Note : Some drivers seem to use a usb_set_interface() when they
657 * need to reset the hardware. Hum...
660 /* if(done == 0) */
663 /************************* RECEIVE ROUTINES *************************/
665 * Receive packets from the USB layer stack and pass them to the IrDA stack.
666 * Try to work around USB failures...
670 * Note :
671 * Some of you may have noticed that most dongle have an interrupt in pipe
672 * that we don't use. Here is the little secret...
673 * When we hang a Rx URB on the bulk in pipe, it generates some USB traffic
674 * in every USB frame. This is unnecessary overhead.
675 * The interrupt in pipe will generate an event every time a packet is
676 * received. Reading an interrupt pipe adds minimal overhead, but has some
677 * latency (~1ms).
678 * If we are connected (speed != 9600), we want to minimise latency, so
679 * we just always hang the Rx URB and ignore the interrupt.
680 * If we are not connected (speed == 9600), there is usually no Rx traffic,
681 * and we want to minimise the USB overhead. In this case we should wait
682 * on the interrupt pipe and hang the Rx URB only when an interrupt is
683 * received.
684 * Jean II
687 /*------------------------------------------------------------------*/
689 * Submit a Rx URB to the USB layer to handle reception of a frame
690 * Mostly called by the completion callback of the previous URB.
692 * Jean II
694 static void irda_usb_submit(struct irda_usb_cb *self, struct sk_buff *skb, struct urb *urb)
696 struct irda_skb_cb *cb;
697 int ret;
699 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
701 /* This should never happen */
702 ASSERT(skb != NULL, return;);
703 ASSERT(urb != NULL, return;);
705 /* Save ourselves in the skb */
706 cb = (struct irda_skb_cb *) skb->cb;
707 cb->context = self;
709 /* Reinitialize URB */
710 usb_fill_bulk_urb(urb, self->usbdev,
711 usb_rcvbulkpipe(self->usbdev, self->bulk_in_ep),
712 skb->data, skb->truesize,
713 irda_usb_receive, skb);
714 /* Note : unlink *must* be synchronous because of the code in
715 * irda_usb_net_close() -> free the skb - Jean II */
716 urb->status = 0;
718 /* Can be called from irda_usb_receive (irq handler) -> GFP_ATOMIC */
719 ret = usb_submit_urb(urb, GFP_ATOMIC);
720 if (ret) {
721 /* If this ever happen, we are in deep s***.
722 * Basically, the Rx path will stop... */
723 WARNING("%s(), Failed to submit Rx URB %d\n", __FUNCTION__, ret);
727 /*------------------------------------------------------------------*/
729 * Function irda_usb_receive(urb)
731 * Called by the USB subsystem when a frame has been received
734 static void irda_usb_receive(struct urb *urb, struct pt_regs *regs)
736 struct sk_buff *skb = (struct sk_buff *) urb->context;
737 struct irda_usb_cb *self;
738 struct irda_skb_cb *cb;
739 struct sk_buff *newskb;
740 struct sk_buff *dataskb;
741 int docopy;
743 IRDA_DEBUG(2, "%s(), len=%d\n", __FUNCTION__, urb->actual_length);
745 /* Find ourselves */
746 cb = (struct irda_skb_cb *) skb->cb;
747 ASSERT(cb != NULL, return;);
748 self = (struct irda_usb_cb *) cb->context;
749 ASSERT(self != NULL, return;);
751 /* If the network is closed or the device gone, stop everything */
752 if ((!self->netopen) || (!self->present)) {
753 IRDA_DEBUG(0, "%s(), Network is gone!\n", __FUNCTION__);
754 /* Don't re-submit the URB : will stall the Rx path */
755 return;
758 /* Check the status */
759 if (urb->status != 0) {
760 switch (urb->status) {
761 case -EILSEQ:
762 self->stats.rx_errors++;
763 self->stats.rx_crc_errors++;
764 break;
765 case -ECONNRESET: /* -104 */
766 IRDA_DEBUG(0, "%s(), Connection Reset (-104), transfer_flags 0x%04X \n", __FUNCTION__, urb->transfer_flags);
767 /* uhci_cleanup_unlink() is going to kill the Rx
768 * URB just after we return. No problem, at this
769 * point the URB will be idle ;-) - Jean II */
770 break;
771 default:
772 IRDA_DEBUG(0, "%s(), RX status %d,transfer_flags 0x%04X \n", __FUNCTION__, urb->status, urb->transfer_flags);
773 break;
775 goto done;
778 /* Check for empty frames */
779 if (urb->actual_length <= USB_IRDA_HEADER) {
780 WARNING("%s(), empty frame!\n", __FUNCTION__);
781 goto done;
785 * Remember the time we received this frame, so we can
786 * reduce the min turn time a bit since we will know
787 * how much time we have used for protocol processing
789 do_gettimeofday(&self->stamp);
791 /* Check if we need to copy the data to a new skb or not.
792 * For most frames, we use ZeroCopy and pass the already
793 * allocated skb up the stack.
794 * If the frame is small, it is more efficient to copy it
795 * to save memory (copy will be fast anyway - that's
796 * called Rx-copy-break). Jean II */
797 docopy = (urb->actual_length < IRDA_RX_COPY_THRESHOLD);
799 /* Allocate a new skb */
800 newskb = dev_alloc_skb(docopy ? urb->actual_length : IRDA_SKB_MAX_MTU);
801 if (!newskb) {
802 self->stats.rx_dropped++;
803 /* We could deliver the current skb, but this would stall
804 * the Rx path. Better drop the packet... Jean II */
805 goto done;
808 /* Make sure IP header get aligned (IrDA header is 5 bytes) */
809 /* But IrDA-USB header is 1 byte. Jean II */
810 //skb_reserve(newskb, USB_IRDA_HEADER - 1);
812 if(docopy) {
813 /* Copy packet, so we can recycle the original */
814 memcpy(newskb->data, skb->data, urb->actual_length);
815 /* Deliver this new skb */
816 dataskb = newskb;
817 /* And hook the old skb to the URB
818 * Note : we don't need to "clean up" the old skb,
819 * as we never touched it. Jean II */
820 } else {
821 /* We are using ZeroCopy. Deliver old skb */
822 dataskb = skb;
823 /* And hook the new skb to the URB */
824 skb = newskb;
827 /* Set proper length on skb & remove USB-IrDA header */
828 skb_put(dataskb, urb->actual_length);
829 skb_pull(dataskb, USB_IRDA_HEADER);
831 /* Ask the networking layer to queue the packet for the IrDA stack */
832 dataskb->dev = self->netdev;
833 dataskb->mac.raw = dataskb->data;
834 dataskb->protocol = htons(ETH_P_IRDA);
835 netif_rx(dataskb);
837 /* Keep stats up to date */
838 self->stats.rx_bytes += dataskb->len;
839 self->stats.rx_packets++;
840 self->netdev->last_rx = jiffies;
842 done:
843 /* Note : at this point, the URB we've just received (urb)
844 * is still referenced by the USB layer. For example, if we
845 * have received a -ECONNRESET, uhci_cleanup_unlink() will
846 * continue to process it (in fact, cleaning it up).
847 * If we were to submit this URB, disaster would ensue.
848 * Therefore, we submit our idle URB, and put this URB in our
849 * idle slot....
850 * Jean II */
851 /* Note : with this scheme, we could submit the idle URB before
852 * processing the Rx URB. Another time... Jean II */
854 /* Submit the idle URB to replace the URB we've just received */
855 irda_usb_submit(self, skb, self->idle_rx_urb);
856 /* Recycle Rx URB : Now, the idle URB is the present one */
857 urb->context = NULL;
858 self->idle_rx_urb = urb;
861 /*------------------------------------------------------------------*/
863 * Callbak from IrDA layer. IrDA wants to know if we have
864 * started receiving anything.
866 static int irda_usb_is_receiving(struct irda_usb_cb *self)
868 /* Note : because of the way UHCI works, it's almost impossible
869 * to get this info. The Controller DMA directly to memory and
870 * signal only when the whole frame is finished. To know if the
871 * first TD of the URB has been filled or not seems hard work...
873 * The other solution would be to use the "receiving" command
874 * on the default decriptor with a usb_control_msg(), but that
875 * would add USB traffic and would return result only in the
876 * next USB frame (~1ms).
878 * I've been told that current dongles send status info on their
879 * interrupt endpoint, and that's what the Windows driver uses
880 * to know this info. Unfortunately, this is not yet in the spec...
882 * Jean II
885 return 0; /* For now */
888 /********************** IRDA DEVICE CALLBACKS **********************/
890 * Main calls from the IrDA/Network subsystem.
891 * Mostly registering a new irda-usb device and removing it....
892 * We only deal with the IrDA side of the business, the USB side will
893 * be dealt with below...
897 /*------------------------------------------------------------------*/
899 * Function irda_usb_net_open (dev)
901 * Network device is taken up. Usually this is done by "ifconfig irda0 up"
903 * Note : don't mess with self->netopen - Jean II
905 static int irda_usb_net_open(struct net_device *netdev)
907 struct irda_usb_cb *self;
908 char hwname[16];
909 int i;
911 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
913 ASSERT(netdev != NULL, return -1;);
914 self = (struct irda_usb_cb *) netdev->priv;
915 ASSERT(self != NULL, return -1;);
917 /* Can only open the device if it's there */
918 if(!self->present) {
919 WARNING("%s(), device not present!\n", __FUNCTION__);
920 return -1;
923 /* Initialise default speed and xbofs value
924 * (IrLAP will change that soon) */
925 self->speed = -1;
926 self->xbofs = -1;
927 self->new_speed = -1;
928 self->new_xbofs = -1;
930 /* To do *before* submitting Rx urbs and starting net Tx queue
931 * Jean II */
932 self->netopen = 1;
935 * Now that everything should be initialized properly,
936 * Open new IrLAP layer instance to take care of us...
937 * Note : will send immediately a speed change...
939 sprintf(hwname, "usb#%d", self->usbdev->devnum);
940 self->irlap = irlap_open(netdev, &self->qos, hwname);
941 ASSERT(self->irlap != NULL, return -1;);
943 /* Allow IrLAP to send data to us */
944 netif_start_queue(netdev);
946 /* We submit all the Rx URB except for one that we keep idle.
947 * Need to be initialised before submitting other USBs, because
948 * in some cases as soon as we submit the URBs the USB layer
949 * will trigger a dummy receive - Jean II */
950 self->idle_rx_urb = self->rx_urb[IU_MAX_ACTIVE_RX_URBS];
951 self->idle_rx_urb->context = NULL;
953 /* Now that we can pass data to IrLAP, allow the USB layer
954 * to send us some data... */
955 for (i = 0; i < IU_MAX_ACTIVE_RX_URBS; i++) {
956 struct sk_buff *skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
957 if (!skb) {
958 /* If this ever happen, we are in deep s***.
959 * Basically, we can't start the Rx path... */
960 WARNING("%s(), Failed to allocate Rx skb\n", __FUNCTION__);
961 return -1;
963 //skb_reserve(newskb, USB_IRDA_HEADER - 1);
964 irda_usb_submit(self, skb, self->rx_urb[i]);
967 /* Ready to play !!! */
968 return 0;
971 /*------------------------------------------------------------------*/
973 * Function irda_usb_net_close (self)
975 * Network device is taken down. Usually this is done by
976 * "ifconfig irda0 down"
978 static int irda_usb_net_close(struct net_device *netdev)
980 struct irda_usb_cb *self;
981 int i;
983 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
985 ASSERT(netdev != NULL, return -1;);
986 self = (struct irda_usb_cb *) netdev->priv;
987 ASSERT(self != NULL, return -1;);
989 /* Clear this flag *before* unlinking the urbs and *before*
990 * stopping the network Tx queue - Jean II */
991 self->netopen = 0;
993 /* Stop network Tx queue */
994 netif_stop_queue(netdev);
996 /* Deallocate all the Rx path buffers (URBs and skb) */
997 for (i = 0; i < IU_MAX_RX_URBS; i++) {
998 struct urb *urb = self->rx_urb[i];
999 struct sk_buff *skb = (struct sk_buff *) urb->context;
1000 /* Cancel the receive command */
1001 usb_unlink_urb(urb);
1002 /* The skb is ours, free it */
1003 if(skb) {
1004 dev_kfree_skb(skb);
1005 urb->context = NULL;
1008 /* Cancel Tx and speed URB - need to be synchronous to avoid races */
1009 self->tx_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
1010 usb_unlink_urb(self->tx_urb);
1011 self->speed_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
1012 usb_unlink_urb(self->speed_urb);
1014 /* Stop and remove instance of IrLAP */
1015 if (self->irlap)
1016 irlap_close(self->irlap);
1017 self->irlap = NULL;
1019 return 0;
1022 /*------------------------------------------------------------------*/
1024 * IOCTLs : Extra out-of-band network commands...
1026 static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1028 unsigned long flags;
1029 struct if_irda_req *irq = (struct if_irda_req *) rq;
1030 struct irda_usb_cb *self;
1031 int ret = 0;
1033 ASSERT(dev != NULL, return -1;);
1034 self = dev->priv;
1035 ASSERT(self != NULL, return -1;);
1037 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
1039 switch (cmd) {
1040 case SIOCSBANDWIDTH: /* Set bandwidth */
1041 if (!capable(CAP_NET_ADMIN))
1042 return -EPERM;
1043 /* Protect us from USB callbacks, net watchdog and else. */
1044 spin_lock_irqsave(&self->lock, flags);
1045 /* Check if the device is still there */
1046 if(self->present) {
1047 /* Set the desired speed */
1048 self->new_speed = irq->ifr_baudrate;
1049 irda_usb_change_speed_xbofs(self);
1051 spin_unlock_irqrestore(&self->lock, flags);
1052 break;
1053 case SIOCSMEDIABUSY: /* Set media busy */
1054 if (!capable(CAP_NET_ADMIN))
1055 return -EPERM;
1056 /* Check if the IrDA stack is still there */
1057 if(self->netopen)
1058 irda_device_set_media_busy(self->netdev, TRUE);
1059 break;
1060 case SIOCGRECEIVING: /* Check if we are receiving right now */
1061 irq->ifr_receiving = irda_usb_is_receiving(self);
1062 break;
1063 default:
1064 ret = -EOPNOTSUPP;
1067 return ret;
1070 /*------------------------------------------------------------------*/
1072 * Get device stats (for /proc/net/dev and ifconfig)
1074 static struct net_device_stats *irda_usb_net_get_stats(struct net_device *dev)
1076 struct irda_usb_cb *self = dev->priv;
1077 return &self->stats;
1080 /********************* IRDA CONFIG SUBROUTINES *********************/
1082 * Various subroutines dealing with IrDA and network stuff we use to
1083 * configure and initialise each irda-usb instance.
1084 * These functions are used below in the main calls of the driver...
1087 /*------------------------------------------------------------------*/
1089 * Set proper values in the IrDA QOS structure
1091 static inline void irda_usb_init_qos(struct irda_usb_cb *self)
1093 struct irda_class_desc *desc;
1095 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
1097 desc = self->irda_desc;
1099 /* Initialize QoS for this device */
1100 irda_init_max_qos_capabilies(&self->qos);
1102 /* See spec section 7.2 for meaning.
1103 * Values are little endian (as most USB stuff), the IrDA stack
1104 * use it in native order (see parameters.c). - Jean II */
1105 self->qos.baud_rate.bits = le16_to_cpu(desc->wBaudRate);
1106 self->qos.min_turn_time.bits = desc->bmMinTurnaroundTime;
1107 self->qos.additional_bofs.bits = desc->bmAdditionalBOFs;
1108 self->qos.window_size.bits = desc->bmWindowSize;
1109 self->qos.data_size.bits = desc->bmDataSize;
1111 IRDA_DEBUG(0, "%s(), dongle says speed=0x%X, size=0x%X, window=0x%X, bofs=0x%X, turn=0x%X\n",
1112 __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);
1114 /* Don't always trust what the dongle tell us */
1115 if(self->capability & IUC_SIR_ONLY)
1116 self->qos.baud_rate.bits &= 0x00ff;
1117 if(self->capability & IUC_SMALL_PKT)
1118 self->qos.data_size.bits = 0x07;
1119 if(self->capability & IUC_NO_WINDOW)
1120 self->qos.window_size.bits = 0x01;
1121 if(self->capability & IUC_MAX_WINDOW)
1122 self->qos.window_size.bits = 0x7f;
1123 if(self->capability & IUC_MAX_XBOFS)
1124 self->qos.additional_bofs.bits = 0x01;
1126 #if 1
1127 /* Module parameter can override the rx window size */
1128 if (qos_mtt_bits)
1129 self->qos.min_turn_time.bits = qos_mtt_bits;
1130 #endif
1132 * Note : most of those values apply only for the receive path,
1133 * the transmit path will be set differently - Jean II
1135 irda_qos_bits_to_value(&self->qos);
1138 /*------------------------------------------------------------------*/
1140 * Initialise the network side of the irda-usb instance
1141 * Called when a new USB instance is registered in irda_usb_probe()
1143 static inline int irda_usb_open(struct irda_usb_cb *self)
1145 struct net_device *netdev = self->netdev;
1147 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1149 irda_usb_init_qos(self);
1151 /* Override the network functions we need to use */
1152 netdev->hard_start_xmit = irda_usb_hard_xmit;
1153 netdev->tx_timeout = irda_usb_net_timeout;
1154 netdev->watchdog_timeo = 250*HZ/1000; /* 250 ms > USB timeout */
1155 netdev->open = irda_usb_net_open;
1156 netdev->stop = irda_usb_net_close;
1157 netdev->get_stats = irda_usb_net_get_stats;
1158 netdev->do_ioctl = irda_usb_net_ioctl;
1160 return register_netdev(netdev);
1163 /*------------------------------------------------------------------*/
1165 * Cleanup the network side of the irda-usb instance
1166 * Called when a USB instance is removed in irda_usb_disconnect()
1168 static inline void irda_usb_close(struct irda_usb_cb *self)
1170 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1172 /* Remove netdevice */
1173 unregister_netdev(self->netdev);
1175 /* Remove the speed buffer */
1176 if (self->speed_buff != NULL) {
1177 kfree(self->speed_buff);
1178 self->speed_buff = NULL;
1182 /********************** USB CONFIG SUBROUTINES **********************/
1184 * Various subroutines dealing with USB stuff we use to configure and
1185 * initialise each irda-usb instance.
1186 * These functions are used below in the main calls of the driver...
1189 /*------------------------------------------------------------------*/
1191 * Function irda_usb_parse_endpoints(dev, ifnum)
1193 * Parse the various endpoints and find the one we need.
1195 * The endpoint are the pipes used to communicate with the USB device.
1196 * The spec defines 2 endpoints of type bulk transfer, one in, and one out.
1197 * These are used to pass frames back and forth with the dongle.
1198 * Most dongle have also an interrupt endpoint, that will be probably
1199 * documented in the next spec...
1201 static inline int irda_usb_parse_endpoints(struct irda_usb_cb *self, struct usb_host_endpoint *endpoint, int ennum)
1203 int i; /* Endpoint index in table */
1205 /* Init : no endpoints */
1206 self->bulk_in_ep = 0;
1207 self->bulk_out_ep = 0;
1208 self->bulk_int_ep = 0;
1210 /* Let's look at all those endpoints */
1211 for(i = 0; i < ennum; i++) {
1212 /* All those variables will get optimised by the compiler,
1213 * so let's aim for clarity... - Jean II */
1214 __u8 ep; /* Endpoint address */
1215 __u8 dir; /* Endpoint direction */
1216 __u8 attr; /* Endpoint attribute */
1217 __u16 psize; /* Endpoint max packet size in bytes */
1219 /* Get endpoint address, direction and attribute */
1220 ep = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1221 dir = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK;
1222 attr = endpoint[i].desc.bmAttributes;
1223 psize = endpoint[i].desc.wMaxPacketSize;
1225 /* Is it a bulk endpoint ??? */
1226 if(attr == USB_ENDPOINT_XFER_BULK) {
1227 /* We need to find an IN and an OUT */
1228 if(dir == USB_DIR_IN) {
1229 /* This is our Rx endpoint */
1230 self->bulk_in_ep = ep;
1231 } else {
1232 /* This is our Tx endpoint */
1233 self->bulk_out_ep = ep;
1234 self->bulk_out_mtu = psize;
1236 } else {
1237 if((attr == USB_ENDPOINT_XFER_INT) &&
1238 (dir == USB_DIR_IN)) {
1239 /* This is our interrupt endpoint */
1240 self->bulk_int_ep = ep;
1241 } else {
1242 ERROR("%s(), Unrecognised endpoint %02X.\n", __FUNCTION__, ep);
1247 IRDA_DEBUG(0, "%s(), And our endpoints are : in=%02X, out=%02X (%d), int=%02X\n",
1248 __FUNCTION__, self->bulk_in_ep, self->bulk_out_ep, self->bulk_out_mtu, self->bulk_int_ep);
1249 /* Should be 8, 16, 32 or 64 bytes */
1250 ASSERT(self->bulk_out_mtu == 64, ;);
1252 return((self->bulk_in_ep != 0) && (self->bulk_out_ep != 0));
1255 #ifdef IU_DUMP_CLASS_DESC
1256 /*------------------------------------------------------------------*/
1258 * Function usb_irda_dump_class_desc(desc)
1260 * Prints out the contents of the IrDA class descriptor
1263 static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
1265 /* Values are little endian */
1266 printk("bLength=%x\n", desc->bLength);
1267 printk("bDescriptorType=%x\n", desc->bDescriptorType);
1268 printk("bcdSpecRevision=%x\n", le16_to_cpu(desc->bcdSpecRevision));
1269 printk("bmDataSize=%x\n", desc->bmDataSize);
1270 printk("bmWindowSize=%x\n", desc->bmWindowSize);
1271 printk("bmMinTurnaroundTime=%d\n", desc->bmMinTurnaroundTime);
1272 printk("wBaudRate=%x\n", le16_to_cpu(desc->wBaudRate));
1273 printk("bmAdditionalBOFs=%x\n", desc->bmAdditionalBOFs);
1274 printk("bIrdaRateSniff=%x\n", desc->bIrdaRateSniff);
1275 printk("bMaxUnicastList=%x\n", desc->bMaxUnicastList);
1277 #endif /* IU_DUMP_CLASS_DESC */
1279 /*------------------------------------------------------------------*/
1281 * Function irda_usb_find_class_desc(intf)
1283 * Returns instance of IrDA class descriptor, or NULL if not found
1285 * The class descriptor is some extra info that IrDA USB devices will
1286 * offer to us, describing their IrDA characteristics. We will use that in
1287 * irda_usb_init_qos()
1289 static inline struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf)
1291 struct usb_device *dev = interface_to_usbdev (intf);
1292 struct irda_class_desc *desc;
1293 int ret;
1295 desc = kmalloc(sizeof (*desc), GFP_KERNEL);
1296 if (desc == NULL)
1297 return NULL;
1298 memset(desc, 0, sizeof(*desc));
1300 /* USB-IrDA class spec 1.0:
1301 * 6.1.3: Standard "Get Descriptor" Device Request is not
1302 * appropriate to retrieve class-specific descriptor
1303 * 6.2.5: Class Specific "Get Class Descriptor" Interface Request
1304 * is mandatory and returns the USB-IrDA class descriptor
1307 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
1308 IU_REQ_GET_CLASS_DESC,
1309 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1310 0, intf->altsetting->desc.bInterfaceNumber, desc,
1311 sizeof(*desc), msecs_to_jiffies(500));
1313 IRDA_DEBUG(1, "%s(), ret=%d\n", __FUNCTION__, ret);
1314 if (ret < sizeof(*desc)) {
1315 WARNING("usb-irda: class_descriptor read %s (%d)\n",
1316 (ret<0) ? "failed" : "too short", ret);
1318 else if (desc->bDescriptorType != USB_DT_IRDA) {
1319 WARNING("usb-irda: bad class_descriptor type\n");
1321 else {
1322 #ifdef IU_DUMP_CLASS_DESC
1323 irda_usb_dump_class_desc(desc);
1324 #endif /* IU_DUMP_CLASS_DESC */
1326 return desc;
1328 kfree(desc);
1329 return NULL;
1332 /*********************** USB DEVICE CALLBACKS ***********************/
1334 * Main calls from the USB subsystem.
1335 * Mostly registering a new irda-usb device and removing it....
1338 /*------------------------------------------------------------------*/
1340 * This routine is called by the USB subsystem for each new device
1341 * in the system. We need to check if the device is ours, and in
1342 * this case start handling it.
1343 * The USB layer protect us from reentrancy (via BKL), so we don't need
1344 * to spinlock in there... Jean II
1346 static int irda_usb_probe(struct usb_interface *intf,
1347 const struct usb_device_id *id)
1349 struct net_device *net;
1350 struct usb_device *dev = interface_to_usbdev(intf);
1351 struct irda_usb_cb *self = NULL;
1352 struct usb_host_interface *interface;
1353 struct irda_class_desc *irda_desc;
1354 int ret = -ENOMEM;
1355 int i; /* Driver instance index / Rx URB index */
1357 /* Note : the probe make sure to call us only for devices that
1358 * matches the list of dongle (top of the file). So, we
1359 * don't need to check if the dongle is really ours.
1360 * Jean II */
1362 MESSAGE("IRDA-USB found at address %d, Vendor: %x, Product: %x\n",
1363 dev->devnum, dev->descriptor.idVendor,
1364 dev->descriptor.idProduct);
1366 net = alloc_irdadev(sizeof(*self));
1367 if (!net)
1368 goto err_out;
1370 self = net->priv;
1371 self->netdev = net;
1372 spin_lock_init(&self->lock);
1374 SET_MODULE_OWNER(net);
1376 /* Create all of the needed urbs */
1377 for (i = 0; i < IU_MAX_RX_URBS; i++) {
1378 self->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
1379 if (!self->rx_urb[i]) {
1380 goto err_out_1;
1383 self->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1384 if (!self->tx_urb) {
1385 goto err_out_1;
1387 self->speed_urb = usb_alloc_urb(0, GFP_KERNEL);
1388 if (!self->speed_urb) {
1389 goto err_out_2;
1392 /* Is this really necessary? (no, except maybe for broken devices) */
1393 if (usb_reset_configuration (dev) < 0) {
1394 err("reset_configuration failed");
1395 ret = -EIO;
1396 goto err_out_3;
1399 /* Is this really necessary? */
1400 /* Note : some driver do hardcode the interface number, some others
1401 * specify an alternate, but very few driver do like this.
1402 * Jean II */
1403 ret = usb_set_interface(dev, intf->altsetting->desc.bInterfaceNumber, 0);
1404 IRDA_DEBUG(1, "usb-irda: set interface %d result %d\n", intf->altsetting->desc.bInterfaceNumber, ret);
1405 switch (ret) {
1406 case 0:
1407 break;
1408 case -EPIPE: /* -EPIPE = -32 */
1409 /* Martin Diehl says if we get a -EPIPE we should
1410 * be fine and we don't need to do a usb_clear_halt().
1411 * - Jean II */
1412 IRDA_DEBUG(0, "%s(), Received -EPIPE, ignoring...\n", __FUNCTION__);
1413 break;
1414 default:
1415 IRDA_DEBUG(0, "%s(), Unknown error %d\n", __FUNCTION__, ret);
1416 ret = -EIO;
1417 goto err_out_3;
1420 /* Find our endpoints */
1421 interface = intf->cur_altsetting;
1422 if(!irda_usb_parse_endpoints(self, interface->endpoint,
1423 interface->desc.bNumEndpoints)) {
1424 ERROR("%s(), Bogus endpoints...\n", __FUNCTION__);
1425 ret = -EIO;
1426 goto err_out_3;
1429 /* Find IrDA class descriptor */
1430 irda_desc = irda_usb_find_class_desc(intf);
1431 ret = -ENODEV;
1432 if (irda_desc == NULL)
1433 goto err_out_3;
1435 self->irda_desc = irda_desc;
1436 self->present = 1;
1437 self->netopen = 0;
1438 self->capability = id->driver_info;
1439 self->usbdev = dev;
1440 self->usbintf = intf;
1442 /* Allocate the buffer for speed changes */
1443 /* Don't change this buffer size and allocation without doing
1444 * some heavy and complete testing. Don't ask why :-(
1445 * Jean II */
1446 self->speed_buff = (char *) kmalloc(IRDA_USB_SPEED_MTU, GFP_KERNEL);
1447 if (self->speed_buff == NULL)
1448 goto err_out_3;
1450 memset(self->speed_buff, 0, IRDA_USB_SPEED_MTU);
1452 ret = irda_usb_open(self);
1453 if (ret)
1454 goto err_out_4;
1456 MESSAGE("IrDA: Registered device %s\n", net->name);
1457 usb_set_intfdata(intf, self);
1458 return 0;
1460 err_out_4:
1461 kfree(self->speed_buff);
1462 err_out_3:
1463 /* Free all urbs that we may have created */
1464 usb_free_urb(self->speed_urb);
1465 err_out_2:
1466 usb_free_urb(self->tx_urb);
1467 err_out_1:
1468 for (i = 0; i < IU_MAX_RX_URBS; i++) {
1469 if (self->rx_urb[i])
1470 usb_free_urb(self->rx_urb[i]);
1472 free_netdev(net);
1473 err_out:
1474 return ret;
1477 /*------------------------------------------------------------------*/
1479 * The current irda-usb device is removed, the USB layer tell us
1480 * to shut it down...
1481 * One of the constraints is that when we exit this function,
1482 * we cannot use the usb_device no more. Gone. Destroyed. kfree().
1483 * Most other subsystem allow you to destroy the instance at a time
1484 * when it's convenient to you, to postpone it to a later date, but
1485 * not the USB subsystem.
1486 * So, we must make bloody sure that everything gets deactivated.
1487 * Jean II
1489 static void irda_usb_disconnect(struct usb_interface *intf)
1491 unsigned long flags;
1492 struct irda_usb_cb *self = usb_get_intfdata(intf);
1493 int i;
1495 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1497 usb_set_intfdata(intf, NULL);
1498 if (!self)
1499 return;
1501 /* Make sure that the Tx path is not executing. - Jean II */
1502 spin_lock_irqsave(&self->lock, flags);
1504 /* Oups ! We are not there any more.
1505 * This will stop/desactivate the Tx path. - Jean II */
1506 self->present = 0;
1508 /* We need to have irq enabled to unlink the URBs. That's OK,
1509 * at this point the Tx path is gone - Jean II */
1510 spin_unlock_irqrestore(&self->lock, flags);
1512 /* Hum... Check if networking is still active (avoid races) */
1513 if((self->netopen) || (self->irlap)) {
1514 /* Accept no more transmissions */
1515 /*netif_device_detach(self->netdev);*/
1516 netif_stop_queue(self->netdev);
1517 /* Stop all the receive URBs */
1518 for (i = 0; i < IU_MAX_RX_URBS; i++)
1519 usb_unlink_urb(self->rx_urb[i]);
1520 /* Cancel Tx and speed URB.
1521 * Toggle flags to make sure it's synchronous. */
1522 self->tx_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
1523 usb_unlink_urb(self->tx_urb);
1524 self->speed_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
1525 usb_unlink_urb(self->speed_urb);
1528 /* Cleanup the device stuff */
1529 irda_usb_close(self);
1530 /* No longer attached to USB bus */
1531 self->usbdev = NULL;
1532 self->usbintf = NULL;
1534 /* Clean up our urbs */
1535 for (i = 0; i < IU_MAX_RX_URBS; i++)
1536 usb_free_urb(self->rx_urb[i]);
1537 /* Clean up Tx and speed URB */
1538 usb_free_urb(self->tx_urb);
1539 usb_free_urb(self->speed_urb);
1541 /* Free self and network device */
1542 free_netdev(self->netdev);
1543 IRDA_DEBUG(0, "%s(), USB IrDA Disconnected\n", __FUNCTION__);
1546 /*------------------------------------------------------------------*/
1548 * USB device callbacks
1550 static struct usb_driver irda_driver = {
1551 .owner = THIS_MODULE,
1552 .name = "irda-usb",
1553 .probe = irda_usb_probe,
1554 .disconnect = irda_usb_disconnect,
1555 .id_table = dongles,
1558 /************************* MODULE CALLBACKS *************************/
1560 * Deal with module insertion/removal
1561 * Mostly tell USB about our existence
1564 /*------------------------------------------------------------------*/
1566 * Module insertion
1568 static int __init usb_irda_init(void)
1570 int ret;
1572 ret = usb_register(&irda_driver);
1573 if (ret < 0)
1574 return ret;
1576 MESSAGE("USB IrDA support registered\n");
1577 return 0;
1579 module_init(usb_irda_init);
1581 /*------------------------------------------------------------------*/
1583 * Module removal
1585 static void __exit usb_irda_cleanup(void)
1587 /* Deregister the driver and remove all pending instances */
1588 usb_deregister(&irda_driver);
1590 module_exit(usb_irda_cleanup);
1592 /*------------------------------------------------------------------*/
1594 * Module parameters
1596 MODULE_PARM(qos_mtt_bits, "i");
1597 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
1598 MODULE_AUTHOR("Roman Weissgaerber <weissg@vienna.at>, Dag Brattli <dag@brattli.net> and Jean Tourrilhes <jt@hpl.hp.com>");
1599 MODULE_DESCRIPTION("IrDA-USB Dongle Driver");
1600 MODULE_LICENSE("GPL");