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 /**   Host Stack                                                          */
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_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_stack_enum_thread_entry                    PORTABLE C      */
37 /*                                                           6.1.10       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This file contains the enum thread for USBX. It is in charge of     */
45 /*    the topology changes either from device insertion\extraction on     */
46 /*    the root hub or on a regular hub.                                   */
47 /*                                                                        */
48 /*    This thread ensures we never have more that 2 instances trying to   */
49 /*    perform a change to the topology (mostly enumeration) for fear that */
50 /*    more than one device could answer to address 0.                     */
51 /*                                                                        */
52 /*    This function is the entry point of the topology thread. It waits   */
53 /*    until one of the HCDs or a hub sets the semaphore to indicate       */
54 /*    there has been a change in the USB topology which could be either   */
55 /*    a insertion or extraction or eventually a hub downstream port       */
56 /*    signal.                                                             */
57 /*                                                                        */
58 /*    This is for RTOS mode.                                              */
59 /*                                                                        */
60 /*  INPUT                                                                 */
61 /*                                                                        */
62 /*    input                                 Not used input                */
63 /*                                                                        */
64 /*  OUTPUT                                                                */
65 /*                                                                        */
66 /*    None                                                                */
67 /*                                                                        */
68 /*  CALLS                                                                 */
69 /*                                                                        */
70 /*    _ux_host_stack_rh_change_process      Root hub processing           */
71 /*    _ux_utility_semaphore_get             Get signal semaphore          */
72 /*    (ux_system_host_enum_hub_function)    HUB enum processing function  */
73 /*                                                                        */
74 /*  CALLED BY                                                             */
75 /*                                                                        */
76 /*    ThreadX                                                             */
77 /*                                                                        */
78 /*  RELEASE HISTORY                                                       */
79 /*                                                                        */
80 /*    DATE              NAME                      DESCRIPTION             */
81 /*                                                                        */
82 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
83 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
84 /*                                            optimized based on compile  */
85 /*                                            definitions,                */
86 /*                                            resulting in version 6.1    */
87 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
88 /*                                            refined macros names,       */
89 /*                                            resulting in version 6.1.10 */
90 /*                                                                        */
91 /**************************************************************************/
_ux_host_stack_enum_thread_entry(ULONG input)92 VOID  _ux_host_stack_enum_thread_entry(ULONG input)
93 {
94 
95     UX_PARAMETER_NOT_USED(input);
96 
97     /* Loop forever waiting for changes signaled through the semaphore. */
98     while (1)
99     {
100 
101         /* Wait for the semaphore to be put by the root hub or a regular hub.  */
102         _ux_host_semaphore_get_norc(&_ux_system_host -> ux_system_host_enum_semaphore, UX_WAIT_FOREVER);
103 
104 #if UX_MAX_DEVICES > 1
105         /* We try the hub first. For this we look into the USBX project
106            structure to see if there is at least one hub.  */
107         if (_ux_system_host -> ux_system_host_enum_hub_function != UX_NULL)
108         {
109 
110             /* Yes, there is a HUB function, call it!  */
111             _ux_system_host -> ux_system_host_enum_hub_function();
112         }
113 #endif
114 
115         /* The signal may be also coming from the root hub, call the root hub handler.  */
116         _ux_host_stack_rh_change_process();
117     }
118 }
119 
120