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 /**   Asix 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_asix.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_asix_configure                       PORTABLE C      */
38 /*                                                           6.1          */
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 /*    asix. Once the asix is configured, its interface will be            */
47 /*    activated. The bulk endpoints (1 IN, 1 OUT ) and the optional       */
48 /*    interrupt endpoint are enumerated.                                  */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    asix                                      Pointer to asix class     */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    Completion Status                                                   */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_host_stack_configuration_interface_get  Get interface           */
61 /*    _ux_host_stack_device_configuration_get     Get configuration       */
62 /*    _ux_host_stack_device_configuration_select  Select configuration    */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    _ux_host_class_asix_activate                  Asix 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 /*                                                                        */
78 /**************************************************************************/
_ux_host_class_asix_configure(UX_HOST_CLASS_ASIX * asix)79 UINT  _ux_host_class_asix_configure(UX_HOST_CLASS_ASIX *asix)
80 {
81 
82 UINT                    status;
83 UX_CONFIGURATION        *configuration;
84 #if UX_MAX_DEVICES > 1
85 UX_DEVICE               *parent_device;
86 #endif
87 
88 
89     /* If the device has been configured already, we don't need to do it
90        again. */
91     if (asix -> ux_host_class_asix_device -> ux_device_state == UX_DEVICE_CONFIGURED)
92         return(UX_SUCCESS);
93 
94     /* A asix normally has one configuration. So retrieve the 1st configuration
95        only.  */
96     status =  _ux_host_stack_device_configuration_get(asix -> ux_host_class_asix_device, 0, &configuration);
97     if (status != UX_SUCCESS)
98     {
99 
100         /* Error trap. */
101         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
102 
103         /* If trace is enabled, insert this event into the trace buffer.  */
104         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, asix -> ux_host_class_asix_device, 0, 0, UX_TRACE_ERRORS, 0, 0)
105 
106         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
107     }
108 
109 #if UX_MAX_DEVICES > 1
110     /* Check the asix power source and check the parent power source for
111        incompatible connections.  */
112     if (asix -> ux_host_class_asix_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED)
113     {
114 
115         /* Get parent device pointer.  */
116         parent_device =  asix -> ux_host_class_asix_device -> ux_device_parent;
117 
118         /* If the device is NULL, the parent is the root asix and we don't have to worry
119            if the parent is not the root asix, check for its power source.  */
120         if ((parent_device != UX_NULL) && (parent_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED))
121         {
122 
123             /* Error trap. */
124             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONNECTION_INCOMPATIBLE);
125 
126             /* If trace is enabled, insert this event into the trace buffer.  */
127             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONNECTION_INCOMPATIBLE, asix, 0, 0, UX_TRACE_ERRORS, 0, 0)
128 
129             return(UX_CONNECTION_INCOMPATIBLE);
130         }
131     }
132 #endif
133 
134     /* We have the valid configuration. Ask the USBX stack to set this configuration.  */
135     status =  _ux_host_stack_device_configuration_select(configuration);
136     if (status != UX_SUCCESS)
137         return(status);
138 
139     /* If the operation went well, the asix default alternate setting for the asix interface is
140        active and the interrupt endpoint is now enabled. We have to memorize the first interface since
141        the interrupt endpoint is hooked to it. */
142     status =  _ux_host_stack_configuration_interface_get(configuration, 0, 0, &asix -> ux_host_class_asix_interface);
143 
144     /* Check status for error.  */
145     if (status == UX_SUCCESS)
146     {
147 
148         /* Store the instance in the interface container, this is for the USB stack
149            when it needs to invoke the class.  */
150         asix -> ux_host_class_asix_interface -> ux_interface_class_instance =  (VOID *) asix;
151 
152         /* Store the class container in the interface.  The device has the correct class, duplicate it to the
153            interface.  */
154         asix -> ux_host_class_asix_interface -> ux_interface_class =  asix -> ux_host_class_asix_device -> ux_device_class ;
155 
156     }
157 
158     /* Return completion status.  */
159     return(status);
160 }
161 
162