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