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