GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_cdc_acm_activate.c Lines: 48 48 100.0 %
Date: 2026-03-06 18:57:10 Branches: 24 24 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
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_activate                     PORTABLE C      */
38
/*                                                           6.3.0        */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function creates the ACM instance, configure the device ...    */
46
/*                                                                        */
47
/*  INPUT                                                                 */
48
/*                                                                        */
49
/*    command                                ACM  class command pointer   */
50
/*                                                                        */
51
/*  OUTPUT                                                                */
52
/*                                                                        */
53
/*    Completion Status                                                   */
54
/*                                                                        */
55
/*  CALLS                                                                 */
56
/*                                                                        */
57
/*    _ux_host_class_cdc_acm_configure        Configure cdc_acm class     */
58
/*    _ux_host_class_cdc_acm_endpoints_get    Get endpoints of cdc_acm    */
59
/*    _ux_host_class_cdc_acm_ioctl            IOCTL function for ACM      */
60
/*    _ux_host_stack_class_instance_destroy   Destroy the class instance  */
61
/*    _ux_host_stack_endpoint_transfer_abort  Abort transfer              */
62
/*    _ux_utility_memory_allocate             Allocate memory block       */
63
/*    _ux_utility_memory_free                 Free memory                 */
64
/*    _ux_host_semaphore_create               Create cdc_acm semaphore    */
65
/*    _ux_host_semaphore_delete               Delete semaphore            */
66
/*    _ux_utility_delay_ms                    Delay                       */
67
/*                                                                        */
68
/*  CALLED BY                                                             */
69
/*                                                                        */
70
/*    _ux_host_class_cdc_acm_entry          Entry of cdc_acm class        */
71
/*    _ux_utility_delay_ms                  Delay ms                      */
72
/*                                                                        */
73
/**************************************************************************/
74
171
UINT  _ux_host_class_cdc_acm_activate(UX_HOST_CLASS_COMMAND *command)
75
{
76
77
UX_INTERFACE                        *interface_ptr;
78
UX_HOST_CLASS_CDC_ACM               *cdc_acm;
79
UINT                                status;
80
#if defined(UX_HOST_STANDALONE)
81
UX_HOST_CLASS                       *cdc_acm_class;
82
UX_HOST_CLASS_CDC_ACM               *cdc_acm_inst;
83
UX_ENDPOINT                         *control_endpoint;
84
UX_TRANSFER                         *transfer_request;
85
ULONG                               descriptors_length;
86
#else
87
UX_HOST_CLASS_CDC_ACM_LINE_CODING   line_coding;
88
UX_HOST_CLASS_CDC_ACM_LINE_STATE    line_state;
89
#endif
90
91
    /* The CDC ACM class is always activated by the interface descriptor and not the
92
       device descriptor.  */
93
171
    interface_ptr =  (UX_INTERFACE *) command -> ux_host_class_command_container;
94
95
    /* Obtain memory for this class instance.  */
96
171
    cdc_acm =  _ux_utility_memory_allocate(UX_NO_ALIGN, UX_CACHE_SAFE_MEMORY, sizeof(UX_HOST_CLASS_CDC_ACM));
97
98
    /* Instance creation fail. */
99
171
    if (cdc_acm == UX_NULL)
100
101
        /* Memory allocation fail. */
102
13
        return(UX_MEMORY_INSUFFICIENT);
103
104
#if !defined(UX_HOST_STANDALONE)
105
106
    /* Create the semaphore to protect 2 threads from accessing the same acm instance.  */
107
158
    status =  _ux_host_semaphore_create(&cdc_acm -> ux_host_class_cdc_acm_semaphore, "ux_host_class_cdc_acm_semaphore", 1);
108
158
    if (status != UX_SUCCESS)
109
    {
110
111
        /* Free instance memory. */
112
3
        _ux_utility_memory_free(cdc_acm);
113
114
        /* Semaphore creation error. */
115
3
        return(UX_SEMAPHORE_ERROR);
116
    }
117
#endif
118
119
    /* Store the class container into this instance.  */
120
155
    cdc_acm -> ux_host_class_cdc_acm_class =  command -> ux_host_class_command_class_ptr;
121
122
    /* Store the interface container into the cdc_acm class instance.  */
123
155
    cdc_acm -> ux_host_class_cdc_acm_interface =  interface_ptr;
124
125
    /* Store the device container into the cdc_acm class instance.  */
126
155
    cdc_acm -> ux_host_class_cdc_acm_device =  interface_ptr -> ux_interface_configuration -> ux_configuration_device;
127
128
    /* This instance of the device must also be stored in the interface container.  */
129
155
    interface_ptr -> ux_interface_class_instance =  (VOID *) cdc_acm;
130
131
    /* Create this class instance.  */
132
155
    _ux_host_stack_class_instance_create(cdc_acm -> ux_host_class_cdc_acm_class, (VOID *) cdc_acm);
133
134
#if defined(UX_HOST_STANDALONE)
135
136
    /* Get the cdc_acm endpoint(s). Depending on the interface type, we will need to search for
137
        Bulk Out and Bulk In endpoints and the optional interrupt endpoint.  */
138
    status =  _ux_host_class_cdc_acm_endpoints_get(cdc_acm);
139
    if (status == UX_SUCCESS)
140
    {
141
142
        /* Mark the cdc_acm as mounting.  */
143
        cdc_acm -> ux_host_class_cdc_acm_state = UX_HOST_CLASS_INSTANCE_MOUNTING;
144
145
        /* If we have the Control Class, we process default setup command sequence.  */
146
        if (interface_ptr -> ux_interface_descriptor.bInterfaceClass == UX_HOST_CLASS_CDC_CONTROL_CLASS)
147
        {
148
149
            /* Get descriptors to see capabilities.  */
150
151
            /* Get default control transfer.  */
152
            control_endpoint = &cdc_acm -> ux_host_class_cdc_acm_device -> ux_device_control_endpoint;
153
            transfer_request = &control_endpoint -> ux_endpoint_transfer_request;
154
155
            /* Allocate memory for the descriptors.  */
156
            descriptors_length = interface_ptr -> ux_interface_configuration ->
157
                                        ux_configuration_descriptor.wTotalLength;
158
            cdc_acm -> ux_host_class_cdc_acm_allocated =
159
                    _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY,
160
                                                            descriptors_length);
161
            if (cdc_acm -> ux_host_class_cdc_acm_allocated != UX_NULL)
162
            {
163
164
                transfer_request -> ux_transfer_request_data_pointer =
165
                                            cdc_acm -> ux_host_class_cdc_acm_allocated;
166
167
                /* Create transfer for GET_DESCRIPTOR.  */
168
                transfer_request -> ux_transfer_request_requested_length = descriptors_length;
169
                transfer_request -> ux_transfer_request_function =         UX_GET_DESCRIPTOR;
170
                transfer_request -> ux_transfer_request_type =             UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
171
                transfer_request -> ux_transfer_request_value =            UX_CONFIGURATION_DESCRIPTOR_ITEM << 8;
172
                transfer_request -> ux_transfer_request_index =            0;
173
                UX_TRANSFER_STATE_RESET(transfer_request);
174
175
                /* Set state to wait and next is "next".  */
176
                cdc_acm -> ux_host_class_cdc_acm_cmd_state = UX_STATE_WAIT;
177
                cdc_acm -> ux_host_class_cdc_acm_next_state = UX_STATE_NEXT;
178
179
                /* ACTIVATE_WAIT will be processed to finish next steps.  */
180
                return(UX_SUCCESS);
181
            }
182
            else
183
                status = UX_MEMORY_INSUFFICIENT;
184
        }
185
        else
186
        {
187
188
            /* We scan CDC ACM instances to find the master instance.  */
189
            /* Get class.  */
190
            cdc_acm_class = cdc_acm -> ux_host_class_cdc_acm_class;
191
192
            /* Get first instance linked to the class.  */
193
            cdc_acm_inst = (UX_HOST_CLASS_CDC_ACM *)cdc_acm_class -> ux_host_class_first_instance;
194
195
            /* Scan all instances.  */
196
            while(cdc_acm_inst)
197
            {
198
199
                /* If this data interface is on the same device and inside the associate list, link it.  */
200
                if ((cdc_acm_inst -> ux_host_class_cdc_acm_device ==
201
                        cdc_acm -> ux_host_class_cdc_acm_device) &&
202
                    (cdc_acm_inst -> ux_host_class_cdc_acm_interfaces_bitmap &
203
                        (1ul << interface_ptr -> ux_interface_descriptor.bInterfaceNumber)))
204
                {
205
206
                    /* Save control instance.  */
207
                    cdc_acm -> ux_host_class_cdc_acm_control = cdc_acm_inst;
208
                }
209
210
                /* Next instance.  */
211
                cdc_acm_inst = cdc_acm_inst -> ux_host_class_cdc_acm_next_instance;
212
            }
213
214
            /* Mark the cdc_acm as live now.  Both interfaces need to be live. */
215
            cdc_acm -> ux_host_class_cdc_acm_state = UX_HOST_CLASS_INSTANCE_LIVE;
216
217
            /* If all is fine and the device is mounted, we may need to inform the application
218
                if a function has been programmed in the system structure.  */
219
            if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
220
            {
221
222
                /* Call system change function.  */
223
                _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, cdc_acm -> ux_host_class_cdc_acm_class, (VOID *) cdc_acm);
224
            }
225
226
            /* If trace is enabled, insert this event into the trace buffer.  */
227
            UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_CDC_ACM_ACTIVATE, cdc_acm, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
228
229
            /* If trace is enabled, register this object.  */
230
            UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, cdc_acm, 0, 0, 0)
231
232
            /* We are done success. */
233
            return(UX_SUCCESS);
234
        }
235
    }
236
237
#else
238
239
    /* Configure the cdc_acm.  */
240
155
    status =  _ux_host_class_cdc_acm_configure(cdc_acm);
241
242
    /* If we are done success, go on to get endpoints.  */
243
155
    if (status == UX_SUCCESS)
244
245
        /* Get the cdc_acm endpoint(s). Depending on the interface type, we will need to search for
246
           Bulk Out and Bulk In endpoints and the optional interrupt endpoint.  */
247
149
        status =  _ux_host_class_cdc_acm_endpoints_get(cdc_acm);
248
249
    /* If we are done success, go on to mount interface.  */
250
155
    if (status == UX_SUCCESS)
251
    {
252
        /* Mark the cdc_acm as mounting now.  Both interfaces need to be mounting. */
253
138
        cdc_acm -> ux_host_class_cdc_acm_state =  UX_HOST_CLASS_INSTANCE_MOUNTING;
254
255
        /* If we have the Control Class, we have to configure the speed, parity ... */
256
138
        if (cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass == UX_HOST_CLASS_CDC_CONTROL_CLASS)
257
        {
258
259
            /* We need to wait for some device to settle. The Radicom USB Modem is an example of
260
               these device who fail the first Set_Line_Coding command if sent too quickly.
261
               The timing does not have to be precise so we use the thread sleep function.
262
               The default sleep value is 1 seconds.  */
263
77
            _ux_utility_delay_ms(UX_HOST_CLASS_CDC_ACM_DEVICE_INIT_DELAY);
264
265
            /* Do a GET_LINE_CODING first.  */
266
77
            status = _ux_host_class_cdc_acm_ioctl(cdc_acm, UX_HOST_CLASS_CDC_ACM_IOCTL_GET_LINE_CODING, (VOID *) &line_coding);
267
268
            /* If we are done success, go on.  */
269
76
            if (status == UX_SUCCESS)
270
            {
271
272
                /* Set the default values to the device, first line coding.  */
273
74
                line_coding.ux_host_class_cdc_acm_line_coding_dter      = UX_HOST_CLASS_CDC_ACM_LINE_CODING_DEFAULT_RATE;
274
74
                line_coding.ux_host_class_cdc_acm_line_coding_stop_bit  = UX_HOST_CLASS_CDC_ACM_LINE_CODING_DEFAULT_STOP_BIT;
275
74
                line_coding.ux_host_class_cdc_acm_line_coding_parity    = UX_HOST_CLASS_CDC_ACM_LINE_CODING_DEFAULT_PARITY;
276
74
                line_coding.ux_host_class_cdc_acm_line_coding_data_bits = UX_HOST_CLASS_CDC_ACM_LINE_CODING_DEFAULT_DATA_BIT;
277
74
                status = _ux_host_class_cdc_acm_ioctl(cdc_acm, UX_HOST_CLASS_CDC_ACM_IOCTL_SET_LINE_CODING, (VOID *) &line_coding);
278
            }
279
280
            /* If we are done success, go on.  */
281
76
            if (status == UX_SUCCESS)
282
            {
283
284
                /* Set the default values to the device, line state.  */
285
72
                line_state.ux_host_class_cdc_acm_line_state_rts       = 1;
286
72
                line_state.ux_host_class_cdc_acm_line_state_dtr       = 1;
287
72
                status = _ux_host_class_cdc_acm_ioctl(cdc_acm, UX_HOST_CLASS_CDC_ACM_IOCTL_SET_LINE_STATE, (VOID *) &line_state);
288
            }
289
290
            /* If we are done success, go on.  */
291
76
            if (status == UX_SUCCESS)
292
            {
293
294
                /* Get the capabilities of the device. We need to know if the commands are multiplexed over the comm
295
                interface or the data interface.  */
296
71
                status =  _ux_host_class_cdc_acm_capabilities_get(cdc_acm);
297
            }
298
        }
299
300
        /* If we are done success, go on.  */
301
137
        if (status == UX_SUCCESS)
302
        {
303
            /* Mark the cdc_acm as live now.  Both interfaces need to be live. */
304
128
            cdc_acm -> ux_host_class_cdc_acm_state =  UX_HOST_CLASS_INSTANCE_LIVE;
305
306
            /* If all is fine and the device is mounted, we may need to inform the application
307
               if a function has been programmed in the system structure.  */
308
128
            if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
309
            {
310
311
                /* Call system change function.  */
312
91
                _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, cdc_acm -> ux_host_class_cdc_acm_class, (VOID *) cdc_acm);
313
            }
314
315
            /* If trace is enabled, insert this event into the trace buffer.  */
316
            UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_CDC_ACM_ACTIVATE, cdc_acm, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
317
318
            /* If trace is enabled, register this object.  */
319
            UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, cdc_acm, 0, 0, 0)
320
321
            /* We are done success. */
322
128
            return(UX_SUCCESS);
323
        }
324
    }
325
#endif
326
327
    /* On error case, it's possible data buffer allocated for interrupt endpoint and transfer started, stop and free it.  */
328
26
    if (cdc_acm -> ux_host_class_cdc_acm_interrupt_endpoint &&
329
11
        cdc_acm -> ux_host_class_cdc_acm_interrupt_endpoint -> ux_endpoint_transfer_request.ux_transfer_request_data_pointer)
330
    {
331
332
        /* The first transfer request has already been initiated. Abort it.  */
333
10
        _ux_host_stack_endpoint_transfer_abort(cdc_acm -> ux_host_class_cdc_acm_interrupt_endpoint);
334
335
        /* Free the memory for the data pointer.  */
336
10
        _ux_utility_memory_free(cdc_acm -> ux_host_class_cdc_acm_interrupt_endpoint -> ux_endpoint_transfer_request.ux_transfer_request_data_pointer);
337
    }
338
339
    /* Destroy the instance.  */
340
26
    _ux_host_stack_class_instance_destroy(cdc_acm -> ux_host_class_cdc_acm_class, (VOID *) cdc_acm);
341
342
#if !defined(UX_HOST_STANDALONE)
343
344
    /* Destroy the semaphore.  */
345
26
    _ux_host_semaphore_delete(&cdc_acm -> ux_host_class_cdc_acm_semaphore);
346
#endif
347
348
    /* Unmount instance. */
349
26
    interface_ptr -> ux_interface_class_instance = UX_NULL;
350
351
    /* Free instance. */
352
26
    _ux_utility_memory_free(cdc_acm);
353
354
26
    return(status);
355
}
356