-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhw_cdc_driver.c
More file actions
executable file
·3938 lines (3380 loc) · 119 KB
/
Copy pathhw_cdc_driver.c
File metadata and controls
executable file
·3938 lines (3380 loc) · 119 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* CDC Ethernet based the networking peripherals of Huawei data card devices
* This driver is developed based on usbnet.c and cdc_ether.c
* Copyright (C) 2009 by Franko Fang (Huawei Technologies Co., Ltd.)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will support Huawei data card devices for Linux networking,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/workqueue.h>
#include <linux/mii.h>
#include <linux/usb.h>
#include <linux/sched.h>
#include <linux/ctype.h>
#include <linux/usb/cdc.h>
#include <linux/usbdevice_fs.h>
#include <linux/version.h>
/////////////////////////////////////////////////////////////////////////////////////////////////
#define DRIVER_VERSION "v2.07.00.00"
#define DRIVER_AUTHOR "Franko Fang <huananhu@huawei.com>"
#define DRIVER_DESC "Huawei ether driver for 3G data card ether device"
//////////////////////////////////////////////////////////////////////////////////////////////////////
#define RX_MAX_QUEUE_MEMORY (60 * 1518)
#define RX_QLEN(dev) ( ((dev)->udev->speed == USB_SPEED_HIGH) ? \
(RX_MAX_QUEUE_MEMORY / (dev)->rx_urb_size) : 4)
#define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
(RX_MAX_QUEUE_MEMORY / (dev)->hard_mtu) : 4)
// reawaken network queue this soon after stopping; else watchdog barks
#define TX_TIMEOUT_JIFFIES (5 * HZ)
// throttle rx/tx briefly after some faults, so khubd might disconnect()
// us (it polls at HZ/4 usually) before we report too many false errors.
#define THROTTLE_JIFFIES (HZ / 8)
// between wakeups
#define UNLINK_TIMEOUT_MS 3
//////////////////////////////////////////////////////////////////////////////////////////////
// randomly generated ethernet address
static u8 node_id [ETH_ALEN];
static const char driver_name [] = "hw_cdc_net";
/* use ethtool to change the level for any given device */
static int msg_level = -1;
module_param (msg_level, int, 0);
MODULE_PARM_DESC (msg_level, "Override default message level");
//////////////////////////////////////////////////////////////////////////////////////////
#define HW_TLP_MASK_SYNC 0xF800
#define HW_TLP_MASK_LENGTH 0x07FF
#define HW_TLP_BITS_SYNC 0xF800
#pragma pack(push, 1)
struct hw_cdc_tlp
{
unsigned short pktlength;
unsigned char payload;
};
#define HW_TLP_HDR_LENGTH sizeof(unsigned short)
#pragma pack(pop)
typedef enum __HW_TLP_BUF_STATE {
HW_TLP_BUF_STATE_IDLE = 0,
HW_TLP_BUF_STATE_PARTIAL_FILL,
HW_TLP_BUF_STATE_PARTIAL_HDR,
HW_TLP_BUF_STATE_HDR_ONLY,
HW_TLP_BUF_STATE_ERROR
}HW_TLP_BUF_STATE;
struct hw_cdc_tlp_tmp{
void *buffer;
unsigned short pktlength;
unsigned short bytesneeded;
};
/*max ethernet pkt size 1514*/
#define HW_USB_RECEIVE_BUFFER_SIZE 1600L
/*for Tin-layer-protocol (TLP)*/
#define HW_USB_MRECEIVE_BUFFER_SIZE 4096L
/*for TLP*/
#define HW_USB_MRECEIVE_MAX_BUFFER_SIZE (1024 * 16)
#define HW_JUNGO_BCDDEVICE_VALUE 0x0102
#define BINTERFACESUBCLASS 0x02
///////////////////////////////////////////////////////////////////////////////////////////
#define EVENT_TX_HALT 0
#define EVENT_RX_HALT 1
#define EVENT_RX_MEMORY 2
#define EVENT_STS_SPLIT 3
#define EVENT_LINK_RESET 4
#define NCM_TX_DEFAULT_TIMEOUT_MS 2
static int ncm_prefer_32 = 1;
//module_param(ncm_prefer_32, bool, S_IRUGO);
module_param(ncm_prefer_32, int, S_IRUGO);
static int ncm_prefer_crc = 0;
//module_param(ncm_prefer_crc, bool, S_IRUGO);
module_param(ncm_prefer_crc, int, S_IRUGO);
static unsigned long ncm_tx_timeout = NCM_TX_DEFAULT_TIMEOUT_MS;
module_param(ncm_tx_timeout, ulong, S_IRUGO);
static unsigned int ncm_read_buf_count = 4;
module_param(ncm_read_buf_count, uint, S_IRUGO);
static unsigned short ncm_read_size_in1k = 4;
module_param(ncm_read_size_in1k, short , S_IRUGO);
static int rt_debug = 0;
//module_param(rt_debug, bool, S_IRUGO|S_IWUSR);
module_param(rt_debug, int, S_IRUGO | S_IWUSR);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
#include <linux/unaligned/access_ok.h>
#else
static inline u16 get_unaligned_le16(const void *p)
{
return le16_to_cpup((__le16 *)p);
}
static inline u32 get_unaligned_le32(const void *p)
{
return le32_to_cpup((__le32 *)p);
}
static inline void put_unaligned_le16(u16 val, void *p)
{
*((__le16 *)p) = cpu_to_le16(val);
}
static inline void put_unaligned_le32(u32 val, void *p)
{
*((__le32 *)p) = cpu_to_le32(val);
}
#endif
/* Add for DTS2011050903736 lxz 20110520 start*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
#define LINUX_VERSION37_LATER 1
#else
#define LINUX_VERSION37_LATER 0
#endif
/* Add for DTS2011050903736 lxz 20110520 end*/
/*
>2.6.36 some syetem not find ncm.h but find cdc.h
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35)
#include <linux/usb/ncm.h>
#else
*/
#define USB_CDC_NCM_TYPE 0x1a
/* NCM Functional Descriptor */
/* change usb_cdc_ncm_desc -> usb_cdc_ncm_desc_hw ,prevent cdc.h redefinition 11-05*/
struct usb_cdc_ncm_desc_hw {
__u8 bLength;
__u8 bDescriptorType;
__u8 bDescriptorSubType;
__le16 bcdNcmVersion;
__u8 bmNetworkCapabilities;
} __attribute__ ((packed));
#ifdef NCM_NCAP_ETH_FILTER
#undef NCM_NCAP_ETH_FILTER
#endif
#ifdef NCM_NCAP_NET_ADDRESS
#undef NCM_NCAP_NET_ADDRESS
#endif
#ifdef NCM_NCAP_ENCAP_COMM
#undef NCM_NCAP_ENCAP_COMM
#endif
#ifdef NCM_NCAP_MAX_DGRAM
#undef NCM_NCAP_MAX_DGRAM
#endif
#ifdef NCM_NCAP_CRC_MODE
#undef NCM_NCAP_CRC_MODE
#endif
#define NCM_NCAP_ETH_FILTER (1 << 0)
#define NCM_NCAP_NET_ADDRESS (1 << 1)
#define NCM_NCAP_ENCAP_COMM (1 << 2)
#define NCM_NCAP_MAX_DGRAM (1 << 3)
#define NCM_NCAP_CRC_MODE (1 << 4)
#ifdef USB_CDC_GET_NTB_PARAMETERS
#undef USB_CDC_GET_NTB_PARAMETERS
#endif
#ifdef USB_CDC_GET_NET_ADDRESS
#undef USB_CDC_GET_NET_ADDRESS
#endif
#ifdef USB_CDC_SET_NET_ADDRESS
#undef USB_CDC_SET_NET_ADDRESS
#endif
#ifdef USB_CDC_GET_NTB_FORMAT
#undef USB_CDC_GET_NTB_FORMAT
#endif
#ifdef USB_CDC_SET_NTB_FORMAT
#undef USB_CDC_SET_NTB_FORMAT
#endif
#ifdef USB_CDC_GET_NTB_INPUT_SIZE
#undef USB_CDC_GET_NTB_INPUT_SIZE
#endif
#ifdef USB_CDC_SET_NTB_INPUT_SIZE
#undef USB_CDC_SET_NTB_INPUT_SIZE
#endif
#ifdef USB_CDC_GET_MAX_DATAGRAM_SIZE
#undef USB_CDC_GET_MAX_DATAGRAM_SIZE
#endif
#ifdef USB_CDC_SET_MAX_DATAGRAM_SIZE
#undef USB_CDC_SET_MAX_DATAGRAM_SIZE
#endif
#ifdef USB_CDC_GET_CRC_MODE
#undef USB_CDC_GET_CRC_MODE
#endif
#ifdef USB_CDC_SET_CRC_MODE
#undef USB_CDC_SET_CRC_MODE
#endif
#define USB_CDC_GET_NTB_PARAMETERS 0x80
#define USB_CDC_GET_NET_ADDRESS 0x81
#define USB_CDC_SET_NET_ADDRESS 0x82
#define USB_CDC_GET_NTB_FORMAT 0x83
#define USB_CDC_SET_NTB_FORMAT 0x84
#define USB_CDC_GET_NTB_INPUT_SIZE 0x85
#define USB_CDC_SET_NTB_INPUT_SIZE 0x86
#define USB_CDC_GET_MAX_DATAGRAM_SIZE 0x87
#define USB_CDC_SET_MAX_DATAGRAM_SIZE 0x88
#define USB_CDC_GET_CRC_MODE 0x89
#define USB_CDC_SET_CRC_MODE 0x8a
/*
* Class Specific structures and constants
*
* CDC NCM parameter structure, CDC NCM subclass 6.2.1
*
*/
struct usb_cdc_ncm_ntb_parameter_hw {
__le16 wLength;
__le16 bmNtbFormatSupported;
__le32 dwNtbInMaxSize;
__le16 wNdpInDivisor;
__le16 wNdpInPayloadRemainder;
__le16 wNdpInAlignment;
__le16 wPadding1;
__le32 dwNtbOutMaxSize;
__le16 wNdpOutDivisor;
__le16 wNdpOutPayloadRemainder;
__le16 wNdpOutAlignment;
__le16 wPadding2;
} __attribute__ ((packed));
/*
* CDC NCM transfer headers, CDC NCM subclass 3.2
*/
#ifdef NCM_NTH16_SIGN
#undef NCM_NTH16_SIGN
#endif
#ifdef NCM_NTH32_SIGN
#undef NCM_NTH32_SIGN
#endif
#define NCM_NTH16_SIGN 0x484D434E /* NCMH */
#define NCM_NTH32_SIGN 0x686D636E /* ncmh */
/* change usb_cdc_ncm_nth16 -> usb_cdc_ncm_nth16_hw ,prevent cdc.h redefinition */
struct usb_cdc_ncm_nth16_hw {
__le32 dwSignature;
__le16 wHeaderLength;
__le16 wSequence;
__le16 wBlockLength;
__le16 wFpIndex;
} __attribute__ ((packed));
/* change usb_cdc_ncm_nth32 -> usb_cdc_ncm_nth_hw ,prevent cdc.h redefinition */
struct usb_cdc_ncm_nth32_hw {
__le32 dwSignature;
__le16 wHeaderLength;
__le16 wSequence;
__le32 dwBlockLength;
__le32 dwFpIndex;
} __attribute__ ((packed));
/*
* CDC NCM datagram pointers, CDC NCM subclass 3.3
*/
#ifdef NCM_NDP16_CRC_SIGN
#undef NCM_NDP16_CRC_SIGN
#endif
#ifdef NCM_NDP16_NOCRC_SIGN
#undef NCM_NDP16_NOCRC_SIGN
#endif
#ifdef NCM_NDP32_CRC_SIGN
#undef NCM_NDP32_CRC_SIGN
#endif
#ifdef NCM_NDP32_NOCRC_SIGN
#undef NCM_NDP32_NOCRC_SIGN
#endif
#define NCM_NDP16_CRC_SIGN 0x314D434E /* NCM1 */
#define NCM_NDP16_NOCRC_SIGN 0x304D434E /* NCM0 */
#define NCM_NDP32_CRC_SIGN 0x316D636E /* ncm1 */
#define NCM_NDP32_NOCRC_SIGN 0x306D636E /* ncm0 */
/* change usb_cdc_ncm_ndp16 -> usb_cdc_ncm_ndp16_hw ,prevent cdc.h redefinition */
struct usb_cdc_ncm_ndp16_hw {
__le32 dwSignature;
__le16 wLength;
__le16 wNextFpIndex;
__u8 data[0];
} __attribute__ ((packed));
/* change usb_cdc_ncm_ndp32 -> usb_cdc_ncm_ndp32_hw ,prevent cdc.h redefinition */
struct usb_cdc_ncm_ndp32_hw {
__le32 dwSignature;
__le16 wLength;
__le16 wReserved6;
__le32 dwNextFpIndex;
__le32 dwReserved12;
__u8 data[0];
} __attribute__ ((packed));
/*
* Here are options for NCM Datagram Pointer table (NDP) parser.
* There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
* in NDP16 offsets and sizes fields are 1 16bit word wide,
* in NDP32 -- 2 16bit words wide. Also signatures are different.
* To make the parser code the same, put the differences in the structure,
* and switch pointers to the structures when the format is changed.
*/
/* change usb_cdc_ncm_ndp32 -> usb_cdc_ncm_ndp32_hw ,prevent redefinition */
struct ndp_parser_opts_hw {
u32 nth_sign;
u32 ndp_sign;
unsigned nth_size;
unsigned ndp_size;
unsigned ndplen_align;
/* sizes in u16 units */
unsigned dgram_item_len; /* index or length */
unsigned block_length;
unsigned fp_index;
unsigned reserved1;
unsigned reserved2;
unsigned next_fp_index;
};
#ifdef INIT_NDP16_OPTS
#undef INIT_NDP16_OPTS
#endif
#ifdef INIT_NDP32_OPTS
#undef INIT_NDP32_OPTS
#endif
#define INIT_NDP16_OPTS { \
.nth_sign = NCM_NTH16_SIGN, \
.ndp_sign = NCM_NDP16_NOCRC_SIGN, \
.nth_size = sizeof(struct usb_cdc_ncm_nth16_hw), \
.ndp_size = sizeof(struct usb_cdc_ncm_ndp16_hw), \
.ndplen_align = 4, \
.dgram_item_len = 1, \
.block_length = 1, \
.fp_index = 1, \
.reserved1 = 0, \
.reserved2 = 0, \
.next_fp_index = 1, \
}
#define INIT_NDP32_OPTS { \
.nth_sign = NCM_NTH32_SIGN, \
.ndp_sign = NCM_NDP32_NOCRC_SIGN, \
.nth_size = sizeof(struct usb_cdc_ncm_nth32_hw), \
.ndp_size = sizeof(struct usb_cdc_ncm_ndp32_hw), \
.ndplen_align = 8, \
.dgram_item_len = 2, \
.block_length = 2, \
.fp_index = 2, \
.reserved1 = 1, \
.reserved2 = 2, \
.next_fp_index = 2, \
}
static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
{
switch (size) {
case 1:
put_unaligned_le16((u16)val, *p);
break;
case 2:
put_unaligned_le32((u32)val, *p);
break;
default:
BUG();
}
*p += size;
}
static inline unsigned get_ncm(__le16 **p, unsigned size)
{
unsigned tmp;
switch (size) {
case 1:
tmp = get_unaligned_le16(*p);
break;
case 2:
tmp = get_unaligned_le32(*p);
break;
default:
BUG();
}
*p += size;
return tmp;
}
#ifdef NCM_CONTROL_TIMEOUT
#undef NCM_CONTROL_TIMEOUT
#endif
#define NCM_CONTROL_TIMEOUT (5 * 1000)
/*#endif*/
/* 'u' must be of unsigned type */
#define IS_POWER2(u) (((u) > 0) && !((u) & ((u) - 1)))
/* 'p' must designate a variable of type * __le16 (in all get/put_ncm_leXX) */
#define get_ncm_le16(p) \
({ __le16 val = get_unaligned_le16(p); p += 1; val; })
#define get_ncm_le32(p) \
({ __le32 val = get_unaligned_le32(p); p += 2; val; })
#define put_ncm_le16(val, p) \
({ put_unaligned_le16((val), p); p += 1; })
#define put_ncm_le32(val, p) \
({ put_unaligned_le32((val), p); p += 2; })
#define NCM_NDP_MIN_ALIGNMENT 4
#ifdef NCM_NTB_MIN_IN_SIZE
#undef NCM_NTB_MIN_IN_SIZE
#endif
#define NCM_NTB_MIN_IN_SIZE 2048
#ifdef NCM_NTB_MIN_OUT_SIZE
#undef NCM_NTB_MIN_OUT_SIZE
#endif
#define NCM_NDP16_ENTRY_LEN 4
/* NTB16 must include: NTB16 header, NDP16 header, datagram pointer entry,
* terminating (NULL) datagram entry
*/
#define NCM_NTB_MIN_OUT_SIZE (sizeof(struct usb_cdc_ncm_nth16_hw) \
+ sizeof(struct usb_cdc_ncm_ndp16_hw) + 2 * NCM_NDP16_ENTRY_LEN)
#ifndef max
#define max(_a, _b) (((_a) > (_b)) ? (_a) : (_b))
#endif
#ifndef min
#define min(_a, _b) (((_a) < (_b)) ? (_a) : (_b))
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35)
#define NCM_NTB_HARD_MAX_IN_SIZE ((u32)(max(16,(int)ncm_read_size_in1k) * 1024))
#else
#define NCM_NTB_HARD_MAX_IN_SIZE ((u32)(max(2,(int)ncm_read_size_in1k) * 1024))
#endif
#define RX_QLEN_NCM ncm_read_buf_count
#define TX_QLEN_NCM 4
/* These are actually defined in usbnet.c and we need to redefine these here in
* order to calculate the size of the SKB pool
*/
static struct ndp_parser_opts_hw ndp16_opts = INIT_NDP16_OPTS;
static struct ndp_parser_opts_hw ndp32_opts = INIT_NDP32_OPTS;
struct ndp_entry {
struct list_head list;
unsigned idx;
unsigned len;
};
struct ntb {
/* Maximum possible length of this NTB */
unsigned max_len;
/* The current offset of the NDP */
unsigned ndp_off;
/* The current length of the NDP */
unsigned ndp_len;
/* End of the datagrams section */
unsigned dgrams_end;
/* Entries list (datagram index/lenght pairs) */
struct list_head entries;
/* Number of datagrams in this NTB */
unsigned ndgrams;
/* The SKB with the actual NTB data */
struct sk_buff *skb;
};
#define NTB_LEN(n) ((n)->ndp_off + (n)->ndp_len)
#define NTB_IS_EMPTY(n) ((n)->ndgrams == 0)
struct ncm_ctx {
struct usb_cdc_ncm_desc_hw *ncm_desc;
//struct usbnet *unet;
struct hw_cdc_net *ndev;
struct usb_interface *control;
struct usb_interface *data;
#define NTB_FORMAT_SUPPORTED_16BIT 0x0001
#define NTB_FORMAT_SUPPORTED_32BIT 0x0002
u16 formats;
u32 rx_max_ntb;
u32 tx_max_ntb;
u16 tx_divisor;
u16 tx_remainder;
u16 tx_align;
#define NCM_BIT_MODE_16 0
#define NCM_BIT_MODE_32 1
u8 bit_mode;
#define NCM_CRC_MODE_NO 0
#define NCM_CRC_MODE_YES 1
u8 crc_mode;
struct ndp_parser_opts_hw popts;
struct ntb curr_ntb;
spinlock_t tx_lock;
struct sk_buff **skb_pool;
unsigned skb_pool_size;
struct timer_list tx_timer;
/* The maximum amount of jiffies that a datagram can be held (in the
* current-NTB) before it must be sent on the bus
*/
unsigned long tx_timeout_jiffies;
#ifdef CONFIG_CDC_ENCAP_COMMAND
struct cdc_encap *cdc_encap_ctx;
#endif
};
struct hw_cdc_net{
/* housekeeping */
struct usb_device *udev;
struct usb_interface *intf;
const char *driver_name;
const char *driver_desc;
void *driver_priv;
wait_queue_head_t *wait;
struct mutex phy_mutex;
unsigned char suspend_count;
/* i/o info: pipes etc */
unsigned in, out;
struct usb_host_endpoint *status;
unsigned maxpacket;
struct timer_list delay;
/* protocol/interface state */
struct net_device *net;
struct net_device_stats stats;
int msg_enable;
unsigned long data [5];
u32 xid;
u32 hard_mtu; /* count any extra framing */
size_t rx_urb_size; /* size for rx urbs */
struct mii_if_info mii;
/* various kinds of pending driver work */
struct sk_buff_head rxq;
struct sk_buff_head txq;
struct sk_buff_head done;
struct urb *interrupt;
struct tasklet_struct bh;
struct work_struct kevent;
struct delayed_work status_work;//fangxiaozhi added for work
int qmi_sync;
unsigned long flags;
/*The state and buffer for the data of TLP*/
HW_TLP_BUF_STATE hw_tlp_buffer_state;
struct hw_cdc_tlp_tmp hw_tlp_tmp_buf;
/*indicate the download tlp feature is activated or not*/
int hw_tlp_download_is_actived;
/*Add for ncm */
int is_ncm;
struct ncm_ctx *ncm_ctx;
};
static inline struct usb_driver *driver_of(struct usb_interface *intf)
{
return to_usb_driver(intf->dev.driver);
}
/* Drivers that reuse some of the standard USB CDC infrastructure
* (notably, using multiple interfaces according to the CDC
* union descriptor) get some helper code.
*/
struct hw_dev_state {
struct usb_cdc_header_desc *header;
struct usb_cdc_union_desc *u;
struct usb_cdc_ether_desc *ether;
struct usb_interface *control;
struct usb_interface *data;
};
/* we record the state for each of our queued skbs */
enum skb_state {
illegal = 0,
tx_start, tx_done,
rx_start, rx_done, rx_cleanup
};
struct skb_data { /* skb->cb is one of these */
struct urb *urb;
struct hw_cdc_net *dev;
enum skb_state state;
size_t length;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define devdbg(hw_cdc_net, fmt, arg...) \
((void)(rt_debug && printk(KERN_ERR "Hw_cdc_driver######: " fmt "\n" , ## arg)))
#define deverr(hw_cdc_net, fmt, arg...) \
printk(KERN_ERR "%s: " fmt "\n" , (hw_cdc_net)->net->name , ## arg)
#define devwarn(hw_cdc_net, fmt, arg...) \
printk(KERN_WARNING "%s: " fmt "\n" , (hw_cdc_net)->net->name , ## arg)
#define devinfo(hw_cdc_net, fmt, arg...) \
printk(KERN_INFO "%s: " fmt "\n" , (hw_cdc_net)->net->name , ## arg); \
////////////////////////////////////////////////////////////////////////////////
static void hw_cdc_status(struct hw_cdc_net *dev, struct urb *urb);
static inline int hw_get_ethernet_addr(struct hw_cdc_net *dev);
static int hw_cdc_bind(struct hw_cdc_net *dev, struct usb_interface *intf);
void hw_cdc_unbind(struct hw_cdc_net *dev, struct usb_interface *intf);
int cdc_ncm_rx_fixup(struct hw_cdc_net *dev, struct sk_buff *skb);
struct sk_buff * cdc_ncm_tx_fixup(struct hw_cdc_net *dev, struct sk_buff *skb,
gfp_t mem_flags);
///////////////////////////
int hw_get_endpoints(struct hw_cdc_net *, struct usb_interface *);
void hw_skb_return (struct hw_cdc_net *, struct sk_buff *);
void hw_unlink_rx_urbs(struct hw_cdc_net *);
void hw_defer_kevent (struct hw_cdc_net *, int );
int hw_get_settings (struct net_device *, struct ethtool_cmd *);
int hw_set_settings (struct net_device *, struct ethtool_cmd *);
u32 hw_get_link (struct net_device *);
int hw_nway_reset(struct net_device *);
void hw_get_drvinfo (struct net_device *, struct ethtool_drvinfo *);
u32 hw_get_msglevel (struct net_device *);
void hw_set_msglevel (struct net_device *, u32 );
void hw_disconnect (struct usb_interface *);
int hw_cdc_probe (struct usb_interface *, const struct usb_device_id *);
int hw_resume (struct usb_interface *);
int hw_suspend (struct usb_interface *, pm_message_t );
//////////////////////////
/*Begin : fangxiaozhi added for work*/
static void hw_cdc_check_status_work(struct work_struct *work);
/*{
struct delayed_work *option_suspend_wq
}*/
/*End : fangxiaozhi added for work*/
/* handles CDC Ethernet and many other network "bulk data" interfaces */
int hw_get_endpoints(struct hw_cdc_net *dev, struct usb_interface *intf)
{
int tmp;
struct usb_host_interface *alt = NULL;
struct usb_host_endpoint *in = NULL, *out = NULL;
struct usb_host_endpoint *status = NULL;
for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
unsigned ep;
//in = out = status = NULL;
in = NULL;
out = NULL;
status = NULL;
alt = intf->altsetting + tmp;
/* take the first altsetting with in-bulk + out-bulk;
* remember any status endpoint, just in case;
* ignore other endpoints and altsetttings.
*/
for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
struct usb_host_endpoint *e;
int intr = 0;
e = alt->endpoint + ep;
switch (e->desc.bmAttributes) {
case USB_ENDPOINT_XFER_INT:
if (!usb_endpoint_dir_in(&e->desc)){
continue;
}
intr = 1;
/* FALLTHROUGH */
case USB_ENDPOINT_XFER_BULK:
break;
default:
continue;
}
if (usb_endpoint_dir_in(&e->desc)) {
if (!intr && !in){
in = e;
}else if (intr && !status){
status = e;
}
} else {
if (!out){
out = e;
}
}
}
if (in && out){
break;
}
}
if (!alt || !in || !out){
return -EINVAL;
}
if (alt->desc.bAlternateSetting != 0) {
tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
alt->desc.bAlternateSetting);
if (tmp < 0){
return tmp;
}
}
dev->in = usb_rcvbulkpipe (dev->udev,
in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
dev->out = usb_sndbulkpipe (dev->udev,
out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
dev->status = status;
return 0;
}
EXPORT_SYMBOL_GPL(hw_get_endpoints);
static void intr_complete (struct urb *urb);
static int init_status (struct hw_cdc_net *dev, struct usb_interface *intf)
{
char *buf = NULL;
unsigned pipe = 0;
unsigned maxp;
unsigned period;
pipe = usb_rcvintpipe (dev->udev,
dev->status->desc.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK);
maxp = usb_maxpacket (dev->udev, pipe, 0);
/* avoid 1 msec chatter: min 8 msec poll rate */
period = max ((int) dev->status->desc.bInterval,
(dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
buf = kmalloc (maxp, GFP_KERNEL);
if (buf) {
dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
if (!dev->interrupt) {
kfree (buf);
return -ENOMEM;
} else {
usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
buf, maxp, intr_complete, dev, period);
dev_dbg(&intf->dev,
"status ep%din, %d bytes period %d\n",
usb_pipeendpoint(pipe), maxp, period);
}
}
return 0;
}
/* Passes this packet up the stack, updating its accounting.
* Some link protocols batch packets, so their rx_fixup paths
* can return clones as well as just modify the original skb.
*/
void hw_skb_return (struct hw_cdc_net *dev, struct sk_buff *skb)
{
int status;
u32 sn;
if(skb->len > 128)
{
sn = be32_to_cpu(*(u32 *)(skb->data + 0x26));
devdbg(dev,"hw_skb_return,len:%d receive sn:%x, time:%ld-%ld",
skb->len,sn,current_kernel_time().tv_sec,current_kernel_time().tv_nsec);
}
else
{
sn = be32_to_cpu(*(u32 *)(skb->data + 0x2a));
devdbg(dev,"hw_skb_return,len:%d receive ack sn:%x, time:%ld-%ld",
skb->len,sn,current_kernel_time().tv_sec,current_kernel_time().tv_nsec);
}
skb->protocol = eth_type_trans (skb, dev->net);
dev->stats.rx_packets++;
dev->stats.rx_bytes += skb->len;
if (netif_msg_rx_status (dev)){
devdbg (dev, "< rx, len %zu, type 0x%x",
skb->len + sizeof (struct ethhdr), skb->protocol);
}
memset (skb->cb, 0, sizeof (struct skb_data));
status = netif_rx (skb);
if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev)){
devdbg (dev, "netif_rx status %d", status);
}
}
EXPORT_SYMBOL_GPL(hw_skb_return);
// unlink pending rx/tx; completion handlers do all other cleanup
static int unlink_urbs (struct hw_cdc_net *dev, struct sk_buff_head *q)
{
unsigned long flags;
struct sk_buff *skb, *skbnext;
int count = 0;
spin_lock_irqsave (&q->lock, flags);
for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
struct skb_data *entry;
struct urb *urb;
int retval;
entry = (struct skb_data *) skb->cb;
urb = entry->urb;
skbnext = skb->next;
// during some PM-driven resume scenarios,
// these (async) unlinks complete immediately
retval = usb_unlink_urb (urb);
if (retval != -EINPROGRESS && retval != 0){
devdbg (dev, "unlink urb err, %d", retval);
}
else
{
count++;
}
}
spin_unlock_irqrestore (&q->lock, flags);
return count;
}
// Flush all pending rx urbs
// minidrivers may need to do this when the MTU changes
void hw_unlink_rx_urbs(struct hw_cdc_net *dev)
{
if (netif_running(dev->net)) {
(void) unlink_urbs (dev, &dev->rxq);
tasklet_schedule(&dev->bh);
}
}
EXPORT_SYMBOL_GPL(hw_unlink_rx_urbs);
/*-------------------------------------------------------------------------
*
* Network Device Driver (peer link to "Host Device", from USB host)
*
*-------------------------------------------------------------------------*/
static int hw_change_mtu (struct net_device *net, int new_mtu)
{
struct hw_cdc_net *dev = netdev_priv(net);
int ll_mtu = new_mtu + net->hard_header_len;
int old_hard_mtu = dev->hard_mtu;
int old_rx_urb_size = dev->rx_urb_size;
if (new_mtu <= 0){
return -EINVAL;
}
// no second zero-length packet read wanted after mtu-sized packets
if ((ll_mtu % dev->maxpacket) == 0){
return -EDOM;
}
net->mtu = new_mtu;
dev->hard_mtu = net->mtu + net->hard_header_len;
if (dev->rx_urb_size == old_hard_mtu && !dev->is_ncm) {
dev->rx_urb_size = dev->hard_mtu;
if (dev->rx_urb_size > old_rx_urb_size)
{
hw_unlink_rx_urbs(dev);
}
}
devdbg(dev,"change mtu :%d, urb_size:%u",new_mtu,(u32)dev->rx_urb_size);
return 0;
}
/*-------------------------------------------------------------------------*/
//#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
static struct net_device_stats *hw_get_stats (struct net_device *net)
{
struct hw_cdc_net *dev = netdev_priv(net);
return &dev->stats;
}
//#endif
/*-------------------------------------------------------------------------*/
static void tx_defer_bh(struct hw_cdc_net *dev,
struct sk_buff *skb,
struct sk_buff_head *list)
{
unsigned long flags;
spin_lock_irqsave(&list->lock, flags);
__skb_unlink(skb, list);
spin_unlock(&list->lock);
spin_lock(&dev->done.lock);
__skb_queue_tail(&dev->done, skb);
if (1 <= dev->done.qlen){
tasklet_schedule(&dev->bh);
}
spin_unlock_irqrestore(&dev->done.lock, flags);
}
////////////////////////////////////////////
static HW_TLP_BUF_STATE submit_skb(struct hw_cdc_net *dev,
unsigned char *data,
unsigned int len)
{
struct sk_buff *skb;
struct skb_data * entry;
unsigned long flags;
if (len > dev->rx_urb_size){
devdbg(dev, "The package length is too large\n");
return HW_TLP_BUF_STATE_ERROR;
}
if ((skb = alloc_skb (len + NET_IP_ALIGN, GFP_ATOMIC)) == NULL) {
return HW_TLP_BUF_STATE_ERROR;
}
skb_reserve (skb, NET_IP_ALIGN);
entry = (struct skb_data *) skb->cb;
entry->urb = NULL;
entry->dev = dev;
entry->state = rx_done;
entry->length = skb->len;
memcpy(skb->data, data, len);
skb->len = len;
spin_lock_irqsave(&dev->done.lock, flags);