1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 /**************************************************************************/
12 /**************************************************************************/
13 /**                                                                       */
14 /** USBX Component                                                        */
15 /**                                                                       */
16 /**   Device CDC_ECM Class                                                */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 #define UX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "ux_api.h"
27 #include "ux_device_class_cdc_ecm.h"
28 #include "ux_device_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_class_cdc_ecm_write                      PORTABLE C      */
36 /*                                                           6.1.11       */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function writes a packet into a queue for later thread         */
44 /*    processing.                                                         */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    cdc_ecm                               Address of cdc_ecm class      */
49 /*                                          instance                      */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    None                                                                */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*   _ux_device_stack_transfer_request      Transfer request              */
58 /*   _ux_device_mutex_off                   Release mutex                 */
59 /*   _ux_device_event_flags_set             Set event flags               */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    ThreadX                                                             */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            used UX prefix to refer to  */
72 /*                                            TX symbols instead of using */
73 /*                                            them directly,              */
74 /*                                            resulting in version 6.1    */
75 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            fixed standalone compile,   */
77 /*                                            resulting in version 6.1.11 */
78 /*                                                                        */
79 /**************************************************************************/
_ux_device_class_cdc_ecm_write(VOID * cdc_ecm_class,NX_PACKET * packet)80 UINT  _ux_device_class_cdc_ecm_write(VOID *cdc_ecm_class, NX_PACKET *packet)
81 {
82 #if defined(UX_DEVICE_STANDALONE)
83     UX_PARAMETER_NOT_USED(cdc_ecm_class);
84     UX_PARAMETER_NOT_USED(packet);
85     return(UX_FUNCTION_NOT_SUPPORTED);
86 #else
87 
88 UINT                        status;
89 UX_SLAVE_CLASS_CDC_ECM      *cdc_ecm;
90 
91     /* Proper class casting.  */
92     cdc_ecm = (UX_SLAVE_CLASS_CDC_ECM *) cdc_ecm_class;
93 
94     /* Protect this thread.  */
95     _ux_device_mutex_on(&cdc_ecm -> ux_slave_class_cdc_ecm_mutex);
96 
97     /* We only want to send the packet if the link is up.  */
98     if (cdc_ecm->ux_slave_class_cdc_ecm_link_state == UX_DEVICE_CLASS_CDC_ECM_LINK_STATE_UP)
99     {
100 
101         /* Check the queue. See if there is something that is being sent.  */
102         if (cdc_ecm -> ux_slave_class_cdc_ecm_xmit_queue == UX_NULL)
103 
104             /* Memorize this packet at the beginning of the queue.  */
105             cdc_ecm -> ux_slave_class_cdc_ecm_xmit_queue =  packet;
106 
107         else
108 
109             /* Add the packet to the end of the queue.  */
110             cdc_ecm -> ux_slave_class_cdc_ecm_xmit_queue_tail -> nx_packet_queue_next =  packet;
111 
112         /* Set the tail.  */
113         cdc_ecm -> ux_slave_class_cdc_ecm_xmit_queue_tail =  packet;
114 
115         /* The packet to be sent is the last in the chain.  */
116         packet -> nx_packet_queue_next =  NX_NULL;
117 
118         /* Free Mutex resource.  */
119         _ux_device_mutex_off(&cdc_ecm -> ux_slave_class_cdc_ecm_mutex);
120 
121         /* Set an event to wake up the bulkin thread.  */
122         _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);
123 
124         /* Packet successfully added. Return success.  */
125         status =  UX_SUCCESS;
126     }
127     else
128     {
129 
130         /* Free Mutex resource.  */
131         _ux_device_mutex_off(&cdc_ecm -> ux_slave_class_cdc_ecm_mutex);
132 
133         /* Report error to application.  */
134         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CLASS_CDC_ECM_LINK_STATE_DOWN_ERROR);
135 
136         /* Return error.  */
137         status =  UX_ERROR;
138     }
139 
140     /* We are done here.  */
141     return(status);
142 #endif
143 }
144