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 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_acm.h"
28 #include "ux_device_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_class_cdc_acm_write_with_callback        PORTABLE C      */
36 /*                                                           6.3.0        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function writes to  the CDC class with callback                */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    cdc_acm                               Address of cdc_acm class      */
48 /*                                                instance                */
49 /*    buffer                                Pointer to data to write      */
50 /*    requested_length                      Length of bytes to write      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    None                                                                */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*   _ux_device_stack_transfer_request                                    */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    Application                                                         */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
69 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
70 /*                                            used UX prefix to refer to  */
71 /*                                            TX symbols instead of using */
72 /*                                            them directly,              */
73 /*                                            resulting in version 6.1    */
74 /*  04-02-2021     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            added macro to disable      */
76 /*                                            transmission support,       */
77 /*                                            resulting in version 6.1.6  */
78 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            added standalone support,   */
80 /*                                            resulting in version 6.1.10 */
81 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
82 /*                                            resulting in version 6.1.11 */
83 /*  10-31-2023     Yajun Xia                Modified comment(s),          */
84 /*                                            resulting in version 6.3.0  */
85 /*                                                                        */
86 /**************************************************************************/
_ux_device_class_cdc_acm_write_with_callback(UX_SLAVE_CLASS_CDC_ACM * cdc_acm,UCHAR * buffer,ULONG requested_length)87 UINT _ux_device_class_cdc_acm_write_with_callback(UX_SLAVE_CLASS_CDC_ACM *cdc_acm, UCHAR *buffer,
88                                 ULONG requested_length)
89 {
90 #ifdef UX_DEVICE_CLASS_CDC_ACM_TRANSMISSION_DISABLE
91     UX_PARAMETER_NOT_USED(cdc_acm);
92     UX_PARAMETER_NOT_USED(buffer);
93     UX_PARAMETER_NOT_USED(requested_length);
94     return(UX_FUNCTION_NOT_SUPPORTED);
95 #else
96 UX_SLAVE_DEVICE             *device;
97 UINT                        status;
98 
99     /* If trace is enabled, insert this event into the trace buffer.  */
100     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_CDC_ACM_WRITE, cdc_acm, buffer, requested_length, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
101 
102     /* Get the pointer to the device.  */
103     device =  &_ux_system_slave -> ux_system_slave_device;
104 
105     /* As long as the device is in the CONFIGURED state.  */
106     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
107     {
108 
109         /* Error trap. */
110         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
111 
112         /* If trace is enabled, insert this event into the trace buffer.  */
113         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, device, 0, 0, UX_TRACE_ERRORS, 0, 0)
114 
115         /* Cannot proceed with command, the interface is down.  */
116         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
117     }
118 
119     /* Are we already in transmission mode ?  */
120     if (cdc_acm -> ux_slave_class_cdc_acm_transmission_status != UX_TRUE)
121     {
122 
123         /* We should not to that ! */
124         return(UX_ERROR);
125 
126     }
127 
128     /* Have we already scheduled a buffer ?   */
129     if (cdc_acm -> ux_slave_class_cdc_acm_scheduled_write == UX_TRUE)
130     {
131 
132         /* We should not to that ! */
133         return(UX_ERROR);
134     }
135 
136 #if defined(UX_DEVICE_STANDALONE)
137 
138     /* Save the length to be sent. */
139     cdc_acm -> ux_device_class_cdc_acm_write_requested_length = requested_length;
140 
141     /* And the buffer pointer.  */
142     cdc_acm -> ux_device_class_cdc_acm_write_buffer = buffer;
143 
144     /* Schedule a transmission.  */
145     cdc_acm -> ux_slave_class_cdc_acm_scheduled_write = UX_TRUE;
146 
147     /* Status success.  */
148     status = (UX_SUCCESS);
149 #else
150 
151     /* Save the length to be sent. */
152     cdc_acm -> ux_slave_class_cdc_acm_callback_total_length = requested_length;
153 
154     /* And the buffer pointer.  */
155     cdc_acm -> ux_slave_class_cdc_acm_callback_data_pointer = buffer;
156 
157     /* Schedule a transmission.  */
158     cdc_acm -> ux_slave_class_cdc_acm_scheduled_write = UX_TRUE;
159 
160     /* Invoke the bulkin thread by sending a flag .  */
161     status = _ux_device_event_flags_set(&cdc_acm -> ux_slave_class_cdc_acm_event_flags_group, UX_DEVICE_CLASS_CDC_ACM_WRITE_EVENT, UX_OR);
162 #endif
163 
164     /* Simply return the last function result.  When we leave this function, the deferred writing has been scheduled. */
165     return(status);
166 #endif
167 }
168 
169 /**************************************************************************/
170 /*                                                                        */
171 /*  FUNCTION                                               RELEASE        */
172 /*                                                                        */
173 /*    _uxe_device_class_cdc_acm_write_with_callback       PORTABLE C      */
174 /*                                                           6.3.0        */
175 /*  AUTHOR                                                                */
176 /*                                                                        */
177 /*    Yajun Xia, Microsoft Corporation                                    */
178 /*                                                                        */
179 /*  DESCRIPTION                                                           */
180 /*                                                                        */
181 /*    This function checks errors in CDC ACM class write with             */
182 /*    callback function.                                                  */
183 /*                                                                        */
184 /*  INPUT                                                                 */
185 /*                                                                        */
186 /*    cdc_acm                               Address of cdc_acm class      */
187 /*                                                instance                */
188 /*    buffer                                Pointer to data to write      */
189 /*    requested_length                      Length of bytes to write      */
190 /*                                                                        */
191 /*  OUTPUT                                                                */
192 /*                                                                        */
193 /*    None                                                                */
194 /*                                                                        */
195 /*  CALLS                                                                 */
196 /*                                                                        */
197 /*   _ux_device_class_cdc_acm_write_with_callback                         */
198 /*                                                                        */
199 /*  CALLED BY                                                             */
200 /*                                                                        */
201 /*    Application                                                         */
202 /*                                                                        */
203 /*  RELEASE HISTORY                                                       */
204 /*                                                                        */
205 /*    DATE              NAME                      DESCRIPTION             */
206 /*                                                                        */
207 /*  10-31-2023     Yajun Xia                Initial Version 6.3.0         */
208 /*                                                                        */
209 /**************************************************************************/
_uxe_device_class_cdc_acm_write_with_callback(UX_SLAVE_CLASS_CDC_ACM * cdc_acm,UCHAR * buffer,ULONG requested_length)210 UINT _uxe_device_class_cdc_acm_write_with_callback(UX_SLAVE_CLASS_CDC_ACM *cdc_acm, UCHAR *buffer,
211                                                    ULONG requested_length)
212 {
213 
214     /* Sanity checks.  */
215     if ((cdc_acm == UX_NULL) || ((buffer == UX_NULL) && (requested_length > 0)))
216     {
217         return (UX_INVALID_PARAMETER);
218     }
219 
220     return (_ux_device_class_cdc_acm_write_with_callback(cdc_acm, buffer, requested_length));
221 }
222