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_change_process                   PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function is called by the topology thread when there has been  */
46 /*    activity on the HUB.                                                */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    hub                                   Pointer to HUB                */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_host_class_hub_port_change_process Port change process          */
59 /*    _ux_host_stack_transfer_request       Process transfer request      */
60 /*    _ux_utility_short_get                 Get 16-bit word               */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    HUB Class                                                           */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
71 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*                                                                        */
74 /**************************************************************************/
_ux_host_class_hub_change_process(UX_HOST_CLASS_HUB * hub)75 UINT  _ux_host_class_hub_change_process(UX_HOST_CLASS_HUB *hub)
76 {
77 
78 UX_TRANSFER     *transfer_request;
79 USHORT          port_status_change_bits;
80 UINT            port_index;
81 UINT            status;
82 
83 
84     /* Now get the transfer_request attached to the interrupt endpoint.  */
85     transfer_request =  &hub -> ux_host_class_hub_interrupt_endpoint -> ux_endpoint_transfer_request;
86 
87     /* The interrupt pipe buffer contains the status change for each of the ports
88        the length of the buffer can be 1 or 2 depending on the number of ports.
89        Usually, since HUBs can be bus powered the maximum number of ports is 4.
90        We must be taking precautions on how we read the buffer content for
91        big endian machines.  */
92     if (transfer_request -> ux_transfer_request_actual_length == 1)
93         port_status_change_bits =  (USHORT) *transfer_request -> ux_transfer_request_data_pointer;
94     else
95         port_status_change_bits =  (USHORT)_ux_utility_short_get(transfer_request -> ux_transfer_request_data_pointer);
96 
97     /* Scan all bits and report the change on each port.  */
98     for (port_index = 1; port_index <= hub -> ux_host_class_hub_descriptor.bNbPorts; port_index++)
99     {
100 
101         if (port_status_change_bits & (1<<port_index))
102             _ux_host_class_hub_port_change_process(hub, port_index);
103     }
104 
105     /* The HUB could also have changed.  */
106     if (port_status_change_bits & 1)
107         _ux_host_class_hub_hub_change_process(hub);
108 
109     /* The actual length should be cleared for the next transfer.  */
110     transfer_request -> ux_transfer_request_actual_length =  0;
111 
112     /* Resend the request to the stack.  */
113     status = _ux_host_stack_transfer_request(transfer_request);
114 
115     /* Return completion status.  */
116     return(status);
117 }
118