1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 /**************************************************************************/
12 /**                                                                       */
13 /** USBX Component                                                        */
14 /**                                                                       */
15 /**   Device CDC Class                                                    */
16 /**                                                                       */
17 /**************************************************************************/
18 /**************************************************************************/
19 
20 #define UX_SOURCE_CODE
21 
22 
23 /* Include necessary system files.  */
24 
25 #include "ux_api.h"
26 #include "ux_device_class_pima.h"
27 #include "ux_device_stack.h"
28 
29 
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    _ux_device_class_pima_activate                      PORTABLE C      */
35 /*                                                           6.3.0        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    Chaoqiong Xiao, Microsoft Corporation                               */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function activates the USB Pima device.                        */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    command                              Pointer to pima command        */
47 /*                                                                        */
48 /*  OUTPUT                                                                */
49 /*                                                                        */
50 /*    Completion Status                                                   */
51 /*                                                                        */
52 /*  CALLS                                                                 */
53 /*                                                                        */
54 /*    _ux_device_thread_resume              Resume thread                 */
55 /*                                                                        */
56 /*  CALLED BY                                                             */
57 /*                                                                        */
58 /*    USBX Source Code                                                    */
59 /*                                                                        */
60 /*  RELEASE HISTORY                                                       */
61 /*                                                                        */
62 /*    DATE              NAME                      DESCRIPTION             */
63 /*                                                                        */
64 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
65 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
66 /*                                            resulting in version 6.1    */
67 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
68 /*                                            refined macros names,       */
69 /*                                            added variables initialize, */
70 /*                                            resulting in version 6.1.10 */
71 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            fixed standalone compile,   */
73 /*                                            resulting in version 6.1.11 */
74 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            fixed parameter/variable    */
76 /*                                            names conflict C++ keyword, */
77 /*                                            resulting in version 6.1.12 */
78 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            supported optional INT EP,  */
80 /*                                            added a new mode to manage  */
81 /*                                            endpoint buffer in classes, */
82 /*                                            resulting in version 6.3.0  */
83 /*                                                                        */
84 /**************************************************************************/
_ux_device_class_pima_activate(UX_SLAVE_CLASS_COMMAND * command)85 UINT  _ux_device_class_pima_activate(UX_SLAVE_CLASS_COMMAND *command)
86 {
87 
88 UX_SLAVE_INTERFACE                      *interface_ptr;
89 UX_SLAVE_CLASS                          *class_ptr;
90 UX_SLAVE_CLASS_PIMA                     *pima;
91 UX_SLAVE_ENDPOINT                       *endpoint_in;
92 UX_SLAVE_ENDPOINT                       *endpoint_out;
93 UX_SLAVE_ENDPOINT                       *endpoint_interrupt;
94 
95 
96     /* Get the class container.  */
97     class_ptr =  command -> ux_slave_class_command_class_ptr;
98 
99     /* Store the class instance in the container.  */
100     pima = (UX_SLAVE_CLASS_PIMA *) class_ptr -> ux_slave_class_instance;
101 
102     /* Get the interface that owns this instance.  */
103     interface_ptr =  (UX_SLAVE_INTERFACE  *) command -> ux_slave_class_command_interface;
104 
105     /* Store the class instance into the interface.  */
106     interface_ptr -> ux_slave_interface_class_instance =  (VOID *)pima;
107 
108     /* Now the opposite, store the interface in the class instance.  */
109     pima -> ux_slave_class_pima_interface =  interface_ptr;
110 
111     /* Locate the endpoints.  */
112     endpoint_in =  interface_ptr -> ux_slave_interface_first_endpoint;
113 
114     /* Check the endpoint direction, if IN we have the correct endpoint.  */
115     if ((endpoint_in -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT)
116     {
117 
118         /* Wrong direction, we found the OUT endpoint first.  */
119         endpoint_out =  endpoint_in;
120 
121         /* So the next endpoint has to be the IN endpoint.  */
122         endpoint_in =  endpoint_out -> ux_slave_endpoint_next_endpoint;
123 
124         /* And the endpoint after that interrupt.  */
125         endpoint_interrupt =  endpoint_in -> ux_slave_endpoint_next_endpoint;
126 
127     }
128     else
129     {
130 
131         /* We found the endpoint IN first, so next endpoint is OUT.  */
132         endpoint_out =  endpoint_in -> ux_slave_endpoint_next_endpoint;
133 
134         /* And the endpoint after that interrupt.  */
135         endpoint_interrupt =  endpoint_out -> ux_slave_endpoint_next_endpoint;
136     }
137 
138     /* Save the endpoints in the pima instance.  */
139     pima -> ux_device_class_pima_bulk_in_endpoint           = endpoint_in;
140     pima -> ux_device_class_pima_bulk_out_endpoint          = endpoint_out;
141     pima -> ux_device_class_pima_interrupt_endpoint         = endpoint_interrupt;
142 
143 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
144     endpoint_in -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer =
145                                     UX_DEVICE_CLASS_PIMA_BULKIN_BUFFER(pima);
146     endpoint_out -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer =
147                                     UX_DEVICE_CLASS_PIMA_BULKOUT_BUFFER(pima);
148     if (endpoint_interrupt)
149         endpoint_interrupt -> ux_slave_endpoint_transfer_request.
150                                     ux_slave_transfer_request_data_pointer =
151                                     UX_DEVICE_CLASS_PIMA_INTERRUPTIN_BUFFER(pima);
152 #endif
153 
154     /* Initialize status code.  */
155     pima -> ux_device_class_pima_state = UX_DEVICE_CLASS_PIMA_PHASE_IDLE;
156     pima -> ux_device_class_pima_session_id = 0;
157     pima -> ux_device_class_pima_device_status = UX_DEVICE_CLASS_PIMA_RC_OK;
158 
159     /* Resume thread.  */
160     _ux_device_thread_resume(&class_ptr -> ux_slave_class_thread);
161 
162     /* If there is a activate function call it.  */
163     if (pima -> ux_device_class_pima_instance_activate != UX_NULL)
164     {
165         /* Invoke the application.  */
166         pima -> ux_device_class_pima_instance_activate(pima);
167     }
168 
169     /* If trace is enabled, insert this event into the trace buffer.  */
170     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_PIMA_ACTIVATE, pima, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
171 
172     /* If trace is enabled, register this object.  */
173     UX_TRACE_OBJECT_REGISTER(UX_TRACE_DEVICE_OBJECT_TYPE_INTERFACE, pima, 0, 0, 0)
174 
175     /* Return completion status.  */
176     return(UX_SUCCESS);
177 }
178 
179