Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#include "tx_api.h"
#include "tx_thread.h"
#include "nx_api.h"
#include "ux_api.h"
#include "ux_network_driver.h"
usb_network_driver_initialized
usb_network_devices
...
...
_ux_network_driver_init()
...
...
_ux_network_driver_activate(void *, UINT (*)(void *, NX_PACKET *), void **, ULONG, ULONG)
...
...
_ux_network_driver_deactivate(void *, void *)
...
...
_ux_network_driver_entry(NX_IP_DRIVER *)
...
...
_ux_network_driver_packet_received(void *, NX_PACKET *)
...
...
_ux_network_driver_link_up(void *)
...
_ux_network_driver_link_down(void *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesusbxcommon/usbx_network/src/ux_network_driver.c
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** USBX Component */ /** */ /** USBX Network Driver for NETX 5.3 and above. */ /** */... /**************************************************************************/ /**************************************************************************/ #include "tx_api.h" #include "tx_thread.h" #include "nx_api.h" #include "ux_api.h" #include "ux_network_driver.h" 5 includes static UINT usb_network_driver_initialized; static USB_NETWORK_DEVICE_TYPE usb_network_devices[USB_NETWORK_DEVICE_MAX_INSTANCES]; ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _ux_network_init PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Chaoqiong Xiao, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is called by the application to initialize the */ /* USBX portion of the network driver. */ /* */ /* INPUT */ /* */ /* */ /* OUTPUT */ /* */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* Application */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */ /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */ /* verified memset and memcpy */ /* cases, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _ux_network_driver_init(VOID) { UINT status = NX_SUCCESS; /* Check if driver is already initialized. */ if (usb_network_driver_initialized == 0) { /* Driver is not initialized yet. */ usb_network_driver_initialized = 1; /* Reset the network device memory array. */ _ux_utility_memory_set(&usb_network_devices[0], 0, sizeof(usb_network_devices)); /* Use case of memset is verified. */ }if (usb_network_driver_initialized == 0) { ... } return(status); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _ux_network_driver_activate PORTABLE C */ /* 6.1.8 */ /* AUTHOR */ /* */ /* Chaoqiong Xiao, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* The USB network driver activate function is called as the USB instance */ /* is created. This API takes a pointer to the instance, and returns a */ /* ux_network_handle back to instance. Every time the instance receives */ /* a network packet, it should call ux_network_driver_packet_received with*/ /* ux_network_handle. */ /* */ /* INPUT */ /* */ /* ux_instance Instance of the USBX network class */ /* ux_network_device_write_function Address of the function to write a */ /* packet when sent by the application */ /* ux_network_handle Address where to store the network */ /* handle */ /* */ /* physical_address_msw Most significant word of network ad */ /* */ /* physical_address_lsw Least significant word of network ad */ /* */ /* */ /* */ /* OUTPUT */ /* */ /* Result */ /* */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* Application */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */ /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */ /* used UX prefix to refer to */ /* TX symbols instead of using */ /* them directly, */ /* resulting in version 6.1 */ /* 08-02-2021 Wen Wang Modified comment(s), */ /* fixed spelling error, */ /* resulting in version 6.1.8 */ /* */... /**************************************************************************/ UINT _ux_network_driver_activate(VOID *ux_instance, UINT(*ux_network_device_write_function)(VOID *, NX_PACKET *), VOID **ux_network_handle, ULONG physical_address_msw, ULONG physical_address_lsw) { UX_INTERRUPT_SAVE_AREA UINT i; /* Critical section. */ UX_DISABLE /* Find an available entry in the usb_network_devices table. */ for (i = 0; i < USB_NETWORK_DEVICE_MAX_INSTANCES; i++) { /* If the ptr to instance is NULL, we have a free entry. */ if (usb_network_devices[i].ux_network_device_usb_instance_ptr == NX_NULL) { /* Add the instance of the USBX class driver to the network device. */ usb_network_devices[i].ux_network_device_usb_instance_ptr = ux_instance; break; }if (usb_network_devices[i].ux_network_device_usb_instance_ptr == NX_NULL) { ... } }for (i = 0; i < USB_NETWORK_DEVICE_MAX_INSTANCES; i++) { ... } /* Unprotect the critical section. */ UX_RESTORE /* Did we reach the max number of instance ? */ if (i == USB_NETWORK_DEVICE_MAX_INSTANCES) { /* Report error to application. */ _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_MEMORY_INSUFFICIENT); /* Return error. */ return(USB_NETWORK_DRIVER_FAILURE); }if (i == USB_NETWORK_DEVICE_MAX_INSTANCES) { ... } /* Store the write function. */ usb_network_devices[i].ux_network_device_write_function = ux_network_device_write_function; /* Store the physical address of the network interface. */ usb_network_devices[i].ux_network_physical_address_msw = physical_address_msw; usb_network_devices[i].ux_network_physical_address_lsw = physical_address_lsw; /* Are we not under interrupt? */ if (TX_THREAD_GET_SYSTEM_STATE() == 0) { /* Note that we were activated by a thread. */ usb_network_devices[i].ux_network_device_activated_by_thread = UX_TRUE; /* Create deactivation sync objects. */ _ux_utility_mutex_create(&usb_network_devices[i].ux_network_device_deactivate_mutex, "usb network device mutex"); _ux_utility_semaphore_create(&usb_network_devices[i].ux_network_device_deactivate_semaphore, "usb network device semaphore", 0); }if (TX_THREAD_GET_SYSTEM_STATE() == 0) { ... } /* Is there an interface at the NETX level ? */ if (usb_network_devices[i].ux_network_device_interface_ptr) { /* Store the physical address at the NETX level. */ usb_network_devices[i].ux_network_device_interface_ptr -> nx_interface_physical_address_msw = physical_address_msw; usb_network_devices[i].ux_network_device_interface_ptr -> nx_interface_physical_address_lsw = physical_address_lsw; /* Is the link UP ? */ if (usb_network_devices[i].ux_network_device_interface_ptr -> nx_interface_link_up == NX_TRUE) /* Yes, store its state. */ usb_network_devices[i].ux_network_device_link_status = NX_TRUE; }if (usb_network_devices[i].ux_network_device_interface_ptr) { ... } else /* Link not yet up. */ usb_network_devices[i].ux_network_device_link_status = NX_FALSE; /* Is there a network handle associated ? */ if (ux_network_handle) /* Yes, the application wants to know its address. */ *ux_network_handle = (VOID*)&usb_network_devices[i]; /* The operation was successful. */ return(USB_NETWORK_DRIVER_SUCCESS); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _ux_network_driver_deactivate PORTABLE C */ /* 6.1.8 */ /* AUTHOR */ /* */ /* Chaoqiong Xiao, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* The USB network driver activate function is called as the USB instance */ /* is created. This API takes a pointer to the instance, and returns a */ /* ux_network_handle back to instance. Every time the instance receives */ /* a network packet, it should call ux_network_driver_packet_received with*/ /* ux_network_handle. */ /* */ /* INPUT */ /* */ /* ux_instance Instance of the USBX network class */ /* ux_network_device_write_function Address of the function to write a */ /* packet when sent by the application */ /* ux_network_handle Address where to store the network */ /* handle */ /* */ /* physical_address_msw Most significant word of network ad */ /* */ /* physical_address_lsw Least significant word of network ad */ /* */ /* */ /* */ /* OUTPUT */ /* */ /* Result */ /* */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* Application */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */ /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */ /* used UX prefix to refer to */ /* TX symbols instead of using */ /* them directly, */ /* resulting in version 6.1 */ /* 08-02-2021 Wen Wang Modified comment(s), */ /* fixed spelling error, */ /* resulting in version 6.1.8 */ /* */... /**************************************************************************/ UINT _ux_network_driver_deactivate(VOID *ux_instance, VOID *ux_network_handle) { UX_INTERRUPT_SAVE_AREA USB_NETWORK_DEVICE_TYPE *usb_network_device; UX_PARAMETER_NOT_USED(ux_instance); /* Check if the handle exists. */ if(ux_network_handle == NX_NULL) return(USB_NETWORK_DRIVER_FAILURE); /* Cast the network handle properly. */ usb_network_device = (USB_NETWORK_DEVICE_TYPE*) ux_network_handle; /* Critical section. */ UX_DISABLE /* The link is down. */ _ux_network_driver_link_down(ux_network_handle); /* Unprotect the critical section. */ UX_RESTORE /* Are the sync objects valid? */ if (usb_network_device -> ux_network_device_activated_by_thread) { /* Get mutex. */ _ux_utility_mutex_on(&usb_network_device -> ux_network_device_deactivate_mutex); /* Any threads in instance? */ if (usb_network_device -> ux_network_device_num_threads_inside != 0) { /* Signal that we're waiting. */ usb_network_device -> ux_network_device_deactivate_thread_waiting = UX_TRUE; /* Release mutex. */ _ux_utility_mutex_off(&usb_network_device -> ux_network_device_deactivate_mutex); /* Wait for last thread inside to resume us. */ _ux_utility_semaphore_get(&usb_network_device -> ux_network_device_deactivate_semaphore, UX_WAIT_FOREVER); /* We're done waiting. */ usb_network_device -> ux_network_device_deactivate_thread_waiting = UX_FALSE; }if (usb_network_device -> ux_network_device_num_threads_inside != 0) { ... } else { /* Release mutex. */ _ux_utility_mutex_off(&usb_network_device -> ux_network_device_deactivate_mutex); }else { ... } /* Delete sync objects. */ _ux_utility_mutex_delete(&usb_network_device -> ux_network_device_deactivate_mutex); _ux_utility_semaphore_delete(&usb_network_device -> ux_network_device_deactivate_semaphore); /* Reset for next activation. */ usb_network_device -> ux_network_device_activated_by_thread = UX_FALSE; }if (usb_network_device -> ux_network_device_activated_by_thread) { ... } /* All threads are outside of the instance, and can't re-enter because the link flag has been set to down. Now we can clean up. *//* ... */ /* Reset the instance pointer. */ usb_network_device -> ux_network_device_usb_instance_ptr = NX_NULL; /* And the write function ptr. */ usb_network_device -> ux_network_device_write_function = NX_NULL; /* The operation was successful. */ return(USB_NETWORK_DRIVER_SUCCESS); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _ux_network_driver_entry PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Chaoqiong Xiao, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is called by NETX. This is the dispatcher to all the */ /* NETX function to the driver layer. */ /* */ /* INPUT */ /* */ /* nx_ip_driver Pointer to the NX_IP driver instance */ /* */ /* OUTPUT */ /* */ /* Result */ /* */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* NETX */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */ /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */ /* used UX prefix to refer to */ /* TX symbols instead of using */ /* them directly, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ VOID _ux_network_driver_entry(NX_IP_DRIVER *nx_ip_driver) { UX_INTERRUPT_SAVE_AREA NX_IP *nx_ip; NX_PACKET *packet_ptr; ULONG *ethernet_frame_ptr; NX_INTERFACE *nx_interface_ptr; USB_NETWORK_DEVICE_TYPE *usb_network_device_ptr; UINT i; /* Get the pointer to the NX_IP instance. */ nx_ip = nx_ip_driver -> nx_ip_driver_ptr; /* Set the default status return. */ nx_ip_driver -> nx_ip_driver_status = NX_NOT_SUCCESSFUL; /* Get the pointer to the interface in local variable. */ nx_interface_ptr = nx_ip_driver -> nx_ip_driver_interface; /* Is this the ATTACH command? */ if (nx_ip_driver -> nx_ip_driver_command == NX_LINK_INTERFACE_ATTACH) { /* Critical section. */ UX_DISABLE /* Find an available entry in the usb_network_devices table. */ for (i = 0; i < USB_NETWORK_DEVICE_MAX_INSTANCES; i++) { /* If the interface pointer is NULL, it is free. */ if (usb_network_devices[i].ux_network_device_interface_ptr == NX_NULL) break; }for (i = 0; i < USB_NETWORK_DEVICE_MAX_INSTANCES; i++) { ... } /* Check if we have ran out of instances. */ if (i == USB_NETWORK_DEVICE_MAX_INSTANCES) /* No more instances, set the error code. */ nx_ip_driver -> nx_ip_driver_status = NX_NO_MORE_ENTRIES; else { /* Save the IP address in the network instance. */ usb_network_devices[i].ux_network_device_ip_instance = nx_ip; /* Save pointer to interface. */ usb_network_devices[i].ux_network_device_interface_ptr = nx_interface_ptr; /* Set the USB class instance in the additional link. This will be used by the USB class driver. */ nx_interface_ptr -> nx_interface_additional_link_info = (VOID *) &usb_network_devices[i]; /* The operation was successful. */ nx_ip_driver -> nx_ip_driver_status = NX_SUCCESS; }else { ... } /* Unprotect the critical section. */ UX_RESTORE }if (nx_ip_driver -> nx_ip_driver_command == NX_LINK_INTERFACE_ATTACH) { ... } else { /* Get the usb instance. */ usb_network_device_ptr = (USB_NETWORK_DEVICE_TYPE *) nx_interface_ptr -> nx_interface_additional_link_info; /* Identify command. */ switch(nx_ip_driver -> nx_ip_driver_command) { case NX_LINK_INITIALIZE: /* INIT command, set the interface parameters. */ nx_interface_ptr -> nx_interface_valid = NX_TRUE; nx_interface_ptr -> nx_interface_address_mapping_needed = NX_TRUE; nx_interface_ptr -> nx_interface_ip_mtu_size = NX_ETHERNET_MTU - NX_ETHERNET_SIZE; /* Set the link to down for now. */ nx_interface_ptr -> nx_interface_link_up = NX_FALSE; /* Check if instance exists. */ if (usb_network_device_ptr -> ux_network_device_usb_instance_ptr) { /* Store the physical address in the nx interface. */ nx_interface_ptr -> nx_interface_physical_address_msw = usb_network_device_ptr -> ux_network_physical_address_msw; nx_interface_ptr -> nx_interface_physical_address_lsw = usb_network_device_ptr -> ux_network_physical_address_lsw; }if (usb_network_device_ptr -> ux_network_device_usb_instance_ptr) { ... } else { /* Reset the physical address. */ nx_interface_ptr -> nx_interface_physical_address_msw = 0; nx_interface_ptr -> nx_interface_physical_address_lsw = 0; }else { ... } /* Operation is successful. */ nx_ip_driver -> nx_ip_driver_status = NX_SUCCESS; break; case NX_LINK_INITIALIZE: case NX_LINK_ENABLE: /* Set the link state to UP. */ nx_interface_ptr -> nx_interface_link_up = NX_TRUE; /* Reflect link state in network device. */ if (usb_network_device_ptr -> ux_network_device_usb_link_up == NX_TRUE) usb_network_device_ptr -> ux_network_device_link_status = NX_TRUE; else usb_network_device_ptr -> ux_network_device_link_status = NX_FALSE; nx_ip_driver -> nx_ip_driver_status = NX_SUCCESS; break; case NX_LINK_ENABLE: case NX_LINK_DISABLE: /* Set the link down. */ nx_interface_ptr -> nx_interface_link_up = NX_FALSE; usb_network_device_ptr -> ux_network_device_link_status = NX_FALSE; nx_ip_driver -> nx_ip_driver_status = NX_SUCCESS; break; case NX_LINK_DISABLE: case NX_LINK_PACKET_SEND: case NX_LINK_PACKET_BROADCAST: case NX_LINK_ARP_SEND: case NX_LINK_ARP_RESPONSE_SEND: case NX_LINK_RARP_SEND: /* Place the ethernet frame at the front of the packet. */ packet_ptr = nx_ip_driver -> nx_ip_driver_packet; /* Are the sync objects valid? */ if (usb_network_device_ptr -> ux_network_device_activated_by_thread == UX_TRUE) /* Get mutex for checking link state and setting our state. */ _ux_utility_mutex_on(&usb_network_device_ptr -> ux_network_device_deactivate_mutex); /* Do not send a packet if the link is not enabled. */ if (usb_network_device_ptr -> ux_network_device_link_status == NX_TRUE) { /* Increment number of threads inside this instance. */ usb_network_device_ptr -> ux_network_device_num_threads_inside++; /* Are the sync objects valid? */ if (usb_network_device_ptr -> ux_network_device_activated_by_thread == UX_TRUE) /* Release mutex. */ _ux_utility_mutex_off(&usb_network_device_ptr -> ux_network_device_deactivate_mutex); /* Adjust the prepend pointer. */ packet_ptr -> nx_packet_prepend_ptr = packet_ptr -> nx_packet_prepend_ptr - NX_ETHERNET_SIZE; /* Adjust the packet length. */ packet_ptr -> nx_packet_length = packet_ptr -> nx_packet_length + NX_ETHERNET_SIZE; /* Setup the ethernet frame pointer to build the ethernet frame. Back up another 2 bytes to get 32-bit word alignment. */ ethernet_frame_ptr = (ULONG*)(packet_ptr -> nx_packet_prepend_ptr - 2); /* Build the ethernet frame. */ *ethernet_frame_ptr = nx_ip_driver -> nx_ip_driver_physical_address_msw; *(ethernet_frame_ptr + 1) = nx_ip_driver -> nx_ip_driver_physical_address_lsw; *(ethernet_frame_ptr + 2) = (nx_interface_ptr -> nx_interface_physical_address_msw << 16) | (nx_interface_ptr -> nx_interface_physical_address_lsw >> 16); *(ethernet_frame_ptr + 3) = (nx_interface_ptr -> nx_interface_physical_address_lsw << 16); if ((nx_ip_driver -> nx_ip_driver_command == NX_LINK_ARP_SEND)|| (nx_ip_driver -> nx_ip_driver_command == NX_LINK_ARP_RESPONSE_SEND)) { *(ethernet_frame_ptr+3) |= NX_ETHERNET_ARP; }if ((nx_ip_driver -> nx_ip_driver_command == NX_LINK_ARP_SEND)|| (nx_ip_driver -> nx_ip_driver_command == NX_LINK_ARP_RESPONSE_SEND)) { ... } else if (nx_ip_driver -> nx_ip_driver_command == NX_LINK_RARP_SEND) { *(ethernet_frame_ptr+3) |= NX_ETHERNET_RARP; }else if (nx_ip_driver -> nx_ip_driver_command == NX_LINK_RARP_SEND) { ... } else { #ifdef FEATURE_NX_IPV6 if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V4) #endif /* FEATURE_NX_IPV6 */ *(ethernet_frame_ptr+3) |= NX_ETHERNET_IP; #ifdef FEATURE_NX_IPV6 else if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V6) *(ethernet_frame_ptr+3) |= NX_ETHERNET_IPV6; else { /* Unknown IP version */ /* free the packet that we will not send */ nx_packet_release(packet_ptr); nx_ip_driver -> nx_ip_driver_status = NX_NOT_SUCCESSFUL; break; }else { ... } /* ... */#endif /* FEATURE_NX_IPV6 */ }else { ... } /* Endian swapping if NX_LITTLE_ENDIAN is defined. */ NX_CHANGE_ULONG_ENDIAN(*(ethernet_frame_ptr)); NX_CHANGE_ULONG_ENDIAN(*(ethernet_frame_ptr+1)); NX_CHANGE_ULONG_ENDIAN(*(ethernet_frame_ptr+2)); NX_CHANGE_ULONG_ENDIAN(*(ethernet_frame_ptr+3)); /* Write the packet or queue it. */ nx_ip_driver -> nx_ip_driver_status = usb_network_device_ptr -> ux_network_device_write_function(usb_network_device_ptr -> ux_network_device_usb_instance_ptr, packet_ptr); /* Are the sync objects valid? */ if (usb_network_device_ptr -> ux_network_device_activated_by_thread == UX_TRUE) { /* Get mutex. */ _ux_utility_mutex_on(&usb_network_device_ptr -> ux_network_device_deactivate_mutex); /* Decrement number of threads in instance. */ usb_network_device_ptr -> ux_network_device_num_threads_inside--; /* No more threads in the instance? */ if (usb_network_device_ptr -> ux_network_device_num_threads_inside == 0) { /* Release mutex. */ _ux_utility_mutex_off(&usb_network_device_ptr -> ux_network_device_deactivate_mutex); /* Anyone waiting for us to exit? */ if (usb_network_device_ptr -> ux_network_device_deactivate_thread_waiting == UX_TRUE) /* Resume deactivate thread waiting. */ _ux_utility_semaphore_put(&usb_network_device_ptr -> ux_network_device_deactivate_semaphore); }if (usb_network_device_ptr -> ux_network_device_num_threads_inside == 0) { ... } else { /* Release mutex. */ _ux_utility_mutex_off(&usb_network_device_ptr -> ux_network_device_deactivate_mutex); }else { ... } }if (usb_network_device_ptr -> ux_network_device_activated_by_thread == UX_TRUE) { ... } }if (usb_network_device_ptr -> ux_network_device_link_status == NX_TRUE) { ... } else { /* Are the sync objects valid? */ if (usb_network_device_ptr -> ux_network_device_activated_by_thread == UX_TRUE) /* Release mutex. */ _ux_utility_mutex_off(&usb_network_device_ptr -> ux_network_device_deactivate_mutex); /* Link down, throw away packet. */ nx_packet_transmit_release(packet_ptr); nx_ip_driver -> nx_ip_driver_status = NX_SUCCESS; /* Report error to application. */ _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CLASS_CDC_ECM_LINK_STATE_DOWN_ERROR); }else { ... } break; case NX_LINK_RARP_SEND: case NX_LINK_UNINITIALIZE: usb_network_driver_initialized = 0; break; case NX_LINK_UNINITIALIZE: case NX_LINK_MULTICAST_JOIN: case NX_LINK_MULTICAST_LEAVE: case NX_LINK_GET_STATUS: case NX_LINK_GET_ERROR_COUNT: case NX_LINK_GET_RX_COUNT: case NX_LINK_GET_TX_COUNT: case NX_LINK_GET_ALLOC_ERRORS: case NX_LINK_GET_SPEED: case NX_LINK_GET_DUPLEX_TYPE: case NX_LINK_USER_COMMAND : default: /* Invalid driver request. */ nx_ip_driver -> nx_ip_driver_status = NX_UNHANDLED_COMMAND; /* Return the link status in the supplied return pointer. */ if(nx_ip_driver -> nx_ip_driver_return_ptr) *(nx_ip_driver -> nx_ip_driver_return_ptr) = (ULONG)0; break;default }switch (nx_ip_driver -> nx_ip_driver_command) { ... } }else { ... } /* We are done here. */ return; }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _ux_network_driver_packet_received PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Chaoqiong Xiao, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is called by USBX when a packet has been receiver over */ /* the USB. */ /* */ /* INPUT */ /* */ /* ux_network_handle Handle of the USB network instance */ /* packet_ptr Pointer to packet received */ /* */ /* OUTPUT */ /* */ /* Result */ /* */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* USBX */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */ /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ VOID _ux_network_driver_packet_received(VOID *ux_network_handle, NX_PACKET *packet_ptr) { USB_NETWORK_DEVICE_TYPE *usb_network_device_ptr = (USB_NETWORK_DEVICE_TYPE*) ux_network_handle; ULONG packet_type; NX_IP *nx_ip; /* Check the state of the Link. */ if (usb_network_device_ptr -> ux_network_device_link_status != NX_TRUE) { /* Link down, throw away packet. */ nx_packet_release(packet_ptr); return; }if (usb_network_device_ptr -> ux_network_device_link_status != NX_TRUE) { ... } /* Pickup the packet header to determine where the packet needs to be sent. *//* ... */ packet_type = _ux_utility_short_get_big_endian(packet_ptr -> nx_packet_prepend_ptr + 12); /* Storing in into the packet the interface. */ packet_ptr -> nx_packet_ip_interface = usb_network_device_ptr -> ux_network_device_interface_ptr; /* Get the IP instance. */ nx_ip = usb_network_device_ptr -> ux_network_device_ip_instance; /* Route the incoming packet according to its ethernet type. */ switch (packet_type) { case NX_ETHERNET_IP : /* Note: The length reported by some Ethernet hardware includes bytes after the packet as well as the Ethernet header. In some cases, the actual packet length after the Ethernet header should be derived from the length in the IP header (lower 16 bits of the first 32-bit word). *//* ... */ /* Clean off the Ethernet header. */ packet_ptr -> nx_packet_prepend_ptr = packet_ptr -> nx_packet_prepend_ptr + NX_ETHERNET_SIZE; /* Adjust the packet length. */ packet_ptr -> nx_packet_length = packet_ptr -> nx_packet_length - NX_ETHERNET_SIZE; /* Route to the ip receive function. */ _nx_ip_packet_deferred_receive(nx_ip, packet_ptr); break; case NX_ETHERNET_IP : case NX_ETHERNET_ARP : /* Clean off the Ethernet header. */ packet_ptr -> nx_packet_prepend_ptr = packet_ptr -> nx_packet_prepend_ptr + NX_ETHERNET_SIZE; /* Adjust the packet length. */ packet_ptr -> nx_packet_length = packet_ptr -> nx_packet_length - NX_ETHERNET_SIZE; /* Route to the ARP receive function. */ _nx_arp_packet_deferred_receive(nx_ip, packet_ptr); break; case NX_ETHERNET_ARP : case NX_ETHERNET_RARP : /* Clean off the Ethernet header. */ packet_ptr -> nx_packet_prepend_ptr = packet_ptr -> nx_packet_prepend_ptr + NX_ETHERNET_SIZE; /* Adjust the packet length. */ packet_ptr -> nx_packet_length = packet_ptr -> nx_packet_length - NX_ETHERNET_SIZE; /* Route to the RARP receive function. */ _nx_rarp_packet_deferred_receive(nx_ip, packet_ptr); break; case NX_ETHERNET_RARP : default : /* Invalid ethernet header... release the packet. */ nx_packet_release(packet_ptr); default }switch (packet_type) { ... } }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _ux_network_driver_link_up PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Chaoqiong Xiao, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is called by USBX when the line link is up */ /* */ /* INPUT */ /* */ /* ux_network_handle Handle of the USB network instance */ /* */ /* OUTPUT */ /* */ /* Result */ /* */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* USBX */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */ /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ VOID _ux_network_driver_link_up(VOID *ux_network_handle) { USB_NETWORK_DEVICE_TYPE *usb_network_device_ptr = (USB_NETWORK_DEVICE_TYPE*)ux_network_handle; /* The USB side of the link is UP. */ usb_network_device_ptr -> ux_network_device_usb_link_up = NX_TRUE; /* Check if there is an existing interface. */ if (usb_network_device_ptr -> ux_network_device_interface_ptr) { /* Set link status. */ if (usb_network_device_ptr -> ux_network_device_interface_ptr -> nx_interface_link_up) usb_network_device_ptr -> ux_network_device_link_status = NX_TRUE; else usb_network_device_ptr -> ux_network_device_link_status = NX_FALSE; }if (usb_network_device_ptr -> ux_network_device_interface_ptr) { ... } }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _ux_network_driver_link_down PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Chaoqiong Xiao, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is called by USBX when the link is down. */ /* */ /* INPUT */ /* */ /* ux_network_handle Handle of the USB network instance */ /* */ /* OUTPUT */ /* */ /* Result */ /* */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* USBX */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */ /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ VOID _ux_network_driver_link_down(VOID *ux_network_handle) { USB_NETWORK_DEVICE_TYPE *usb_network_device_ptr = (USB_NETWORK_DEVICE_TYPE*)ux_network_handle; /* Set the USB link status. */ usb_network_device_ptr -> ux_network_device_usb_link_up = NX_FALSE; /* Set the link status. */ usb_network_device_ptr -> ux_network_device_link_status = NX_FALSE; }{ ... }