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_start_stop                   PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function starts or stops the UFI device.                       */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    storage                               Pointer to storage class      */
50 /*    start_stop_flag                       1 or 0 if start/stop          */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_host_class_storage_cbw_initialize Initialize the CBW            */
59 /*    _ux_host_class_storage_transport      Send transport layer command  */
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 /*                                                                        */
73 /**************************************************************************/
_ux_host_class_storage_start_stop(UX_HOST_CLASS_STORAGE * storage,ULONG start_stop_signal)74 UINT  _ux_host_class_storage_start_stop(UX_HOST_CLASS_STORAGE *storage,
75                                             ULONG start_stop_signal)
76 {
77 
78 UINT            status;
79 UCHAR             *cbw;
80 UINT            command_length;
81 ULONG           command_retry;
82 
83     /* If trace is enabled, insert this event into the trace buffer.  */
84     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_STORAGE_START_STOP, storage, start_stop_signal, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
85 
86     /* Use a pointer for the CBW, easier to manipulate.  */
87     cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
88 
89     /* Get the START_STOP Command Length.  */
90 #ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
91     if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_STORAGE_SUBCLASS_UFI)
92         command_length =  UX_HOST_CLASS_STORAGE_START_STOP_COMMAND_LENGTH_UFI;
93     else
94         command_length =  UX_HOST_CLASS_STORAGE_START_STOP_COMMAND_LENGTH_SBC;
95 #else
96     command_length =  UX_HOST_CLASS_STORAGE_START_STOP_COMMAND_LENGTH_SBC;
97 #endif
98 
99     /* Initialize the CBW for this command.  */
100     _ux_host_class_storage_cbw_initialize(storage, 0, 0, command_length);
101 
102     /* Prepare the START STOP command block.  */
103     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_START_STOP_OPERATION) =  UX_HOST_CLASS_STORAGE_SCSI_START_STOP;
104 
105     /* Set the required signal.  */
106     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_START_STOP_START_BIT) =  (UCHAR) start_stop_signal;
107 
108     /* On floppies, this operation tends to fail a few times. So we try harder.  */
109     for (command_retry = 0; command_retry < 50; command_retry++)
110     {
111 
112         /* Send the command to transport layer.  */
113         status =  _ux_host_class_storage_transport(storage, UX_NULL);
114 
115         /* If we have a transport error give up. */
116         if(status != UX_SUCCESS)
117 
118             /* Return completion status.  */
119             return(status);
120 
121         /* Check the CSW. We may learn something there about the state of the device.  */
122         if (storage -> ux_host_class_storage_sense_code == 0)
123             return(UX_SUCCESS);
124     }
125 
126     /* The start/Stop did not work.  */
127     return(UX_ERROR);
128 }
129 
130