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_class_hub.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_hub_status_get                       PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function will do a GET_STATUS from the HUB. This function      */
46 /*    retrieves the current status and the changed bits.                  */
47 /*                                                                        */
48 /*    In standalone mode, this functioin prepares the control transfer    */
49 /*    request data memory and request context of specific command, for    */
50 /*    host stack transfer function to process, in next steps. The         */
51 /*    allocated memory must be freed after transfer request is processed. */
52 /*                                                                        */
53 /*  INPUT                                                                 */
54 /*                                                                        */
55 /*    hub                                   Pointer to HUB class          */
56 /*    port                                  Port of device                */
57 /*    port_status                           Destination for port status   */
58 /*    port_change                           Destination for port change   */
59 /*                                                                        */
60 /*  OUTPUT                                                                */
61 /*                                                                        */
62 /*    Completion Status                                                   */
63 /*                                                                        */
64 /*  CALLS                                                                 */
65 /*                                                                        */
66 /*    _ux_host_stack_transfer_request       Process transfer request      */
67 /*    _ux_utility_memory_allocate           Allocate memory block         */
68 /*    _ux_utility_memory_free               Release memory block          */
69 /*    _ux_utility_short_get                 Get 16-bit word               */
70 /*                                                                        */
71 /*  CALLED BY                                                             */
72 /*                                                                        */
73 /*    HUB Class                                                           */
74 /*                                                                        */
75 /*  RELEASE HISTORY                                                       */
76 /*                                                                        */
77 /*    DATE              NAME                      DESCRIPTION             */
78 /*                                                                        */
79 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
80 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
81 /*                                            resulting in version 6.1    */
82 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
83 /*                                            added standalone support,   */
84 /*                                            resulting in version 6.1.12 */
85 /*                                                                        */
86 /**************************************************************************/
_ux_host_class_hub_status_get(UX_HOST_CLASS_HUB * hub,UINT port,USHORT * port_status,USHORT * port_change)87 UINT  _ux_host_class_hub_status_get(UX_HOST_CLASS_HUB *hub, UINT port, USHORT *port_status, USHORT *port_change)
88 {
89 
90 UCHAR           *port_data;
91 UX_ENDPOINT     *control_endpoint;
92 UX_TRANSFER     *transfer_request;
93 UINT            target;
94 UINT            status;
95 
96 
97     /* We need to get the default control endpoint transfer request pointer.  */
98     control_endpoint =  &hub -> ux_host_class_hub_device -> ux_device_control_endpoint;
99     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
100 
101     /* The target is DEVICE for the HUB and OTHER for the downstream ports.  */
102     if (port == 0)
103         target =  UX_REQUEST_TARGET_DEVICE;
104     else
105         target =  UX_REQUEST_TARGET_OTHER;
106 
107     /* Allocate a buffer for the port status and change: 2 words.  */
108     port_data =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, 4);
109     if(port_data == UX_NULL)
110         return(UX_MEMORY_INSUFFICIENT);
111 
112     /* Create a transfer request for the GET_STATUS request.  */
113     transfer_request -> ux_transfer_request_requested_length =  4;
114     transfer_request -> ux_transfer_request_data_pointer =      port_data;
115     transfer_request -> ux_transfer_request_function =          UX_HOST_CLASS_HUB_GET_STATUS;
116     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | target;
117     transfer_request -> ux_transfer_request_value =             0;
118     transfer_request -> ux_transfer_request_index =             port;
119 
120 #if defined(UX_HOST_STANDALONE)
121 
122     /* No status change change copy.  */
123     UX_PARAMETER_NOT_USED(port_status);
124     UX_PARAMETER_NOT_USED(port_change);
125 
126     /* Save allocated buffer.  */
127     hub -> ux_host_class_hub_allocated = port_data;
128 
129     /* Reset transfer state for _run.  */
130     UX_TRANSFER_STATE_RESET(transfer_request);
131     status = UX_SUCCESS;
132 #else
133 
134     /* Send request to HCD layer.  */
135     status =  _ux_host_stack_transfer_request(transfer_request);
136 
137     /* Check for error and completion of the transfer.  */
138     if (status == UX_SUCCESS)
139     {
140 
141         if (transfer_request -> ux_transfer_request_actual_length == 4)
142         {
143 
144             /* The 2 words are now in the buffer. We need to resolve their endianness
145                and report them as separate items to the called.  */
146             *port_status =  (USHORT)_ux_utility_short_get(port_data);
147             *port_change =  (USHORT)_ux_utility_short_get(port_data+2);
148         }
149         else
150         {
151 
152             /* Invalid length. Return error.  */
153             status =  UX_TRANSFER_DATA_LESS_THAN_EXPECTED;
154         }
155     }
156 
157     /* Free the buffer resource now.  */
158     _ux_utility_memory_free(port_data);
159 
160 #endif
161 
162     /* Return completion status.  */
163     return(status);
164 }
165