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 /**                                                                       */
16 /** USBX Component                                                        */
17 /**                                                                       */
18 /**   CDC ACM Class                                                       */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /* Include necessary system files.  */
25 
26 #define UX_SOURCE_CODE
27 
28 #include "ux_api.h"
29 #include "ux_host_class_cdc_acm.h"
30 #include "ux_host_stack.h"
31 
32 
33 #if defined(UX_HOST_STANDALONE)
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _ux_host_class_cdc_acm_write_with_callback          PORTABLE C      */
39 /*                                                           6.1.10       */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Chaoqiong Xiao, Microsoft Corporation                               */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function writes to the cdc_acm interface. The call starts      */
47 /*    background transmission and returns immediately.                    */
48 /*    A callback is invoked when background transmission is done or there */
49 /*    is error during transmission.                                       */
50 /*                                                                        */
51 /*    Following IOCTL functions can be used with this function:           */
52 /*    - _WRITE_CALLBACK                     Set the callback function     */
53 /*    - _GET_WRITE_STATUS                   Return UX_BUSY if transfer is */
54 /*                                          pending, otherwise report     */
55 /*                                          actual length of bytes done,  */
56 /*                                          a pointer to ULONG must be    */
57 /*                                          passed to fill the actual     */
58 /*                                          length value, as parameter    */
59 /*                                                                        */
60 /*    It's for standalone mode.                                           */
61 /*                                                                        */
62 /*                                                                        */
63 /*  INPUT                                                                 */
64 /*                                                                        */
65 /*    cdc_acm                               Pointer to cdc_acm class      */
66 /*    data_pointer                          Pointer to data to write      */
67 /*    requested_length                      Length of data to write       */
68 /*                                                                        */
69 /*  OUTPUT                                                                */
70 /*                                                                        */
71 /*    Completion Status                                                   */
72 /*                                                                        */
73 /*  CALLS                                                                 */
74 /*                                                                        */
75 /*    _ux_host_stack_transfer_run           Run transfer state machine    */
76 /*                                                                        */
77 /*  CALLED BY                                                             */
78 /*                                                                        */
79 /*    Application                                                         */
80 /*                                                                        */
81 /*  RELEASE HISTORY                                                       */
82 /*                                                                        */
83 /*    DATE              NAME                      DESCRIPTION             */
84 /*                                                                        */
85 /*  01-31-2022     Chaoqiong Xiao           Initial Version 6.1.10        */
86 /*                                                                        */
87 /**************************************************************************/
_ux_host_class_cdc_acm_write_with_callback(UX_HOST_CLASS_CDC_ACM * cdc_acm,UCHAR * data_pointer,ULONG requested_length)88 UINT  _ux_host_class_cdc_acm_write_with_callback(UX_HOST_CLASS_CDC_ACM *cdc_acm,
89                                     UCHAR *data_pointer, ULONG requested_length)
90 {
91 
92 UX_TRANSFER     *transfer_request;
93 UINT            status;
94 ULONG           transfer_request_length;
95 
96 
97     /* If trace is enabled, insert this event into the trace buffer.  */
98     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_CDC_ACM_WRITE, cdc_acm, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
99 
100     /* Ensure the instance is valid.  */
101     if (cdc_acm -> ux_host_class_cdc_acm_state !=  UX_HOST_CLASS_INSTANCE_LIVE)
102     {
103 
104         /* Error trap. */
105         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
106 
107         /* If trace is enabled, insert this event into the trace buffer.  */
108         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, cdc_acm, 0, 0, UX_TRACE_ERRORS, 0, 0)
109 
110         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
111     }
112 
113     /* As further protection, we must ensure this instance of the interface is the data interface and not
114        the control interface !  */
115     if (cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass != UX_HOST_CLASS_CDC_DATA_CLASS)
116     {
117 
118         /* Error trap. */
119         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
120 
121         /* If trace is enabled, insert this event into the trace buffer.  */
122         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, cdc_acm, 0, 0, UX_TRACE_ERRORS, 0, 0)
123 
124         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
125     }
126 
127     /* Check write state.  */
128     if (cdc_acm -> ux_host_class_cdc_acm_write_state != UX_STATE_RESET)
129         return(UX_BUSY);
130 
131     /* Set state to busy.  */
132     cdc_acm -> ux_host_class_cdc_acm_write_state = UX_STATE_WAIT;
133 
134     /* Reset write status.  */
135     cdc_acm -> ux_host_class_cdc_acm_write_length = requested_length;
136     cdc_acm -> ux_host_class_cdc_acm_write_count = 0;
137 
138     /* Get the pointer to the bulk out endpoint transfer request.  */
139     transfer_request =  &cdc_acm -> ux_host_class_cdc_acm_bulk_out_endpoint -> ux_endpoint_transfer_request;
140 
141     /* Initialize request for callback mode.  */
142     transfer_request -> ux_transfer_request_class_instance      = (VOID *) cdc_acm;
143     transfer_request -> ux_transfer_request_completion_function = _ux_host_class_cdc_acm_transmission_callback;
144 
145     /* Program the maximum authorized length for this transfer_request.  */
146     if (requested_length > transfer_request -> ux_transfer_request_maximum_length)
147         transfer_request_length =  transfer_request -> ux_transfer_request_maximum_length;
148     else
149         transfer_request_length =  requested_length;
150 
151     /* Initialize the transfer_request.  */
152     transfer_request -> ux_transfer_request_data_pointer = data_pointer;
153     transfer_request -> ux_transfer_request_requested_length = transfer_request_length;
154     UX_TRANSFER_STATE_RESET(transfer_request);
155 
156     /* Start the transfer.  */
157     status =  _ux_host_stack_transfer_run(transfer_request);
158 
159     /* We get here when all the transfers went through without errors.  */
160     return((status == UX_STATE_WAIT) ?
161         UX_SUCCESS : transfer_request -> ux_transfer_request_completion_code);
162 }
163 #endif
164