1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57#include <linux/module.h>
58#include <linux/moduleparam.h>
59#include <linux/kernel.h>
60#include <linux/types.h>
61#include <linux/skbuff.h>
62#include <linux/netdevice.h>
63#include <linux/slab.h>
64#include <linux/rtnetlink.h>
65#include <linux/usb.h>
66#include <linux/firmware.h>
67
68#include "irda-usb.h"
69
70
71
72static int qos_mtt_bits = 0;
73
74
75static struct usb_device_id dongles[] = {
76
77 { USB_DEVICE(0x9c4, 0x011), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
78
79 { USB_DEVICE(0x4428, 0x012), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
80
81 { USB_DEVICE(0x50f, 0x180), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
82
83 { USB_DEVICE(0x8e9, 0x100), .driver_info = IUC_SPEED_BUG | IUC_NO_WINDOW },
84
85 { USB_DEVICE(0x66f, 0x4210), .driver_info = IUC_STIR421X | IUC_SPEED_BUG },
86 { USB_DEVICE(0x66f, 0x4220), .driver_info = IUC_STIR421X | IUC_SPEED_BUG },
87 { USB_DEVICE(0x66f, 0x4116), .driver_info = IUC_STIR421X | IUC_SPEED_BUG },
88 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
89 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
90 .bInterfaceClass = USB_CLASS_APP_SPEC,
91 .bInterfaceSubClass = USB_CLASS_IRDA,
92 .driver_info = IUC_DEFAULT, },
93 { },
94};
95
96
97
98
99
100
101
102
103
104
105MODULE_DEVICE_TABLE(usb, dongles);
106
107
108
109static void irda_usb_init_qos(struct irda_usb_cb *self) ;
110static struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf);
111static void irda_usb_disconnect(struct usb_interface *intf);
112static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self);
113static netdev_tx_t irda_usb_hard_xmit(struct sk_buff *skb,
114 struct net_device *dev);
115static int irda_usb_open(struct irda_usb_cb *self);
116static void irda_usb_close(struct irda_usb_cb *self);
117static void speed_bulk_callback(struct urb *urb);
118static void write_bulk_callback(struct urb *urb);
119static void irda_usb_receive(struct urb *urb);
120static void irda_usb_rx_defer_expired(unsigned long data);
121static int irda_usb_net_open(struct net_device *dev);
122static int irda_usb_net_close(struct net_device *dev);
123static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
124static void irda_usb_net_timeout(struct net_device *dev);
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148static void irda_usb_build_header(struct irda_usb_cb *self,
149 __u8 *header,
150 int force)
151{
152
153
154
155
156 if (self->capability & IUC_STIR421X &&
157 ((self->new_speed != -1) || (self->new_xbofs != -1))) {
158
159
160
161
162 if (self->new_speed == -1)
163 self->new_speed = self->speed ;
164
165 if (self->new_xbofs == -1)
166 self->new_xbofs = self->xbofs ;
167 }
168
169
170 if (self->new_speed != -1) {
171
172
173
174
175 if ((self->capability & IUC_SPEED_BUG) &&
176 (!force) && (self->speed != -1)) {
177
178
179 pr_debug("%s(), not changing speed yet\n", __func__);
180 *header = 0;
181 return;
182 }
183
184 pr_debug("%s(), changing speed to %d\n",
185 __func__, self->new_speed);
186 self->speed = self->new_speed;
187
188
189
190 switch (self->speed) {
191 case 2400:
192 *header = SPEED_2400;
193 break;
194 default:
195 case 9600:
196 *header = SPEED_9600;
197 break;
198 case 19200:
199 *header = SPEED_19200;
200 break;
201 case 38400:
202 *header = SPEED_38400;
203 break;
204 case 57600:
205 *header = SPEED_57600;
206 break;
207 case 115200:
208 *header = SPEED_115200;
209 break;
210 case 576000:
211 *header = SPEED_576000;
212 break;
213 case 1152000:
214 *header = SPEED_1152000;
215 break;
216 case 4000000:
217 *header = SPEED_4000000;
218 self->new_xbofs = 0;
219 break;
220 case 16000000:
221 *header = SPEED_16000000;
222 self->new_xbofs = 0;
223 break;
224 }
225 } else
226
227 *header = 0;
228
229
230 if (self->new_xbofs != -1) {
231 pr_debug("%s(), changing xbofs to %d\n",
232 __func__, self->new_xbofs);
233 self->xbofs = self->new_xbofs;
234
235
236
237 switch (self->xbofs) {
238 case 48:
239 *header |= 0x10;
240 break;
241 case 28:
242 case 24:
243 *header |= 0x20;
244 break;
245 default:
246 case 12:
247 *header |= 0x30;
248 break;
249 case 5:
250 case 6:
251 *header |= 0x40;
252 break;
253 case 3:
254 *header |= 0x50;
255 break;
256 case 2:
257 *header |= 0x60;
258 break;
259 case 1:
260 *header |= 0x70;
261 break;
262 case 0:
263 *header |= 0x80;
264 break;
265 }
266 }
267}
268
269
270
271
272static __u8 get_turnaround_time(struct sk_buff *skb)
273{
274 int turnaround_time = irda_get_mtt(skb);
275
276 if ( turnaround_time == 0 )
277 return 0;
278 else if ( turnaround_time <= 10 )
279 return 1;
280 else if ( turnaround_time <= 50 )
281 return 2;
282 else if ( turnaround_time <= 100 )
283 return 3;
284 else if ( turnaround_time <= 500 )
285 return 4;
286 else if ( turnaround_time <= 1000 )
287 return 5;
288 else if ( turnaround_time <= 5000 )
289 return 6;
290 else
291 return 7;
292}
293
294
295
296
297
298
299
300static void irda_usb_change_speed_xbofs(struct irda_usb_cb *self)
301{
302 __u8 *frame;
303 struct urb *urb;
304 int ret;
305
306 pr_debug("%s(), speed=%d, xbofs=%d\n", __func__,
307 self->new_speed, self->new_xbofs);
308
309
310 urb = self->speed_urb;
311 if (urb->status != 0) {
312 net_warn_ratelimited("%s(), URB still in use!\n", __func__);
313 return;
314 }
315
316
317 frame = self->speed_buff;
318
319
320 irda_usb_build_header(self, frame, 1);
321
322 if (self->capability & IUC_STIR421X) {
323 if (frame[0] == 0) return ;
324 frame[1] = 0;
325 frame[2] = 0;
326 }
327
328
329 usb_fill_bulk_urb(urb, self->usbdev,
330 usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
331 frame, IRDA_USB_SPEED_MTU,
332 speed_bulk_callback, self);
333 urb->transfer_buffer_length = self->header_length;
334 urb->transfer_flags = 0;
335
336
337 if ((ret = usb_submit_urb(urb, GFP_ATOMIC))) {
338 net_warn_ratelimited("%s(), failed Speed URB\n", __func__);
339 }
340}
341
342
343
344
345
346
347static void speed_bulk_callback(struct urb *urb)
348{
349 struct irda_usb_cb *self = urb->context;
350
351
352 IRDA_ASSERT(self != NULL, return;);
353
354 IRDA_ASSERT(urb == self->speed_urb, return;);
355
356
357 if (urb->status != 0) {
358
359 pr_debug("%s(), URB complete status %d, transfer_flags 0x%04X\n",
360 __func__, urb->status, urb->transfer_flags);
361
362
363
364
365
366
367 return;
368 }
369
370
371
372
373
374 self->new_speed = -1;
375 self->new_xbofs = -1;
376
377
378 netif_wake_queue(self->netdev);
379}
380
381
382
383
384
385static netdev_tx_t irda_usb_hard_xmit(struct sk_buff *skb,
386 struct net_device *netdev)
387{
388 struct irda_usb_cb *self = netdev_priv(netdev);
389 struct urb *urb = self->tx_urb;
390 unsigned long flags;
391 s32 speed;
392 s16 xbofs;
393 int res, mtt;
394
395 pr_debug("%s() on %s\n", __func__, netdev->name);
396
397 netif_stop_queue(netdev);
398
399
400 spin_lock_irqsave(&self->lock, flags);
401
402
403
404
405 if (!self->present) {
406 pr_debug("%s(), Device is gone...\n", __func__);
407 goto drop;
408 }
409
410
411 xbofs = irda_get_next_xbofs(skb);
412 if ((xbofs != self->xbofs) && (xbofs != -1)) {
413 self->new_xbofs = xbofs;
414 }
415
416
417 speed = irda_get_next_speed(skb);
418 if ((speed != self->speed) && (speed != -1)) {
419
420 self->new_speed = speed;
421
422
423 if (!skb->len) {
424
425
426
427
428
429
430
431 irda_usb_change_speed_xbofs(self);
432 netdev->trans_start = jiffies;
433
434 goto drop;
435 }
436 }
437
438 if (urb->status != 0) {
439 net_warn_ratelimited("%s(), URB still in use!\n", __func__);
440 goto drop;
441 }
442
443 skb_copy_from_linear_data(skb, self->tx_buff + self->header_length, skb->len);
444
445
446 if (self->capability & IUC_STIR421X) {
447 __u8 turnaround_time;
448 __u8* frame = self->tx_buff;
449 turnaround_time = get_turnaround_time( skb );
450 irda_usb_build_header(self, frame, 0);
451 frame[2] = turnaround_time;
452 if ((skb->len != 0) &&
453 ((skb->len % 128) == 0) &&
454 ((skb->len % 512) != 0)) {
455
456 frame[1] = 1;
457 skb_put(skb, 1);
458 } else {
459 frame[1] = 0;
460 }
461 } else {
462 irda_usb_build_header(self, self->tx_buff, 0);
463 }
464
465
466 ((struct irda_skb_cb *)skb->cb)->context = self;
467
468 usb_fill_bulk_urb(urb, self->usbdev,
469 usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
470 self->tx_buff, skb->len + self->header_length,
471 write_bulk_callback, skb);
472
473
474
475
476
477
478 urb->transfer_flags = URB_ZERO_PACKET;
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494 if (self->capability & IUC_NO_TURN) {
495 mtt = irda_get_mtt(skb);
496 if (mtt) {
497 int diff;
498 do_gettimeofday(&self->now);
499 diff = self->now.tv_usec - self->stamp.tv_usec;
500#ifdef IU_USB_MIN_RTT
501
502
503 diff += IU_USB_MIN_RTT;
504#endif
505
506
507
508 if (diff < 0)
509 diff += 1000000;
510
511
512
513
514 if (mtt > diff) {
515 mtt -= diff;
516 if (mtt > 1000)
517 mdelay(mtt/1000);
518 else
519 udelay(mtt);
520 }
521 }
522 }
523
524
525 if ((res = usb_submit_urb(urb, GFP_ATOMIC))) {
526 net_warn_ratelimited("%s(), failed Tx URB\n", __func__);
527 netdev->stats.tx_errors++;
528
529
530 } else {
531
532 netdev->stats.tx_packets++;
533 netdev->stats.tx_bytes += skb->len;
534
535 netdev->trans_start = jiffies;
536 }
537 spin_unlock_irqrestore(&self->lock, flags);
538
539 return NETDEV_TX_OK;
540
541drop:
542
543 dev_kfree_skb(skb);
544 spin_unlock_irqrestore(&self->lock, flags);
545 return NETDEV_TX_OK;
546}
547
548
549
550
551
552static void write_bulk_callback(struct urb *urb)
553{
554 unsigned long flags;
555 struct sk_buff *skb = urb->context;
556 struct irda_usb_cb *self = ((struct irda_skb_cb *) skb->cb)->context;
557
558
559 IRDA_ASSERT(self != NULL, return;);
560
561 IRDA_ASSERT(urb == self->tx_urb, return;);
562
563
564 dev_kfree_skb_any(skb);
565 urb->context = NULL;
566
567
568 if (urb->status != 0) {
569
570 pr_debug("%s(), URB complete status %d, transfer_flags 0x%04X\n",
571 __func__, urb->status, urb->transfer_flags);
572
573
574
575
576
577
578
579 return;
580 }
581
582
583
584
585
586 spin_lock_irqsave(&self->lock, flags);
587
588
589 if ((!self->netopen) || (!self->present)) {
590 pr_debug("%s(), Network is gone...\n", __func__);
591 spin_unlock_irqrestore(&self->lock, flags);
592 return;
593 }
594
595
596 if ((self->new_speed != -1) || (self->new_xbofs != -1)) {
597 if ((self->new_speed != self->speed) ||
598 (self->new_xbofs != self->xbofs)) {
599
600
601 pr_debug("%s(), Changing speed now...\n", __func__);
602 irda_usb_change_speed_xbofs(self);
603 } else {
604
605 self->new_speed = -1;
606 self->new_xbofs = -1;
607
608 netif_wake_queue(self->netdev);
609 }
610 } else {
611
612 netif_wake_queue(self->netdev);
613 }
614 spin_unlock_irqrestore(&self->lock, flags);
615}
616
617
618
619
620
621
622
623
624
625
626static void irda_usb_net_timeout(struct net_device *netdev)
627{
628 unsigned long flags;
629 struct irda_usb_cb *self = netdev_priv(netdev);
630 struct urb *urb;
631 int done = 0;
632
633 pr_debug("%s(), Network layer thinks we timed out!\n", __func__);
634 IRDA_ASSERT(self != NULL, return;);
635
636
637 spin_lock_irqsave(&self->lock, flags);
638
639
640 if (!self->present) {
641 net_warn_ratelimited("%s(), device not present!\n", __func__);
642 netif_stop_queue(netdev);
643 spin_unlock_irqrestore(&self->lock, flags);
644 return;
645 }
646
647
648 urb = self->speed_urb;
649 if (urb->status != 0) {
650 pr_debug("%s: Speed change timed out, urb->status=%d, urb->transfer_flags=0x%04X\n",
651 netdev->name, urb->status, urb->transfer_flags);
652
653 switch (urb->status) {
654 case -EINPROGRESS:
655 usb_unlink_urb(urb);
656
657
658
659 done = 1;
660 break;
661 case -ECONNRESET:
662 case -ENOENT:
663 default:
664 urb->status = 0;
665 netif_wake_queue(self->netdev);
666 done = 1;
667 break;
668 }
669 }
670
671
672 urb = self->tx_urb;
673 if (urb->status != 0) {
674 struct sk_buff *skb = urb->context;
675
676 pr_debug("%s: Tx timed out, urb->status=%d, urb->transfer_flags=0x%04X\n",
677 netdev->name, urb->status, urb->transfer_flags);
678
679
680 netdev->stats.tx_errors++;
681
682#ifdef IU_BUG_KICK_TIMEOUT
683
684 if(self->new_speed == -1)
685 self->new_speed = self->speed;
686 if(self->new_xbofs == -1)
687 self->new_xbofs = self->xbofs;
688 irda_usb_change_speed_xbofs(self);
689#endif
690
691 switch (urb->status) {
692 case -EINPROGRESS:
693 usb_unlink_urb(urb);
694
695
696
697
698
699 done = 1;
700 break;
701 case -ECONNRESET:
702 case -ENOENT:
703 default:
704 if(skb != NULL) {
705 dev_kfree_skb_any(skb);
706 urb->context = NULL;
707 }
708 urb->status = 0;
709 netif_wake_queue(self->netdev);
710 done = 1;
711 break;
712 }
713 }
714 spin_unlock_irqrestore(&self->lock, flags);
715
716
717
718
719
720
721
722}
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761static void irda_usb_submit(struct irda_usb_cb *self, struct sk_buff *skb, struct urb *urb)
762{
763 struct irda_skb_cb *cb;
764 int ret;
765
766
767 IRDA_ASSERT(skb != NULL, return;);
768 IRDA_ASSERT(urb != NULL, return;);
769
770
771 cb = (struct irda_skb_cb *) skb->cb;
772 cb->context = self;
773
774
775 usb_fill_bulk_urb(urb, self->usbdev,
776 usb_rcvbulkpipe(self->usbdev, self->bulk_in_ep),
777 skb->data, IRDA_SKB_MAX_MTU,
778 irda_usb_receive, skb);
779 urb->status = 0;
780
781
782 ret = usb_submit_urb(urb, GFP_ATOMIC);
783 if (ret) {
784
785
786 net_warn_ratelimited("%s(), Failed to submit Rx URB %d\n",
787 __func__, ret);
788 }
789}
790
791
792
793
794
795
796
797
798static void irda_usb_receive(struct urb *urb)
799{
800 struct sk_buff *skb = (struct sk_buff *) urb->context;
801 struct irda_usb_cb *self;
802 struct irda_skb_cb *cb;
803 struct sk_buff *newskb;
804 struct sk_buff *dataskb;
805 struct urb *next_urb;
806 unsigned int len, docopy;
807
808 pr_debug("%s(), len=%d\n", __func__, urb->actual_length);
809
810
811 cb = (struct irda_skb_cb *) skb->cb;
812 IRDA_ASSERT(cb != NULL, return;);
813 self = (struct irda_usb_cb *) cb->context;
814 IRDA_ASSERT(self != NULL, return;);
815
816
817 if ((!self->netopen) || (!self->present)) {
818 pr_debug("%s(), Network is gone!\n", __func__);
819
820 return;
821 }
822
823
824 if (urb->status != 0) {
825 switch (urb->status) {
826 case -EILSEQ:
827 self->netdev->stats.rx_crc_errors++;
828
829
830 case -ECONNRESET:
831
832
833
834
835 case -ESHUTDOWN:
836
837 case -ETIME:
838
839 default:
840 self->netdev->stats.rx_errors++;
841 pr_debug("%s(), RX status %d, transfer_flags 0x%04X\n",
842 __func__, urb->status, urb->transfer_flags);
843 break;
844 }
845
846
847
848
849
850
851
852
853
854
855 self->rx_defer_timer.function = irda_usb_rx_defer_expired;
856 self->rx_defer_timer.data = (unsigned long) urb;
857 mod_timer(&self->rx_defer_timer, jiffies + (10 * HZ / 1000));
858 return;
859 }
860
861
862 if (urb->actual_length <= self->header_length) {
863 net_warn_ratelimited("%s(), empty frame!\n", __func__);
864 goto done;
865 }
866
867
868
869
870
871
872 do_gettimeofday(&self->stamp);
873
874
875
876
877
878
879
880 docopy = (urb->actual_length < IRDA_RX_COPY_THRESHOLD);
881
882
883 if (self->capability & IUC_STIR421X)
884 newskb = dev_alloc_skb(docopy ? urb->actual_length :
885 IRDA_SKB_MAX_MTU +
886 USB_IRDA_STIR421X_HEADER);
887 else
888 newskb = dev_alloc_skb(docopy ? urb->actual_length :
889 IRDA_SKB_MAX_MTU);
890
891 if (!newskb) {
892 self->netdev->stats.rx_dropped++;
893
894
895 goto done;
896 }
897
898
899
900
901
902 if(docopy) {
903
904 skb_copy_from_linear_data(skb, newskb->data, urb->actual_length);
905
906 dataskb = newskb;
907
908
909
910 } else {
911
912 dataskb = skb;
913
914 skb = newskb;
915 }
916
917
918 skb_put(dataskb, urb->actual_length);
919 skb_pull(dataskb, self->header_length);
920
921
922 dataskb->dev = self->netdev;
923 skb_reset_mac_header(dataskb);
924 dataskb->protocol = htons(ETH_P_IRDA);
925 len = dataskb->len;
926 netif_rx(dataskb);
927
928
929 self->netdev->stats.rx_bytes += len;
930 self->netdev->stats.rx_packets++;
931
932done:
933
934
935
936
937
938
939
940
941
942
943
944 next_urb = self->idle_rx_urb;
945
946
947 urb->context = NULL;
948 self->idle_rx_urb = urb;
949
950
951
952 irda_usb_submit(self, skb, next_urb);
953}
954
955
956
957
958
959
960static void irda_usb_rx_defer_expired(unsigned long data)
961{
962 struct urb *urb = (struct urb *) data;
963 struct sk_buff *skb = (struct sk_buff *) urb->context;
964 struct irda_usb_cb *self;
965 struct irda_skb_cb *cb;
966 struct urb *next_urb;
967
968
969 cb = (struct irda_skb_cb *) skb->cb;
970 IRDA_ASSERT(cb != NULL, return;);
971 self = (struct irda_usb_cb *) cb->context;
972 IRDA_ASSERT(self != NULL, return;);
973
974
975 next_urb = self->idle_rx_urb;
976 urb->context = NULL;
977 self->idle_rx_urb = urb;
978 irda_usb_submit(self, skb, next_urb);
979}
980
981
982
983
984
985
986static int irda_usb_is_receiving(struct irda_usb_cb *self)
987{
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005 return 0;
1006}
1007
1008#define STIR421X_PATCH_PRODUCT_VER "Product Version: "
1009#define STIR421X_PATCH_STMP_TAG "STMP"
1010#define STIR421X_PATCH_CODE_OFFSET 512
1011
1012#define STIR421X_PATCH_END_OF_HDR_TAG 0x1A
1013#define STIR421X_PATCH_BLOCK_SIZE 1023
1014
1015
1016
1017
1018
1019
1020
1021
1022static int stir421x_fw_upload(struct irda_usb_cb *self,
1023 const unsigned char *patch,
1024 const unsigned int patch_len)
1025{
1026 int ret = -ENOMEM;
1027 int actual_len = 0;
1028 unsigned int i;
1029 unsigned int block_size = 0;
1030 unsigned char *patch_block;
1031
1032 patch_block = kzalloc(STIR421X_PATCH_BLOCK_SIZE, GFP_KERNEL);
1033 if (patch_block == NULL)
1034 return -ENOMEM;
1035
1036
1037 for (i = 0; i < patch_len; i += block_size) {
1038 block_size = patch_len - i;
1039
1040 if (block_size > STIR421X_PATCH_BLOCK_SIZE)
1041 block_size = STIR421X_PATCH_BLOCK_SIZE;
1042
1043
1044 memcpy(patch_block, patch + i, block_size);
1045
1046 ret = usb_bulk_msg(self->usbdev,
1047 usb_sndbulkpipe(self->usbdev,
1048 self->bulk_out_ep),
1049 patch_block, block_size,
1050 &actual_len, msecs_to_jiffies(500));
1051 pr_debug("%s(): Bulk send %u bytes, ret=%d\n",
1052 __func__, actual_len, ret);
1053
1054 if (ret < 0)
1055 break;
1056
1057 mdelay(10);
1058 }
1059
1060 kfree(patch_block);
1061
1062 return ret;
1063 }
1064
1065
1066
1067
1068
1069
1070static int stir421x_patch_device(struct irda_usb_cb *self)
1071{
1072 unsigned int i;
1073 int ret;
1074 char stir421x_fw_name[12];
1075 const struct firmware *fw;
1076 const unsigned char *fw_version_ptr;
1077 unsigned long fw_version = 0;
1078
1079
1080
1081
1082
1083 sprintf(stir421x_fw_name, "4210%4X.sb",
1084 self->usbdev->descriptor.bcdDevice);
1085 ret = request_firmware(&fw, stir421x_fw_name, &self->usbdev->dev);
1086 if (ret < 0)
1087 return ret;
1088
1089
1090 net_info_ratelimited("%s(): Received firmware %s (%zu bytes)\n",
1091 __func__, stir421x_fw_name, fw->size);
1092
1093 ret = -EINVAL;
1094
1095
1096 if (!memcmp(fw->data, STIR421X_PATCH_PRODUCT_VER,
1097 sizeof(STIR421X_PATCH_PRODUCT_VER) - 1)) {
1098 fw_version_ptr = fw->data +
1099 sizeof(STIR421X_PATCH_PRODUCT_VER) - 1;
1100
1101
1102 if (fw_version_ptr[3] == '.' &&
1103 fw_version_ptr[7] == '.') {
1104 unsigned long major, minor, build;
1105 major = simple_strtoul(fw_version_ptr, NULL, 10);
1106 minor = simple_strtoul(fw_version_ptr + 4, NULL, 10);
1107 build = simple_strtoul(fw_version_ptr + 8, NULL, 10);
1108
1109 fw_version = (major << 12)
1110 + (minor << 8)
1111 + ((build / 10) << 4)
1112 + (build % 10);
1113
1114 pr_debug("%s(): Firmware Product version %ld\n",
1115 __func__, fw_version);
1116 }
1117 }
1118
1119 if (self->usbdev->descriptor.bcdDevice == cpu_to_le16(fw_version)) {
1120
1121
1122
1123
1124
1125 for (i = 0; i < fw->size && fw->data[i] !=
1126 STIR421X_PATCH_END_OF_HDR_TAG; i++) ;
1127
1128 if (i < STIR421X_PATCH_CODE_OFFSET && i < fw->size &&
1129 STIR421X_PATCH_END_OF_HDR_TAG == fw->data[i]) {
1130 if (!memcmp(fw->data + i + 1, STIR421X_PATCH_STMP_TAG,
1131 sizeof(STIR421X_PATCH_STMP_TAG) - 1)) {
1132
1133
1134 i += sizeof(STIR421X_PATCH_STMP_TAG);
1135 ret = stir421x_fw_upload(self, &fw->data[i],
1136 fw->size - i);
1137 }
1138 }
1139 }
1140
1141 release_firmware(fw);
1142
1143 return ret;
1144}
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164static int irda_usb_net_open(struct net_device *netdev)
1165{
1166 struct irda_usb_cb *self;
1167 unsigned long flags;
1168 char hwname[16];
1169 int i;
1170
1171 IRDA_ASSERT(netdev != NULL, return -1;);
1172 self = netdev_priv(netdev);
1173 IRDA_ASSERT(self != NULL, return -1;);
1174
1175 spin_lock_irqsave(&self->lock, flags);
1176
1177 if(!self->present) {
1178 spin_unlock_irqrestore(&self->lock, flags);
1179 net_warn_ratelimited("%s(), device not present!\n", __func__);
1180 return -1;
1181 }
1182
1183 if(self->needspatch) {
1184 spin_unlock_irqrestore(&self->lock, flags);
1185 net_warn_ratelimited("%s(), device needs patch\n", __func__);
1186 return -EIO ;
1187 }
1188
1189
1190
1191 self->speed = -1;
1192 self->xbofs = -1;
1193 self->new_speed = -1;
1194 self->new_xbofs = -1;
1195
1196
1197
1198 self->netopen = 1;
1199 spin_unlock_irqrestore(&self->lock, flags);
1200
1201
1202
1203
1204
1205
1206 sprintf(hwname, "usb#%d", self->usbdev->devnum);
1207 self->irlap = irlap_open(netdev, &self->qos, hwname);
1208 IRDA_ASSERT(self->irlap != NULL, return -1;);
1209
1210
1211 netif_start_queue(netdev);
1212
1213
1214
1215
1216
1217 self->idle_rx_urb = self->rx_urb[IU_MAX_ACTIVE_RX_URBS];
1218 self->idle_rx_urb->context = NULL;
1219
1220
1221
1222 for (i = 0; i < IU_MAX_ACTIVE_RX_URBS; i++) {
1223 struct sk_buff *skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
1224 if (!skb) {
1225
1226
1227 return -1;
1228 }
1229
1230 irda_usb_submit(self, skb, self->rx_urb[i]);
1231 }
1232
1233
1234 return 0;
1235}
1236
1237
1238
1239
1240
1241
1242
1243
1244static int irda_usb_net_close(struct net_device *netdev)
1245{
1246 struct irda_usb_cb *self;
1247 int i;
1248
1249 IRDA_ASSERT(netdev != NULL, return -1;);
1250 self = netdev_priv(netdev);
1251 IRDA_ASSERT(self != NULL, return -1;);
1252
1253
1254
1255 self->netopen = 0;
1256
1257
1258 netif_stop_queue(netdev);
1259
1260
1261 del_timer(&self->rx_defer_timer);
1262
1263
1264 for (i = 0; i < self->max_rx_urb; i++) {
1265 struct urb *urb = self->rx_urb[i];
1266 struct sk_buff *skb = (struct sk_buff *) urb->context;
1267
1268 usb_kill_urb(urb);
1269
1270 if(skb) {
1271 dev_kfree_skb(skb);
1272 urb->context = NULL;
1273 }
1274 }
1275
1276 usb_kill_urb(self->tx_urb);
1277 usb_kill_urb(self->speed_urb);
1278
1279
1280 if (self->irlap)
1281 irlap_close(self->irlap);
1282 self->irlap = NULL;
1283
1284 return 0;
1285}
1286
1287
1288
1289
1290
1291static int irda_usb_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1292{
1293 unsigned long flags;
1294 struct if_irda_req *irq = (struct if_irda_req *) rq;
1295 struct irda_usb_cb *self;
1296 int ret = 0;
1297
1298 IRDA_ASSERT(dev != NULL, return -1;);
1299 self = netdev_priv(dev);
1300 IRDA_ASSERT(self != NULL, return -1;);
1301
1302 pr_debug("%s(), %s, (cmd=0x%X)\n", __func__, dev->name, cmd);
1303
1304 switch (cmd) {
1305 case SIOCSBANDWIDTH:
1306 if (!capable(CAP_NET_ADMIN))
1307 return -EPERM;
1308
1309 spin_lock_irqsave(&self->lock, flags);
1310
1311 if(self->present) {
1312
1313 self->new_speed = irq->ifr_baudrate;
1314 irda_usb_change_speed_xbofs(self);
1315 }
1316 spin_unlock_irqrestore(&self->lock, flags);
1317 break;
1318 case SIOCSMEDIABUSY:
1319 if (!capable(CAP_NET_ADMIN))
1320 return -EPERM;
1321
1322 if(self->netopen)
1323 irda_device_set_media_busy(self->netdev, TRUE);
1324 break;
1325 case SIOCGRECEIVING:
1326 irq->ifr_receiving = irda_usb_is_receiving(self);
1327 break;
1328 default:
1329 ret = -EOPNOTSUPP;
1330 }
1331
1332 return ret;
1333}
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348static inline void irda_usb_init_qos(struct irda_usb_cb *self)
1349{
1350 struct irda_class_desc *desc;
1351
1352
1353 desc = self->irda_desc;
1354
1355
1356 irda_init_max_qos_capabilies(&self->qos);
1357
1358
1359
1360
1361 self->qos.baud_rate.bits = le16_to_cpu(desc->wBaudRate);
1362 self->qos.min_turn_time.bits = desc->bmMinTurnaroundTime;
1363 self->qos.additional_bofs.bits = desc->bmAdditionalBOFs;
1364 self->qos.window_size.bits = desc->bmWindowSize;
1365 self->qos.data_size.bits = desc->bmDataSize;
1366
1367 pr_debug("%s(), dongle says speed=0x%X, size=0x%X, window=0x%X, bofs=0x%X, turn=0x%X\n",
1368 __func__, self->qos.baud_rate.bits, self->qos.data_size.bits,
1369 self->qos.window_size.bits, self->qos.additional_bofs.bits,
1370 self->qos.min_turn_time.bits);
1371
1372
1373 if(self->capability & IUC_SIR_ONLY)
1374 self->qos.baud_rate.bits &= 0x00ff;
1375 if(self->capability & IUC_SMALL_PKT)
1376 self->qos.data_size.bits = 0x07;
1377 if(self->capability & IUC_NO_WINDOW)
1378 self->qos.window_size.bits = 0x01;
1379 if(self->capability & IUC_MAX_WINDOW)
1380 self->qos.window_size.bits = 0x7f;
1381 if(self->capability & IUC_MAX_XBOFS)
1382 self->qos.additional_bofs.bits = 0x01;
1383
1384#if 1
1385
1386 if (qos_mtt_bits)
1387 self->qos.min_turn_time.bits = qos_mtt_bits;
1388#endif
1389
1390
1391
1392
1393 irda_qos_bits_to_value(&self->qos);
1394}
1395
1396
1397static const struct net_device_ops irda_usb_netdev_ops = {
1398 .ndo_open = irda_usb_net_open,
1399 .ndo_stop = irda_usb_net_close,
1400 .ndo_do_ioctl = irda_usb_net_ioctl,
1401 .ndo_start_xmit = irda_usb_hard_xmit,
1402 .ndo_tx_timeout = irda_usb_net_timeout,
1403};
1404
1405
1406
1407
1408
1409static inline int irda_usb_open(struct irda_usb_cb *self)
1410{
1411 struct net_device *netdev = self->netdev;
1412
1413 netdev->netdev_ops = &irda_usb_netdev_ops;
1414
1415 irda_usb_init_qos(self);
1416
1417 return register_netdev(netdev);
1418}
1419
1420
1421
1422
1423
1424
1425static inline void irda_usb_close(struct irda_usb_cb *self)
1426{
1427
1428 unregister_netdev(self->netdev);
1429
1430
1431 kfree(self->speed_buff);
1432 self->speed_buff = NULL;
1433
1434 kfree(self->tx_buff);
1435 self->tx_buff = NULL;
1436}
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457static inline int irda_usb_parse_endpoints(struct irda_usb_cb *self, struct usb_host_endpoint *endpoint, int ennum)
1458{
1459 int i;
1460
1461
1462 self->bulk_in_ep = 0;
1463 self->bulk_out_ep = 0;
1464 self->bulk_int_ep = 0;
1465
1466
1467 for(i = 0; i < ennum; i++) {
1468
1469
1470 __u8 ep;
1471 __u8 dir;
1472 __u8 attr;
1473 __u16 psize;
1474
1475
1476 ep = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1477 dir = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK;
1478 attr = endpoint[i].desc.bmAttributes;
1479 psize = le16_to_cpu(endpoint[i].desc.wMaxPacketSize);
1480
1481
1482 if(attr == USB_ENDPOINT_XFER_BULK) {
1483
1484 if(dir == USB_DIR_IN) {
1485
1486 self->bulk_in_ep = ep;
1487 } else {
1488
1489 self->bulk_out_ep = ep;
1490 self->bulk_out_mtu = psize;
1491 }
1492 } else {
1493 if((attr == USB_ENDPOINT_XFER_INT) &&
1494 (dir == USB_DIR_IN)) {
1495
1496 self->bulk_int_ep = ep;
1497 } else {
1498 net_err_ratelimited("%s(), Unrecognised endpoint %02X\n",
1499 __func__, ep);
1500 }
1501 }
1502 }
1503
1504 pr_debug("%s(), And our endpoints are : in=%02X, out=%02X (%d), int=%02X\n",
1505 __func__, self->bulk_in_ep, self->bulk_out_ep,
1506 self->bulk_out_mtu, self->bulk_int_ep);
1507
1508 return (self->bulk_in_ep != 0) && (self->bulk_out_ep != 0);
1509}
1510
1511#ifdef IU_DUMP_CLASS_DESC
1512
1513
1514
1515
1516
1517
1518
1519static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
1520{
1521
1522 printk("bLength=%x\n", desc->bLength);
1523 printk("bDescriptorType=%x\n", desc->bDescriptorType);
1524 printk("bcdSpecRevision=%x\n", le16_to_cpu(desc->bcdSpecRevision));
1525 printk("bmDataSize=%x\n", desc->bmDataSize);
1526 printk("bmWindowSize=%x\n", desc->bmWindowSize);
1527 printk("bmMinTurnaroundTime=%d\n", desc->bmMinTurnaroundTime);
1528 printk("wBaudRate=%x\n", le16_to_cpu(desc->wBaudRate));
1529 printk("bmAdditionalBOFs=%x\n", desc->bmAdditionalBOFs);
1530 printk("bIrdaRateSniff=%x\n", desc->bIrdaRateSniff);
1531 printk("bMaxUnicastList=%x\n", desc->bMaxUnicastList);
1532}
1533#endif
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545static inline struct irda_class_desc *irda_usb_find_class_desc(struct usb_interface *intf)
1546{
1547 struct usb_device *dev = interface_to_usbdev (intf);
1548 struct irda_class_desc *desc;
1549 int ret;
1550
1551 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
1552 if (!desc)
1553 return NULL;
1554
1555
1556
1557
1558
1559
1560
1561
1562 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
1563 IU_REQ_GET_CLASS_DESC,
1564 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1565 0, intf->altsetting->desc.bInterfaceNumber, desc,
1566 sizeof(*desc), 500);
1567
1568 pr_debug("%s(), ret=%d\n", __func__, ret);
1569 if (ret < sizeof(*desc)) {
1570 net_warn_ratelimited("usb-irda: class_descriptor read %s (%d)\n",
1571 ret < 0 ? "failed" : "too short", ret);
1572 }
1573 else if (desc->bDescriptorType != USB_DT_IRDA) {
1574 net_warn_ratelimited("usb-irda: bad class_descriptor type\n");
1575 }
1576 else {
1577#ifdef IU_DUMP_CLASS_DESC
1578 irda_usb_dump_class_desc(desc);
1579#endif
1580
1581 return desc;
1582 }
1583 kfree(desc);
1584 return NULL;
1585}
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601static int irda_usb_probe(struct usb_interface *intf,
1602 const struct usb_device_id *id)
1603{
1604 struct net_device *net;
1605 struct usb_device *dev = interface_to_usbdev(intf);
1606 struct irda_usb_cb *self;
1607 struct usb_host_interface *interface;
1608 struct irda_class_desc *irda_desc;
1609 int ret = -ENOMEM;
1610 int i;
1611
1612
1613
1614
1615
1616
1617 net_info_ratelimited("IRDA-USB found at address %d, Vendor: %x, Product: %x\n",
1618 dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
1619 le16_to_cpu(dev->descriptor.idProduct));
1620
1621 net = alloc_irdadev(sizeof(*self));
1622 if (!net)
1623 goto err_out;
1624
1625 SET_NETDEV_DEV(net, &intf->dev);
1626 self = netdev_priv(net);
1627 self->netdev = net;
1628 spin_lock_init(&self->lock);
1629 init_timer(&self->rx_defer_timer);
1630
1631 self->capability = id->driver_info;
1632 self->needspatch = ((self->capability & IUC_STIR421X) != 0);
1633
1634
1635 if (self->capability & IUC_STIR421X) {
1636 self->max_rx_urb = IU_SIGMATEL_MAX_RX_URBS;
1637 self->header_length = USB_IRDA_STIR421X_HEADER;
1638 } else {
1639 self->max_rx_urb = IU_MAX_RX_URBS;
1640 self->header_length = USB_IRDA_HEADER;
1641 }
1642
1643 self->rx_urb = kcalloc(self->max_rx_urb, sizeof(struct urb *),
1644 GFP_KERNEL);
1645 if (!self->rx_urb)
1646 goto err_free_net;
1647
1648 for (i = 0; i < self->max_rx_urb; i++) {
1649 self->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
1650 if (!self->rx_urb[i]) {
1651 goto err_out_1;
1652 }
1653 }
1654 self->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1655 if (!self->tx_urb) {
1656 goto err_out_1;
1657 }
1658 self->speed_urb = usb_alloc_urb(0, GFP_KERNEL);
1659 if (!self->speed_urb) {
1660 goto err_out_2;
1661 }
1662
1663
1664 if (usb_reset_configuration (dev) < 0) {
1665 dev_err(&intf->dev, "reset_configuration failed\n");
1666 ret = -EIO;
1667 goto err_out_3;
1668 }
1669
1670
1671
1672
1673
1674 ret = usb_set_interface(dev, intf->altsetting->desc.bInterfaceNumber, 0);
1675 pr_debug("usb-irda: set interface %d result %d\n",
1676 intf->altsetting->desc.bInterfaceNumber, ret);
1677 switch (ret) {
1678 case 0:
1679 break;
1680 case -EPIPE:
1681
1682
1683
1684 pr_debug("%s(), Received -EPIPE, ignoring...\n",
1685 __func__);
1686 break;
1687 default:
1688 pr_debug("%s(), Unknown error %d\n", __func__, ret);
1689 ret = -EIO;
1690 goto err_out_3;
1691 }
1692
1693
1694 interface = intf->cur_altsetting;
1695 if(!irda_usb_parse_endpoints(self, interface->endpoint,
1696 interface->desc.bNumEndpoints)) {
1697 net_err_ratelimited("%s(), Bogus endpoints...\n", __func__);
1698 ret = -EIO;
1699 goto err_out_3;
1700 }
1701
1702 self->usbdev = dev;
1703
1704
1705 irda_desc = irda_usb_find_class_desc(intf);
1706 ret = -ENODEV;
1707 if (!irda_desc)
1708 goto err_out_3;
1709
1710 if (self->needspatch) {
1711 ret = usb_control_msg (self->usbdev, usb_sndctrlpipe (self->usbdev, 0),
1712 0x02, 0x40, 0, 0, NULL, 0, 500);
1713 if (ret < 0) {
1714 pr_debug("usb_control_msg failed %d\n", ret);
1715 goto err_out_3;
1716 } else {
1717 mdelay(10);
1718 }
1719 }
1720
1721 self->irda_desc = irda_desc;
1722 self->present = 1;
1723 self->netopen = 0;
1724 self->usbintf = intf;
1725
1726
1727
1728
1729
1730 self->speed_buff = kzalloc(IRDA_USB_SPEED_MTU, GFP_KERNEL);
1731 if (!self->speed_buff)
1732 goto err_out_3;
1733
1734 self->tx_buff = kzalloc(IRDA_SKB_MAX_MTU + self->header_length,
1735 GFP_KERNEL);
1736 if (!self->tx_buff)
1737 goto err_out_4;
1738
1739 ret = irda_usb_open(self);
1740 if (ret)
1741 goto err_out_5;
1742
1743 net_info_ratelimited("IrDA: Registered device %s\n", net->name);
1744 usb_set_intfdata(intf, self);
1745
1746 if (self->needspatch) {
1747
1748 ret = stir421x_patch_device(self);
1749 self->needspatch = (ret < 0);
1750 if (self->needspatch) {
1751 net_err_ratelimited("STIR421X: Couldn't upload patch\n");
1752 goto err_out_6;
1753 }
1754
1755
1756 irda_desc = irda_usb_find_class_desc (self->usbintf);
1757 if (!irda_desc) {
1758 ret = -ENODEV;
1759 goto err_out_6;
1760 }
1761 kfree(self->irda_desc);
1762 self->irda_desc = irda_desc;
1763 irda_usb_init_qos(self);
1764 }
1765
1766 return 0;
1767err_out_6:
1768 unregister_netdev(self->netdev);
1769err_out_5:
1770 kfree(self->tx_buff);
1771err_out_4:
1772 kfree(self->speed_buff);
1773err_out_3:
1774
1775 usb_free_urb(self->speed_urb);
1776err_out_2:
1777 usb_free_urb(self->tx_urb);
1778err_out_1:
1779 for (i = 0; i < self->max_rx_urb; i++)
1780 usb_free_urb(self->rx_urb[i]);
1781 kfree(self->rx_urb);
1782err_free_net:
1783 free_netdev(net);
1784err_out:
1785 return ret;
1786}
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800static void irda_usb_disconnect(struct usb_interface *intf)
1801{
1802 unsigned long flags;
1803 struct irda_usb_cb *self = usb_get_intfdata(intf);
1804 int i;
1805
1806 usb_set_intfdata(intf, NULL);
1807 if (!self)
1808 return;
1809
1810
1811 spin_lock_irqsave(&self->lock, flags);
1812
1813
1814
1815 self->present = 0;
1816
1817
1818 del_timer(&self->rx_defer_timer);
1819
1820
1821
1822 spin_unlock_irqrestore(&self->lock, flags);
1823
1824
1825 if((self->netopen) || (self->irlap)) {
1826
1827
1828 netif_stop_queue(self->netdev);
1829
1830 for (i = 0; i < self->max_rx_urb; i++)
1831 usb_kill_urb(self->rx_urb[i]);
1832
1833
1834 usb_kill_urb(self->tx_urb);
1835 usb_kill_urb(self->speed_urb);
1836 }
1837
1838
1839 irda_usb_close(self);
1840
1841 self->usbdev = NULL;
1842 self->usbintf = NULL;
1843
1844
1845 for (i = 0; i < self->max_rx_urb; i++)
1846 usb_free_urb(self->rx_urb[i]);
1847 kfree(self->rx_urb);
1848
1849 usb_free_urb(self->tx_urb);
1850 usb_free_urb(self->speed_urb);
1851
1852
1853 free_netdev(self->netdev);
1854 pr_debug("%s(), USB IrDA Disconnected\n", __func__);
1855}
1856
1857#ifdef CONFIG_PM
1858
1859static int irda_usb_suspend(struct usb_interface *intf, pm_message_t message)
1860{
1861 struct irda_usb_cb *self = usb_get_intfdata(intf);
1862 int i;
1863
1864 netif_device_detach(self->netdev);
1865
1866 if (self->tx_urb != NULL)
1867 usb_kill_urb(self->tx_urb);
1868 if (self->speed_urb != NULL)
1869 usb_kill_urb(self->speed_urb);
1870 for (i = 0; i < self->max_rx_urb; i++) {
1871 if (self->rx_urb[i] != NULL)
1872 usb_kill_urb(self->rx_urb[i]);
1873 }
1874 return 0;
1875}
1876
1877
1878static int irda_usb_resume(struct usb_interface *intf)
1879{
1880 struct irda_usb_cb *self = usb_get_intfdata(intf);
1881 int i;
1882
1883 for (i = 0; i < self->max_rx_urb; i++) {
1884 if (self->rx_urb[i] != NULL)
1885 usb_submit_urb(self->rx_urb[i], GFP_KERNEL);
1886 }
1887
1888 netif_device_attach(self->netdev);
1889 return 0;
1890}
1891#endif
1892
1893
1894
1895
1896
1897static struct usb_driver irda_driver = {
1898 .name = "irda-usb",
1899 .probe = irda_usb_probe,
1900 .disconnect = irda_usb_disconnect,
1901 .id_table = dongles,
1902#ifdef CONFIG_PM
1903 .suspend = irda_usb_suspend,
1904 .resume = irda_usb_resume,
1905#endif
1906};
1907
1908module_usb_driver(irda_driver);
1909
1910
1911
1912
1913module_param(qos_mtt_bits, int, 0);
1914MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
1915MODULE_AUTHOR("Roman Weissgaerber <weissg@vienna.at>, Dag Brattli <dag@brattli.net>, Jean Tourrilhes <jt@hpl.hp.com> and Nick Fedchik <nick@fedchik.org.ua>");
1916MODULE_DESCRIPTION("IrDA-USB Dongle Driver");
1917MODULE_LICENSE("GPL");
1918