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 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Device Data Pump Class                                              */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define UX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "ux_api.h"
28 #include "ux_device_class_dpump.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_class_dpump_activate                     PORTABLE C      */
36 /*                                                           6.3.0        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function activates the USB dpump device.                       */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    command                                 Pointer to dpump command    */
48 /*                                                                        */
49 /*  OUTPUT                                                                */
50 /*                                                                        */
51 /*    Completion Status                                                   */
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLED BY                                                             */
58 /*                                                                        */
59 /*    Device Data Pump Class                                              */
60 /*                                                                        */
61 /*  RELEASE HISTORY                                                       */
62 /*                                                                        */
63 /*    DATE              NAME                      DESCRIPTION             */
64 /*                                                                        */
65 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
66 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
67 /*                                            resulting in version 6.1    */
68 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
69 /*                                            added standalone support,   */
70 /*                                            resulting in version 6.1.10 */
71 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            fixed parameter/variable    */
73 /*                                            names conflict C++ keyword, */
74 /*                                            resulting in version 6.1.12 */
75 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            added a new mode to manage  */
77 /*                                            endpoint buffer in classes, */
78 /*                                            resulting in version 6.3.0  */
79 /*                                                                        */
80 /**************************************************************************/
_ux_device_class_dpump_activate(UX_SLAVE_CLASS_COMMAND * command)81 UINT  _ux_device_class_dpump_activate(UX_SLAVE_CLASS_COMMAND *command)
82 {
83 
84 UX_SLAVE_INTERFACE                      *interface_ptr;
85 UX_SLAVE_CLASS_DPUMP                    *dpump;
86 UX_SLAVE_CLASS                          *class_ptr;
87 UX_SLAVE_ENDPOINT                       *endpoint;
88 
89     /* Get the class container.  */
90     class_ptr =  command -> ux_slave_class_command_class_ptr;
91 
92     /* Store the class instance in the container.  */
93     dpump = (UX_SLAVE_CLASS_DPUMP *) class_ptr -> ux_slave_class_instance;
94 
95     /* Get the interface that owns this instance.  */
96     interface_ptr =  (UX_SLAVE_INTERFACE  *) command -> ux_slave_class_command_interface;
97 
98     /* Store the class instance into the interface.  */
99     interface_ptr -> ux_slave_interface_class_instance =  (VOID *)dpump;
100 
101     /* Now the opposite, store the interface in the class instance.  */
102     dpump -> ux_slave_class_dpump_interface =  interface_ptr;
103 
104     /* Locate the endpoints.  Interrupt for Control and Bulk in/out for Data.  */
105     endpoint =  interface_ptr -> ux_slave_interface_first_endpoint;
106 
107     /* Parse all endpoints.  */
108     while (endpoint != UX_NULL)
109     {
110 
111         /* Check the endpoint direction, and type.  */
112         if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN)
113         {
114 
115             /* Look at type.  */
116             if ((endpoint -> ux_slave_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT)
117             {
118 
119                 /* We have found the bulk in endpoint, save it.  */
120                 dpump -> ux_slave_class_dpump_bulkin_endpoint =  endpoint;
121 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
122                 endpoint -> ux_slave_endpoint_transfer_request.
123                         ux_slave_transfer_request_data_pointer =
124                                 UX_DEVICE_CLASS_DPUMP_WRITE_BUFFER(dpump);
125 #endif
126             }
127         }
128         else
129         {
130             /* Look at type for out endpoint.  */
131             if ((endpoint -> ux_slave_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT)
132             {
133 
134                 /* We have found the bulk out endpoint, save it.  */
135                 dpump -> ux_slave_class_dpump_bulkout_endpoint =  endpoint;
136 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
137                 endpoint -> ux_slave_endpoint_transfer_request.
138                         ux_slave_transfer_request_data_pointer =
139                                 UX_DEVICE_CLASS_DPUMP_READ_BUFFER(dpump);
140 #endif
141             }
142         }
143 
144         /* Next endpoint.  */
145         endpoint =  endpoint -> ux_slave_endpoint_next_endpoint;
146     }
147 
148 #if defined(UX_DEVICE_STANDALONE)
149 
150     /* Reset read/write states.  */
151     dpump -> ux_device_class_dpump_read_state = 0;
152     dpump -> ux_device_class_dpump_write_state = 0;
153 #endif
154 
155     /* If there is a activate function call it.  */
156     if (dpump -> ux_slave_class_dpump_parameter.ux_slave_class_dpump_instance_activate != UX_NULL)
157     {
158 
159         /* Invoke the application.  */
160         dpump -> ux_slave_class_dpump_parameter.ux_slave_class_dpump_instance_activate(dpump);
161     }
162 
163     /* If trace is enabled, insert this event into the trace buffer.  */
164     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_DPUMP_ACTIVATE, dpump, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
165 
166     /* If trace is enabled, register this object.  */
167     UX_TRACE_OBJECT_REGISTER(UX_TRACE_DEVICE_OBJECT_TYPE_INTERFACE, dpump, 0, 0, 0)
168 
169     /* Return completion status.  */
170     return(UX_SUCCESS);
171 }
172 
173