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_callback              PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function is the callback from the USBX transfer functions,     */
46 /*    it is called when a full or partial transfer has been done for a    */
47 /*    bulk in transfer. It calls back the application.                    */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    transfer_request                      Pointer to transfer request   */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_stack_transfer_request       Process transfer request      */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application                                                         */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*                                                                        */
73 /**************************************************************************/
_ux_host_class_gser_reception_callback(UX_TRANSFER * transfer_request)74 VOID  _ux_host_class_gser_reception_callback (UX_TRANSFER *transfer_request)
75 {
76 
77 UX_HOST_CLASS_GSER              *gser;
78 UX_HOST_CLASS_GSER_RECEPTION    *gser_reception;
79 ULONG                           interface_index;
80 
81     /* Get the class instance for this transfer request.  */
82     gser =  (UX_HOST_CLASS_GSER *) transfer_request -> ux_transfer_request_class_instance;
83 
84     /* The interface index was stored into the user specific field.  */
85     interface_index = (ULONG) (ALIGN_TYPE) transfer_request -> ux_transfer_request_user_specific;
86 
87     /* Get the pointer to the acm reception structure.  */
88     gser_reception =  gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_reception;
89 
90     /* Check the state of the transfer.  If there is an error, we do not proceed with this report.  */
91     if (transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
92     {
93 
94         /* The reception is stopped.  */
95         gser_reception -> ux_host_class_gser_reception_state =  UX_HOST_CLASS_GSER_RECEPTION_STATE_STOPPED;
96 
97         /* We do not proceed.  */
98         return;
99 
100     }
101 
102     /* And move to the next reception buffer.  Check if we are at the end of the application buffer.  */
103     if (gser_reception -> ux_host_class_gser_reception_data_head + gser_reception -> ux_host_class_gser_reception_block_size >=
104         gser_reception -> ux_host_class_gser_reception_data_buffer + gser_reception -> ux_host_class_gser_reception_data_buffer_size)
105     {
106 
107         /* We are at the end of the buffer. Move back to the beginning if we have space available. */
108         if (gser_reception -> ux_host_class_gser_reception_data_tail ==  gser_reception -> ux_host_class_gser_reception_data_buffer)
109         {
110 
111             /* Error trap. */
112             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
113 
114             /* If trace is enabled, insert this event into the trace buffer.  */
115             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_BUFFER_OVERFLOW, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0)
116 
117             /* We have an overflow. We cannot continue.  Report to the application.  */
118             gser_reception -> ux_host_class_gser_reception_callback(gser, UX_BUFFER_OVERFLOW, UX_NULL, 0);
119 
120             /* And stop the transfer in progress flag.  */
121             gser_reception -> ux_host_class_gser_reception_state =  UX_HOST_CLASS_GSER_RECEPTION_STATE_STOPPED;
122 
123             return;
124         }
125         else
126 
127             /* Program the head to be at the beginning of the application buffer.  */
128             gser_reception -> ux_host_class_gser_reception_data_head =  gser_reception -> ux_host_class_gser_reception_data_buffer;
129 
130     }
131     else
132 
133             /* Program the head to be after the current buffer.  */
134             gser_reception -> ux_host_class_gser_reception_data_head +=  gser_reception -> ux_host_class_gser_reception_block_size;
135 
136 
137     /* We need to report this transfer to the application.  */
138     gser_reception -> ux_host_class_gser_reception_callback(gser,
139                                                                     transfer_request -> ux_transfer_request_completion_code,
140                                                                     transfer_request -> ux_transfer_request_data_pointer,
141                                                                     transfer_request -> ux_transfer_request_actual_length);
142 
143     /* Arm another transfer.  */
144     _ux_host_stack_transfer_request(transfer_request);
145 
146     /* There is no status to be reported back to the stack.  */
147     return;
148 }
149 
150