summaryrefslogtreecommitdiff
path: root/common/usbx_host_classes/src/ux_host_class_cdc_acm_activate.c
blob: 2c9b18c5766818777b1e69d5b154e11eb5f6eac0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** USBX Component                                                        */
/**                                                                       */
/**   CDC ACM Class                                                       */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

#include "ux_api.h"
#include "ux_host_class_cdc_acm.h"
#include "ux_host_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_class_cdc_acm_activate                     PORTABLE C      */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function creates the ACM instance, configure the device ...    */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    command                                ACM  class command pointer   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_class_cdc_acm_configure        Configure cdc_acm class     */
/*    _ux_host_class_cdc_acm_endpoints_get    Get endpoints of cdc_acm    */
/*    _ux_host_class_cdc_acm_ioctl            IOCTL function for ACM      */
/*    _ux_host_stack_class_instance_destroy   Destroy the class instance  */
/*    _ux_host_stack_endpoint_transfer_abort  Abort transfer              */
/*    _ux_utility_memory_allocate             Allocate memory block       */
/*    _ux_utility_memory_free                 Free memory                 */
/*    _ux_host_semaphore_create               Create cdc_acm semaphore    */
/*    _ux_host_semaphore_delete               Delete semaphore            */
/*    _ux_utility_delay_ms                    Delay                       */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _ux_host_class_cdc_acm_entry          Entry of cdc_acm class        */
/*    _ux_utility_delay_ms                  Delay ms                      */
/*                                                                        */
/**************************************************************************/
UINT  _ux_host_class_cdc_acm_activate(UX_HOST_CLASS_COMMAND *command)
{

UX_INTERFACE                        *interface_ptr;
UX_HOST_CLASS_CDC_ACM               *cdc_acm;
UINT                                status;
#if defined(UX_HOST_STANDALONE)
UX_HOST_CLASS                       *cdc_acm_class;
UX_HOST_CLASS_CDC_ACM               *cdc_acm_inst;
UX_ENDPOINT                         *control_endpoint;
UX_TRANSFER                         *transfer_request;
ULONG                               descriptors_length;
#else
UX_HOST_CLASS_CDC_ACM_LINE_CODING   line_coding;
UX_HOST_CLASS_CDC_ACM_LINE_STATE    line_state;
#endif

    /* The CDC ACM class is always activated by the interface descriptor and not the
       device descriptor.  */
    interface_ptr =  (UX_INTERFACE *) command -> ux_host_class_command_container;

    /* Obtain memory for this class instance.  */
    cdc_acm =  _ux_utility_memory_allocate(UX_NO_ALIGN, UX_CACHE_SAFE_MEMORY, sizeof(UX_HOST_CLASS_CDC_ACM));

    /* Instance creation fail. */
    if (cdc_acm == UX_NULL)

        /* Memory allocation fail. */
        return(UX_MEMORY_INSUFFICIENT);

#if !defined(UX_HOST_STANDALONE)

    /* Create the semaphore to protect 2 threads from accessing the same acm instance.  */
    status =  _ux_host_semaphore_create(&cdc_acm -> ux_host_class_cdc_acm_semaphore, "ux_host_class_cdc_acm_semaphore", 1);
    if (status != UX_SUCCESS)
    {

        /* Free instance memory. */
        _ux_utility_memory_free(cdc_acm);

        /* Semaphore creation error. */
        return(UX_SEMAPHORE_ERROR);
    }
#endif

    /* Store the class container into this instance.  */
    cdc_acm -> ux_host_class_cdc_acm_class =  command -> ux_host_class_command_class_ptr;

    /* Store the interface container into the cdc_acm class instance.  */
    cdc_acm -> ux_host_class_cdc_acm_interface =  interface_ptr;

    /* Store the device container into the cdc_acm class instance.  */
    cdc_acm -> ux_host_class_cdc_acm_device =  interface_ptr -> ux_interface_configuration -> ux_configuration_device;

    /* This instance of the device must also be stored in the interface container.  */
    interface_ptr -> ux_interface_class_instance =  (VOID *) cdc_acm;

    /* Create this class instance.  */
    _ux_host_stack_class_instance_create(cdc_acm -> ux_host_class_cdc_acm_class, (VOID *) cdc_acm);

#if defined(UX_HOST_STANDALONE)

    /* Get the cdc_acm endpoint(s). Depending on the interface type, we will need to search for
        Bulk Out and Bulk In endpoints and the optional interrupt endpoint.  */
    status =  _ux_host_class_cdc_acm_endpoints_get(cdc_acm);
    if (status == UX_SUCCESS)
    {

        /* Mark the cdc_acm as mounting.  */
        cdc_acm -> ux_host_class_cdc_acm_state = UX_HOST_CLASS_INSTANCE_MOUNTING;

        /* If we have the Control Class, we process default setup command sequence.  */
        if (interface_ptr -> ux_interface_descriptor.bInterfaceClass == UX_HOST_CLASS_CDC_CONTROL_CLASS)
        {

            /* Get descriptors to see capabilities.  */

            /* Get default control transfer.  */
            control_endpoint = &cdc_acm -> ux_host_class_cdc_acm_device -> ux_device_control_endpoint;
            transfer_request = &control_endpoint -> ux_endpoint_transfer_request;

            /* Allocate memory for the descriptors.  */
            descriptors_length = interface_ptr -> ux_interface_configuration ->
                                        ux_configuration_descriptor.wTotalLength;
            cdc_acm -> ux_host_class_cdc_acm_allocated =
                    _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY,
                                                            descriptors_length);
            if (cdc_acm -> ux_host_class_cdc_acm_allocated != UX_NULL)
            {

                transfer_request -> ux_transfer_request_data_pointer =
                                            cdc_acm -> ux_host_class_cdc_acm_allocated;

                /* Create transfer for GET_DESCRIPTOR.  */
                transfer_request -> ux_transfer_request_requested_length = descriptors_length;
                transfer_request -> ux_transfer_request_function =         UX_GET_DESCRIPTOR;
                transfer_request -> ux_transfer_request_type =             UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
                transfer_request -> ux_transfer_request_value =            UX_CONFIGURATION_DESCRIPTOR_ITEM << 8;
                transfer_request -> ux_transfer_request_index =            0;
                UX_TRANSFER_STATE_RESET(transfer_request);

                /* Set state to wait and next is "next".  */
                cdc_acm -> ux_host_class_cdc_acm_cmd_state = UX_STATE_WAIT;
                cdc_acm -> ux_host_class_cdc_acm_next_state = UX_STATE_NEXT;

                /* ACTIVATE_WAIT will be processed to finish next steps.  */
                return(UX_SUCCESS);
            }
            else
                status = UX_MEMORY_INSUFFICIENT;
        }
        else
        {

            /* We scan CDC ACM instances to find the master instance.  */
            /* Get class.  */
            cdc_acm_class = cdc_acm -> ux_host_class_cdc_acm_class;

            /* Get first instance linked to the class.  */
            cdc_acm_inst = (UX_HOST_CLASS_CDC_ACM *)cdc_acm_class -> ux_host_class_first_instance;

            /* Scan all instances.  */
            while(cdc_acm_inst)
            {

                /* If this data interface is on the same device and inside the associate list, link it.  */
                if ((cdc_acm_inst -> ux_host_class_cdc_acm_device ==
                        cdc_acm -> ux_host_class_cdc_acm_device) &&
                    (cdc_acm_inst -> ux_host_class_cdc_acm_interfaces_bitmap &
                        (1ul << interface_ptr -> ux_interface_descriptor.bInterfaceNumber)))
                {

                    /* Save control instance.  */
                    cdc_acm -> ux_host_class_cdc_acm_control = cdc_acm_inst;
                }

                /* Next instance.  */
                cdc_acm_inst = cdc_acm_inst -> ux_host_class_cdc_acm_next_instance;
            }

            /* Mark the cdc_acm as live now.  Both interfaces need to be live. */
            cdc_acm -> ux_host_class_cdc_acm_state = UX_HOST_CLASS_INSTANCE_LIVE;

            /* If all is fine and the device is mounted, we may need to inform the application
                if a function has been programmed in the system structure.  */
            if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
            {

                /* Call system change function.  */
                _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, cdc_acm -> ux_host_class_cdc_acm_class, (VOID *) cdc_acm);
            }

            /* If trace is enabled, insert this event into the trace buffer.  */
            UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_CDC_ACM_ACTIVATE, cdc_acm, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)

            /* If trace is enabled, register this object.  */
            UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, cdc_acm, 0, 0, 0)

            /* We are done success. */
            return(UX_SUCCESS);
        }
    }

#else

    /* Configure the cdc_acm.  */
    status =  _ux_host_class_cdc_acm_configure(cdc_acm);

    /* If we are done success, go on to get endpoints.  */
    if (status == UX_SUCCESS)

        /* Get the cdc_acm endpoint(s). Depending on the interface type, we will need to search for
           Bulk Out and Bulk In endpoints and the optional interrupt endpoint.  */
        status =  _ux_host_class_cdc_acm_endpoints_get(cdc_acm);

    /* If we are done success, go on to mount interface.  */
    if (status == UX_SUCCESS)
    {
        /* Mark the cdc_acm as mounting now.  Both interfaces need to be mounting. */
        cdc_acm -> ux_host_class_cdc_acm_state =  UX_HOST_CLASS_INSTANCE_MOUNTING;

        /* If we have the Control Class, we have to configure the speed, parity ... */
        if (cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass == UX_HOST_CLASS_CDC_CONTROL_CLASS)
        {

            /* We need to wait for some device to settle. The Radicom USB Modem is an example of
               these device who fail the first Set_Line_Coding command if sent too quickly.
               The timing does not have to be precise so we use the thread sleep function.
               The default sleep value is 1 seconds.  */
            _ux_utility_delay_ms(UX_HOST_CLASS_CDC_ACM_DEVICE_INIT_DELAY);

            /* Do a GET_LINE_CODING first.  */
            status = _ux_host_class_cdc_acm_ioctl(cdc_acm, UX_HOST_CLASS_CDC_ACM_IOCTL_GET_LINE_CODING, (VOID *) &line_coding);

            /* If we are done success, go on.  */
            if (status == UX_SUCCESS)
            {

                /* Set the default values to the device, first line coding.  */
                line_coding.ux_host_class_cdc_acm_line_coding_dter      = UX_HOST_CLASS_CDC_ACM_LINE_CODING_DEFAULT_RATE;
                line_coding.ux_host_class_cdc_acm_line_coding_stop_bit  = UX_HOST_CLASS_CDC_ACM_LINE_CODING_DEFAULT_STOP_BIT;
                line_coding.ux_host_class_cdc_acm_line_coding_parity    = UX_HOST_CLASS_CDC_ACM_LINE_CODING_DEFAULT_PARITY;
                line_coding.ux_host_class_cdc_acm_line_coding_data_bits = UX_HOST_CLASS_CDC_ACM_LINE_CODING_DEFAULT_DATA_BIT;
                status = _ux_host_class_cdc_acm_ioctl(cdc_acm, UX_HOST_CLASS_CDC_ACM_IOCTL_SET_LINE_CODING, (VOID *) &line_coding);
            }

            /* If we are done success, go on.  */
            if (status == UX_SUCCESS)
            {

                /* Set the default values to the device, line state.  */
                line_state.ux_host_class_cdc_acm_line_state_rts       = 1;
                line_state.ux_host_class_cdc_acm_line_state_dtr       = 1;
                status = _ux_host_class_cdc_acm_ioctl(cdc_acm, UX_HOST_CLASS_CDC_ACM_IOCTL_SET_LINE_STATE, (VOID *) &line_state);
            }

            /* If we are done success, go on.  */
            if (status == UX_SUCCESS)
            {

                /* Get the capabilities of the device. We need to know if the commands are multiplexed over the comm
                interface or the data interface.  */
                status =  _ux_host_class_cdc_acm_capabilities_get(cdc_acm);
            }
        }

        /* If we are done success, go on.  */
        if (status == UX_SUCCESS)
        {
            /* Mark the cdc_acm as live now.  Both interfaces need to be live. */
            cdc_acm -> ux_host_class_cdc_acm_state =  UX_HOST_CLASS_INSTANCE_LIVE;

            /* If all is fine and the device is mounted, we may need to inform the application
               if a function has been programmed in the system structure.  */
            if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
            {

                /* Call system change function.  */
                _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, cdc_acm -> ux_host_class_cdc_acm_class, (VOID *) cdc_acm);
            }

            /* If trace is enabled, insert this event into the trace buffer.  */
            UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_CDC_ACM_ACTIVATE, cdc_acm, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)

            /* If trace is enabled, register this object.  */
            UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, cdc_acm, 0, 0, 0)

            /* We are done success. */
            return(UX_SUCCESS);
        }
    }
#endif

    /* On error case, it's possible data buffer allocated for interrupt endpoint and transfer started, stop and free it.  */
    if (cdc_acm -> ux_host_class_cdc_acm_interrupt_endpoint &&
        cdc_acm -> ux_host_class_cdc_acm_interrupt_endpoint -> ux_endpoint_transfer_request.ux_transfer_request_data_pointer)
    {

        /* The first transfer request has already been initiated. Abort it.  */
        _ux_host_stack_endpoint_transfer_abort(cdc_acm -> ux_host_class_cdc_acm_interrupt_endpoint);

        /* Free the memory for the data pointer.  */
        _ux_utility_memory_free(cdc_acm -> ux_host_class_cdc_acm_interrupt_endpoint -> ux_endpoint_transfer_request.ux_transfer_request_data_pointer);
    }

    /* Destroy the instance.  */
    _ux_host_stack_class_instance_destroy(cdc_acm -> ux_host_class_cdc_acm_class, (VOID *) cdc_acm);

#if !defined(UX_HOST_STANDALONE)

    /* Destroy the semaphore.  */
    _ux_host_semaphore_delete(&cdc_acm -> ux_host_class_cdc_acm_semaphore);
#endif

    /* Unmount instance. */
    interface_ptr -> ux_interface_class_instance = UX_NULL;

    /* Free instance. */
    _ux_utility_memory_free(cdc_acm);

    return(status);
}