aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/irda/irda-usb.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/net/irda/irda-usb.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/net/irda/irda-usb.c')
-rw-r--r--drivers/net/irda/irda-usb.c1602
1 files changed, 1602 insertions, 0 deletions
diff --git a/drivers/net/irda/irda-usb.c b/drivers/net/irda/irda-usb.c
new file mode 100644
index 000000000000..46e0022d3258
--- /dev/null
+++ b/drivers/net/irda/irda-usb.c
@@ -0,0 +1,1602 @@
1/*****************************************************************************
2 *
3 * Filename: irda-usb.c
4 * Version: 0.9b
5 * Description: IrDA-USB Driver
6 * Status: Experimental
7 * Author: Dag Brattli <dag@brattli.net>
8 *
9 * Copyright (C) 2000, Roman Weissgaerber <weissg@vienna.at>
10 * Copyright (C) 2001, Dag Brattli <dag@brattli.net>
11 * Copyright (C) 2001, Jean Tourrilhes <jt@hpl.hp.com>
12 *
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.
17 *
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.
22 *
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.
26 *
27 *****************************************************************************/
28
29/*
30 * IMPORTANT NOTE
31 * --------------
32 *
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...
35 *
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)
40 *
41 * This driver has NOT been tested with the following drivers :
42 * o ehci-hcd (USB 2.0 controllers)
43 *
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...
48 *
49 * Jean II
50 */
51
52/*------------------------------------------------------------------*/
53
54#include <linux/module.h>
55#include <linux/moduleparam.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>
64
65#include "irda-usb.h"
66
67/*------------------------------------------------------------------*/
68
69static int qos_mtt_bits = 0;
70
71/* These are the currently known IrDA USB dongles. Add new dongles here */
72static 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 */
87};
88
89/*
90 * Important note :
91 * Devices based on the SigmaTel chipset (0x66f, 0x4200) are not designed
92 * using the "USB-IrDA specification" (yes, there exist such a thing), and
93 * therefore not supported by this driver (don't add them above).
94 * There is a Linux driver, stir4200, that support those USB devices.
95 * Jean II
96 */
97
98MODULE_DEVICE_TABLE(usb, dongles);
99
100/*------------------------------------------------------------------*/
101
102static struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf);
103static void irda_usb_disconnect(struct usb_interface *intf);
104static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self);
105static int irda_usb_hard_xmit(struct sk_buff *skb, struct net_device *dev);
106static int irda_usb_open(struct irda_usb_cb *self);
107static void irda_usb_close(struct irda_usb_cb *self);
108static void speed_bulk_callback(struct urb *urb, struct pt_regs *regs);
109static void write_bulk_callback(struct urb *urb, struct pt_regs *regs);
110static void irda_usb_receive(struct urb *urb, struct pt_regs *regs);
111static int irda_usb_net_open(struct net_device *dev);
112static int irda_usb_net_close(struct net_device *dev);
113static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
114static void irda_usb_net_timeout(struct net_device *dev);
115static struct net_device_stats *irda_usb_net_get_stats(struct net_device *dev);
116
117/************************ TRANSMIT ROUTINES ************************/
118/*
119 * Receive packets from the IrDA stack and send them on the USB pipe.
120 * Handle speed change, timeout and lot's of ugliness...
121 */
122
123/*------------------------------------------------------------------*/
124/*
125 * Function irda_usb_build_header(self, skb, header)
126 *
127 * Builds USB-IrDA outbound header
128 *
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.
131 *
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
138 */
139static void irda_usb_build_header(struct irda_usb_cb *self,
140 __u8 *header,
141 int force)
142{
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
148 */
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;
156 }
157
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 */
162
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;
193 }
194 } else
195 /* No change */
196 *header = 0;
197
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 */
204
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;
233 }
234 }
235}
236
237/*------------------------------------------------------------------*/
238/*
239 * Send a command to change the speed of the dongle
240 * Need to be called with spinlock on.
241 */
242static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self)
243{
244 __u8 *frame;
245 struct urb *urb;
246 int ret;
247
248 IRDA_DEBUG(2, "%s(), speed=%d, xbofs=%d\n", __FUNCTION__,
249 self->new_speed, self->new_xbofs);
250
251 /* Grab the speed URB */
252 urb = self->speed_urb;
253 if (urb->status != 0) {
254 IRDA_WARNING("%s(), URB still in use!\n", __FUNCTION__);
255 return;
256 }
257
258 /* Allocate the fake frame */
259 frame = self->speed_buff;
260
261 /* Set the new speed and xbofs in this fake frame */
262 irda_usb_build_header(self, frame, 1);
263
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;
271
272 /* Irq disabled -> GFP_ATOMIC */
273 if ((ret = usb_submit_urb(urb, GFP_ATOMIC))) {
274 IRDA_WARNING("%s(), failed Speed URB\n", __FUNCTION__);
275 }
276}
277
278/*------------------------------------------------------------------*/
279/*
280 * Speed URB callback
281 * Now, we can only get called for the speed URB.
282 */
283static void speed_bulk_callback(struct urb *urb, struct pt_regs *regs)
284{
285 struct irda_usb_cb *self = urb->context;
286
287 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
288
289 /* We should always have a context */
290 IRDA_ASSERT(self != NULL, return;);
291 /* We should always be called for the speed URB */
292 IRDA_ASSERT(urb == self->speed_urb, return;);
293
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);
298
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;
305 }
306
307 /* urb is now available */
308 //urb->status = 0; -> tested above
309
310 /* New speed and xbof is now commited in hardware */
311 self->new_speed = -1;
312 self->new_xbofs = -1;
313
314 /* Allow the stack to send more packets */
315 netif_wake_queue(self->netdev);
316}
317
318/*------------------------------------------------------------------*/
319/*
320 * Send an IrDA frame to the USB dongle (for transmission)
321 */
322static int irda_usb_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
323{
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 */
331
332 IRDA_DEBUG(4, "%s() on %s\n", __FUNCTION__, netdev->name);
333
334 netif_stop_queue(netdev);
335
336 /* Protect us from USB callbacks, net watchdog and else. */
337 spin_lock_irqsave(&self->lock, flags);
338
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;
345 }
346
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;
351 }
352
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;
358
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;
373 }
374 }
375
376 if (urb->status != 0) {
377 IRDA_WARNING("%s(), URB still in use!\n", __FUNCTION__);
378 goto drop;
379 }
380
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 IRDA_WARNING("%s(), failed skb_cow() !!!\n", __FUNCTION__);
389 goto drop;
390 }
391 }
392
393 /* Change setting for next frame */
394 irda_usb_build_header(self, skb_push(skb, USB_IRDA_HEADER), 0);
395
396 /* FIXME: Make macro out of this one */
397 ((struct irda_skb_cb *)skb->cb)->context = self;
398
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;
413
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...
417 *
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...
422 *
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;
444
445 /* Check if the mtt is larger than the time we have
446 * already used by all the protocol processing
447 */
448 if (mtt > diff) {
449 mtt -= diff;
450 if (mtt > 1000)
451 mdelay(mtt/1000);
452 else
453 udelay(mtt);
454 }
455 }
456 }
457
458 /* Ask USB to send the packet - Irq disabled -> GFP_ATOMIC */
459 if ((res = usb_submit_urb(urb, GFP_ATOMIC))) {
460 IRDA_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;
468
469 netdev->trans_start = jiffies;
470 }
471 spin_unlock_irqrestore(&self->lock, flags);
472
473 return 0;
474
475drop:
476 /* Drop silently the skb and exit */
477 dev_kfree_skb(skb);
478 spin_unlock_irqrestore(&self->lock, flags);
479 return err; /* Usually 1 */
480}
481
482/*------------------------------------------------------------------*/
483/*
484 * Note : this function will be called only for tx_urb...
485 */
486static void write_bulk_callback(struct urb *urb, struct pt_regs *regs)
487{
488 unsigned long flags;
489 struct sk_buff *skb = urb->context;
490 struct irda_usb_cb *self = ((struct irda_skb_cb *) skb->cb)->context;
491
492 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
493
494 /* We should always have a context */
495 IRDA_ASSERT(self != NULL, return;);
496 /* We should always be called for the speed URB */
497 IRDA_ASSERT(urb == self->tx_urb, return;);
498
499 /* Free up the skb */
500 dev_kfree_skb_any(skb);
501 urb->context = NULL;
502
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);
507
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;
515 }
516
517 /* urb is now available */
518 //urb->status = 0; -> tested above
519
520 /* Make sure we read self->present properly */
521 spin_lock_irqsave(&self->lock, flags);
522
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;
528 }
529
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);
544 }
545 } else {
546 /* Otherwise, allow the stack to send more packets */
547 netif_wake_queue(self->netdev);
548 }
549 spin_unlock_irqrestore(&self->lock, flags);
550}
551
552/*------------------------------------------------------------------*/
553/*
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()).
560 */
561static void irda_usb_net_timeout(struct net_device *netdev)
562{
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 */
567
568 IRDA_DEBUG(0, "%s(), Network layer thinks we timed out!\n", __FUNCTION__);
569 IRDA_ASSERT(self != NULL, return;);
570
571 /* Protect us from USB callbacks, net Tx and else. */
572 spin_lock_irqsave(&self->lock, flags);
573
574 /* self->present *MUST* be read under spinlock */
575 if (!self->present) {
576 IRDA_WARNING("%s(), device not present!\n", __FUNCTION__);
577 netif_stop_queue(netdev);
578 spin_unlock_irqrestore(&self->lock, flags);
579 return;
580 }
581
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);
586
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;
604 }
605 }
606
607 /* Check Tx URB */
608 urb = self->tx_urb;
609 if (urb->status != 0) {
610 struct sk_buff *skb = urb->context;
611
612 IRDA_DEBUG(0, "%s: Tx timed out, urb->status=%d, urb->transfer_flags=0x%04X\n", netdev->name, urb->status, urb->transfer_flags);
613
614 /* Increase error count */
615 self->stats.tx_errors++;
616
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 */
625
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;
646 }
647 urb->status = 0;
648 netif_wake_queue(self->netdev);
649 done = 1;
650 break;
651 }
652 }
653 spin_unlock_irqrestore(&self->lock, flags);
654
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...
658 */
659
660 /* if(done == 0) */
661}
662
663/************************* RECEIVE ROUTINES *************************/
664/*
665 * Receive packets from the USB layer stack and pass them to the IrDA stack.
666 * Try to work around USB failures...
667 */
668
669/*
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
685 */
686
687/*------------------------------------------------------------------*/
688/*
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.
691 *
692 * Jean II
693 */
694static void irda_usb_submit(struct irda_usb_cb *self, struct sk_buff *skb, struct urb *urb)
695{
696 struct irda_skb_cb *cb;
697 int ret;
698
699 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
700
701 /* This should never happen */
702 IRDA_ASSERT(skb != NULL, return;);
703 IRDA_ASSERT(urb != NULL, return;);
704
705 /* Save ourselves in the skb */
706 cb = (struct irda_skb_cb *) skb->cb;
707 cb->context = self;
708
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;
717
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 IRDA_WARNING("%s(), Failed to submit Rx URB %d\n",
724 __FUNCTION__, ret);
725 }
726}
727
728/*------------------------------------------------------------------*/
729/*
730 * Function irda_usb_receive(urb)
731 *
732 * Called by the USB subsystem when a frame has been received
733 *
734 */
735static void irda_usb_receive(struct urb *urb, struct pt_regs *regs)
736{
737 struct sk_buff *skb = (struct sk_buff *) urb->context;
738 struct irda_usb_cb *self;
739 struct irda_skb_cb *cb;
740 struct sk_buff *newskb;
741 struct sk_buff *dataskb;
742 int docopy;
743
744 IRDA_DEBUG(2, "%s(), len=%d\n", __FUNCTION__, urb->actual_length);
745
746 /* Find ourselves */
747 cb = (struct irda_skb_cb *) skb->cb;
748 IRDA_ASSERT(cb != NULL, return;);
749 self = (struct irda_usb_cb *) cb->context;
750 IRDA_ASSERT(self != NULL, return;);
751
752 /* If the network is closed or the device gone, stop everything */
753 if ((!self->netopen) || (!self->present)) {
754 IRDA_DEBUG(0, "%s(), Network is gone!\n", __FUNCTION__);
755 /* Don't re-submit the URB : will stall the Rx path */
756 return;
757 }
758
759 /* Check the status */
760 if (urb->status != 0) {
761 switch (urb->status) {
762 case -EILSEQ:
763 self->stats.rx_errors++;
764 self->stats.rx_crc_errors++;
765 break;
766 case -ECONNRESET: /* -104 */
767 IRDA_DEBUG(0, "%s(), Connection Reset (-104), transfer_flags 0x%04X \n", __FUNCTION__, urb->transfer_flags);
768 /* uhci_cleanup_unlink() is going to kill the Rx
769 * URB just after we return. No problem, at this
770 * point the URB will be idle ;-) - Jean II */
771 break;
772 default:
773 IRDA_DEBUG(0, "%s(), RX status %d,transfer_flags 0x%04X \n", __FUNCTION__, urb->status, urb->transfer_flags);
774 break;
775 }
776 goto done;
777 }
778
779 /* Check for empty frames */
780 if (urb->actual_length <= USB_IRDA_HEADER) {
781 IRDA_WARNING("%s(), empty frame!\n", __FUNCTION__);
782 goto done;
783 }
784
785 /*
786 * Remember the time we received this frame, so we can
787 * reduce the min turn time a bit since we will know
788 * how much time we have used for protocol processing
789 */
790 do_gettimeofday(&self->stamp);
791
792 /* Check if we need to copy the data to a new skb or not.
793 * For most frames, we use ZeroCopy and pass the already
794 * allocated skb up the stack.
795 * If the frame is small, it is more efficient to copy it
796 * to save memory (copy will be fast anyway - that's
797 * called Rx-copy-break). Jean II */
798 docopy = (urb->actual_length < IRDA_RX_COPY_THRESHOLD);
799
800 /* Allocate a new skb */
801 newskb = dev_alloc_skb(docopy ? urb->actual_length : IRDA_SKB_MAX_MTU);
802 if (!newskb) {
803 self->stats.rx_dropped++;
804 /* We could deliver the current skb, but this would stall
805 * the Rx path. Better drop the packet... Jean II */
806 goto done;
807 }
808
809 /* Make sure IP header get aligned (IrDA header is 5 bytes) */
810 /* But IrDA-USB header is 1 byte. Jean II */
811 //skb_reserve(newskb, USB_IRDA_HEADER - 1);
812
813 if(docopy) {
814 /* Copy packet, so we can recycle the original */
815 memcpy(newskb->data, skb->data, urb->actual_length);
816 /* Deliver this new skb */
817 dataskb = newskb;
818 /* And hook the old skb to the URB
819 * Note : we don't need to "clean up" the old skb,
820 * as we never touched it. Jean II */
821 } else {
822 /* We are using ZeroCopy. Deliver old skb */
823 dataskb = skb;
824 /* And hook the new skb to the URB */
825 skb = newskb;
826 }
827
828 /* Set proper length on skb & remove USB-IrDA header */
829 skb_put(dataskb, urb->actual_length);
830 skb_pull(dataskb, USB_IRDA_HEADER);
831
832 /* Ask the networking layer to queue the packet for the IrDA stack */
833 dataskb->dev = self->netdev;
834 dataskb->mac.raw = dataskb->data;
835 dataskb->protocol = htons(ETH_P_IRDA);
836 netif_rx(dataskb);
837
838 /* Keep stats up to date */
839 self->stats.rx_bytes += dataskb->len;
840 self->stats.rx_packets++;
841 self->netdev->last_rx = jiffies;
842
843done:
844 /* Note : at this point, the URB we've just received (urb)
845 * is still referenced by the USB layer. For example, if we
846 * have received a -ECONNRESET, uhci_cleanup_unlink() will
847 * continue to process it (in fact, cleaning it up).
848 * If we were to submit this URB, disaster would ensue.
849 * Therefore, we submit our idle URB, and put this URB in our
850 * idle slot....
851 * Jean II */
852 /* Note : with this scheme, we could submit the idle URB before
853 * processing the Rx URB. Another time... Jean II */
854
855 /* Submit the idle URB to replace the URB we've just received */
856 irda_usb_submit(self, skb, self->idle_rx_urb);
857 /* Recycle Rx URB : Now, the idle URB is the present one */
858 urb->context = NULL;
859 self->idle_rx_urb = urb;
860}
861
862/*------------------------------------------------------------------*/
863/*
864 * Callbak from IrDA layer. IrDA wants to know if we have
865 * started receiving anything.
866 */
867static int irda_usb_is_receiving(struct irda_usb_cb *self)
868{
869 /* Note : because of the way UHCI works, it's almost impossible
870 * to get this info. The Controller DMA directly to memory and
871 * signal only when the whole frame is finished. To know if the
872 * first TD of the URB has been filled or not seems hard work...
873 *
874 * The other solution would be to use the "receiving" command
875 * on the default decriptor with a usb_control_msg(), but that
876 * would add USB traffic and would return result only in the
877 * next USB frame (~1ms).
878 *
879 * I've been told that current dongles send status info on their
880 * interrupt endpoint, and that's what the Windows driver uses
881 * to know this info. Unfortunately, this is not yet in the spec...
882 *
883 * Jean II
884 */
885
886 return 0; /* For now */
887}
888
889/********************** IRDA DEVICE CALLBACKS **********************/
890/*
891 * Main calls from the IrDA/Network subsystem.
892 * Mostly registering a new irda-usb device and removing it....
893 * We only deal with the IrDA side of the business, the USB side will
894 * be dealt with below...
895 */
896
897
898/*------------------------------------------------------------------*/
899/*
900 * Function irda_usb_net_open (dev)
901 *
902 * Network device is taken up. Usually this is done by "ifconfig irda0 up"
903 *
904 * Note : don't mess with self->netopen - Jean II
905 */
906static int irda_usb_net_open(struct net_device *netdev)
907{
908 struct irda_usb_cb *self;
909 char hwname[16];
910 int i;
911
912 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
913
914 IRDA_ASSERT(netdev != NULL, return -1;);
915 self = (struct irda_usb_cb *) netdev->priv;
916 IRDA_ASSERT(self != NULL, return -1;);
917
918 /* Can only open the device if it's there */
919 if(!self->present) {
920 IRDA_WARNING("%s(), device not present!\n", __FUNCTION__);
921 return -1;
922 }
923
924 /* Initialise default speed and xbofs value
925 * (IrLAP will change that soon) */
926 self->speed = -1;
927 self->xbofs = -1;
928 self->new_speed = -1;
929 self->new_xbofs = -1;
930
931 /* To do *before* submitting Rx urbs and starting net Tx queue
932 * Jean II */
933 self->netopen = 1;
934
935 /*
936 * Now that everything should be initialized properly,
937 * Open new IrLAP layer instance to take care of us...
938 * Note : will send immediately a speed change...
939 */
940 sprintf(hwname, "usb#%d", self->usbdev->devnum);
941 self->irlap = irlap_open(netdev, &self->qos, hwname);
942 IRDA_ASSERT(self->irlap != NULL, return -1;);
943
944 /* Allow IrLAP to send data to us */
945 netif_start_queue(netdev);
946
947 /* We submit all the Rx URB except for one that we keep idle.
948 * Need to be initialised before submitting other USBs, because
949 * in some cases as soon as we submit the URBs the USB layer
950 * will trigger a dummy receive - Jean II */
951 self->idle_rx_urb = self->rx_urb[IU_MAX_ACTIVE_RX_URBS];
952 self->idle_rx_urb->context = NULL;
953
954 /* Now that we can pass data to IrLAP, allow the USB layer
955 * to send us some data... */
956 for (i = 0; i < IU_MAX_ACTIVE_RX_URBS; i++) {
957 struct sk_buff *skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
958 if (!skb) {
959 /* If this ever happen, we are in deep s***.
960 * Basically, we can't start the Rx path... */
961 IRDA_WARNING("%s(), Failed to allocate Rx skb\n",
962 __FUNCTION__);
963 return -1;
964 }
965 //skb_reserve(newskb, USB_IRDA_HEADER - 1);
966 irda_usb_submit(self, skb, self->rx_urb[i]);
967 }
968
969 /* Ready to play !!! */
970 return 0;
971}
972
973/*------------------------------------------------------------------*/
974/*
975 * Function irda_usb_net_close (self)
976 *
977 * Network device is taken down. Usually this is done by
978 * "ifconfig irda0 down"
979 */
980static int irda_usb_net_close(struct net_device *netdev)
981{
982 struct irda_usb_cb *self;
983 int i;
984
985 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
986
987 IRDA_ASSERT(netdev != NULL, return -1;);
988 self = (struct irda_usb_cb *) netdev->priv;
989 IRDA_ASSERT(self != NULL, return -1;);
990
991 /* Clear this flag *before* unlinking the urbs and *before*
992 * stopping the network Tx queue - Jean II */
993 self->netopen = 0;
994
995 /* Stop network Tx queue */
996 netif_stop_queue(netdev);
997
998 /* Deallocate all the Rx path buffers (URBs and skb) */
999 for (i = 0; i < IU_MAX_RX_URBS; i++) {
1000 struct urb *urb = self->rx_urb[i];
1001 struct sk_buff *skb = (struct sk_buff *) urb->context;
1002 /* Cancel the receive command */
1003 usb_kill_urb(urb);
1004 /* The skb is ours, free it */
1005 if(skb) {
1006 dev_kfree_skb(skb);
1007 urb->context = NULL;
1008 }
1009 }
1010 /* Cancel Tx and speed URB - need to be synchronous to avoid races */
1011 self->tx_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
1012 usb_kill_urb(self->tx_urb);
1013 self->speed_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
1014 usb_kill_urb(self->speed_urb);
1015
1016 /* Stop and remove instance of IrLAP */
1017 if (self->irlap)
1018 irlap_close(self->irlap);
1019 self->irlap = NULL;
1020
1021 return 0;
1022}
1023
1024/*------------------------------------------------------------------*/
1025/*
1026 * IOCTLs : Extra out-of-band network commands...
1027 */
1028static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1029{
1030 unsigned long flags;
1031 struct if_irda_req *irq = (struct if_irda_req *) rq;
1032 struct irda_usb_cb *self;
1033 int ret = 0;
1034
1035 IRDA_ASSERT(dev != NULL, return -1;);
1036 self = dev->priv;
1037 IRDA_ASSERT(self != NULL, return -1;);
1038
1039 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
1040
1041 switch (cmd) {
1042 case SIOCSBANDWIDTH: /* Set bandwidth */
1043 if (!capable(CAP_NET_ADMIN))
1044 return -EPERM;
1045 /* Protect us from USB callbacks, net watchdog and else. */
1046 spin_lock_irqsave(&self->lock, flags);
1047 /* Check if the device is still there */
1048 if(self->present) {
1049 /* Set the desired speed */
1050 self->new_speed = irq->ifr_baudrate;
1051 irda_usb_change_speed_xbofs(self);
1052 }
1053 spin_unlock_irqrestore(&self->lock, flags);
1054 break;
1055 case SIOCSMEDIABUSY: /* Set media busy */
1056 if (!capable(CAP_NET_ADMIN))
1057 return -EPERM;
1058 /* Check if the IrDA stack is still there */
1059 if(self->netopen)
1060 irda_device_set_media_busy(self->netdev, TRUE);
1061 break;
1062 case SIOCGRECEIVING: /* Check if we are receiving right now */
1063 irq->ifr_receiving = irda_usb_is_receiving(self);
1064 break;
1065 default:
1066 ret = -EOPNOTSUPP;
1067 }
1068
1069 return ret;
1070}
1071
1072/*------------------------------------------------------------------*/
1073/*
1074 * Get device stats (for /proc/net/dev and ifconfig)
1075 */
1076static struct net_device_stats *irda_usb_net_get_stats(struct net_device *dev)
1077{
1078 struct irda_usb_cb *self = dev->priv;
1079 return &self->stats;
1080}
1081
1082/********************* IRDA CONFIG SUBROUTINES *********************/
1083/*
1084 * Various subroutines dealing with IrDA and network stuff we use to
1085 * configure and initialise each irda-usb instance.
1086 * These functions are used below in the main calls of the driver...
1087 */
1088
1089/*------------------------------------------------------------------*/
1090/*
1091 * Set proper values in the IrDA QOS structure
1092 */
1093static inline void irda_usb_init_qos(struct irda_usb_cb *self)
1094{
1095 struct irda_class_desc *desc;
1096
1097 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
1098
1099 desc = self->irda_desc;
1100
1101 /* Initialize QoS for this device */
1102 irda_init_max_qos_capabilies(&self->qos);
1103
1104 /* See spec section 7.2 for meaning.
1105 * Values are little endian (as most USB stuff), the IrDA stack
1106 * use it in native order (see parameters.c). - Jean II */
1107 self->qos.baud_rate.bits = le16_to_cpu(desc->wBaudRate);
1108 self->qos.min_turn_time.bits = desc->bmMinTurnaroundTime;
1109 self->qos.additional_bofs.bits = desc->bmAdditionalBOFs;
1110 self->qos.window_size.bits = desc->bmWindowSize;
1111 self->qos.data_size.bits = desc->bmDataSize;
1112
1113 IRDA_DEBUG(0, "%s(), dongle says speed=0x%X, size=0x%X, window=0x%X, bofs=0x%X, turn=0x%X\n",
1114 __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);
1115
1116 /* Don't always trust what the dongle tell us */
1117 if(self->capability & IUC_SIR_ONLY)
1118 self->qos.baud_rate.bits &= 0x00ff;
1119 if(self->capability & IUC_SMALL_PKT)
1120 self->qos.data_size.bits = 0x07;
1121 if(self->capability & IUC_NO_WINDOW)
1122 self->qos.window_size.bits = 0x01;
1123 if(self->capability & IUC_MAX_WINDOW)
1124 self->qos.window_size.bits = 0x7f;
1125 if(self->capability & IUC_MAX_XBOFS)
1126 self->qos.additional_bofs.bits = 0x01;
1127
1128#if 1
1129 /* Module parameter can override the rx window size */
1130 if (qos_mtt_bits)
1131 self->qos.min_turn_time.bits = qos_mtt_bits;
1132#endif
1133 /*
1134 * Note : most of those values apply only for the receive path,
1135 * the transmit path will be set differently - Jean II
1136 */
1137 irda_qos_bits_to_value(&self->qos);
1138}
1139
1140/*------------------------------------------------------------------*/
1141/*
1142 * Initialise the network side of the irda-usb instance
1143 * Called when a new USB instance is registered in irda_usb_probe()
1144 */
1145static inline int irda_usb_open(struct irda_usb_cb *self)
1146{
1147 struct net_device *netdev = self->netdev;
1148
1149 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1150
1151 irda_usb_init_qos(self);
1152
1153 /* Override the network functions we need to use */
1154 netdev->hard_start_xmit = irda_usb_hard_xmit;
1155 netdev->tx_timeout = irda_usb_net_timeout;
1156 netdev->watchdog_timeo = 250*HZ/1000; /* 250 ms > USB timeout */
1157 netdev->open = irda_usb_net_open;
1158 netdev->stop = irda_usb_net_close;
1159 netdev->get_stats = irda_usb_net_get_stats;
1160 netdev->do_ioctl = irda_usb_net_ioctl;
1161
1162 return register_netdev(netdev);
1163}
1164
1165/*------------------------------------------------------------------*/
1166/*
1167 * Cleanup the network side of the irda-usb instance
1168 * Called when a USB instance is removed in irda_usb_disconnect()
1169 */
1170static inline void irda_usb_close(struct irda_usb_cb *self)
1171{
1172 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1173
1174 /* Remove netdevice */
1175 unregister_netdev(self->netdev);
1176
1177 /* Remove the speed buffer */
1178 if (self->speed_buff != NULL) {
1179 kfree(self->speed_buff);
1180 self->speed_buff = NULL;
1181 }
1182}
1183
1184/********************** USB CONFIG SUBROUTINES **********************/
1185/*
1186 * Various subroutines dealing with USB stuff we use to configure and
1187 * initialise each irda-usb instance.
1188 * These functions are used below in the main calls of the driver...
1189 */
1190
1191/*------------------------------------------------------------------*/
1192/*
1193 * Function irda_usb_parse_endpoints(dev, ifnum)
1194 *
1195 * Parse the various endpoints and find the one we need.
1196 *
1197 * The endpoint are the pipes used to communicate with the USB device.
1198 * The spec defines 2 endpoints of type bulk transfer, one in, and one out.
1199 * These are used to pass frames back and forth with the dongle.
1200 * Most dongle have also an interrupt endpoint, that will be probably
1201 * documented in the next spec...
1202 */
1203static inline int irda_usb_parse_endpoints(struct irda_usb_cb *self, struct usb_host_endpoint *endpoint, int ennum)
1204{
1205 int i; /* Endpoint index in table */
1206
1207 /* Init : no endpoints */
1208 self->bulk_in_ep = 0;
1209 self->bulk_out_ep = 0;
1210 self->bulk_int_ep = 0;
1211
1212 /* Let's look at all those endpoints */
1213 for(i = 0; i < ennum; i++) {
1214 /* All those variables will get optimised by the compiler,
1215 * so let's aim for clarity... - Jean II */
1216 __u8 ep; /* Endpoint address */
1217 __u8 dir; /* Endpoint direction */
1218 __u8 attr; /* Endpoint attribute */
1219 __u16 psize; /* Endpoint max packet size in bytes */
1220
1221 /* Get endpoint address, direction and attribute */
1222 ep = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1223 dir = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK;
1224 attr = endpoint[i].desc.bmAttributes;
1225 psize = le16_to_cpu(endpoint[i].desc.wMaxPacketSize);
1226
1227 /* Is it a bulk endpoint ??? */
1228 if(attr == USB_ENDPOINT_XFER_BULK) {
1229 /* We need to find an IN and an OUT */
1230 if(dir == USB_DIR_IN) {
1231 /* This is our Rx endpoint */
1232 self->bulk_in_ep = ep;
1233 } else {
1234 /* This is our Tx endpoint */
1235 self->bulk_out_ep = ep;
1236 self->bulk_out_mtu = psize;
1237 }
1238 } else {
1239 if((attr == USB_ENDPOINT_XFER_INT) &&
1240 (dir == USB_DIR_IN)) {
1241 /* This is our interrupt endpoint */
1242 self->bulk_int_ep = ep;
1243 } else {
1244 IRDA_ERROR("%s(), Unrecognised endpoint %02X.\n", __FUNCTION__, ep);
1245 }
1246 }
1247 }
1248
1249 IRDA_DEBUG(0, "%s(), And our endpoints are : in=%02X, out=%02X (%d), int=%02X\n",
1250 __FUNCTION__, self->bulk_in_ep, self->bulk_out_ep, self->bulk_out_mtu, self->bulk_int_ep);
1251 /* Should be 8, 16, 32 or 64 bytes */
1252 IRDA_ASSERT(self->bulk_out_mtu == 64, ;);
1253
1254 return((self->bulk_in_ep != 0) && (self->bulk_out_ep != 0));
1255}
1256
1257#ifdef IU_DUMP_CLASS_DESC
1258/*------------------------------------------------------------------*/
1259/*
1260 * Function usb_irda_dump_class_desc(desc)
1261 *
1262 * Prints out the contents of the IrDA class descriptor
1263 *
1264 */
1265static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
1266{
1267 /* Values are little endian */
1268 printk("bLength=%x\n", desc->bLength);
1269 printk("bDescriptorType=%x\n", desc->bDescriptorType);
1270 printk("bcdSpecRevision=%x\n", le16_to_cpu(desc->bcdSpecRevision));
1271 printk("bmDataSize=%x\n", desc->bmDataSize);
1272 printk("bmWindowSize=%x\n", desc->bmWindowSize);
1273 printk("bmMinTurnaroundTime=%d\n", desc->bmMinTurnaroundTime);
1274 printk("wBaudRate=%x\n", le16_to_cpu(desc->wBaudRate));
1275 printk("bmAdditionalBOFs=%x\n", desc->bmAdditionalBOFs);
1276 printk("bIrdaRateSniff=%x\n", desc->bIrdaRateSniff);
1277 printk("bMaxUnicastList=%x\n", desc->bMaxUnicastList);
1278}
1279#endif /* IU_DUMP_CLASS_DESC */
1280
1281/*------------------------------------------------------------------*/
1282/*
1283 * Function irda_usb_find_class_desc(intf)
1284 *
1285 * Returns instance of IrDA class descriptor, or NULL if not found
1286 *
1287 * The class descriptor is some extra info that IrDA USB devices will
1288 * offer to us, describing their IrDA characteristics. We will use that in
1289 * irda_usb_init_qos()
1290 */
1291static inline struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf)
1292{
1293 struct usb_device *dev = interface_to_usbdev (intf);
1294 struct irda_class_desc *desc;
1295 int ret;
1296
1297 desc = kmalloc(sizeof (*desc), GFP_KERNEL);
1298 if (desc == NULL)
1299 return NULL;
1300 memset(desc, 0, sizeof(*desc));
1301
1302 /* USB-IrDA class spec 1.0:
1303 * 6.1.3: Standard "Get Descriptor" Device Request is not
1304 * appropriate to retrieve class-specific descriptor
1305 * 6.2.5: Class Specific "Get Class Descriptor" Interface Request
1306 * is mandatory and returns the USB-IrDA class descriptor
1307 */
1308
1309 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
1310 IU_REQ_GET_CLASS_DESC,
1311 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1312 0, intf->altsetting->desc.bInterfaceNumber, desc,
1313 sizeof(*desc), 500);
1314
1315 IRDA_DEBUG(1, "%s(), ret=%d\n", __FUNCTION__, ret);
1316 if (ret < sizeof(*desc)) {
1317 IRDA_WARNING("usb-irda: class_descriptor read %s (%d)\n",
1318 (ret<0) ? "failed" : "too short", ret);
1319 }
1320 else if (desc->bDescriptorType != USB_DT_IRDA) {
1321 IRDA_WARNING("usb-irda: bad class_descriptor type\n");
1322 }
1323 else {
1324#ifdef IU_DUMP_CLASS_DESC
1325 irda_usb_dump_class_desc(desc);
1326#endif /* IU_DUMP_CLASS_DESC */
1327
1328 return desc;
1329 }
1330 kfree(desc);
1331 return NULL;
1332}
1333
1334/*********************** USB DEVICE CALLBACKS ***********************/
1335/*
1336 * Main calls from the USB subsystem.
1337 * Mostly registering a new irda-usb device and removing it....
1338 */
1339
1340/*------------------------------------------------------------------*/
1341/*
1342 * This routine is called by the USB subsystem for each new device
1343 * in the system. We need to check if the device is ours, and in
1344 * this case start handling it.
1345 * The USB layer protect us from reentrancy (via BKL), so we don't need
1346 * to spinlock in there... Jean II
1347 */
1348static int irda_usb_probe(struct usb_interface *intf,
1349 const struct usb_device_id *id)
1350{
1351 struct net_device *net;
1352 struct usb_device *dev = interface_to_usbdev(intf);
1353 struct irda_usb_cb *self = NULL;
1354 struct usb_host_interface *interface;
1355 struct irda_class_desc *irda_desc;
1356 int ret = -ENOMEM;
1357 int i; /* Driver instance index / Rx URB index */
1358
1359 /* Note : the probe make sure to call us only for devices that
1360 * matches the list of dongle (top of the file). So, we
1361 * don't need to check if the dongle is really ours.
1362 * Jean II */
1363
1364 IRDA_MESSAGE("IRDA-USB found at address %d, Vendor: %x, Product: %x\n",
1365 dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
1366 le16_to_cpu(dev->descriptor.idProduct));
1367
1368 net = alloc_irdadev(sizeof(*self));
1369 if (!net)
1370 goto err_out;
1371
1372 SET_MODULE_OWNER(net);
1373 SET_NETDEV_DEV(net, &intf->dev);
1374 self = net->priv;
1375 self->netdev = net;
1376 spin_lock_init(&self->lock);
1377
1378 /* Create all of the needed urbs */
1379 for (i = 0; i < IU_MAX_RX_URBS; i++) {
1380 self->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
1381 if (!self->rx_urb[i]) {
1382 goto err_out_1;
1383 }
1384 }
1385 self->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1386 if (!self->tx_urb) {
1387 goto err_out_1;
1388 }
1389 self->speed_urb = usb_alloc_urb(0, GFP_KERNEL);
1390 if (!self->speed_urb) {
1391 goto err_out_2;
1392 }
1393
1394 /* Is this really necessary? (no, except maybe for broken devices) */
1395 if (usb_reset_configuration (dev) < 0) {
1396 err("reset_configuration failed");
1397 ret = -EIO;
1398 goto err_out_3;
1399 }
1400
1401 /* Is this really necessary? */
1402 /* Note : some driver do hardcode the interface number, some others
1403 * specify an alternate, but very few driver do like this.
1404 * Jean II */
1405 ret = usb_set_interface(dev, intf->altsetting->desc.bInterfaceNumber, 0);
1406 IRDA_DEBUG(1, "usb-irda: set interface %d result %d\n", intf->altsetting->desc.bInterfaceNumber, ret);
1407 switch (ret) {
1408 case 0:
1409 break;
1410 case -EPIPE: /* -EPIPE = -32 */
1411 /* Martin Diehl says if we get a -EPIPE we should
1412 * be fine and we don't need to do a usb_clear_halt().
1413 * - Jean II */
1414 IRDA_DEBUG(0, "%s(), Received -EPIPE, ignoring...\n", __FUNCTION__);
1415 break;
1416 default:
1417 IRDA_DEBUG(0, "%s(), Unknown error %d\n", __FUNCTION__, ret);
1418 ret = -EIO;
1419 goto err_out_3;
1420 }
1421
1422 /* Find our endpoints */
1423 interface = intf->cur_altsetting;
1424 if(!irda_usb_parse_endpoints(self, interface->endpoint,
1425 interface->desc.bNumEndpoints)) {
1426 IRDA_ERROR("%s(), Bogus endpoints...\n", __FUNCTION__);
1427 ret = -EIO;
1428 goto err_out_3;
1429 }
1430
1431 /* Find IrDA class descriptor */
1432 irda_desc = irda_usb_find_class_desc(intf);
1433 ret = -ENODEV;
1434 if (irda_desc == NULL)
1435 goto err_out_3;
1436
1437 self->irda_desc = irda_desc;
1438 self->present = 1;
1439 self->netopen = 0;
1440 self->capability = id->driver_info;
1441 self->usbdev = dev;
1442 self->usbintf = intf;
1443
1444 /* Allocate the buffer for speed changes */
1445 /* Don't change this buffer size and allocation without doing
1446 * some heavy and complete testing. Don't ask why :-(
1447 * Jean II */
1448 self->speed_buff = (char *) kmalloc(IRDA_USB_SPEED_MTU, GFP_KERNEL);
1449 if (self->speed_buff == NULL)
1450 goto err_out_3;
1451
1452 memset(self->speed_buff, 0, IRDA_USB_SPEED_MTU);
1453
1454 ret = irda_usb_open(self);
1455 if (ret)
1456 goto err_out_4;
1457
1458 IRDA_MESSAGE("IrDA: Registered device %s\n", net->name);
1459 usb_set_intfdata(intf, self);
1460 return 0;
1461
1462err_out_4:
1463 kfree(self->speed_buff);
1464err_out_3:
1465 /* Free all urbs that we may have created */
1466 usb_free_urb(self->speed_urb);
1467err_out_2:
1468 usb_free_urb(self->tx_urb);
1469err_out_1:
1470 for (i = 0; i < IU_MAX_RX_URBS; i++) {
1471 if (self->rx_urb[i])
1472 usb_free_urb(self->rx_urb[i]);
1473 }
1474 free_netdev(net);
1475err_out:
1476 return ret;
1477}
1478
1479/*------------------------------------------------------------------*/
1480/*
1481 * The current irda-usb device is removed, the USB layer tell us
1482 * to shut it down...
1483 * One of the constraints is that when we exit this function,
1484 * we cannot use the usb_device no more. Gone. Destroyed. kfree().
1485 * Most other subsystem allow you to destroy the instance at a time
1486 * when it's convenient to you, to postpone it to a later date, but
1487 * not the USB subsystem.
1488 * So, we must make bloody sure that everything gets deactivated.
1489 * Jean II
1490 */
1491static void irda_usb_disconnect(struct usb_interface *intf)
1492{
1493 unsigned long flags;
1494 struct irda_usb_cb *self = usb_get_intfdata(intf);
1495 int i;
1496
1497 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1498
1499 usb_set_intfdata(intf, NULL);
1500 if (!self)
1501 return;
1502
1503 /* Make sure that the Tx path is not executing. - Jean II */
1504 spin_lock_irqsave(&self->lock, flags);
1505
1506 /* Oups ! We are not there any more.
1507 * This will stop/desactivate the Tx path. - Jean II */
1508 self->present = 0;
1509
1510 /* We need to have irq enabled to unlink the URBs. That's OK,
1511 * at this point the Tx path is gone - Jean II */
1512 spin_unlock_irqrestore(&self->lock, flags);
1513
1514 /* Hum... Check if networking is still active (avoid races) */
1515 if((self->netopen) || (self->irlap)) {
1516 /* Accept no more transmissions */
1517 /*netif_device_detach(self->netdev);*/
1518 netif_stop_queue(self->netdev);
1519 /* Stop all the receive URBs */
1520 for (i = 0; i < IU_MAX_RX_URBS; i++)
1521 usb_kill_urb(self->rx_urb[i]);
1522 /* Cancel Tx and speed URB.
1523 * Toggle flags to make sure it's synchronous. */
1524 self->tx_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
1525 usb_kill_urb(self->tx_urb);
1526 self->speed_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
1527 usb_kill_urb(self->speed_urb);
1528 }
1529
1530 /* Cleanup the device stuff */
1531 irda_usb_close(self);
1532 /* No longer attached to USB bus */
1533 self->usbdev = NULL;
1534 self->usbintf = NULL;
1535
1536 /* Clean up our urbs */
1537 for (i = 0; i < IU_MAX_RX_URBS; i++)
1538 usb_free_urb(self->rx_urb[i]);
1539 /* Clean up Tx and speed URB */
1540 usb_free_urb(self->tx_urb);
1541 usb_free_urb(self->speed_urb);
1542
1543 /* Free self and network device */
1544 free_netdev(self->netdev);
1545 IRDA_DEBUG(0, "%s(), USB IrDA Disconnected\n", __FUNCTION__);
1546}
1547
1548/*------------------------------------------------------------------*/
1549/*
1550 * USB device callbacks
1551 */
1552static struct usb_driver irda_driver = {
1553 .owner = THIS_MODULE,
1554 .name = "irda-usb",
1555 .probe = irda_usb_probe,
1556 .disconnect = irda_usb_disconnect,
1557 .id_table = dongles,
1558};
1559
1560/************************* MODULE CALLBACKS *************************/
1561/*
1562 * Deal with module insertion/removal
1563 * Mostly tell USB about our existence
1564 */
1565
1566/*------------------------------------------------------------------*/
1567/*
1568 * Module insertion
1569 */
1570static int __init usb_irda_init(void)
1571{
1572 int ret;
1573
1574 ret = usb_register(&irda_driver);
1575 if (ret < 0)
1576 return ret;
1577
1578 IRDA_MESSAGE("USB IrDA support registered\n");
1579 return 0;
1580}
1581module_init(usb_irda_init);
1582
1583/*------------------------------------------------------------------*/
1584/*
1585 * Module removal
1586 */
1587static void __exit usb_irda_cleanup(void)
1588{
1589 /* Deregister the driver and remove all pending instances */
1590 usb_deregister(&irda_driver);
1591}
1592module_exit(usb_irda_cleanup);
1593
1594/*------------------------------------------------------------------*/
1595/*
1596 * Module parameters
1597 */
1598module_param(qos_mtt_bits, int, 0);
1599MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
1600MODULE_AUTHOR("Roman Weissgaerber <weissg@vienna.at>, Dag Brattli <dag@brattli.net> and Jean Tourrilhes <jt@hpl.hp.com>");
1601MODULE_DESCRIPTION("IrDA-USB Dongle Driver");
1602MODULE_LICENSE("GPL");