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 /**   Generic Serial Host module 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_gser.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_gser_activate                        PORTABLE C      */
38 /*                                                           6.1.11       */
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_gser_configure           Configure gser class        */
58 /*    _ux_host_class_gser_endpoints_get       Get endpoints of gser       */
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_gser_entry            Entry of gser 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 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            internal clean up,          */
77 /*                                            resulting in version 6.1.11 */
78 /*                                                                        */
79 /**************************************************************************/
_ux_host_class_gser_activate(UX_HOST_CLASS_COMMAND * command)80 UINT  _ux_host_class_gser_activate(UX_HOST_CLASS_COMMAND *command)
81 {
82 
83 UX_DEVICE                           *device;
84 UX_HOST_CLASS_GSER                  *gser;
85 UINT                                status;
86 
87 
88     /* The Generic Modem class is always activated by the device descriptor. */
89     device =  (UX_DEVICE *) command -> ux_host_class_command_container;
90 
91     /* Obtain memory for this class instance.  */
92     gser =  _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_HOST_CLASS_GSER));
93     if (gser == UX_NULL)
94         return(UX_MEMORY_INSUFFICIENT);
95 
96     /* Store the class container into this instance.  */
97     gser -> ux_host_class_gser_class =  command -> ux_host_class_command_class_ptr;
98 
99     /* Store the device container into the gser class instance.  */
100     gser -> ux_host_class_gser_device =  device;
101 
102     /* Store the instance in the device container, this is for the USBX stack
103        when it needs to invoke the class for deactivation.  */
104     device -> ux_device_class_instance =  (VOID *) gser;
105 
106     /* Create this class instance.  */
107     _ux_host_stack_class_instance_create(gser -> ux_host_class_gser_class, (VOID *) gser);
108 
109     /* Configure the gser class.  */
110     status =  _ux_host_class_gser_configure(gser);
111 
112     /* Get the gser endpoint(s). We will need to search for Bulk Out and Bulk In endpoints on each interface .  */
113     if (status == UX_SUCCESS)
114         status =  _ux_host_class_gser_endpoints_get(gser);
115 
116     /* Success things.  */
117     if (status == UX_SUCCESS)
118     {
119 
120         /* Mark the gser as live now.  */
121         gser -> ux_host_class_gser_state =  UX_HOST_CLASS_INSTANCE_LIVE;
122 
123         /* If all is fine and the device is mounted, we may need to inform the application
124         if a function has been programmed in the system structure.  */
125         if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
126         {
127 
128             /* Call system change function.  */
129             _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, gser -> ux_host_class_gser_class, (VOID *) gser);
130         }
131 
132         /* If trace is enabled, insert this event into the trace buffer.  */
133         UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_GSER_ACTIVATE, gser, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
134 
135         /* If trace is enabled, register this object.  */
136         UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, gser, 0, 0, 0)
137 
138         /* Return success.  */
139         return(UX_SUCCESS);
140     }
141 
142     /* Destroy class instance.  */
143     _ux_host_stack_class_instance_destroy(gser -> ux_host_class_gser_class, (VOID *) gser);
144 
145     /* Clear the instance in the device container.  */
146     device -> ux_device_class_instance = UX_NULL;
147 
148     /* Free instance memory.  */
149     _ux_utility_memory_free(gser);
150 
151     /* Return completion status.  */
152     return(status);
153 }
154 
155