blob: c22c0517883c2b8566f3b4c1788cbab6b866feda [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************
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;
Alan Sternb375a042005-07-29 16:11:07 -0400270 urb->transfer_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* This flag (URB_ZERO_PACKET) indicates that what we send is not
405 * a continuous stream of data but separate packets.
406 * In this case, the USB layer will insert an empty USB frame (TD)
407 * after each of our packets that is exact multiple of the frame size.
408 * This is how the dongle will detect the end of packet - Jean II */
Alan Sternb375a042005-07-29 16:11:07 -0400409 urb->transfer_flags = URB_ZERO_PACKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* Generate min turn time. FIXME: can we do better than this? */
412 /* Trying to a turnaround time at this level is trying to measure
413 * processor clock cycle with a wrist-watch, approximate at best...
414 *
415 * What we know is the last time we received a frame over USB.
416 * Due to latency over USB that depend on the USB load, we don't
417 * know when this frame was received over IrDA (a few ms before ?)
418 * Then, same story for our outgoing frame...
419 *
420 * In theory, the USB dongle is supposed to handle the turnaround
421 * by itself (spec 1.0, chater 4, page 6). Who knows ??? That's
422 * why this code is enabled only for dongles that doesn't meet
423 * the spec.
424 * Jean II */
425 if (self->capability & IUC_NO_TURN) {
426 mtt = irda_get_mtt(skb);
427 if (mtt) {
428 int diff;
429 do_gettimeofday(&self->now);
430 diff = self->now.tv_usec - self->stamp.tv_usec;
431#ifdef IU_USB_MIN_RTT
432 /* Factor in USB delays -> Get rid of udelay() that
433 * would be lost in the noise - Jean II */
434 diff += IU_USB_MIN_RTT;
435#endif /* IU_USB_MIN_RTT */
436 /* If the usec counter did wraparound, the diff will
437 * go negative (tv_usec is a long), so we need to
438 * correct it by one second. Jean II */
439 if (diff < 0)
440 diff += 1000000;
441
442 /* Check if the mtt is larger than the time we have
443 * already used by all the protocol processing
444 */
445 if (mtt > diff) {
446 mtt -= diff;
447 if (mtt > 1000)
448 mdelay(mtt/1000);
449 else
450 udelay(mtt);
451 }
452 }
453 }
454
455 /* Ask USB to send the packet - Irq disabled -> GFP_ATOMIC */
456 if ((res = usb_submit_urb(urb, GFP_ATOMIC))) {
457 IRDA_WARNING("%s(), failed Tx URB\n", __FUNCTION__);
458 self->stats.tx_errors++;
459 /* Let USB recover : We will catch that in the watchdog */
460 /*netif_start_queue(netdev);*/
461 } else {
462 /* Increment packet stats */
463 self->stats.tx_packets++;
464 self->stats.tx_bytes += skb->len;
465
466 netdev->trans_start = jiffies;
467 }
468 spin_unlock_irqrestore(&self->lock, flags);
469
470 return 0;
471
472drop:
473 /* Drop silently the skb and exit */
474 dev_kfree_skb(skb);
475 spin_unlock_irqrestore(&self->lock, flags);
476 return err; /* Usually 1 */
477}
478
479/*------------------------------------------------------------------*/
480/*
481 * Note : this function will be called only for tx_urb...
482 */
483static void write_bulk_callback(struct urb *urb, struct pt_regs *regs)
484{
485 unsigned long flags;
486 struct sk_buff *skb = urb->context;
487 struct irda_usb_cb *self = ((struct irda_skb_cb *) skb->cb)->context;
488
489 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
490
491 /* We should always have a context */
492 IRDA_ASSERT(self != NULL, return;);
493 /* We should always be called for the speed URB */
494 IRDA_ASSERT(urb == self->tx_urb, return;);
495
496 /* Free up the skb */
497 dev_kfree_skb_any(skb);
498 urb->context = NULL;
499
500 /* Check for timeout and other USB nasties */
501 if (urb->status != 0) {
502 /* I get a lot of -ECONNABORTED = -103 here - Jean II */
503 IRDA_DEBUG(0, "%s(), URB complete status %d, transfer_flags 0x%04X\n", __FUNCTION__, urb->status, urb->transfer_flags);
504
505 /* Don't do anything here, that might confuse the USB layer,
506 * and we could go in recursion and blow the kernel stack...
507 * Instead, we will wait for irda_usb_net_timeout(), the
508 * network layer watchdog, to fix the situation.
509 * Jean II */
510 /* A reset of the dongle might be welcomed here - Jean II */
511 return;
512 }
513
514 /* urb is now available */
515 //urb->status = 0; -> tested above
516
517 /* Make sure we read self->present properly */
518 spin_lock_irqsave(&self->lock, flags);
519
520 /* If the network is closed, stop everything */
521 if ((!self->netopen) || (!self->present)) {
522 IRDA_DEBUG(0, "%s(), Network is gone...\n", __FUNCTION__);
523 spin_unlock_irqrestore(&self->lock, flags);
524 return;
525 }
526
527 /* If changes to speed or xbofs is pending... */
528 if ((self->new_speed != -1) || (self->new_xbofs != -1)) {
529 if ((self->new_speed != self->speed) ||
530 (self->new_xbofs != self->xbofs)) {
531 /* We haven't changed speed yet (because of
532 * IUC_SPEED_BUG), so do it now - Jean II */
533 IRDA_DEBUG(1, "%s(), Changing speed now...\n", __FUNCTION__);
534 irda_usb_change_speed_xbofs(self);
535 } else {
536 /* New speed and xbof is now commited in hardware */
537 self->new_speed = -1;
538 self->new_xbofs = -1;
539 /* Done, waiting for next packet */
540 netif_wake_queue(self->netdev);
541 }
542 } else {
543 /* Otherwise, allow the stack to send more packets */
544 netif_wake_queue(self->netdev);
545 }
546 spin_unlock_irqrestore(&self->lock, flags);
547}
548
549/*------------------------------------------------------------------*/
550/*
551 * Watchdog timer from the network layer.
552 * After a predetermined timeout, if we don't give confirmation that
553 * the packet has been sent (i.e. no call to netif_wake_queue()),
554 * the network layer will call this function.
555 * Note that URB that we submit have also a timeout. When the URB timeout
556 * expire, the normal URB callback is called (write_bulk_callback()).
557 */
558static void irda_usb_net_timeout(struct net_device *netdev)
559{
560 unsigned long flags;
561 struct irda_usb_cb *self = netdev->priv;
562 struct urb *urb;
563 int done = 0; /* If we have made any progress */
564
565 IRDA_DEBUG(0, "%s(), Network layer thinks we timed out!\n", __FUNCTION__);
566 IRDA_ASSERT(self != NULL, return;);
567
568 /* Protect us from USB callbacks, net Tx and else. */
569 spin_lock_irqsave(&self->lock, flags);
570
571 /* self->present *MUST* be read under spinlock */
572 if (!self->present) {
573 IRDA_WARNING("%s(), device not present!\n", __FUNCTION__);
574 netif_stop_queue(netdev);
575 spin_unlock_irqrestore(&self->lock, flags);
576 return;
577 }
578
579 /* Check speed URB */
580 urb = self->speed_urb;
581 if (urb->status != 0) {
582 IRDA_DEBUG(0, "%s: Speed change timed out, urb->status=%d, urb->transfer_flags=0x%04X\n", netdev->name, urb->status, urb->transfer_flags);
583
584 switch (urb->status) {
585 case -EINPROGRESS:
586 usb_unlink_urb(urb);
587 /* Note : above will *NOT* call netif_wake_queue()
588 * in completion handler, we will come back here.
589 * Jean II */
590 done = 1;
591 break;
592 case -ECONNABORTED: /* -103 */
593 case -ECONNRESET: /* -104 */
594 case -ETIMEDOUT: /* -110 */
595 case -ENOENT: /* -2 (urb unlinked by us) */
596 default: /* ??? - Play safe */
597 urb->status = 0;
598 netif_wake_queue(self->netdev);
599 done = 1;
600 break;
601 }
602 }
603
604 /* Check Tx URB */
605 urb = self->tx_urb;
606 if (urb->status != 0) {
607 struct sk_buff *skb = urb->context;
608
609 IRDA_DEBUG(0, "%s: Tx timed out, urb->status=%d, urb->transfer_flags=0x%04X\n", netdev->name, urb->status, urb->transfer_flags);
610
611 /* Increase error count */
612 self->stats.tx_errors++;
613
614#ifdef IU_BUG_KICK_TIMEOUT
615 /* Can't be a bad idea to reset the speed ;-) - Jean II */
616 if(self->new_speed == -1)
617 self->new_speed = self->speed;
618 if(self->new_xbofs == -1)
619 self->new_xbofs = self->xbofs;
620 irda_usb_change_speed_xbofs(self);
621#endif /* IU_BUG_KICK_TIMEOUT */
622
623 switch (urb->status) {
624 case -EINPROGRESS:
625 usb_unlink_urb(urb);
626 /* Note : above will *NOT* call netif_wake_queue()
627 * in completion handler, because urb->status will
628 * be -ENOENT. We will fix that at the next watchdog,
629 * leaving more time to USB to recover...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 * Jean II */
631 done = 1;
632 break;
633 case -ECONNABORTED: /* -103 */
634 case -ECONNRESET: /* -104 */
635 case -ETIMEDOUT: /* -110 */
636 case -ENOENT: /* -2 (urb unlinked by us) */
637 default: /* ??? - Play safe */
638 if(skb != NULL) {
639 dev_kfree_skb_any(skb);
640 urb->context = NULL;
641 }
642 urb->status = 0;
643 netif_wake_queue(self->netdev);
644 done = 1;
645 break;
646 }
647 }
648 spin_unlock_irqrestore(&self->lock, flags);
649
650 /* Maybe we need a reset */
651 /* Note : Some drivers seem to use a usb_set_interface() when they
652 * need to reset the hardware. Hum...
653 */
654
655 /* if(done == 0) */
656}
657
658/************************* RECEIVE ROUTINES *************************/
659/*
660 * Receive packets from the USB layer stack and pass them to the IrDA stack.
661 * Try to work around USB failures...
662 */
663
664/*
665 * Note :
666 * Some of you may have noticed that most dongle have an interrupt in pipe
667 * that we don't use. Here is the little secret...
668 * When we hang a Rx URB on the bulk in pipe, it generates some USB traffic
669 * in every USB frame. This is unnecessary overhead.
670 * The interrupt in pipe will generate an event every time a packet is
671 * received. Reading an interrupt pipe adds minimal overhead, but has some
672 * latency (~1ms).
673 * If we are connected (speed != 9600), we want to minimise latency, so
674 * we just always hang the Rx URB and ignore the interrupt.
675 * If we are not connected (speed == 9600), there is usually no Rx traffic,
676 * and we want to minimise the USB overhead. In this case we should wait
677 * on the interrupt pipe and hang the Rx URB only when an interrupt is
678 * received.
679 * Jean II
680 */
681
682/*------------------------------------------------------------------*/
683/*
684 * Submit a Rx URB to the USB layer to handle reception of a frame
685 * Mostly called by the completion callback of the previous URB.
686 *
687 * Jean II
688 */
689static void irda_usb_submit(struct irda_usb_cb *self, struct sk_buff *skb, struct urb *urb)
690{
691 struct irda_skb_cb *cb;
692 int ret;
693
694 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
695
696 /* This should never happen */
697 IRDA_ASSERT(skb != NULL, return;);
698 IRDA_ASSERT(urb != NULL, return;);
699
700 /* Save ourselves in the skb */
701 cb = (struct irda_skb_cb *) skb->cb;
702 cb->context = self;
703
704 /* Reinitialize URB */
705 usb_fill_bulk_urb(urb, self->usbdev,
706 usb_rcvbulkpipe(self->usbdev, self->bulk_in_ep),
707 skb->data, skb->truesize,
708 irda_usb_receive, skb);
709 /* Note : unlink *must* be synchronous because of the code in
710 * irda_usb_net_close() -> free the skb - Jean II */
711 urb->status = 0;
712
713 /* Can be called from irda_usb_receive (irq handler) -> GFP_ATOMIC */
714 ret = usb_submit_urb(urb, GFP_ATOMIC);
715 if (ret) {
716 /* If this ever happen, we are in deep s***.
717 * Basically, the Rx path will stop... */
718 IRDA_WARNING("%s(), Failed to submit Rx URB %d\n",
719 __FUNCTION__, ret);
720 }
721}
722
723/*------------------------------------------------------------------*/
724/*
725 * Function irda_usb_receive(urb)
726 *
727 * Called by the USB subsystem when a frame has been received
728 *
729 */
730static void irda_usb_receive(struct urb *urb, struct pt_regs *regs)
731{
732 struct sk_buff *skb = (struct sk_buff *) urb->context;
733 struct irda_usb_cb *self;
734 struct irda_skb_cb *cb;
735 struct sk_buff *newskb;
736 struct sk_buff *dataskb;
737 int docopy;
738
739 IRDA_DEBUG(2, "%s(), len=%d\n", __FUNCTION__, urb->actual_length);
740
741 /* Find ourselves */
742 cb = (struct irda_skb_cb *) skb->cb;
743 IRDA_ASSERT(cb != NULL, return;);
744 self = (struct irda_usb_cb *) cb->context;
745 IRDA_ASSERT(self != NULL, return;);
746
747 /* If the network is closed or the device gone, stop everything */
748 if ((!self->netopen) || (!self->present)) {
749 IRDA_DEBUG(0, "%s(), Network is gone!\n", __FUNCTION__);
750 /* Don't re-submit the URB : will stall the Rx path */
751 return;
752 }
753
754 /* Check the status */
755 if (urb->status != 0) {
756 switch (urb->status) {
757 case -EILSEQ:
758 self->stats.rx_errors++;
759 self->stats.rx_crc_errors++;
760 break;
761 case -ECONNRESET: /* -104 */
762 IRDA_DEBUG(0, "%s(), Connection Reset (-104), transfer_flags 0x%04X \n", __FUNCTION__, urb->transfer_flags);
763 /* uhci_cleanup_unlink() is going to kill the Rx
764 * URB just after we return. No problem, at this
765 * point the URB will be idle ;-) - Jean II */
766 break;
767 default:
768 IRDA_DEBUG(0, "%s(), RX status %d,transfer_flags 0x%04X \n", __FUNCTION__, urb->status, urb->transfer_flags);
769 break;
770 }
771 goto done;
772 }
773
774 /* Check for empty frames */
775 if (urb->actual_length <= USB_IRDA_HEADER) {
776 IRDA_WARNING("%s(), empty frame!\n", __FUNCTION__);
777 goto done;
778 }
779
780 /*
781 * Remember the time we received this frame, so we can
782 * reduce the min turn time a bit since we will know
783 * how much time we have used for protocol processing
784 */
785 do_gettimeofday(&self->stamp);
786
787 /* Check if we need to copy the data to a new skb or not.
788 * For most frames, we use ZeroCopy and pass the already
789 * allocated skb up the stack.
790 * If the frame is small, it is more efficient to copy it
791 * to save memory (copy will be fast anyway - that's
792 * called Rx-copy-break). Jean II */
793 docopy = (urb->actual_length < IRDA_RX_COPY_THRESHOLD);
794
795 /* Allocate a new skb */
796 newskb = dev_alloc_skb(docopy ? urb->actual_length : IRDA_SKB_MAX_MTU);
797 if (!newskb) {
798 self->stats.rx_dropped++;
799 /* We could deliver the current skb, but this would stall
800 * the Rx path. Better drop the packet... Jean II */
801 goto done;
802 }
803
804 /* Make sure IP header get aligned (IrDA header is 5 bytes) */
805 /* But IrDA-USB header is 1 byte. Jean II */
806 //skb_reserve(newskb, USB_IRDA_HEADER - 1);
807
808 if(docopy) {
809 /* Copy packet, so we can recycle the original */
810 memcpy(newskb->data, skb->data, urb->actual_length);
811 /* Deliver this new skb */
812 dataskb = newskb;
813 /* And hook the old skb to the URB
814 * Note : we don't need to "clean up" the old skb,
815 * as we never touched it. Jean II */
816 } else {
817 /* We are using ZeroCopy. Deliver old skb */
818 dataskb = skb;
819 /* And hook the new skb to the URB */
820 skb = newskb;
821 }
822
823 /* Set proper length on skb & remove USB-IrDA header */
824 skb_put(dataskb, urb->actual_length);
825 skb_pull(dataskb, USB_IRDA_HEADER);
826
827 /* Ask the networking layer to queue the packet for the IrDA stack */
828 dataskb->dev = self->netdev;
829 dataskb->mac.raw = dataskb->data;
830 dataskb->protocol = htons(ETH_P_IRDA);
831 netif_rx(dataskb);
832
833 /* Keep stats up to date */
834 self->stats.rx_bytes += dataskb->len;
835 self->stats.rx_packets++;
836 self->netdev->last_rx = jiffies;
837
838done:
839 /* Note : at this point, the URB we've just received (urb)
840 * is still referenced by the USB layer. For example, if we
841 * have received a -ECONNRESET, uhci_cleanup_unlink() will
842 * continue to process it (in fact, cleaning it up).
843 * If we were to submit this URB, disaster would ensue.
844 * Therefore, we submit our idle URB, and put this URB in our
845 * idle slot....
846 * Jean II */
847 /* Note : with this scheme, we could submit the idle URB before
848 * processing the Rx URB. Another time... Jean II */
849
850 /* Submit the idle URB to replace the URB we've just received */
851 irda_usb_submit(self, skb, self->idle_rx_urb);
852 /* Recycle Rx URB : Now, the idle URB is the present one */
853 urb->context = NULL;
854 self->idle_rx_urb = urb;
855}
856
857/*------------------------------------------------------------------*/
858/*
859 * Callbak from IrDA layer. IrDA wants to know if we have
860 * started receiving anything.
861 */
862static int irda_usb_is_receiving(struct irda_usb_cb *self)
863{
864 /* Note : because of the way UHCI works, it's almost impossible
865 * to get this info. The Controller DMA directly to memory and
866 * signal only when the whole frame is finished. To know if the
867 * first TD of the URB has been filled or not seems hard work...
868 *
869 * The other solution would be to use the "receiving" command
870 * on the default decriptor with a usb_control_msg(), but that
871 * would add USB traffic and would return result only in the
872 * next USB frame (~1ms).
873 *
874 * I've been told that current dongles send status info on their
875 * interrupt endpoint, and that's what the Windows driver uses
876 * to know this info. Unfortunately, this is not yet in the spec...
877 *
878 * Jean II
879 */
880
881 return 0; /* For now */
882}
883
884/********************** IRDA DEVICE CALLBACKS **********************/
885/*
886 * Main calls from the IrDA/Network subsystem.
887 * Mostly registering a new irda-usb device and removing it....
888 * We only deal with the IrDA side of the business, the USB side will
889 * be dealt with below...
890 */
891
892
893/*------------------------------------------------------------------*/
894/*
895 * Function irda_usb_net_open (dev)
896 *
897 * Network device is taken up. Usually this is done by "ifconfig irda0 up"
898 *
899 * Note : don't mess with self->netopen - Jean II
900 */
901static int irda_usb_net_open(struct net_device *netdev)
902{
903 struct irda_usb_cb *self;
904 char hwname[16];
905 int i;
906
907 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
908
909 IRDA_ASSERT(netdev != NULL, return -1;);
910 self = (struct irda_usb_cb *) netdev->priv;
911 IRDA_ASSERT(self != NULL, return -1;);
912
913 /* Can only open the device if it's there */
914 if(!self->present) {
915 IRDA_WARNING("%s(), device not present!\n", __FUNCTION__);
916 return -1;
917 }
918
919 /* Initialise default speed and xbofs value
920 * (IrLAP will change that soon) */
921 self->speed = -1;
922 self->xbofs = -1;
923 self->new_speed = -1;
924 self->new_xbofs = -1;
925
926 /* To do *before* submitting Rx urbs and starting net Tx queue
927 * Jean II */
928 self->netopen = 1;
929
930 /*
931 * Now that everything should be initialized properly,
932 * Open new IrLAP layer instance to take care of us...
933 * Note : will send immediately a speed change...
934 */
935 sprintf(hwname, "usb#%d", self->usbdev->devnum);
936 self->irlap = irlap_open(netdev, &self->qos, hwname);
937 IRDA_ASSERT(self->irlap != NULL, return -1;);
938
939 /* Allow IrLAP to send data to us */
940 netif_start_queue(netdev);
941
942 /* We submit all the Rx URB except for one that we keep idle.
943 * Need to be initialised before submitting other USBs, because
944 * in some cases as soon as we submit the URBs the USB layer
945 * will trigger a dummy receive - Jean II */
946 self->idle_rx_urb = self->rx_urb[IU_MAX_ACTIVE_RX_URBS];
947 self->idle_rx_urb->context = NULL;
948
949 /* Now that we can pass data to IrLAP, allow the USB layer
950 * to send us some data... */
951 for (i = 0; i < IU_MAX_ACTIVE_RX_URBS; i++) {
952 struct sk_buff *skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
953 if (!skb) {
954 /* If this ever happen, we are in deep s***.
955 * Basically, we can't start the Rx path... */
956 IRDA_WARNING("%s(), Failed to allocate Rx skb\n",
957 __FUNCTION__);
958 return -1;
959 }
960 //skb_reserve(newskb, USB_IRDA_HEADER - 1);
961 irda_usb_submit(self, skb, self->rx_urb[i]);
962 }
963
964 /* Ready to play !!! */
965 return 0;
966}
967
968/*------------------------------------------------------------------*/
969/*
970 * Function irda_usb_net_close (self)
971 *
972 * Network device is taken down. Usually this is done by
973 * "ifconfig irda0 down"
974 */
975static int irda_usb_net_close(struct net_device *netdev)
976{
977 struct irda_usb_cb *self;
978 int i;
979
980 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
981
982 IRDA_ASSERT(netdev != NULL, return -1;);
983 self = (struct irda_usb_cb *) netdev->priv;
984 IRDA_ASSERT(self != NULL, return -1;);
985
986 /* Clear this flag *before* unlinking the urbs and *before*
987 * stopping the network Tx queue - Jean II */
988 self->netopen = 0;
989
990 /* Stop network Tx queue */
991 netif_stop_queue(netdev);
992
993 /* Deallocate all the Rx path buffers (URBs and skb) */
994 for (i = 0; i < IU_MAX_RX_URBS; i++) {
995 struct urb *urb = self->rx_urb[i];
996 struct sk_buff *skb = (struct sk_buff *) urb->context;
997 /* Cancel the receive command */
998 usb_kill_urb(urb);
999 /* The skb is ours, free it */
1000 if(skb) {
1001 dev_kfree_skb(skb);
1002 urb->context = NULL;
1003 }
1004 }
1005 /* Cancel Tx and speed URB - need to be synchronous to avoid races */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 usb_kill_urb(self->tx_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 usb_kill_urb(self->speed_urb);
1008
1009 /* Stop and remove instance of IrLAP */
1010 if (self->irlap)
1011 irlap_close(self->irlap);
1012 self->irlap = NULL;
1013
1014 return 0;
1015}
1016
1017/*------------------------------------------------------------------*/
1018/*
1019 * IOCTLs : Extra out-of-band network commands...
1020 */
1021static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1022{
1023 unsigned long flags;
1024 struct if_irda_req *irq = (struct if_irda_req *) rq;
1025 struct irda_usb_cb *self;
1026 int ret = 0;
1027
1028 IRDA_ASSERT(dev != NULL, return -1;);
1029 self = dev->priv;
1030 IRDA_ASSERT(self != NULL, return -1;);
1031
1032 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
1033
1034 switch (cmd) {
1035 case SIOCSBANDWIDTH: /* Set bandwidth */
1036 if (!capable(CAP_NET_ADMIN))
1037 return -EPERM;
1038 /* Protect us from USB callbacks, net watchdog and else. */
1039 spin_lock_irqsave(&self->lock, flags);
1040 /* Check if the device is still there */
1041 if(self->present) {
1042 /* Set the desired speed */
1043 self->new_speed = irq->ifr_baudrate;
1044 irda_usb_change_speed_xbofs(self);
1045 }
1046 spin_unlock_irqrestore(&self->lock, flags);
1047 break;
1048 case SIOCSMEDIABUSY: /* Set media busy */
1049 if (!capable(CAP_NET_ADMIN))
1050 return -EPERM;
1051 /* Check if the IrDA stack is still there */
1052 if(self->netopen)
1053 irda_device_set_media_busy(self->netdev, TRUE);
1054 break;
1055 case SIOCGRECEIVING: /* Check if we are receiving right now */
1056 irq->ifr_receiving = irda_usb_is_receiving(self);
1057 break;
1058 default:
1059 ret = -EOPNOTSUPP;
1060 }
1061
1062 return ret;
1063}
1064
1065/*------------------------------------------------------------------*/
1066/*
1067 * Get device stats (for /proc/net/dev and ifconfig)
1068 */
1069static struct net_device_stats *irda_usb_net_get_stats(struct net_device *dev)
1070{
1071 struct irda_usb_cb *self = dev->priv;
1072 return &self->stats;
1073}
1074
1075/********************* IRDA CONFIG SUBROUTINES *********************/
1076/*
1077 * Various subroutines dealing with IrDA and network stuff we use to
1078 * configure and initialise each irda-usb instance.
1079 * These functions are used below in the main calls of the driver...
1080 */
1081
1082/*------------------------------------------------------------------*/
1083/*
1084 * Set proper values in the IrDA QOS structure
1085 */
1086static inline void irda_usb_init_qos(struct irda_usb_cb *self)
1087{
1088 struct irda_class_desc *desc;
1089
1090 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
1091
1092 desc = self->irda_desc;
1093
1094 /* Initialize QoS for this device */
1095 irda_init_max_qos_capabilies(&self->qos);
1096
1097 /* See spec section 7.2 for meaning.
1098 * Values are little endian (as most USB stuff), the IrDA stack
1099 * use it in native order (see parameters.c). - Jean II */
1100 self->qos.baud_rate.bits = le16_to_cpu(desc->wBaudRate);
1101 self->qos.min_turn_time.bits = desc->bmMinTurnaroundTime;
1102 self->qos.additional_bofs.bits = desc->bmAdditionalBOFs;
1103 self->qos.window_size.bits = desc->bmWindowSize;
1104 self->qos.data_size.bits = desc->bmDataSize;
1105
1106 IRDA_DEBUG(0, "%s(), dongle says speed=0x%X, size=0x%X, window=0x%X, bofs=0x%X, turn=0x%X\n",
1107 __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);
1108
1109 /* Don't always trust what the dongle tell us */
1110 if(self->capability & IUC_SIR_ONLY)
1111 self->qos.baud_rate.bits &= 0x00ff;
1112 if(self->capability & IUC_SMALL_PKT)
1113 self->qos.data_size.bits = 0x07;
1114 if(self->capability & IUC_NO_WINDOW)
1115 self->qos.window_size.bits = 0x01;
1116 if(self->capability & IUC_MAX_WINDOW)
1117 self->qos.window_size.bits = 0x7f;
1118 if(self->capability & IUC_MAX_XBOFS)
1119 self->qos.additional_bofs.bits = 0x01;
1120
1121#if 1
1122 /* Module parameter can override the rx window size */
1123 if (qos_mtt_bits)
1124 self->qos.min_turn_time.bits = qos_mtt_bits;
1125#endif
1126 /*
1127 * Note : most of those values apply only for the receive path,
1128 * the transmit path will be set differently - Jean II
1129 */
1130 irda_qos_bits_to_value(&self->qos);
1131}
1132
1133/*------------------------------------------------------------------*/
1134/*
1135 * Initialise the network side of the irda-usb instance
1136 * Called when a new USB instance is registered in irda_usb_probe()
1137 */
1138static inline int irda_usb_open(struct irda_usb_cb *self)
1139{
1140 struct net_device *netdev = self->netdev;
1141
1142 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1143
1144 irda_usb_init_qos(self);
1145
1146 /* Override the network functions we need to use */
1147 netdev->hard_start_xmit = irda_usb_hard_xmit;
1148 netdev->tx_timeout = irda_usb_net_timeout;
1149 netdev->watchdog_timeo = 250*HZ/1000; /* 250 ms > USB timeout */
1150 netdev->open = irda_usb_net_open;
1151 netdev->stop = irda_usb_net_close;
1152 netdev->get_stats = irda_usb_net_get_stats;
1153 netdev->do_ioctl = irda_usb_net_ioctl;
1154
1155 return register_netdev(netdev);
1156}
1157
1158/*------------------------------------------------------------------*/
1159/*
1160 * Cleanup the network side of the irda-usb instance
1161 * Called when a USB instance is removed in irda_usb_disconnect()
1162 */
1163static inline void irda_usb_close(struct irda_usb_cb *self)
1164{
1165 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1166
1167 /* Remove netdevice */
1168 unregister_netdev(self->netdev);
1169
1170 /* Remove the speed buffer */
Jesper Juhlb4558ea2005-10-28 16:53:13 -04001171 kfree(self->speed_buff);
1172 self->speed_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175/********************** USB CONFIG SUBROUTINES **********************/
1176/*
1177 * Various subroutines dealing with USB stuff we use to configure and
1178 * initialise each irda-usb instance.
1179 * These functions are used below in the main calls of the driver...
1180 */
1181
1182/*------------------------------------------------------------------*/
1183/*
1184 * Function irda_usb_parse_endpoints(dev, ifnum)
1185 *
1186 * Parse the various endpoints and find the one we need.
1187 *
1188 * The endpoint are the pipes used to communicate with the USB device.
1189 * The spec defines 2 endpoints of type bulk transfer, one in, and one out.
1190 * These are used to pass frames back and forth with the dongle.
1191 * Most dongle have also an interrupt endpoint, that will be probably
1192 * documented in the next spec...
1193 */
1194static inline int irda_usb_parse_endpoints(struct irda_usb_cb *self, struct usb_host_endpoint *endpoint, int ennum)
1195{
1196 int i; /* Endpoint index in table */
1197
1198 /* Init : no endpoints */
1199 self->bulk_in_ep = 0;
1200 self->bulk_out_ep = 0;
1201 self->bulk_int_ep = 0;
1202
1203 /* Let's look at all those endpoints */
1204 for(i = 0; i < ennum; i++) {
1205 /* All those variables will get optimised by the compiler,
1206 * so let's aim for clarity... - Jean II */
1207 __u8 ep; /* Endpoint address */
1208 __u8 dir; /* Endpoint direction */
1209 __u8 attr; /* Endpoint attribute */
1210 __u16 psize; /* Endpoint max packet size in bytes */
1211
1212 /* Get endpoint address, direction and attribute */
1213 ep = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1214 dir = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK;
1215 attr = endpoint[i].desc.bmAttributes;
1216 psize = le16_to_cpu(endpoint[i].desc.wMaxPacketSize);
1217
1218 /* Is it a bulk endpoint ??? */
1219 if(attr == USB_ENDPOINT_XFER_BULK) {
1220 /* We need to find an IN and an OUT */
1221 if(dir == USB_DIR_IN) {
1222 /* This is our Rx endpoint */
1223 self->bulk_in_ep = ep;
1224 } else {
1225 /* This is our Tx endpoint */
1226 self->bulk_out_ep = ep;
1227 self->bulk_out_mtu = psize;
1228 }
1229 } else {
1230 if((attr == USB_ENDPOINT_XFER_INT) &&
1231 (dir == USB_DIR_IN)) {
1232 /* This is our interrupt endpoint */
1233 self->bulk_int_ep = ep;
1234 } else {
1235 IRDA_ERROR("%s(), Unrecognised endpoint %02X.\n", __FUNCTION__, ep);
1236 }
1237 }
1238 }
1239
1240 IRDA_DEBUG(0, "%s(), And our endpoints are : in=%02X, out=%02X (%d), int=%02X\n",
1241 __FUNCTION__, self->bulk_in_ep, self->bulk_out_ep, self->bulk_out_mtu, self->bulk_int_ep);
1242 /* Should be 8, 16, 32 or 64 bytes */
1243 IRDA_ASSERT(self->bulk_out_mtu == 64, ;);
1244
1245 return((self->bulk_in_ep != 0) && (self->bulk_out_ep != 0));
1246}
1247
1248#ifdef IU_DUMP_CLASS_DESC
1249/*------------------------------------------------------------------*/
1250/*
1251 * Function usb_irda_dump_class_desc(desc)
1252 *
1253 * Prints out the contents of the IrDA class descriptor
1254 *
1255 */
1256static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
1257{
1258 /* Values are little endian */
1259 printk("bLength=%x\n", desc->bLength);
1260 printk("bDescriptorType=%x\n", desc->bDescriptorType);
1261 printk("bcdSpecRevision=%x\n", le16_to_cpu(desc->bcdSpecRevision));
1262 printk("bmDataSize=%x\n", desc->bmDataSize);
1263 printk("bmWindowSize=%x\n", desc->bmWindowSize);
1264 printk("bmMinTurnaroundTime=%d\n", desc->bmMinTurnaroundTime);
1265 printk("wBaudRate=%x\n", le16_to_cpu(desc->wBaudRate));
1266 printk("bmAdditionalBOFs=%x\n", desc->bmAdditionalBOFs);
1267 printk("bIrdaRateSniff=%x\n", desc->bIrdaRateSniff);
1268 printk("bMaxUnicastList=%x\n", desc->bMaxUnicastList);
1269}
1270#endif /* IU_DUMP_CLASS_DESC */
1271
1272/*------------------------------------------------------------------*/
1273/*
1274 * Function irda_usb_find_class_desc(intf)
1275 *
1276 * Returns instance of IrDA class descriptor, or NULL if not found
1277 *
1278 * The class descriptor is some extra info that IrDA USB devices will
1279 * offer to us, describing their IrDA characteristics. We will use that in
1280 * irda_usb_init_qos()
1281 */
1282static inline struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf)
1283{
1284 struct usb_device *dev = interface_to_usbdev (intf);
1285 struct irda_class_desc *desc;
1286 int ret;
1287
1288 desc = kmalloc(sizeof (*desc), GFP_KERNEL);
1289 if (desc == NULL)
1290 return NULL;
1291 memset(desc, 0, sizeof(*desc));
1292
1293 /* USB-IrDA class spec 1.0:
1294 * 6.1.3: Standard "Get Descriptor" Device Request is not
1295 * appropriate to retrieve class-specific descriptor
1296 * 6.2.5: Class Specific "Get Class Descriptor" Interface Request
1297 * is mandatory and returns the USB-IrDA class descriptor
1298 */
1299
1300 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
1301 IU_REQ_GET_CLASS_DESC,
1302 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1303 0, intf->altsetting->desc.bInterfaceNumber, desc,
1304 sizeof(*desc), 500);
1305
1306 IRDA_DEBUG(1, "%s(), ret=%d\n", __FUNCTION__, ret);
1307 if (ret < sizeof(*desc)) {
1308 IRDA_WARNING("usb-irda: class_descriptor read %s (%d)\n",
1309 (ret<0) ? "failed" : "too short", ret);
1310 }
1311 else if (desc->bDescriptorType != USB_DT_IRDA) {
1312 IRDA_WARNING("usb-irda: bad class_descriptor type\n");
1313 }
1314 else {
1315#ifdef IU_DUMP_CLASS_DESC
1316 irda_usb_dump_class_desc(desc);
1317#endif /* IU_DUMP_CLASS_DESC */
1318
1319 return desc;
1320 }
1321 kfree(desc);
1322 return NULL;
1323}
1324
1325/*********************** USB DEVICE CALLBACKS ***********************/
1326/*
1327 * Main calls from the USB subsystem.
1328 * Mostly registering a new irda-usb device and removing it....
1329 */
1330
1331/*------------------------------------------------------------------*/
1332/*
1333 * This routine is called by the USB subsystem for each new device
1334 * in the system. We need to check if the device is ours, and in
1335 * this case start handling it.
1336 * The USB layer protect us from reentrancy (via BKL), so we don't need
1337 * to spinlock in there... Jean II
1338 */
1339static int irda_usb_probe(struct usb_interface *intf,
1340 const struct usb_device_id *id)
1341{
1342 struct net_device *net;
1343 struct usb_device *dev = interface_to_usbdev(intf);
1344 struct irda_usb_cb *self = NULL;
1345 struct usb_host_interface *interface;
1346 struct irda_class_desc *irda_desc;
1347 int ret = -ENOMEM;
1348 int i; /* Driver instance index / Rx URB index */
1349
1350 /* Note : the probe make sure to call us only for devices that
1351 * matches the list of dongle (top of the file). So, we
1352 * don't need to check if the dongle is really ours.
1353 * Jean II */
1354
1355 IRDA_MESSAGE("IRDA-USB found at address %d, Vendor: %x, Product: %x\n",
1356 dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
1357 le16_to_cpu(dev->descriptor.idProduct));
1358
1359 net = alloc_irdadev(sizeof(*self));
1360 if (!net)
1361 goto err_out;
1362
1363 SET_MODULE_OWNER(net);
1364 SET_NETDEV_DEV(net, &intf->dev);
1365 self = net->priv;
1366 self->netdev = net;
1367 spin_lock_init(&self->lock);
1368
1369 /* Create all of the needed urbs */
1370 for (i = 0; i < IU_MAX_RX_URBS; i++) {
1371 self->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
1372 if (!self->rx_urb[i]) {
1373 goto err_out_1;
1374 }
1375 }
1376 self->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1377 if (!self->tx_urb) {
1378 goto err_out_1;
1379 }
1380 self->speed_urb = usb_alloc_urb(0, GFP_KERNEL);
1381 if (!self->speed_urb) {
1382 goto err_out_2;
1383 }
1384
1385 /* Is this really necessary? (no, except maybe for broken devices) */
1386 if (usb_reset_configuration (dev) < 0) {
1387 err("reset_configuration failed");
1388 ret = -EIO;
1389 goto err_out_3;
1390 }
1391
1392 /* Is this really necessary? */
1393 /* Note : some driver do hardcode the interface number, some others
1394 * specify an alternate, but very few driver do like this.
1395 * Jean II */
1396 ret = usb_set_interface(dev, intf->altsetting->desc.bInterfaceNumber, 0);
1397 IRDA_DEBUG(1, "usb-irda: set interface %d result %d\n", intf->altsetting->desc.bInterfaceNumber, ret);
1398 switch (ret) {
1399 case 0:
1400 break;
1401 case -EPIPE: /* -EPIPE = -32 */
1402 /* Martin Diehl says if we get a -EPIPE we should
1403 * be fine and we don't need to do a usb_clear_halt().
1404 * - Jean II */
1405 IRDA_DEBUG(0, "%s(), Received -EPIPE, ignoring...\n", __FUNCTION__);
1406 break;
1407 default:
1408 IRDA_DEBUG(0, "%s(), Unknown error %d\n", __FUNCTION__, ret);
1409 ret = -EIO;
1410 goto err_out_3;
1411 }
1412
1413 /* Find our endpoints */
1414 interface = intf->cur_altsetting;
1415 if(!irda_usb_parse_endpoints(self, interface->endpoint,
1416 interface->desc.bNumEndpoints)) {
1417 IRDA_ERROR("%s(), Bogus endpoints...\n", __FUNCTION__);
1418 ret = -EIO;
1419 goto err_out_3;
1420 }
1421
1422 /* Find IrDA class descriptor */
1423 irda_desc = irda_usb_find_class_desc(intf);
1424 ret = -ENODEV;
1425 if (irda_desc == NULL)
1426 goto err_out_3;
1427
1428 self->irda_desc = irda_desc;
1429 self->present = 1;
1430 self->netopen = 0;
1431 self->capability = id->driver_info;
1432 self->usbdev = dev;
1433 self->usbintf = intf;
1434
1435 /* Allocate the buffer for speed changes */
1436 /* Don't change this buffer size and allocation without doing
1437 * some heavy and complete testing. Don't ask why :-(
1438 * Jean II */
1439 self->speed_buff = (char *) kmalloc(IRDA_USB_SPEED_MTU, GFP_KERNEL);
1440 if (self->speed_buff == NULL)
1441 goto err_out_3;
1442
1443 memset(self->speed_buff, 0, IRDA_USB_SPEED_MTU);
1444
1445 ret = irda_usb_open(self);
1446 if (ret)
1447 goto err_out_4;
1448
1449 IRDA_MESSAGE("IrDA: Registered device %s\n", net->name);
1450 usb_set_intfdata(intf, self);
1451 return 0;
1452
1453err_out_4:
1454 kfree(self->speed_buff);
1455err_out_3:
1456 /* Free all urbs that we may have created */
1457 usb_free_urb(self->speed_urb);
1458err_out_2:
1459 usb_free_urb(self->tx_urb);
1460err_out_1:
1461 for (i = 0; i < IU_MAX_RX_URBS; i++) {
1462 if (self->rx_urb[i])
1463 usb_free_urb(self->rx_urb[i]);
1464 }
1465 free_netdev(net);
1466err_out:
1467 return ret;
1468}
1469
1470/*------------------------------------------------------------------*/
1471/*
1472 * The current irda-usb device is removed, the USB layer tell us
1473 * to shut it down...
1474 * One of the constraints is that when we exit this function,
1475 * we cannot use the usb_device no more. Gone. Destroyed. kfree().
1476 * Most other subsystem allow you to destroy the instance at a time
1477 * when it's convenient to you, to postpone it to a later date, but
1478 * not the USB subsystem.
1479 * So, we must make bloody sure that everything gets deactivated.
1480 * Jean II
1481 */
1482static void irda_usb_disconnect(struct usb_interface *intf)
1483{
1484 unsigned long flags;
1485 struct irda_usb_cb *self = usb_get_intfdata(intf);
1486 int i;
1487
1488 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1489
1490 usb_set_intfdata(intf, NULL);
1491 if (!self)
1492 return;
1493
1494 /* Make sure that the Tx path is not executing. - Jean II */
1495 spin_lock_irqsave(&self->lock, flags);
1496
1497 /* Oups ! We are not there any more.
1498 * This will stop/desactivate the Tx path. - Jean II */
1499 self->present = 0;
1500
1501 /* We need to have irq enabled to unlink the URBs. That's OK,
1502 * at this point the Tx path is gone - Jean II */
1503 spin_unlock_irqrestore(&self->lock, flags);
1504
1505 /* Hum... Check if networking is still active (avoid races) */
1506 if((self->netopen) || (self->irlap)) {
1507 /* Accept no more transmissions */
1508 /*netif_device_detach(self->netdev);*/
1509 netif_stop_queue(self->netdev);
1510 /* Stop all the receive URBs */
1511 for (i = 0; i < IU_MAX_RX_URBS; i++)
1512 usb_kill_urb(self->rx_urb[i]);
1513 /* Cancel Tx and speed URB.
1514 * Toggle flags to make sure it's synchronous. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 usb_kill_urb(self->tx_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 usb_kill_urb(self->speed_urb);
1517 }
1518
1519 /* Cleanup the device stuff */
1520 irda_usb_close(self);
1521 /* No longer attached to USB bus */
1522 self->usbdev = NULL;
1523 self->usbintf = NULL;
1524
1525 /* Clean up our urbs */
1526 for (i = 0; i < IU_MAX_RX_URBS; i++)
1527 usb_free_urb(self->rx_urb[i]);
1528 /* Clean up Tx and speed URB */
1529 usb_free_urb(self->tx_urb);
1530 usb_free_urb(self->speed_urb);
1531
1532 /* Free self and network device */
1533 free_netdev(self->netdev);
1534 IRDA_DEBUG(0, "%s(), USB IrDA Disconnected\n", __FUNCTION__);
1535}
1536
1537/*------------------------------------------------------------------*/
1538/*
1539 * USB device callbacks
1540 */
1541static struct usb_driver irda_driver = {
1542 .owner = THIS_MODULE,
1543 .name = "irda-usb",
1544 .probe = irda_usb_probe,
1545 .disconnect = irda_usb_disconnect,
1546 .id_table = dongles,
1547};
1548
1549/************************* MODULE CALLBACKS *************************/
1550/*
1551 * Deal with module insertion/removal
1552 * Mostly tell USB about our existence
1553 */
1554
1555/*------------------------------------------------------------------*/
1556/*
1557 * Module insertion
1558 */
1559static int __init usb_irda_init(void)
1560{
1561 int ret;
1562
1563 ret = usb_register(&irda_driver);
1564 if (ret < 0)
1565 return ret;
1566
1567 IRDA_MESSAGE("USB IrDA support registered\n");
1568 return 0;
1569}
1570module_init(usb_irda_init);
1571
1572/*------------------------------------------------------------------*/
1573/*
1574 * Module removal
1575 */
1576static void __exit usb_irda_cleanup(void)
1577{
1578 /* Deregister the driver and remove all pending instances */
1579 usb_deregister(&irda_driver);
1580}
1581module_exit(usb_irda_cleanup);
1582
1583/*------------------------------------------------------------------*/
1584/*
1585 * Module parameters
1586 */
1587module_param(qos_mtt_bits, int, 0);
1588MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
1589MODULE_AUTHOR("Roman Weissgaerber <weissg@vienna.at>, Dag Brattli <dag@brattli.net> and Jean Tourrilhes <jt@hpl.hp.com>");
1590MODULE_DESCRIPTION("IrDA-USB Dongle Driver");
1591MODULE_LICENSE("GPL");