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 /**   HUB 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_hub.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_hub_change_detect                    PORTABLE C      */
37 /*                                                           6.1.12       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function is called by the enumeration thread when there has    */
45 /*    been activity on the HUB.                                           */
46 /*                                                                        */
47 /*    In standalone mode there is nothing to do here, activities are      */
48 /*    processed in hub tasks function.                                    */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    None                                                                */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_host_class_hub_change_process     Process HUB change            */
61 /*    _ux_host_stack_class_get              Get class                     */
62 /*    _ux_host_stack_class_instance_get     Get class instance            */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    HUB 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 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            added standalone support,   */
77 /*                                            resulting in version 6.1.12 */
78 /*                                                                        */
79 /**************************************************************************/
_ux_host_class_hub_change_detect(VOID)80 VOID  _ux_host_class_hub_change_detect(VOID)
81 {
82 #if defined(UX_HOST_STANDALONE)
83 
84     /* Things are done in hub task nothing to do here.  */
85 #else
86 
87 UX_HOST_CLASS           *class;
88 UX_HOST_CLASS_HUB       *hub;
89 UINT                    status;
90 UINT                    class_index;
91 
92     /* Get the class container first.  */
93     _ux_host_stack_class_get(_ux_system_host_class_hub_name, &class);
94 
95     /* We start with the first index of the class instance.  */
96     class_index =  0;
97 
98     /* We have found the class, now parse the instances.  */
99     do
100     {
101 
102         /* Get class instance.  */
103         status =  _ux_host_stack_class_instance_get(class, class_index++, (VOID **) &hub);
104 
105         /* Check completion status.  */
106         if (status == UX_SUCCESS)
107         {
108 
109             /* We have found an instance of a HUB, check if it is live and if the HUB has
110                detected a change before we proceed.  */
111             if (hub -> ux_host_class_hub_change_semaphore != 0)
112             {
113 
114                 /* If trace is enabled, insert this event into the trace buffer.  */
115                 UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HUB_CHANGE_DETECT, hub, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
116 
117                 /* Call the HUB function that will diagnose the origin of the change.  */
118                 _ux_host_class_hub_change_process(hub);
119 
120                 /* Decrement the HUB instance semaphore change so we don't get awaken again.  */
121                 hub -> ux_host_class_hub_change_semaphore--;
122             }
123         }
124     } while (status == UX_SUCCESS);
125 
126     /* We have parsed all the HUB instances.  */
127     return;
128 #endif
129 }
130