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