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 CDC_ECM 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_cdc_ecm.h"
29 #include "ux_device_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_device_class_cdc_ecm_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 /*    cdc_ecm                               Address of cdc_ecm class      */
50 /*                                          instance                      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    None                                                                */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*   _ux_device_stack_transfer_request      Transfer request              */
59 /*   _ux_device_mutex_off                   Release 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_cdc_ecm_write(VOID * cdc_ecm_class,NX_PACKET * packet)81 UINT  _ux_device_class_cdc_ecm_write(VOID *cdc_ecm_class, NX_PACKET *packet)
82 {
83 #if defined(UX_DEVICE_STANDALONE)
84     UX_PARAMETER_NOT_USED(cdc_ecm_class);
85     UX_PARAMETER_NOT_USED(packet);
86     return(UX_FUNCTION_NOT_SUPPORTED);
87 #else
88 
89 UINT                        status;
90 UX_SLAVE_CLASS_CDC_ECM      *cdc_ecm;
91 
92     /* Proper class casting.  */
93     cdc_ecm = (UX_SLAVE_CLASS_CDC_ECM *) cdc_ecm_class;
94 
95     /* Protect this thread.  */
96     _ux_device_mutex_on(&cdc_ecm -> ux_slave_class_cdc_ecm_mutex);
97 
98     /* We only want to send the packet if the link is up.  */
99     if (cdc_ecm->ux_slave_class_cdc_ecm_link_state == UX_DEVICE_CLASS_CDC_ECM_LINK_STATE_UP)
100     {
101 
102         /* Check the queue. See if there is something that is being sent.  */
103         if (cdc_ecm -> ux_slave_class_cdc_ecm_xmit_queue == UX_NULL)
104 
105             /* Memorize this packet at the beginning of the queue.  */
106             cdc_ecm -> ux_slave_class_cdc_ecm_xmit_queue =  packet;
107 
108         else
109 
110             /* Add the packet to the end of the queue.  */
111             cdc_ecm -> ux_slave_class_cdc_ecm_xmit_queue_tail -> nx_packet_queue_next =  packet;
112 
113         /* Set the tail.  */
114         cdc_ecm -> ux_slave_class_cdc_ecm_xmit_queue_tail =  packet;
115 
116         /* The packet to be sent is the last in the chain.  */
117         packet -> nx_packet_queue_next =  NX_NULL;
118 
119         /* Free Mutex resource.  */
120         _ux_device_mutex_off(&cdc_ecm -> ux_slave_class_cdc_ecm_mutex);
121 
122         /* Set an event to wake up the bulkin thread.  */
123         _ux_device_event_flags_set(&cdc_ecm -> ux_slave_class_cdc_ecm_event_flags_group, UX_DEVICE_CLASS_CDC_ECM_NEW_BULKIN_EVENT, UX_OR);
124 
125         /* Packet successfully added. Return success.  */
126         status =  UX_SUCCESS;
127     }
128     else
129     {
130 
131         /* Free Mutex resource.  */
132         _ux_device_mutex_off(&cdc_ecm -> ux_slave_class_cdc_ecm_mutex);
133 
134         /* Report error to application.  */
135         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CLASS_CDC_ECM_LINK_STATE_DOWN_ERROR);
136 
137         /* Return error.  */
138         status =  UX_ERROR;
139     }
140 
141     /* We are done here.  */
142     return(status);
143 #endif
144 }
145