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 /**   PIMA 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_pima.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_pima_activate                        PORTABLE C      */
38 /*                                                           6.1.12       */
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                                Pima class command pointer   */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_host_class_pima_configure           Configure pima class        */
58 /*    _ux_host_class_pima_endpoints_get       Get endpoints of pima       */
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_memory_free                 Free memory block           */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    _ux_host_class_pima_entry               Entry of pima 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 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            fixed parameter/variable    */
77 /*                                            names conflict C++ keyword, */
78 /*                                            resulting in version 6.1.12 */
79 /*                                                                        */
80 /**************************************************************************/
_ux_host_class_pima_activate(UX_HOST_CLASS_COMMAND * command)81 UINT  _ux_host_class_pima_activate(UX_HOST_CLASS_COMMAND *command)
82 {
83 
84 UX_INTERFACE                        *interface_ptr;
85 UX_HOST_CLASS_PIMA                  *pima;
86 UINT                                 status;
87 
88     /* The PIMA class is always activated by the interface descriptor and not the
89        device descriptor.  */
90     interface_ptr =  (UX_INTERFACE *) command -> ux_host_class_command_container;
91 
92     /* Obtain memory for this class instance.  */
93     pima =  _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_HOST_CLASS_PIMA));
94     if (pima == UX_NULL)
95         return(UX_MEMORY_INSUFFICIENT);
96 
97     /* Allocate some DMA safe memory for sending/receiving headers.  */
98     pima -> ux_host_class_pima_container =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_PIMA_CONTAINER_SIZE);
99     if (pima -> ux_host_class_pima_container == UX_NULL)
100         status = UX_MEMORY_INSUFFICIENT;
101     else
102         status = UX_SUCCESS;
103 
104     /* Allocate some DMA safe memory for receiving pima events.  */
105     if (status == UX_SUCCESS)
106     {
107 
108         pima -> ux_host_class_pima_event_buffer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_PIMA_AEI_MAX_LENGTH);
109         if (pima -> ux_host_class_pima_event_buffer == UX_NULL)
110             status = UX_MEMORY_INSUFFICIENT;
111     }
112 
113     /* Go on if no error.  */
114     if (status == UX_SUCCESS)
115     {
116 
117         /* Store the class container into this instance.  */
118         pima -> ux_host_class_pima_class =  command -> ux_host_class_command_class_ptr;
119 
120         /* Store the interface container into the pima class instance.  */
121         pima -> ux_host_class_pima_interface =  interface_ptr;
122 
123         /* Store the device container into the pima class instance.  */
124         pima -> ux_host_class_pima_device =  interface_ptr -> ux_interface_configuration -> ux_configuration_device;
125 
126         /* This instance of the device must also be stored in the interface container.  */
127         interface_ptr -> ux_interface_class_instance =  (VOID *) pima;
128 
129         /* Create this class instance.  */
130         _ux_host_stack_class_instance_create(pima -> ux_host_class_pima_class, (VOID *) pima);
131 
132         /* Configure the pima.  */
133         status =  _ux_host_class_pima_configure(pima);
134     }
135 
136     /* Get the pima endpoint(s). We will need to search for Bulk Out, Bulk In and interrupt endpoints.  */
137     if (status == UX_SUCCESS)
138         status =  _ux_host_class_pima_endpoints_get(pima);
139 
140     /* Success things.  */
141     if (status == UX_SUCCESS)
142     {
143 
144         /* Mark the pima as live now.  */
145         pima -> ux_host_class_pima_state =  UX_HOST_CLASS_INSTANCE_LIVE;
146 
147         /* If all is fine and the device is mounted, we may need to inform the application
148         if a function has been programmed in the system structure.  */
149         if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
150         {
151 
152             /* Call system change function.  */
153             _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, pima -> ux_host_class_pima_class, (VOID *) pima);
154         }
155 
156         /* If trace is enabled, insert this event into the trace buffer.  */
157         UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PIMA_ACTIVATE, pima, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
158 
159         /* If trace is enabled, register this object.  */
160         UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, pima, 0, 0, 0)
161 
162         /* Return success.  */
163         return(UX_SUCCESS);
164     }
165 
166     /* Free existing resources.  */
167     if (pima -> ux_host_class_pima_event_buffer)
168     {
169         _ux_host_stack_class_instance_destroy(pima -> ux_host_class_pima_class, (VOID *) pima);
170         interface_ptr -> ux_interface_class_instance = UX_NULL;
171         _ux_utility_memory_free(pima -> ux_host_class_pima_event_buffer);
172     }
173 
174     if (pima -> ux_host_class_pima_container)
175         _ux_utility_memory_free(pima -> ux_host_class_pima_container);
176 
177     _ux_utility_memory_free(pima);
178 
179     /* Return completion status.  */
180     return(status);
181 }
182 
183