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 /* FUNCTION RELEASE */
35 /* */
36 /* _ux_host_class_gser_write PORTABLE C */
37 /* 6.1.11 */
38 /* AUTHOR */
39 /* */
40 /* Chaoqiong Xiao, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function writes to the gser interface. The call is blocking */
45 /* and only returns when there is either an error or when the transfer */
46 /* is complete. */
47 /* */
48 /* INPUT */
49 /* */
50 /* gser Pointer to gser class */
51 /* data_pointer Pointer to data to write */
52 /* requested_length Length of data to write */
53 /* actual_length Actual length of data written */
54 /* */
55 /* OUTPUT */
56 /* */
57 /* Completion Status */
58 /* */
59 /* CALLS */
60 /* */
61 /* _ux_host_stack_transfer_request Process transfer request */
62 /* _ux_host_stack_transfer_request_abort Abort transfer request */
63 /* _ux_host_semaphore_get Get protection semaphore */
64 /* _ux_host_semaphore_put Release protection semaphore */
65 /* */
66 /* CALLED BY */
67 /* */
68 /* Application */
69 /* */
70 /* RELEASE HISTORY */
71 /* */
72 /* DATE NAME DESCRIPTION */
73 /* */
74 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
75 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
76 /* prefixed UX to MS_TO_TICK, */
77 /* resulting in version 6.1 */
78 /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */
79 /* refined macros names, */
80 /* resulting in version 6.1.10 */
81 /* 04-25-2022 Chaoqiong Xiao Modified comment(s), */
82 /* fixed standalone compile, */
83 /* resulting in version 6.1.11 */
84 /* */
85 /**************************************************************************/
_ux_host_class_gser_write(UX_HOST_CLASS_GSER * gser,ULONG interface_index,UCHAR * data_pointer,ULONG requested_length,ULONG * actual_length)86 UINT _ux_host_class_gser_write(UX_HOST_CLASS_GSER *gser,
87 ULONG interface_index,
88 UCHAR * data_pointer,
89 ULONG requested_length,
90 ULONG *actual_length)
91 {
92
93 UX_TRANSFER *transfer_request;
94 UINT status;
95 ULONG transfer_request_length;
96
97 /* If trace is enabled, insert this event into the trace buffer. */
98 UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_GSER_WRITE, gser, data_pointer, requested_length, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
99
100 /* Ensure the instance is valid. */
101 if (gser -> ux_host_class_gser_state != UX_HOST_CLASS_INSTANCE_LIVE)
102 {
103
104 /* If trace is enabled, insert this event into the trace buffer. */
105 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, gser, 0, 0, UX_TRACE_ERRORS, 0, 0)
106
107 }
108
109 /* Protect thread reentry to this instance. */
110 status = _ux_host_semaphore_get(&gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_semaphore, UX_WAIT_FOREVER);
111 if (status != UX_SUCCESS)
112 return(status);
113
114 /* Start by resetting the actual length of the transfer. */
115 *actual_length = 0;
116
117 /* Get the pointer to the bulk out endpoint transfer request. */
118 transfer_request = &gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_bulk_out_endpoint -> ux_endpoint_transfer_request;
119
120 /* Save the interface number in the Transfer Request. */
121 transfer_request -> ux_transfer_request_user_specific = (VOID *) (ALIGN_TYPE) interface_index;
122
123 /* Perform a transfer on the bulk out endpoint until either the transfer is
124 completed or when there is an error. */
125 do
126 {
127
128 /* Program the maximum authorized length for this transfer_request. */
129 if (requested_length > transfer_request -> ux_transfer_request_maximum_length)
130 transfer_request_length = transfer_request -> ux_transfer_request_maximum_length;
131 else
132 transfer_request_length = requested_length;
133
134 /* Initialize the transfer_request. */
135 transfer_request -> ux_transfer_request_data_pointer = data_pointer;
136 transfer_request -> ux_transfer_request_requested_length = transfer_request_length;
137
138 /* Perform the transfer. */
139 status = _ux_host_stack_transfer_request(transfer_request);
140
141 /* If the transfer is successful, we need to wait for the transfer request to be completed. */
142 if (status == UX_SUCCESS)
143 {
144
145 /* Wait for the completion of the transfer request. */
146 status = _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_GSER_CLASS_TRANSFER_TIMEOUT));
147
148 /* If the semaphore did not succeed we probably have a time out. */
149 if (status != UX_SUCCESS)
150 {
151
152 /* All transfers pending need to abort. There may have been a partial transfer. */
153 _ux_host_stack_transfer_request_abort(transfer_request);
154
155 /* Update the length of the actual data transferred. We do this after the
156 abort of the transfer_request in case some data actually went out. */
157 *actual_length += transfer_request -> ux_transfer_request_actual_length;
158
159 /* Unprotect thread reentry to this instance. */
160 _ux_host_semaphore_put(&gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_semaphore);
161
162 /* Set the completion code. */
163 transfer_request -> ux_transfer_request_completion_code = UX_TRANSFER_TIMEOUT;
164
165 /* If trace is enabled, insert this event into the trace buffer. */
166 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_TRANSFER_TIMEOUT, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0)
167
168 /* There was an error, return to the caller. */
169 return(UX_TRANSFER_TIMEOUT);
170 }
171 }
172 else
173 {
174
175 /* Unprotect thread reentry to this instance. */
176 _ux_host_semaphore_put(&gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_semaphore);
177
178 /* There was a non transfer error, no partial transfer to be checked */
179 return(status);
180 }
181
182 /* Update the length of the transfer. Normally all the data has to be sent. */
183 *actual_length += transfer_request -> ux_transfer_request_actual_length;
184
185 /* Check for completion of transfer. If the transfer is partial, return to caller.
186 The transfer is marked as successful but the caller will need to check the length
187 actually sent and determine if a partial transfer is OK. */
188 if (transfer_request_length != transfer_request -> ux_transfer_request_actual_length)
189 {
190
191 /* Unprotect thread reentry to this instance. */
192 _ux_host_semaphore_put(&gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_semaphore);
193
194 /* Return success. */
195 return(UX_SUCCESS);
196 }
197
198 /* Update the data pointer for next transfer. */
199 data_pointer += transfer_request_length;
200
201 /* Update what is left to send out. */
202 requested_length -= transfer_request_length;
203
204 } while (requested_length != 0);
205
206
207 /* Unprotect thread reentry to this instance. */
208 _ux_host_semaphore_put(&gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_semaphore);
209
210 /* We get here when all the transfers went through without errors. */
211 return(UX_SUCCESS);
212 }
213
214