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 /**   Generic Serial Host module 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_gser.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_gser_reception_start                 PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function starts a reception with the generic serial class. This*/
46 /*    allows for non blocking calls based on a packet orientated round    */
47 /*    robbin buffer. When a packet is fully or partially received, an     */
48 /*    application callback function is invoked and a new transfer request */
49 /*    is rescheduled.                                                     */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    gser                               Pointer to gser class            */
54 /*    gser_reception                     Pointer to reception struct      */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    Completion Status                                                   */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    _ux_host_stack_transfer_request       Process transfer request      */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application                                                         */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*                                                                        */
76 /**************************************************************************/
_ux_host_class_gser_reception_start(UX_HOST_CLASS_GSER * gser,UX_HOST_CLASS_GSER_RECEPTION * gser_reception)77 UINT  _ux_host_class_gser_reception_start (UX_HOST_CLASS_GSER *gser,
78                                     UX_HOST_CLASS_GSER_RECEPTION *gser_reception)
79 {
80 
81 UX_TRANSFER     *transfer_request;
82 UINT            status;
83 ULONG           interface_index;
84 
85     /* If trace is enabled, insert this event into the trace buffer.  */
86     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_GSER_RECEPTION_START, gser, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
87 
88     /* Ensure the instance is valid.  */
89     if (gser -> ux_host_class_gser_state !=  UX_HOST_CLASS_INSTANCE_LIVE)
90     {
91 
92         /* Error trap. */
93         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
94 
95         /* If trace is enabled, insert this event into the trace buffer.  */
96         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, gser, 0, 0, UX_TRACE_ERRORS, 0, 0)
97 
98         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
99     }
100 
101     /* Get the interface index.  */
102     interface_index = gser_reception -> ux_host_class_gser_reception_interface_index;
103 
104     /* Start by aligning the head and tail of buffers to the same address supplied by the application.  */
105     gser_reception -> ux_host_class_gser_reception_data_head =  gser_reception -> ux_host_class_gser_reception_data_buffer;
106     gser_reception -> ux_host_class_gser_reception_data_tail =  gser_reception -> ux_host_class_gser_reception_data_buffer;
107 
108     /* Get the pointer to the bulk in endpoint in the transfer_request.  */
109     transfer_request =  &gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_bulk_in_endpoint -> ux_endpoint_transfer_request;
110 
111     /* Save the interface number in the Transfer Request. */
112     transfer_request -> ux_transfer_request_user_specific = (VOID *) (ALIGN_TYPE) interface_index;
113 
114     /* Initialize the transfer request.  */
115     transfer_request -> ux_transfer_request_class_instance      =  (VOID *) gser;
116     transfer_request -> ux_transfer_request_data_pointer        =  gser_reception -> ux_host_class_gser_reception_data_head;
117     transfer_request -> ux_transfer_request_requested_length    =  gser_reception -> ux_host_class_gser_reception_block_size;
118     transfer_request -> ux_transfer_request_completion_function =  _ux_host_class_gser_reception_callback;
119 
120     /* Save the acm reception structure in the acm structure.  */
121     gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_reception = gser_reception;
122 
123     /* And declare we have a transfer in progress.  */
124     gser_reception -> ux_host_class_gser_reception_state =  UX_HOST_CLASS_GSER_RECEPTION_STATE_STARTED;
125 
126     /* Arm a first transfer on the bulk in endpoint. There is a callback to this function so we return to the caller
127        right away. */
128     status =  _ux_host_stack_transfer_request(transfer_request);
129 
130     /* We do not know if the first transfer was successful yet.  If the status is not OK, we need to stop the transfer
131        in progress flag. */
132     if (status != UX_SUCCESS)
133         gser_reception -> ux_host_class_gser_reception_state =  UX_HOST_CLASS_GSER_RECEPTION_STATE_STOPPED;
134 
135     return(status);
136 }
137 
138