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_media_recovery_sense_get     PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function will send a MODE_SENSE command to recover from a read */
46 /*    and write error.                                                    */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    storage                               Pointer to storage class      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_host_class_storage_cbw_initialize Initialize CBW                */
59 /*    _ux_host_class_storage_transport      Send command                  */
60 /*    _ux_utility_memory_allocate           Allocate memory block         */
61 /*    _ux_utility_memory_free               Release memory block          */
62 /*    _ux_utility_short_put_big_endian      Put short value               */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Storage Class                                                       */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*                                                                        */
76 /**************************************************************************/
_ux_host_class_storage_media_recovery_sense_get(UX_HOST_CLASS_STORAGE * storage)77 UINT  _ux_host_class_storage_media_recovery_sense_get(UX_HOST_CLASS_STORAGE *storage)
78 {
79 
80 UINT            status;
81 UCHAR           *cbw;
82 UCHAR           *mode_sense_response;
83 UINT            command_length;
84 
85 
86     /* Use a pointer for the cbw, easier to manipulate.  */
87     cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
88 
89     /* Get the Write 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_MODE_SENSE_COMMAND_LENGTH_UFI;
93     else
94         command_length =  UX_HOST_CLASS_STORAGE_MODE_SENSE_COMMAND_LENGTH_SBC;
95 #else
96     command_length =  UX_HOST_CLASS_STORAGE_MODE_SENSE_COMMAND_LENGTH_SBC;
97 #endif
98 
99     /* Initialize the CBW for this command.  */
100     _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_MODE_SENSE_TP_PAGE, command_length);
101 
102     /* Prepare the MODE_SENSE command block.  Distinguish between SUBCLASSES. */
103     switch (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceSubClass)
104     {
105 
106         case UX_HOST_CLASS_STORAGE_SUBCLASS_RBC         :
107 #ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
108         case UX_HOST_CLASS_STORAGE_SUBCLASS_UFI         :
109 #endif
110             *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_MODE_SENSE_OPERATION) =  UX_HOST_CLASS_STORAGE_SCSI_MODE_SENSE;
111             break;
112 
113         default                                         :
114             *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_MODE_SENSE_OPERATION) =  UX_HOST_CLASS_STORAGE_SCSI_MODE_SENSE_SHORT;
115             break;
116     }
117 
118     /* We ask for a specific page page but we put the buffer to maximum size.  */
119     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_MODE_SENSE_PC_PAGE_CODE) = UX_HOST_CLASS_STORAGE_MODE_SENSE_TP_PAGE;
120 
121     /* Store the length of the Mode Sense Response.  */
122     _ux_utility_short_put_big_endian(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_MODE_SENSE_PARAMETER_LIST_LENGTH, UX_HOST_CLASS_STORAGE_MODE_SENSE_ALL_PAGE_LENGTH);
123 
124     /* Obtain a block of memory for the answer.  */
125     mode_sense_response =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_MODE_SENSE_ALL_PAGE_LENGTH);
126     if (mode_sense_response == UX_NULL)
127         return(UX_MEMORY_INSUFFICIENT);
128 
129     /* Send the command to transport layer.  */
130     status =  _ux_host_class_storage_transport(storage, mode_sense_response);
131 
132     /* Free the memory resource used for the command response.  */
133     _ux_utility_memory_free(mode_sense_response);
134 
135     /* Return completion status.  */
136     return(status);
137 }
138 
139