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_characteristics_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 INQUIRY command to get the type of        */
46 /*    device/media we are dealing with.                                   */
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_capacity_get                           */
61 /*                                          Get media capacity            */
62 /*    _ux_utility_memory_allocate           Allocate memory block         */
63 /*    _ux_utility_memory_free               Release memory block          */
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_characteristics_get(UX_HOST_CLASS_STORAGE * storage)81 UINT  _ux_host_class_storage_media_characteristics_get(UX_HOST_CLASS_STORAGE *storage)
82 {
83 
84 UINT            status;
85 UCHAR           *cbw;
86 UCHAR           *inquiry_response;
87 UINT            command_length;
88 
89     /* Use a pointer for the cbw, easier to manipulate.  */
90     cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
91 
92     /* Get the Write Command Length.  */
93 #ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
94     if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_STORAGE_SUBCLASS_UFI)
95         command_length =  UX_HOST_CLASS_STORAGE_INQUIRY_COMMAND_LENGTH_UFI;
96     else
97         command_length =  UX_HOST_CLASS_STORAGE_INQUIRY_COMMAND_LENGTH_SBC;
98 #else
99     command_length =  UX_HOST_CLASS_STORAGE_INQUIRY_COMMAND_LENGTH_SBC;
100 #endif
101 
102     /* Initialize the CBW for this command.  */
103     _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH, command_length);
104 
105     /* Prepare the INQUIRY command block.  */
106     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_OPERATION) =  UX_HOST_CLASS_STORAGE_SCSI_INQUIRY;
107 
108     /* Store the length of the Inquiry Response.  */
109     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_ALLOCATION_LENGTH) =  UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH;
110 
111     /* Obtain a block of memory for the answer.  */
112     inquiry_response =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH);
113     if (inquiry_response == UX_NULL)
114         return(UX_MEMORY_INSUFFICIENT);
115 
116 #if defined(UX_HOST_STANDALONE)
117     UX_HOST_CLASS_STORAGE_TRANS_STATE_RESET(storage);
118     storage -> ux_host_class_storage_memory = inquiry_response;
119     storage -> ux_host_class_storage_state_state = UX_HOST_CLASS_STORAGE_STATE_TRANSPORT;
120     storage -> ux_host_class_storage_state_next = UX_HOST_CLASS_STORAGE_STATE_INQUIRY_SAVE;
121     storage -> ux_host_class_storage_trans_data = inquiry_response;
122     status = UX_SUCCESS;
123     return(status);
124 #else
125     /* Send the command to transport layer.  */
126     status =  _ux_host_class_storage_transport(storage, inquiry_response);
127 
128     /* If we have a transport error, there is not much we can do, simply return the
129        error.  */
130     if (status == UX_SUCCESS)
131     {
132 
133         /* The Inquiry response contains the type of device/media and a media removable flag.  */
134         storage -> ux_host_class_storage_media_type =                                                       *(inquiry_response + UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_PERIPHERAL_TYPE);
135         storage -> ux_host_class_storage_lun_removable_media_flags[storage -> ux_host_class_storage_lun] =  *(inquiry_response + UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_REMOVABLE_MEDIA);
136 
137         /* Attempt to read the device capacity in order to retrieve the Sector Size. If the command fails,
138            we will default to 512 bytes for a regular drive and 2048 bytes for a CD-ROM or optical drive.  */
139         status =  _ux_host_class_storage_media_capacity_get(storage);
140     }
141 
142     /* Free the memory resource used for the command response.  */
143     _ux_utility_memory_free(inquiry_response);
144 
145     /* Return completion status.  */
146     return(status);
147 #endif
148 }
149 
150