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 /**   Generic Serial Host module 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_gser.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_gser_activate                        PORTABLE C      */
37 /*                                                           6.3.0        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function calls the USBX stack to activate the class.           */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    command                               Dpump class command pointer   */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    Completion Status                                                   */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _ux_host_class_gser_configure           Configure gser class        */
57 /*    _ux_host_class_gser_endpoints_get       Get endpoints of gser       */
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_gser_entry            Entry of gser 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 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            internal clean up,          */
76 /*                                            resulting in version 6.1.11 */
77 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            improved error handling,    */
79 /*                                            resulting in version 6.3.0  */
80 /*                                                                        */
81 /**************************************************************************/
_ux_host_class_gser_activate(UX_HOST_CLASS_COMMAND * command)82 UINT  _ux_host_class_gser_activate(UX_HOST_CLASS_COMMAND *command)
83 {
84 
85 UX_DEVICE                           *device;
86 UX_HOST_CLASS_GSER                  *gser;
87 UINT                                status;
88 ULONG                               interface_index;
89 
90 
91     /* The Generic Modem class is always activated by the device descriptor. */
92     device =  (UX_DEVICE *) command -> ux_host_class_command_container;
93 
94     /* Obtain memory for this class instance.  */
95     gser =  _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_HOST_CLASS_GSER));
96     if (gser == UX_NULL)
97         return(UX_MEMORY_INSUFFICIENT);
98 
99     /* Store the class container into this instance.  */
100     gser -> ux_host_class_gser_class =  command -> ux_host_class_command_class_ptr;
101 
102     /* Store the device container into the gser class instance.  */
103     gser -> ux_host_class_gser_device =  device;
104 
105     /* Store the instance in the device container, this is for the USBX stack
106        when it needs to invoke the class for deactivation.  */
107     device -> ux_device_class_instance =  (VOID *) gser;
108 
109     /* Create this class instance.  */
110     _ux_host_stack_class_instance_create(gser -> ux_host_class_gser_class, (VOID *) gser);
111 
112     /* Configure the gser class.  */
113     status =  _ux_host_class_gser_configure(gser);
114 
115     /* Get the gser endpoint(s). We will need to search for Bulk Out and Bulk In endpoints on each interface .  */
116     if (status == UX_SUCCESS)
117         status =  _ux_host_class_gser_endpoints_get(gser);
118 
119     /* Success things.  */
120     if (status == UX_SUCCESS)
121     {
122 
123         /* Mark the gser as live now.  */
124         gser -> ux_host_class_gser_state =  UX_HOST_CLASS_INSTANCE_LIVE;
125 
126         /* If all is fine and the device is mounted, we may need to inform the application
127         if a function has been programmed in the system structure.  */
128         if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
129         {
130 
131             /* Call system change function.  */
132             _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, gser -> ux_host_class_gser_class, (VOID *) gser);
133         }
134 
135         /* If trace is enabled, insert this event into the trace buffer.  */
136         UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_GSER_ACTIVATE, gser, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
137 
138         /* If trace is enabled, register this object.  */
139         UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, gser, 0, 0, 0)
140 
141         /* Return success.  */
142         return(UX_SUCCESS);
143     }
144 
145     /* Free created resources.  */
146     for (interface_index = 0; interface_index < UX_HOST_CLASS_GSER_INTERFACE_NUMBER; interface_index ++)
147     {
148         if (_ux_host_semaphore_created(&gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_semaphore))
149             _ux_host_semaphore_delete(&gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_semaphore);
150     }
151 
152     /* Destroy class instance.  */
153     _ux_host_stack_class_instance_destroy(gser -> ux_host_class_gser_class, (VOID *) gser);
154 
155     /* Clear the instance in the device container.  */
156     device -> ux_device_class_instance = UX_NULL;
157 
158     /* Free instance memory.  */
159     _ux_utility_memory_free(gser);
160 
161     /* Return completion status.  */
162     return(status);
163 }
164 
165