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 /**   CDC ACM 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_cdc_acm.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_cdc_acm_ioctl                        PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function is the ioctl entry point for the application to       */
46 /*    configure the ACM device.                                           */
47 /*                                                                        */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    acm                                   Pointer to acm class          */
52 /*    ioctl_function                        ioctl function                */
53 /*    parameter                             pointer to structure          */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    Completion Status                                                   */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _ux_host_stack_endpoint_transfer_abort                              */
62 /*                                          Abort transfer                */
63 /*    _ux_host_class_cdc_acm_command        Send command to acm device    */
64 /*    _ux_utility_memory_allocate           Allocate memory               */
65 /*    _ux_utility_memory_free               Free memory                   */
66 /*    _ux_utility_long_put                  Put 32-bit value              */
67 /*                                                                        */
68 /*  CALLED BY                                                             */
69 /*                                                                        */
70 /*    Storage Class                                                       */
71 /*                                                                        */
72 /*  RELEASE HISTORY                                                       */
73 /*                                                                        */
74 /*    DATE              NAME                      DESCRIPTION             */
75 /*                                                                        */
76 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
77 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            resulting in version 6.1    */
79 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            added standalone support,   */
81 /*                                            resulting in version 6.1.10 */
82 /*                                                                        */
83 /**************************************************************************/
_ux_host_class_cdc_acm_ioctl(UX_HOST_CLASS_CDC_ACM * cdc_acm,ULONG ioctl_function,VOID * parameter)84 UINT  _ux_host_class_cdc_acm_ioctl(UX_HOST_CLASS_CDC_ACM *cdc_acm, ULONG ioctl_function,
85                                     VOID *parameter)
86 {
87 
88 UINT                                    status;
89 UCHAR                                   *data_buffer;
90 UX_HOST_CLASS_CDC_ACM_LINE_CODING       *line_coding;
91 UX_HOST_CLASS_CDC_ACM_LINE_STATE        *line_state;
92 VOID                                    (*callback_function) (struct UX_HOST_CLASS_CDC_ACM_STRUCT *, ULONG, ULONG );
93 ULONG                                   value;
94 #if defined(UX_HOST_STANDALONE)
95 VOID                                    (*write_callback_function) (struct UX_HOST_CLASS_CDC_ACM_STRUCT *, UINT, ULONG );
96 UX_TRANSFER                             *transfer;
97 #endif
98 
99     /* Ensure the instance is valid.  */
100     if ((cdc_acm -> ux_host_class_cdc_acm_state !=  UX_HOST_CLASS_INSTANCE_LIVE) &&
101         (cdc_acm -> ux_host_class_cdc_acm_state !=  UX_HOST_CLASS_INSTANCE_MOUNTING))
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, cdc_acm, 0, 0, UX_TRACE_ERRORS, 0, 0)
106 
107         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
108     }
109 
110     /* The command request will tell us what we need to do here.  */
111     switch (ioctl_function)
112     {
113 
114     case UX_HOST_CLASS_CDC_ACM_IOCTL_SET_LINE_CODING:
115 
116         /* If trace is enabled, insert this event into the trace buffer.  */
117         UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_CDC_ACM_IOCTL_SET_LINE_CODING, cdc_acm, parameter, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
118 
119         /* Allocate some cache safe memory for the control command.  */
120         data_buffer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_CDC_ACM_LINE_CODING_LENGTH);
121 
122         /* Check if error. Return with error if no memory could be allocated.  */
123         if (data_buffer == UX_NULL)
124 
125             /* Do not proceed. Set error code.  */
126             status = UX_MEMORY_INSUFFICIENT;
127         else
128         {
129 
130             /* Build the buffer from the calling parameter. Cast the calling parameter.  */
131             line_coding = (UX_HOST_CLASS_CDC_ACM_LINE_CODING *) parameter;
132 
133             /* Put the data rate.  */
134             _ux_utility_long_put(data_buffer + UX_HOST_CLASS_CDC_ACM_LINE_CODING_RATE,
135                                 line_coding -> ux_host_class_cdc_acm_line_coding_dter);
136 
137             /* Then the stop bit.  */
138             *(data_buffer + UX_HOST_CLASS_CDC_ACM_LINE_CODING_STOP_BIT) =
139             (UCHAR) line_coding -> ux_host_class_cdc_acm_line_coding_stop_bit;
140 
141             /* Then the parity.  */
142             *(data_buffer + UX_HOST_CLASS_CDC_ACM_LINE_CODING_PARITY) =
143             (UCHAR) line_coding -> ux_host_class_cdc_acm_line_coding_parity;
144 
145             /* Finally the data bits.  */
146             *(data_buffer + UX_HOST_CLASS_CDC_ACM_LINE_CODING_DATA_BIT) =
147             (UCHAR) line_coding -> ux_host_class_cdc_acm_line_coding_data_bits;
148 
149             /* Send the command to the device.  */
150             status = _ux_host_class_cdc_acm_command(cdc_acm, UX_HOST_CLASS_CDC_ACM_REQ_SET_LINE_CODING,
151                                     0, data_buffer, UX_HOST_CLASS_CDC_ACM_LINE_CODING_LENGTH);
152 
153             /* We free the resources allocated no matter what.  */
154             _ux_utility_memory_free(data_buffer);
155         }
156         break;
157 
158     case UX_HOST_CLASS_CDC_ACM_IOCTL_GET_LINE_CODING:
159 
160         /* If trace is enabled, insert this event into the trace buffer.  */
161         UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_CDC_ACM_IOCTL_GET_LINE_CODING, cdc_acm, parameter, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
162 
163         /* Allocate some cache safe memory for the control command.  */
164         data_buffer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_CDC_ACM_LINE_CODING_LENGTH);
165 
166         /* Check if error. Return with error if no memory could be allocated.  */
167         if (data_buffer == UX_NULL)
168 
169             /* Do not proceed. Set error code.  */
170             status = UX_MEMORY_INSUFFICIENT;
171         else
172         {
173 
174             /* Send the command to the device.  */
175             status = _ux_host_class_cdc_acm_command(cdc_acm, UX_HOST_CLASS_CDC_ACM_REQ_GET_LINE_CODING,
176                                     0, data_buffer, UX_HOST_CLASS_CDC_ACM_LINE_CODING_LENGTH);
177 
178             /* Fill in the calling buffer if the result is successful. */
179             if (status == UX_SUCCESS)
180             {
181 
182                 /* Build the buffer from the calling parameter. Cast the calling parameter.  */
183                 line_coding = (UX_HOST_CLASS_CDC_ACM_LINE_CODING *) parameter;
184 
185                 /* Get the data rate.  */
186                 line_coding -> ux_host_class_cdc_acm_line_coding_dter = _ux_utility_long_get(data_buffer + UX_HOST_CLASS_CDC_ACM_LINE_CODING_RATE);
187 
188                 /* Then the stop bit.  */
189                 line_coding -> ux_host_class_cdc_acm_line_coding_stop_bit =
190                 (ULONG) *(data_buffer + UX_HOST_CLASS_CDC_ACM_LINE_CODING_STOP_BIT);
191 
192                 /* Then the parity.  */
193                 line_coding -> ux_host_class_cdc_acm_line_coding_parity =
194                 (ULONG) *(data_buffer + UX_HOST_CLASS_CDC_ACM_LINE_CODING_PARITY);
195 
196                 /* Finally the data bits.  */
197                 line_coding -> ux_host_class_cdc_acm_line_coding_data_bits =
198                 (ULONG) *(data_buffer + UX_HOST_CLASS_CDC_ACM_LINE_CODING_DATA_BIT);
199             }
200 
201             /* We free the resources allocated no matter what.  */
202             _ux_utility_memory_free(data_buffer);
203         }
204         break;
205 
206     case UX_HOST_CLASS_CDC_ACM_IOCTL_SET_LINE_STATE:
207 
208         /* If trace is enabled, insert this event into the trace buffer.  */
209         UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_CDC_ACM_IOCTL_SET_LINE_STATE, cdc_acm, parameter, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
210 
211         /* Cast the calling parameter.  */
212         line_state = (UX_HOST_CLASS_CDC_ACM_LINE_STATE *) parameter;
213 
214         /* Build the value field.  */
215         value = (line_state -> ux_host_class_cdc_acm_line_state_dtr |
216                 (line_state -> ux_host_class_cdc_acm_line_state_rts << 1));
217 
218         /* Send the command to the device.  */
219         status = _ux_host_class_cdc_acm_command(cdc_acm, UX_HOST_CLASS_CDC_ACM_REQ_SET_LINE_STATE,
220                                     value, UX_NULL,0);
221         break;
222 
223     case UX_HOST_CLASS_CDC_ACM_IOCTL_SEND_BREAK :
224 
225         /* If trace is enabled, insert this event into the trace buffer.  */
226         UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_CDC_ACM_IOCTL_SEND_BREAK, cdc_acm, parameter, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
227 
228         /* Build the value field.  */
229         value = *((ULONG *) parameter);
230 
231         /* Send the command to the device.  */
232         status = _ux_host_class_cdc_acm_command(cdc_acm, UX_HOST_CLASS_CDC_ACM_REQ_SEND_BREAK,
233                                     value, UX_NULL,0);
234         break;
235 
236 
237 
238     case UX_HOST_CLASS_CDC_ACM_IOCTL_ABORT_IN_PIPE :
239 
240         /* If trace is enabled, insert this event into the trace buffer.  */
241         UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_CDC_ACM_IOCTL_ABORT_IN_PIPE, cdc_acm, cdc_acm -> ux_host_class_cdc_acm_bulk_in_endpoint, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
242 
243         /* We need to abort transactions on the bulk In pipe.  */
244         _ux_host_stack_endpoint_transfer_abort(cdc_acm -> ux_host_class_cdc_acm_bulk_in_endpoint);
245 
246         /* Status is successful.  */
247         status = UX_SUCCESS;
248         break;
249 
250     case UX_HOST_CLASS_CDC_ACM_IOCTL_ABORT_OUT_PIPE :
251 
252         /* If trace is enabled, insert this event into the trace buffer.  */
253         UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_CDC_ACM_IOCTL_ABORT_OUT_PIPE, cdc_acm, cdc_acm -> ux_host_class_cdc_acm_bulk_out_endpoint, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
254 
255         /* We need to abort transactions on the bulk Out pipe.  */
256         _ux_host_stack_endpoint_transfer_abort(cdc_acm -> ux_host_class_cdc_acm_bulk_out_endpoint);
257 
258 #if defined(UX_HOST_STANDALONE)
259 
260         /* Reset write state.  */
261         cdc_acm -> ux_host_class_cdc_acm_write_state = UX_STATE_RESET;
262 #endif
263 
264         /* Status is successful.  */
265         status = UX_SUCCESS;
266         break;
267 
268     case UX_HOST_CLASS_CDC_ACM_IOCTL_NOTIFICATION_CALLBACK :
269 
270         /* If trace is enabled, insert this event into the trace buffer.  */
271         UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_CDC_ACM_IOCTL_NOTIFICATION_CALLBACK, cdc_acm, parameter, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
272 
273         /* Register a callback when the line state has changed. */
274         callback_function = ((VOID (*) (struct UX_HOST_CLASS_CDC_ACM_STRUCT *, ULONG, ULONG )) (ALIGN_TYPE)parameter);
275         cdc_acm -> ux_host_class_cdc_acm_device_status_change_callback = callback_function;
276 
277         /* Status is successful.  */
278         status = UX_SUCCESS;
279         break;
280 
281     case UX_HOST_CLASS_CDC_ACM_IOCTL_GET_DEVICE_STATUS :
282 
283         /* If trace is enabled, insert this event into the trace buffer.  */
284         UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_CDC_ACM_IOCTL_GET_DEVICE_STATUS, cdc_acm, cdc_acm -> ux_host_class_cdc_acm_device_state, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
285 
286         /* Return the device status.  */
287         *((ULONG *) parameter) = cdc_acm -> ux_host_class_cdc_acm_device_state;
288 
289         /* Status is successful.  */
290         status = UX_SUCCESS;
291         break;
292 
293 #if defined(UX_HOST_STANDALONE)
294     case UX_HOST_CLASS_CDC_ACM_IOCTL_WRITE_CALLBACK:
295 
296         /* Register a callback when write is done. */
297         write_callback_function = ((VOID (*) (struct UX_HOST_CLASS_CDC_ACM_STRUCT *, UINT, ULONG )) (ALIGN_TYPE)parameter);
298         cdc_acm -> ux_host_class_cdc_acm_write_callback = write_callback_function;
299 
300         /* Status is successful.  */
301         status = UX_SUCCESS;
302         break;
303 
304     case UX_HOST_CLASS_CDC_ACM_IOCTL_GET_WRITE_STATUS:
305 
306         /* Check write state.  */
307         if (cdc_acm -> ux_host_class_cdc_acm_write_state == UX_STATE_WAIT)
308         {
309             status = UX_BUSY;
310         }
311         else
312         {
313 
314             /* Get transfer for write.  */
315             transfer = &cdc_acm -> ux_host_class_cdc_acm_bulk_out_endpoint ->
316                                                 ux_endpoint_transfer_request;
317 
318             /* Status is from transfer completion code.  */
319             status = transfer -> ux_transfer_request_completion_code;
320 
321             /* Actual length is from latest write count.  */
322             *(ULONG *)parameter = cdc_acm -> ux_host_class_cdc_acm_write_count;
323         }
324         break;
325 #endif
326 
327     default:
328 
329         /* Error trap. */
330         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_FUNCTION_NOT_SUPPORTED);
331 
332         /* If trace is enabled, insert this event into the trace buffer.  */
333         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_FUNCTION_NOT_SUPPORTED, 0, 0, 0, UX_TRACE_ERRORS, 0, 0)
334 
335         /* Function not supported. Return an error.  */
336         status =  UX_FUNCTION_NOT_SUPPORTED;
337     }
338 
339     /* Return status to caller.  */
340     return(status);
341 }
342 
343