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 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Generic Serial Host module class                                    */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_host_class_gser.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_gser_reception_start                 PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function starts a reception with the generic serial class. This*/
45 /*    allows for non blocking calls based on a packet orientated round    */
46 /*    robbin buffer. When a packet is fully or partially received, an     */
47 /*    application callback function is invoked and a new transfer request */
48 /*    is rescheduled.                                                     */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    gser                               Pointer to gser class            */
53 /*    gser_reception                     Pointer to reception struct      */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    Completion Status                                                   */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _ux_host_stack_transfer_request       Process transfer request      */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application                                                         */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
72 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
_ux_host_class_gser_reception_start(UX_HOST_CLASS_GSER * gser,UX_HOST_CLASS_GSER_RECEPTION * gser_reception)76 UINT  _ux_host_class_gser_reception_start (UX_HOST_CLASS_GSER *gser,
77                                     UX_HOST_CLASS_GSER_RECEPTION *gser_reception)
78 {
79 
80 UX_TRANSFER     *transfer_request;
81 UINT            status;
82 ULONG           interface_index;
83 
84     /* If trace is enabled, insert this event into the trace buffer.  */
85     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_GSER_RECEPTION_START, gser, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
86 
87     /* Ensure the instance is valid.  */
88     if (gser -> ux_host_class_gser_state !=  UX_HOST_CLASS_INSTANCE_LIVE)
89     {
90 
91         /* Error trap. */
92         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
93 
94         /* If trace is enabled, insert this event into the trace buffer.  */
95         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, gser, 0, 0, UX_TRACE_ERRORS, 0, 0)
96 
97         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
98     }
99 
100     /* Get the interface index.  */
101     interface_index = gser_reception -> ux_host_class_gser_reception_interface_index;
102 
103     /* Start by aligning the head and tail of buffers to the same address supplied by the application.  */
104     gser_reception -> ux_host_class_gser_reception_data_head =  gser_reception -> ux_host_class_gser_reception_data_buffer;
105     gser_reception -> ux_host_class_gser_reception_data_tail =  gser_reception -> ux_host_class_gser_reception_data_buffer;
106 
107     /* Get the pointer to the bulk in endpoint in the transfer_request.  */
108     transfer_request =  &gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_bulk_in_endpoint -> ux_endpoint_transfer_request;
109 
110     /* Save the interface number in the Transfer Request. */
111     transfer_request -> ux_transfer_request_user_specific = (VOID *) (ALIGN_TYPE) interface_index;
112 
113     /* Initialize the transfer request.  */
114     transfer_request -> ux_transfer_request_class_instance      =  (VOID *) gser;
115     transfer_request -> ux_transfer_request_data_pointer        =  gser_reception -> ux_host_class_gser_reception_data_head;
116     transfer_request -> ux_transfer_request_requested_length    =  gser_reception -> ux_host_class_gser_reception_block_size;
117     transfer_request -> ux_transfer_request_completion_function =  _ux_host_class_gser_reception_callback;
118 
119     /* Save the acm reception structure in the acm structure.  */
120     gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_reception = gser_reception;
121 
122     /* And declare we have a transfer in progress.  */
123     gser_reception -> ux_host_class_gser_reception_state =  UX_HOST_CLASS_GSER_RECEPTION_STATE_STARTED;
124 
125     /* Arm a first transfer on the bulk in endpoint. There is a callback to this function so we return to the caller
126        right away. */
127     status =  _ux_host_stack_transfer_request(transfer_request);
128 
129     /* We do not know if the first transfer was successful yet.  If the status is not OK, we need to stop the transfer
130        in progress flag. */
131     if (status != UX_SUCCESS)
132         gser_reception -> ux_host_class_gser_reception_state =  UX_HOST_CLASS_GSER_RECEPTION_STATE_STOPPED;
133 
134     return(status);
135 }
136 
137 
138 /**************************************************************************/
139 /*                                                                        */
140 /*  FUNCTION                                               RELEASE        */
141 /*                                                                        */
142 /*    _uxe_host_class_gser_reception_start                PORTABLE C      */
143 /*                                                           6.3.0        */
144 /*  AUTHOR                                                                */
145 /*                                                                        */
146 /*    Chaoqiong Xiao, Microsoft Corporation                               */
147 /*                                                                        */
148 /*  DESCRIPTION                                                           */
149 /*                                                                        */
150 /*    This function checks errors in CDC ACM reception function call.     */
151 /*                                                                        */
152 /*  INPUT                                                                 */
153 /*                                                                        */
154 /*    gser                                  Pointer to CDC ACM class      */
155 /*    gser_reception                        Pointer to reception struct   */
156 /*                                                                        */
157 /*  OUTPUT                                                                */
158 /*                                                                        */
159 /*    Status                                                              */
160 /*                                                                        */
161 /*  CALLS                                                                 */
162 /*                                                                        */
163 /*    _ux_host_class_gser_reception_start                                 */
164 /*                                          GSER reception start          */
165 /*                                                                        */
166 /*  CALLED BY                                                             */
167 /*                                                                        */
168 /*    Application                                                         */
169 /*                                                                        */
170 /*  RELEASE HISTORY                                                       */
171 /*                                                                        */
172 /*    DATE              NAME                      DESCRIPTION             */
173 /*                                                                        */
174 /*  10-31-2023     Chaoqiong Xiao           Initial Version 6.3.0         */
175 /*                                                                        */
176 /**************************************************************************/
_uxe_host_class_gser_reception_start(UX_HOST_CLASS_GSER * gser,UX_HOST_CLASS_GSER_RECEPTION * gser_reception)177 UINT  _uxe_host_class_gser_reception_start (UX_HOST_CLASS_GSER *gser,
178                                     UX_HOST_CLASS_GSER_RECEPTION *gser_reception)
179 {
180 
181     /* Sanity checks.  */
182     if ((gser == UX_NULL) || (gser_reception == UX_NULL))
183         return(UX_INVALID_PARAMETER);
184 
185     /* Invoke CDC ACM reception start function.  */
186     return(_ux_host_class_gser_reception_start(gser, gser_reception));
187 }
188