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 /**   Host Stack                                                          */
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 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_stack_role_swap                            PORTABLE C      */
37 /*                                                           6.1.10       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function is called when the host or the device demand a role   */
45 /*    swap. This function may be called by an application or by the HNP   */
46 /*    thread.                                                             */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    hcd                                       Pointer to HCD            */
51 /*    device                                    Device pointer            */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Status                                                              */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_utility_semaphore_get                 Get semaphore             */
60 /*    _ux_host_stack_transfer_request           Transfer request          */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Application of HNP thread.                                          */
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 /*                                            optimized based on compile  */
73 /*                                            definitions,                */
74 /*                                            resulting in version 6.1    */
75 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            refined macros names,       */
77 /*                                            resulting in version 6.1.10 */
78 /*                                                                        */
79 /**************************************************************************/
_ux_host_stack_role_swap(UX_DEVICE * device)80 UINT  _ux_host_stack_role_swap(UX_DEVICE *device)
81 {
82 
83 #if !defined(UX_OTG_SUPPORT)
84 
85     UX_PARAMETER_NOT_USED(device);
86     return(UX_FUNCTION_NOT_SUPPORTED);
87 #else
88 
89 UX_ENDPOINT     *control_endpoint;
90 UX_TRANSFER     *transfer_request;
91 UINT            status;
92 
93     /* Retrieve the control endpoint and the transfer request associated with it.  */
94     control_endpoint =  &device -> ux_device_control_endpoint;
95     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
96 
97     /* Protect the control endpoint semaphore here.  It will be unprotected in the
98        transfer request function.  */
99     status =  _ux_host_semaphore_get(&device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
100 
101     /* Perform a SET_FEATURE on this device to inform the it we are ready to change role.  */
102     transfer_request -> ux_transfer_request_requested_length =  0;
103     transfer_request -> ux_transfer_request_function =          UX_SET_FEATURE;
104     transfer_request -> ux_transfer_request_type =              UX_REQUEST_OUT | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
105     transfer_request -> ux_transfer_request_value =             UX_OTG_FEATURE_B_HNP_ENABLE;
106     transfer_request -> ux_transfer_request_index =             0;
107 
108     /* Send request to HCD layer.  */
109     status =  _ux_host_stack_transfer_request(transfer_request);
110 
111     /* If the status fails, simply ignore the command but do not proceed.  */
112     if (status != UX_SUCCESS)
113 
114         /* We have an error. Do not proceed. */
115         return(status);
116 
117     /* Keep track of the event for HNP synchronization.  */
118     _ux_system_otg -> ux_system_otg_change_mode_event = UX_OTG_HOST_TO_SLAVE;
119 
120     /* Call the OTG function that will perform the swap.  */
121     _ux_system_otg -> ux_system_otg_function(UX_OTG_HOST_TO_SLAVE);
122 
123     /* Reset the event.  */
124     _ux_system_otg -> ux_system_otg_change_mode_event = 0;
125 
126     return(UX_SUCCESS);
127 #endif
128 }
129