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_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_CAPACITY command to the storage      */
46 /*    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_class_storage_cbw_initialize Initialize CBW                */
59 /*    _ux_host_class_storage_transport      Send command                  */
60 /*    _ux_host_class_storage_media_format_capacity_get                    */
61 /*                                          Get format capacity           */
62 /*    _ux_utility_memory_allocate           Allocate memory block         */
63 /*    _ux_utility_memory_free               Release memory block          */
64 /*    _ux_utility_long_get_big_endian       Get 32-bit big endian         */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Storage Class                                                       */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
75 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            added option to disable FX  */
77 /*                                            media integration,          */
78 /*                                            resulting in version 6.1    */
79 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            added standalone support,   */
81 /*                                            resulting in version 6.1.10 */
82 /*                                                                        */
83 /**************************************************************************/
_ux_host_class_storage_media_capacity_get(UX_HOST_CLASS_STORAGE * storage)84 UINT  _ux_host_class_storage_media_capacity_get(UX_HOST_CLASS_STORAGE *storage)
85 {
86 
87 UINT            status;
88 UCHAR           *cbw;
89 UCHAR           *capacity_response;
90 UINT            command_length;
91 #if !defined(UX_HOST_STANDALONE)
92 ULONG           command_retry;
93 #endif
94 
95     /* If trace is enabled, insert this event into the trace buffer.  */
96     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_STORAGE_MEDIA_CAPACITY_GET, storage, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
97 
98 #if !defined(UX_HOST_STANDALONE)
99 
100     /* Set the default to either 512 or 2048 bytes.  */
101     switch (storage -> ux_host_class_storage_media_type)
102     {
103 
104     case UX_HOST_CLASS_STORAGE_MEDIA_FAT_DISK:
105     case UX_HOST_CLASS_STORAGE_MEDIA_IOMEGA_CLICK:
106 
107         storage -> ux_host_class_storage_sector_size =  UX_HOST_CLASS_STORAGE_SECTOR_SIZE_FAT;
108         break;
109 
110     case UX_HOST_CLASS_STORAGE_MEDIA_CDROM:
111     case UX_HOST_CLASS_STORAGE_MEDIA_OPTICAL_DISK:
112 
113         storage -> ux_host_class_storage_sector_size =  UX_HOST_CLASS_STORAGE_SECTOR_SIZE_OTHER;
114         break;
115 
116     default:
117 
118         /* Unsupported device.  */
119         return(UX_HOST_CLASS_MEDIA_NOT_SUPPORTED);
120     }
121 
122     /* First, we test if the device is ready.  Then try to read its capacity.
123        On floppies, this operation tends to fail a few times. So we try harder.  */
124     for (command_retry = 0; command_retry < UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RETRY; command_retry++)
125     {
126 
127         /* Some devices require we do this.  */
128         status =  _ux_host_class_storage_media_format_capacity_get(storage);
129         if (status != UX_SUCCESS)
130             return(status);
131 #endif
132 
133         /* Use a pointer for the cbw, easier to manipulate.  */
134         cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
135 
136         /* Get the READ_CAPACITY command Length.  */
137 #ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
138         if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_STORAGE_SUBCLASS_UFI)
139             command_length =  UX_HOST_CLASS_STORAGE_READ_CAPACITY_COMMAND_LENGTH_UFI;
140         else
141             command_length =  UX_HOST_CLASS_STORAGE_READ_CAPACITY_COMMAND_LENGTH_SBC;
142 #else
143         command_length =  UX_HOST_CLASS_STORAGE_READ_CAPACITY_COMMAND_LENGTH_SBC;
144 #endif
145 
146         /* Initialize the CBW for this command.  */
147         _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH, command_length);
148 
149         /* Prepare the READ_CAPACITY command block.  */
150         *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_READ_CAPACITY_OPERATION) =  UX_HOST_CLASS_STORAGE_SCSI_READ_CAPACITY;
151 
152         /* Obtain a block of memory for the answer.  */
153         capacity_response =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH);
154         if (capacity_response == UX_NULL)
155             return(UX_MEMORY_INSUFFICIENT);
156 
157 #if defined(UX_HOST_STANDALONE)
158 
159         /* Initialize state for transport.  */
160         UX_HOST_CLASS_STORAGE_TRANS_STATE_RESET(storage);
161         storage -> ux_host_class_storage_state_state = UX_HOST_CLASS_STORAGE_STATE_TRANSPORT;
162         storage -> ux_host_class_storage_state_next = UX_HOST_CLASS_STORAGE_STATE_CAP_SAVE;
163         storage -> ux_host_class_storage_trans_data = capacity_response;
164         status = UX_SUCCESS;
165         return(status);
166 #else
167 
168         /* Send the command to transport layer.  */
169         status =  _ux_host_class_storage_transport(storage, capacity_response);
170 
171         /* Check for error during transfer.  */
172         if (status != UX_SUCCESS)
173         {
174 
175             /* Free the memory resource used for the command response.  */
176             _ux_utility_memory_free(capacity_response);
177 
178             /* We return a sad status.  */
179             return(status);
180         }
181 
182         /* Check the sense code */
183         if (storage -> ux_host_class_storage_sense_code == UX_SUCCESS)
184         {
185 
186 #if defined(UX_HOST_CLASS_STORAGE_NO_FILEX)
187             /* Save the number of sectors.  */
188             storage -> ux_host_class_storage_last_sector_number = _ux_utility_long_get_big_endian(capacity_response + UX_HOST_CLASS_STORAGE_READ_CAPACITY_DATA_LBA);
189 #endif
190 
191             /* The data is valid, save the sector size.  */
192             storage -> ux_host_class_storage_sector_size =  _ux_utility_long_get_big_endian(capacity_response + UX_HOST_CLASS_STORAGE_READ_CAPACITY_DATA_SECTOR_SIZE);
193 
194             /* Free the memory resource used for the command response.  */
195             _ux_utility_memory_free(capacity_response);
196 
197             /* We return a happy status.  */
198             return(UX_SUCCESS);
199         }
200 
201         /* Free the memory resource used for the command response.  */
202         _ux_utility_memory_free(capacity_response);
203     }
204 
205     /* We get here when we could not retrieve the sector size through the READ_CAPACITY command.
206        It's OK, we still calculated the default based on the device type.  */
207     return(UX_SUCCESS);
208 #endif
209 }
210