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 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_acm.h"
29 #include "ux_device_stack.h"
30 
31 
32 #if !defined(UX_DEVICE_STANDALONE)
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_device_class_cdc_acm_write                      PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function writes to  the CDC class.                             */
46 /*                                                                        */
47 /*    It's for RTOS mode.                                                 */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    cdc_acm                               Address of cdc_acm class      */
52 /*                                                instance                */
53 /*    buffer                                Pointer to data to write      */
54 /*    requested_length                      Length of bytes to write,     */
55 /*                                                set to 0 to issue ZLP   */
56 /*    actual_length                         Pointer to save number of     */
57 /*                                                bytes written           */
58 /*                                                                        */
59 /*  OUTPUT                                                                */
60 /*                                                                        */
61 /*    None                                                                */
62 /*                                                                        */
63 /*  CALLS                                                                 */
64 /*                                                                        */
65 /*   _ux_utility_memory_copy                Copy memory                   */
66 /*   _ux_device_stack_transfer_request      Transfer request              */
67 /*   _ux_device_mutex_on                    Take Mutex                    */
68 /*   _ux_device_mutex_off                   Release Mutex                 */
69 /*                                                                        */
70 /*  CALLED BY                                                             */
71 /*                                                                        */
72 /*    ThreadX                                                             */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
79 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            verified memset and memcpy  */
81 /*                                            cases,                      */
82 /*                                            resulting in version 6.1    */
83 /*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
84 /*                                            fixed compile issue,        */
85 /*                                            resulting in version 6.1.9  */
86 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
87 /*                                            resulting in version 6.1.10 */
88 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
89 /*                                            resulting in version 6.1.11 */
90 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
91 /*                                            fixed parameter/variable    */
92 /*                                            names conflict C++ keyword, */
93 /*                                            added auto ZLP support,     */
94 /*                                            resulting in version 6.1.12 */
95 /*                                                                        */
96 /**************************************************************************/
_ux_device_class_cdc_acm_write(UX_SLAVE_CLASS_CDC_ACM * cdc_acm,UCHAR * buffer,ULONG requested_length,ULONG * actual_length)97 UINT _ux_device_class_cdc_acm_write(UX_SLAVE_CLASS_CDC_ACM *cdc_acm, UCHAR *buffer,
98                                 ULONG requested_length, ULONG *actual_length)
99 {
100 
101 UX_SLAVE_ENDPOINT           *endpoint;
102 UX_SLAVE_DEVICE             *device;
103 UX_SLAVE_INTERFACE          *interface_ptr;
104 UX_SLAVE_TRANSFER           *transfer_request;
105 ULONG                       local_requested_length;
106 ULONG                       local_host_length;
107 UINT                        status = 0;
108 
109     /* If trace is enabled, insert this event into the trace buffer.  */
110     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)
111 
112 #ifndef UX_DEVICE_CLASS_CDC_ACM_TRANSMISSION_DISABLE
113 
114     /* Check if current cdc-acm is using callback or not. We cannot use direct reads with callback on.  */
115     if (cdc_acm -> ux_slave_class_cdc_acm_transmission_status == UX_TRUE)
116 
117         /* Not allowed. */
118         return(UX_ERROR);
119 #endif
120 
121     /* Get the pointer to the device.  */
122     device =  &_ux_system_slave -> ux_system_slave_device;
123 
124     /* As long as the device is in the CONFIGURED state.  */
125     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
126     {
127 
128         /* Error trap. */
129         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
130 
131         /* If trace is enabled, insert this event into the trace buffer.  */
132         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, device, 0, 0, UX_TRACE_ERRORS, 0, 0)
133 
134         /* Cannot proceed with command, the interface is down.  */
135         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
136     }
137 
138     /* We need the interface to the class.  */
139     interface_ptr =  cdc_acm -> ux_slave_class_cdc_acm_interface;
140 
141     /* Locate the endpoints.  */
142     endpoint =  interface_ptr -> ux_slave_interface_first_endpoint;
143 
144     /* Check the endpoint direction, if IN we have the correct endpoint.  */
145     if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_IN)
146     {
147 
148         /* So the next endpoint has to be the IN endpoint.  */
149         endpoint =  endpoint -> ux_slave_endpoint_next_endpoint;
150     }
151 
152     /* Protect this thread.  */
153     _ux_device_mutex_on(&cdc_acm -> ux_slave_class_cdc_acm_endpoint_in_mutex);
154 
155     /* We are writing to the IN endpoint.  */
156     transfer_request =  &endpoint -> ux_slave_endpoint_transfer_request;
157 
158     /* Reset the actual length.  */
159     *actual_length =  0;
160 
161     /* Check if the application forces a 0 length packet.  */
162     if (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED && requested_length == 0)
163     {
164 
165         /* Send the request for 0 byte packet to the device controller.  */
166         status =  _ux_device_stack_transfer_request(transfer_request, 0, 0);
167 
168         /* Free Mutex resource.  */
169         _ux_device_mutex_off(&cdc_acm -> ux_slave_class_cdc_acm_endpoint_in_mutex);
170 
171         /* Return the status.  */
172         return(status);
173 
174 
175     }
176     else
177     {
178         /* Check if we need more transactions.  */
179         local_host_length = UX_SLAVE_REQUEST_DATA_MAX_LENGTH;
180         while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED && requested_length != 0)
181         {
182 
183             /* Check if we have enough in the local buffer.  */
184             if (requested_length > UX_SLAVE_REQUEST_DATA_MAX_LENGTH)
185 
186                 /* We have too much to transfer.  */
187                 local_requested_length = UX_SLAVE_REQUEST_DATA_MAX_LENGTH;
188 
189             else
190             {
191 
192                 /* We can proceed with the demanded length.  */
193                 local_requested_length = requested_length;
194 
195 #if !defined(UX_DEVICE_CLASS_CDC_ACM_WRITE_AUTO_ZLP)
196 
197                 /* Assume the length match expectation.  */
198                 local_host_length = requested_length;
199 #else
200 
201                 /* Assume expecting more, so ZLP is appended in stack.  */
202                 local_host_length = UX_SLAVE_REQUEST_DATA_MAX_LENGTH + 1;
203 #endif
204             }
205 
206             /* On a out, we copy the buffer to the caller. Not very efficient but it makes the API
207                easier.  */
208             _ux_utility_memory_copy(transfer_request -> ux_slave_transfer_request_data_pointer,
209                                 buffer, local_requested_length); /* Use case of memcpy is verified. */
210 
211             /* Send the request to the device controller.  */
212             status =  _ux_device_stack_transfer_request(transfer_request, local_requested_length, local_host_length);
213 
214             /* Check the status */
215             if (status == UX_SUCCESS)
216             {
217 
218                 /* Next buffer address.  */
219                 buffer += transfer_request -> ux_slave_transfer_request_actual_length;
220 
221                 /* Set the length actually received. */
222                 *actual_length += transfer_request -> ux_slave_transfer_request_actual_length;
223 
224                 /* Decrement what left has to be done.  */
225                 requested_length -= transfer_request -> ux_slave_transfer_request_actual_length;
226 
227             }
228 
229             else
230             {
231 
232                 /* Free Mutex resource.  */
233                 _ux_device_mutex_off(&cdc_acm -> ux_slave_class_cdc_acm_endpoint_in_mutex);
234 
235                 /* We had an error, abort.  */
236                 return(status);
237             }
238         }
239     }
240 
241 
242     /* Free Mutex resource.  */
243     _ux_device_mutex_off(&cdc_acm -> ux_slave_class_cdc_acm_endpoint_in_mutex);
244 
245     /* Check why we got here, either completion or device was extracted.  */
246     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
247     {
248 
249         /* Error trap. */
250         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_NO_ANSWER);
251 
252         /* If trace is enabled, insert this event into the trace buffer.  */
253         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_TRANSFER_NO_ANSWER, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0)
254 
255         /* Device must have been extracted.  */
256         return (UX_TRANSFER_NO_ANSWER);
257     }
258     else
259 
260         /* Simply return the last transaction result.  */
261         return(status);
262 
263 }
264 #endif
265