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 /**   HUB 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_stack.h"
30 #include "ux_host_class_hub.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_hub_transfer_request_completed       PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function is called by the completion thread when a transfer    */
46 /*    request has been completed either because the transfer is           */
47 /*    successful or there was an error.                                   */
48 /*                                                                        */
49 /*    Because the HUB influences the topology of the USB, the insertion   */
50 /*    or extraction of devices cannot be done during the transfer request */
51 /*    thread. We post a signal to the topology thread to wake up and      */
52 /*    treat these changes on the HUB status.                              */
53 /*                                                                        */
54 /*    In RTOS mode, the interrupt pipe is not reactivated here. We will   */
55 /*    do this when the topology thread has investigated the reason of the */
56 /*    transfer completion.                                                */
57 /*                                                                        */
58 /*    In standalone mode, the interrupt pipe is reactivated here. The     */
59 /*    bitmap in buffer is examined in hub tasks function.                 */
60 /*                                                                        */
61 /*  INPUT                                                                 */
62 /*                                                                        */
63 /*    transfer_request                      Pointer to transfer request   */
64 /*                                                                        */
65 /*  OUTPUT                                                                */
66 /*                                                                        */
67 /*    None                                                                */
68 /*                                                                        */
69 /*  CALLS                                                                 */
70 /*                                                                        */
71 /*    _ux_host_semaphore_put                Put the signaling semaphore   */
72 /*                                                                        */
73 /*  CALLED BY                                                             */
74 /*                                                                        */
75 /*    HUB Class                                                           */
76 /*                                                                        */
77 /*  RELEASE HISTORY                                                       */
78 /*                                                                        */
79 /*    DATE              NAME                      DESCRIPTION             */
80 /*                                                                        */
81 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
82 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
83 /*                                            resulting in version 6.1    */
84 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
85 /*                                            refined macros names,       */
86 /*                                            resulting in version 6.1.10 */
87 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
88 /*                                            added standalone support,   */
89 /*                                            resulting in version 6.1.12 */
90 /*                                                                        */
91 /**************************************************************************/
_ux_host_class_hub_transfer_request_completed(UX_TRANSFER * transfer_request)92 VOID  _ux_host_class_hub_transfer_request_completed(UX_TRANSFER *transfer_request)
93 {
94 
95 UX_HOST_CLASS_HUB        *hub;
96 
97 
98     /* Get the class instance for this transfer request.  */
99     hub =  (UX_HOST_CLASS_HUB *) transfer_request -> ux_transfer_request_class_instance;
100 
101 
102     /* Check the state of the transfer.  If there is an error, we do not proceed with this report.  */
103     if (transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
104     {
105 
106         /* We have an error. We do not rehook another transfer if the device instance is shutting down or
107            if the transfer was aborted by the class.  */
108         if ((hub -> ux_host_class_hub_state ==  UX_HOST_CLASS_INSTANCE_SHUTDOWN) ||
109             (transfer_request -> ux_transfer_request_completion_code == UX_TRANSFER_STATUS_ABORT) ||
110             (transfer_request -> ux_transfer_request_completion_code == UX_TRANSFER_NO_ANSWER))
111 
112             /* We do not proceed.  */
113             return;
114         else
115 
116         {
117 
118             /* Reactivate the HUB interrupt pipe.  */
119             _ux_host_stack_transfer_request(transfer_request);
120 
121             /* We do not proceed.  */
122             return;
123         }
124     }
125 
126 #if defined(UX_HOST_STANDALONE)
127 
128     /* Reactivate the HUB interrupt pipe.  */
129     _ux_host_stack_transfer_request(transfer_request);
130 #else
131 
132     /* We need to memorize which HUB instance has received a change signal.  */
133     hub -> ux_host_class_hub_change_semaphore++;
134 
135     /* Now we can set the semaphore, the enum thread will wake up and will
136        call the HUB instance which has a status change.  */
137     _ux_host_semaphore_put(&_ux_system_host -> ux_system_host_enum_semaphore);
138 #endif
139 
140     /* Return to caller.  */
141     return;
142 }
143