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 /**   Storage 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_storage.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_storage_device_reset                 PORTABLE C      */
38 /*                                                           6.1.11       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function will perform a reset on the device if it is a         */
46 /*    Bulk Only device.                                                   */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    storage                               Pointer to storage class      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_host_stack_transfer_request       Process transfer request      */
59 /*    _ux_host_stack_endpoint_reset         Reset endpoint                */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Storage Class                                                       */
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 /*                                            resulting in version 6.1    */
72 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            added standalone support,   */
74 /*                                            resulting in version 6.1.10 */
75 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            internal clean up,          */
77 /*                                            resulting in version 6.1.11 */
78 /*                                                                        */
79 /**************************************************************************/
_ux_host_class_storage_device_reset(UX_HOST_CLASS_STORAGE * storage)80 UINT  _ux_host_class_storage_device_reset(UX_HOST_CLASS_STORAGE *storage)
81 {
82 
83 UX_ENDPOINT     *control_endpoint;
84 UX_TRANSFER     *transfer_request;
85 UINT            status;
86 
87 
88 #ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
89     /* We need to perform a reset only for BO devices.  */
90     if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceProtocol == UX_HOST_CLASS_STORAGE_PROTOCOL_BO)
91     {
92 #endif
93 
94     /* We need to get the default control endpoint transfer request pointer.  */
95     control_endpoint =  &storage -> ux_host_class_storage_device -> ux_device_control_endpoint;
96     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
97 
98     /* We need to prevent other threads from simultaneously using the endpoint. */
99     _ux_host_semaphore_get_norc(&control_endpoint -> ux_endpoint_device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
100 
101     /* Create a transfer_request for the RESET request.  */
102     transfer_request -> ux_transfer_request_data_pointer =      UX_NULL;
103     transfer_request -> ux_transfer_request_requested_length =  0;
104     transfer_request -> ux_transfer_request_function =          UX_HOST_CLASS_STORAGE_RESET;
105     transfer_request -> ux_transfer_request_type =              UX_REQUEST_OUT | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
106     transfer_request -> ux_transfer_request_value =             0;
107     transfer_request -> ux_transfer_request_index  =            storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceNumber;
108 
109     /* Send request to HCD layer.  */
110     status =  _ux_host_stack_transfer_request(transfer_request);
111 
112     /* Per the spec, reset the endpoints as well.  */
113     _ux_host_stack_endpoint_reset(storage -> ux_host_class_storage_bulk_in_endpoint);
114     _ux_host_stack_endpoint_reset(storage -> ux_host_class_storage_bulk_out_endpoint);
115 
116     /* Return completion status.  */
117     return(status);
118 
119 #ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
120     }
121 
122     /* In case the device is not a BO device, we always succeed.  */
123     return(UX_SUCCESS);
124 #endif
125 }
126