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_configure                       PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function calls the USBX stack to do a SET_CONFIGURATION to the */
46 /*    gser. Once the gser is configured, its interface will be            */
47 /*    activated. The bulk endpoints enumerated(1 IN, 1 OUT ).             */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    gser                                   Pointer to gser class        */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_stack_configuration_interface_get  Get interface           */
60 /*    _ux_host_stack_device_configuration_get     Get configuration       */
61 /*    _ux_host_stack_device_configuration_select  Select configuration    */
62 /*    _ux_host_semaphore_create                   Create semaphore        */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    _ux_host_class_gser_activate                gser class activate     */
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 /*                                            optimized based on compile  */
75 /*                                            definitions,                */
76 /*                                            resulting in version 6.1    */
77 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            refined macros names,       */
79 /*                                            resulting in version 6.1.10 */
80 /*                                                                        */
81 /**************************************************************************/
_ux_host_class_gser_configure(UX_HOST_CLASS_GSER * gser)82 UINT  _ux_host_class_gser_configure(UX_HOST_CLASS_GSER *gser)
83 {
84 
85 UINT                    status;
86 UX_CONFIGURATION        *configuration;
87 ULONG                   interface_index;
88 #if UX_MAX_DEVICES > 1
89 UX_DEVICE               *parent_device;
90 #endif
91 
92 
93     /* If the device has been configured already, we don't need to do it
94        again. */
95     if (gser -> ux_host_class_gser_device -> ux_device_state == UX_DEVICE_CONFIGURED)
96         return(UX_SUCCESS);
97 
98     /* A gser normally has one configuration. So retrieve the 1st configuration
99        only.  */
100     status =  _ux_host_stack_device_configuration_get(gser -> ux_host_class_gser_device, 0, &configuration);
101     if (status != UX_SUCCESS)
102     {
103 
104         /* Error trap. */
105         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
106 
107         /* If trace is enabled, insert this event into the trace buffer.  */
108         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, gser -> ux_host_class_gser_device, 0, 0, UX_TRACE_ERRORS, 0, 0)
109 
110         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
111     }
112 
113 #if UX_MAX_DEVICES > 1
114     /* Check the gser power source and check the parent power source for
115        incompatible connections.  */
116     if (gser -> ux_host_class_gser_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED)
117     {
118 
119         /* Get parent device pointer.  */
120         parent_device =  gser -> ux_host_class_gser_device -> ux_device_parent;
121 
122         /* If the device is NULL, the parent is the root gser and we don't have to worry
123            if the parent is not the root gser, check for its power source.  */
124         if ((parent_device != UX_NULL) && (parent_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED))
125         {
126 
127             /* Error trap. */
128             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
129 
130             /* If trace is enabled, insert this event into the trace buffer.  */
131             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONNECTION_INCOMPATIBLE, gser, 0, 0, UX_TRACE_ERRORS, 0, 0)
132 
133             return(UX_CONNECTION_INCOMPATIBLE);
134         }
135     }
136 #endif
137 
138     /* We have the valid configuration. Ask the USBX stack to set this configuration.  */
139     status =  _ux_host_stack_device_configuration_select(configuration);
140     if (status != UX_SUCCESS)
141         return(status);
142 
143     /* If the operation went well, the gser default alternate setting for the gser interface is
144        active. We have to scan all interfaces and attach them. Each interface has an semaphore as well for protection. */
145     for (interface_index = 0; interface_index < UX_HOST_CLASS_GSER_INTERFACE_NUMBER; interface_index++)
146     {
147 
148         /* Get that interface.  */
149         status =  _ux_host_stack_configuration_interface_get(configuration, interface_index, 0, &gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_interface);
150 
151         /* Should not fail. But do a sanity check.  */
152         if (status == UX_SUCCESS)
153         {
154 
155             /* Store the instance in the interface container, this is for the USB stack
156                when it needs to invoke the class.  */
157             gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_interface -> ux_interface_class_instance =  (VOID *) gser;
158 
159             /* Store the class container in the interface.  The device has the correct class, duplicate it to the
160                interface.  */
161             gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_interface -> ux_interface_class =  gser -> ux_host_class_gser_device -> ux_device_class ;
162 
163             /* Create the semaphore to protect 2 threads from accessing the same gser instance.  */
164             status =  _ux_host_semaphore_create(&gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_semaphore, "ux_host_class_gser_semaphore", 1);
165 
166             /* Check status.  */
167             if (status != UX_SUCCESS)
168                 return(UX_SEMAPHORE_ERROR);
169 
170         }
171     }
172 
173     /* Return completion status.  */
174     return(UX_SUCCESS);
175 }
176 
177