1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Device RNDIS Class                                                  */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define UX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "ux_api.h"
28 #include "ux_device_class_rndis.h"
29 #include "ux_device_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_device_class_rndis_write                        PORTABLE C      */
37 /*                                                           6.1.11       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function writes a packet into a queue for later thread         */
45 /*    processing.                                                         */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    rndis                                   Address of rndis class      */
50 /*                                                instance                */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    None                                                                */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*   _ux_device_mutex_on                     Take mutex                  */
59 /*   _ux_device_mutex_off                    Free mutex                  */
60 /*   _ux_device_event_flags_set              Set event flags             */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    ThreadX                                                             */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
71 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            used UX prefix to refer to  */
73 /*                                            TX symbols instead of using */
74 /*                                            them directly,              */
75 /*                                            resulting in version 6.1    */
76 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            fixed standalone compile,   */
78 /*                                            resulting in version 6.1.11 */
79 /*                                                                        */
80 /**************************************************************************/
_ux_device_class_rndis_write(VOID * rndis_class,NX_PACKET * packet)81 UINT  _ux_device_class_rndis_write(VOID *rndis_class, NX_PACKET *packet)
82 {
83 #if defined(UX_DEVICE_STANDALONE)
84     UX_PARAMETER_NOT_USED(rndis_class);
85     UX_PARAMETER_NOT_USED(packet);
86     return(UX_FUNCTION_NOT_SUPPORTED);
87 #else
88 
89 NX_PACKET               *current_packet;
90 NX_PACKET               *next_packet;
91 UX_SLAVE_CLASS_RNDIS     *rndis;
92 
93     /* Proper class casting.  */
94     rndis = (UX_SLAVE_CLASS_RNDIS *) rndis_class;
95 
96     /* Protect this thread.  */
97     _ux_device_mutex_on(&rndis -> ux_slave_class_rndis_mutex);
98 
99     /* Check the queue. See if there is something that is being sent. */
100     if (rndis -> ux_slave_class_rndis_xmit_queue == UX_NULL)
101 
102         /* Memorize this packet at the beginning of the queue.  */
103         rndis -> ux_slave_class_rndis_xmit_queue = packet;
104 
105     else
106 
107     {
108 
109         /* We get here when there is something in the queue.  */
110         current_packet =  rndis -> ux_slave_class_rndis_xmit_queue;
111 
112         /* Get the next packet associated with the first packet.  */
113         next_packet = current_packet -> nx_packet_queue_next;
114 
115         /* Parse the current chain for the end.  */
116         while (next_packet != NX_NULL)
117         {
118             /* Remember the current packet.  */
119             current_packet = next_packet;
120 
121             /* See what the next packet in the chain is.  */
122             next_packet = current_packet -> nx_packet_queue_next;
123         }
124 
125         /* Memorize the packet to be sent.  */
126         current_packet -> nx_packet_queue_next = packet;
127 
128     }
129 
130     /* Free Mutex resource.  */
131     _ux_device_mutex_off(&rndis -> ux_slave_class_rndis_mutex);
132 
133     /* The packet to be sent is the last in the chain.  */
134     packet -> nx_packet_queue_next = NX_NULL;
135 
136     /* Set an event to wake up the bulkin thread.  */
137     _ux_device_event_flags_set(&rndis -> ux_slave_class_rndis_event_flags_group, UX_DEVICE_CLASS_RNDIS_NEW_BULKIN_EVENT, UX_OR);
138 
139     /* We are done here.  */
140     return(UX_SUCCESS);
141 #endif
142 }
143