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 /**   Prolific 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_prolific.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_prolific_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_prolific_reception_callback(UX_TRANSFER * transfer_request)74 VOID  _ux_host_class_prolific_reception_callback (UX_TRANSFER *transfer_request)
75 {
76 
77 UX_HOST_CLASS_PROLIFIC               *prolific;
78 UX_HOST_CLASS_PROLIFIC_RECEPTION     *prolific_reception;
79 
80     /* Get the class instance for this transfer request.  */
81     prolific =  (UX_HOST_CLASS_PROLIFIC *) transfer_request -> ux_transfer_request_class_instance;
82 
83     /* Get the pointer to the prolific reception structure.  */
84     prolific_reception =  prolific -> ux_host_class_prolific_reception;
85 
86     /* Check the state of the transfer.  If there is an error, we do not proceed with this report.  */
87     if (transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
88     {
89 
90         /* The reception is stopped.  */
91         prolific_reception -> ux_host_class_prolific_reception_state =  UX_HOST_CLASS_PROLIFIC_RECEPTION_STATE_STOPPED;
92 
93         /* We may have an device extraction. We cannot continue.  Report to the application.  */
94         prolific_reception -> ux_host_class_prolific_reception_callback(prolific, transfer_request -> ux_transfer_request_completion_code, UX_NULL, 0);
95 
96 
97         /* We do not proceed.  */
98         return;
99 
100     }
101 
102     /* Check if the class is in shutdown.  */
103     if (prolific -> ux_host_class_prolific_state ==  UX_HOST_CLASS_INSTANCE_SHUTDOWN)
104 
105         /* We do not proceed.  */
106         return;
107 
108     /* And move to the next reception buffer.  Check if we are at the end of the application buffer.  */
109     if (prolific_reception -> ux_host_class_prolific_reception_data_head + prolific_reception -> ux_host_class_prolific_reception_block_size >=
110         prolific_reception -> ux_host_class_prolific_reception_data_buffer + prolific_reception -> ux_host_class_prolific_reception_data_buffer_size)
111     {
112 
113         /* We are at the end of the buffer. Move back to the beginning if we have space available. */
114         if (prolific_reception -> ux_host_class_prolific_reception_data_tail ==  prolific_reception -> ux_host_class_prolific_reception_data_buffer)
115         {
116 
117             /* Error trap. */
118             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
119 
120             /* If trace is enabled, insert this event into the trace buffer.  */
121             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_BUFFER_OVERFLOW, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0)
122 
123             /* We have an overflow. We cannot continue.  Report to the application.  */
124             prolific_reception -> ux_host_class_prolific_reception_callback(prolific, UX_BUFFER_OVERFLOW, UX_NULL, 0);
125 
126             /* And stop the transfer in progress flag.  */
127             prolific_reception -> ux_host_class_prolific_reception_state =  UX_HOST_CLASS_PROLIFIC_RECEPTION_STATE_STOPPED;
128 
129             return;
130         }
131         else
132 
133             /* Program the head to be at the beginning of the application buffer.  */
134             prolific_reception -> ux_host_class_prolific_reception_data_head =  prolific_reception -> ux_host_class_prolific_reception_data_buffer;
135 
136     }
137     else
138 
139             /* Program the head to be after the current buffer.  */
140             prolific_reception -> ux_host_class_prolific_reception_data_head +=  prolific_reception -> ux_host_class_prolific_reception_block_size;
141 
142 
143     /* We need to report this transfer to the application.  */
144     prolific_reception -> ux_host_class_prolific_reception_callback(prolific,
145                                                                     transfer_request -> ux_transfer_request_completion_code,
146                                                                     transfer_request -> ux_transfer_request_data_pointer,
147                                                                     transfer_request -> ux_transfer_request_actual_length);
148     /* Update the next buffer for reception.  */
149     transfer_request -> ux_transfer_request_data_pointer =  prolific_reception -> ux_host_class_prolific_reception_data_head;
150 
151     /* Arm another transfer.  */
152     _ux_host_stack_transfer_request(transfer_request);
153 
154     /* There is no status to be reported back to the stack.  */
155     return;
156 }
157 
158