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_format_capacity_get    PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function will send a READ_FORMAT_CAPACITY command to the       */
46 /*    device. Some USB storage disk require this function for the sanity  */
47 /*    of their state machine.                                             */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    storage                               Pointer to storage class      */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_class_storage_cbw_initialize Initialize CBW                */
60 /*    _ux_host_class_storage_transport      Send command                  */
61 /*    _ux_utility_memory_allocate           Allocate memory block         */
62 /*    _ux_utility_memory_free               Release memory block          */
63 /*    _ux_utility_short_put_big_endian      Put 32-bit big endian         */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Storage Class                                                       */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
74 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
76 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            added standalone support,   */
78 /*                                            resulting in version 6.1.10 */
79 /*                                                                        */
80 /**************************************************************************/
_ux_host_class_storage_media_format_capacity_get(UX_HOST_CLASS_STORAGE * storage)81 UINT  _ux_host_class_storage_media_format_capacity_get(UX_HOST_CLASS_STORAGE *storage)
82 {
83 
84 UCHAR           *cbw;
85 UCHAR           *read_format_capacity_response;
86 UINT            command_length;
87 
88     /* If trace is enabled, insert this event into the trace buffer.  */
89     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_STORAGE_MEDIA_FORMAT_CAPACITY_GET, storage, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
90 
91     /* Use a pointer for the cbw, easier to manipulate.  */
92     cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
93 
94     /* Get the Write Command Length.  */
95 #ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
96     if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_STORAGE_SUBCLASS_UFI)
97         command_length =  UX_HOST_CLASS_STORAGE_READ_FORMAT_COMMAND_LENGTH_UFI;
98     else
99         command_length =  UX_HOST_CLASS_STORAGE_READ_FORMAT_COMMAND_LENGTH_SBC;
100 #else
101     command_length =  UX_HOST_CLASS_STORAGE_READ_FORMAT_COMMAND_LENGTH_SBC;
102 #endif
103 
104     /* Initialize the CBW for this command.  */
105     _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_READ_FORMAT_RESPONSE_LENGTH, command_length);
106 
107     /* Prepare the READ FORMAT CAPACITY command block.  */
108     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_READ_FORMAT_OPERATION) =  UX_HOST_CLASS_STORAGE_SCSI_READ_FORMAT_CAPACITY;
109 
110     /* Store the number of sectors to read.  */
111     _ux_utility_short_put_big_endian(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_READ_FORMAT_PARAMETER_LIST_LENGTH, UX_HOST_CLASS_STORAGE_READ_FORMAT_RESPONSE_LENGTH);
112 
113     /* Obtain a block of memory for the answer.  */
114     read_format_capacity_response =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_READ_FORMAT_RESPONSE_LENGTH);
115     if (read_format_capacity_response == UX_NULL)
116         return(UX_MEMORY_INSUFFICIENT);
117 
118 #if defined(UX_HOST_STANDALONE)
119 
120     /* Initialize main states for GetFormatCapacity().  */
121     UX_HOST_CLASS_STORAGE_TRANS_STATE_RESET(storage);
122     storage -> ux_host_class_storage_memory = read_format_capacity_response;
123     storage -> ux_host_class_storage_state_state = UX_HOST_CLASS_STORAGE_STATE_TRANSPORT;
124     storage -> ux_host_class_storage_state_next = UX_HOST_CLASS_STORAGE_STATE_FORMAT_CAP_SAVE;
125     storage -> ux_host_class_storage_trans_data = read_format_capacity_response;
126     return(UX_SUCCESS);
127 #else
128 
129     /* Send the command to transport layer.  */
130     _ux_host_class_storage_transport(storage, read_format_capacity_response);
131 
132     /* Free the memory resource used for the command response.  */
133     _ux_utility_memory_free(read_format_capacity_response);
134 
135     /* If we have a transport error, there is not much we can do, we do not fail.  */
136     return(UX_SUCCESS);
137 #endif
138 }
139 
140