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 /**   Host Data Pump 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_dpump.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_dpump_activate                       PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function calls the USBX stack to activate the class.           */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    command                               Dpump class command pointer   */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_host_class_dpump_configure          Configure dpump class       */
58 /*    _ux_host_class_dpump_endpoints_get      Get endpoints of dpump      */
59 /*    _ux_host_stack_class_instance_create    Create class instance       */
60 /*    _ux_host_stack_class_instance_destroy   Destroy the class instance  */
61 /*    _ux_utility_memory_allocate             Allocate memory block       */
62 /*    _ux_utility_semaphore_create            Create dpump semaphore      */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    _ux_host_class_dpump_entry          Entry of dpump class            */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            added standalone support,   */
77 /*                                            resulting in version 6.1.10 */
78 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            internal clean up,          */
80 /*                                            resulting in version 6.1.11 */
81 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
82 /*                                            fixed parameter/variable    */
83 /*                                            names conflict C++ keyword, */
84 /*                                            resulting in version 6.1.12 */
85 /*                                                                        */
86 /**************************************************************************/
_ux_host_class_dpump_activate(UX_HOST_CLASS_COMMAND * command)87 UINT  _ux_host_class_dpump_activate(UX_HOST_CLASS_COMMAND *command)
88 {
89 
90 UX_INTERFACE                *interface_ptr;
91 UX_HOST_CLASS_DPUMP         *dpump;
92 UINT                        status;
93 
94 
95     /* The data pump is always activated by the interface descriptor and not the
96        device descriptor.  */
97     interface_ptr =  (UX_INTERFACE *) command -> ux_host_class_command_container;
98 
99     /* Obtain memory for this class instance.  */
100     dpump =  _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_HOST_CLASS_DPUMP));
101     if (dpump == UX_NULL)
102         return(UX_MEMORY_INSUFFICIENT);
103 
104     /* Store the class container into this instance.  */
105     dpump -> ux_host_class_dpump_class =  command -> ux_host_class_command_class_ptr;
106 
107     /* Store the interface container into the dpump class instance.  */
108     dpump -> ux_host_class_dpump_interface =  interface_ptr;
109 
110     /* Store the device container into the dpump class instance.  */
111     dpump -> ux_host_class_dpump_device =  interface_ptr -> ux_interface_configuration -> ux_configuration_device;
112 
113     /* This instance of the device must also be stored in the interface container.  */
114     interface_ptr -> ux_interface_class_instance =  (VOID *) dpump;
115 
116     /* Create this class instance.  */
117     _ux_host_stack_class_instance_create(dpump -> ux_host_class_dpump_class, (VOID *) dpump);
118 
119     /* Configure the dpump.  */
120     status =  _ux_host_class_dpump_configure(dpump);
121     if (status != UX_SUCCESS)
122     {
123 
124         _ux_host_stack_class_instance_destroy(dpump -> ux_host_class_dpump_class, (VOID *) dpump);
125         return(status);
126     }
127 
128     /* Get the dpump endpoint(s). We will need to search for Bulk Out and Bulk In endpoints.  Do not check for errors
129        here as the alternate setting for this interface may be 0 which has no endpoints.  */
130     _ux_host_class_dpump_endpoints_get(dpump);
131 
132     /* Create the semaphore to protect 2 threads from accessing the same dpump instance.  */
133     status =  _ux_host_semaphore_create(&dpump -> ux_host_class_dpump_semaphore, "ux_dpump_semaphore", 1);
134     if (status != UX_SUCCESS)
135         return(UX_SEMAPHORE_ERROR);
136 
137     /* Mark the dpump as live now.  */
138     dpump -> ux_host_class_dpump_state =  UX_HOST_CLASS_INSTANCE_LIVE;
139 
140     /* If all is fine and the device is mounted, we may need to inform the application
141        if a function has been programmed in the system structure.  */
142     if ((status == UX_SUCCESS) && (_ux_system_host -> ux_system_host_change_function != UX_NULL))
143     {
144 
145         /* Call system change function.  */
146         _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, dpump -> ux_host_class_dpump_class, (VOID *) dpump);
147     }
148 
149     /* If trace is enabled, insert this event into the trace buffer.  */
150     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_DPUMP_ACTIVATE, dpump, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
151 
152     /* If trace is enabled, register this object.  */
153     UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, dpump, 0, 0, 0)
154 
155     /* Return completion status.  */
156     return(status);
157 }
158 
159