1 |
|
|
/*************************************************************************** |
2 |
|
|
* Copyright (c) 2024 Microsoft Corporation |
3 |
|
|
* Copyright (c) 2026-present Eclipse ThreadX contributors |
4 |
|
|
* |
5 |
|
|
* This program and the accompanying materials are made available under the |
6 |
|
|
* terms of the MIT License which is available at |
7 |
|
|
* https://opensource.org/licenses/MIT. |
8 |
|
|
* |
9 |
|
|
* SPDX-License-Identifier: MIT |
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 |
|
|
/**************************************************************************/ |
71 |
|
180 |
UINT _ux_host_class_storage_media_capacity_get(UX_HOST_CLASS_STORAGE *storage) |
72 |
|
|
{ |
73 |
|
|
|
74 |
|
|
UINT status; |
75 |
|
|
UCHAR *cbw; |
76 |
|
|
UCHAR *capacity_response; |
77 |
|
|
UINT command_length; |
78 |
|
|
#if !defined(UX_HOST_STANDALONE) |
79 |
|
|
ULONG command_retry; |
80 |
|
|
#endif |
81 |
|
|
|
82 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
83 |
|
|
UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_STORAGE_MEDIA_CAPACITY_GET, storage, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0) |
84 |
|
|
|
85 |
|
|
#if !defined(UX_HOST_STANDALONE) |
86 |
|
|
|
87 |
|
|
/* Set the default to either 512 or 2048 bytes. */ |
88 |
✓✓✓ |
180 |
switch (storage -> ux_host_class_storage_media_type) |
89 |
|
|
{ |
90 |
|
|
|
91 |
|
174 |
case UX_HOST_CLASS_STORAGE_MEDIA_FAT_DISK: |
92 |
|
|
case UX_HOST_CLASS_STORAGE_MEDIA_IOMEGA_CLICK: |
93 |
|
|
|
94 |
|
174 |
storage -> ux_host_class_storage_sector_size = UX_HOST_CLASS_STORAGE_SECTOR_SIZE_FAT; |
95 |
|
174 |
break; |
96 |
|
|
|
97 |
|
3 |
case UX_HOST_CLASS_STORAGE_MEDIA_CDROM: |
98 |
|
|
case UX_HOST_CLASS_STORAGE_MEDIA_OPTICAL_DISK: |
99 |
|
|
|
100 |
|
3 |
storage -> ux_host_class_storage_sector_size = UX_HOST_CLASS_STORAGE_SECTOR_SIZE_OTHER; |
101 |
|
3 |
break; |
102 |
|
|
|
103 |
|
3 |
default: |
104 |
|
|
|
105 |
|
|
/* Unsupported device. */ |
106 |
|
3 |
return(UX_HOST_CLASS_MEDIA_NOT_SUPPORTED); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
/* First, we test if the device is ready. Then try to read its capacity. |
110 |
|
|
On floppies, this operation tends to fail a few times. So we try harder. */ |
111 |
✓✓ |
190 |
for (command_retry = 0; command_retry < UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RETRY; command_retry++) |
112 |
|
|
{ |
113 |
|
|
|
114 |
|
|
/* Some devices require we do this. */ |
115 |
|
189 |
status = _ux_host_class_storage_media_format_capacity_get(storage); |
116 |
✓✓ |
189 |
if (status != UX_SUCCESS) |
117 |
|
2 |
return(status); |
118 |
|
|
#endif |
119 |
|
|
|
120 |
|
|
/* Use a pointer for the cbw, easier to manipulate. */ |
121 |
|
187 |
cbw = (UCHAR *) storage -> ux_host_class_storage_cbw; |
122 |
|
|
|
123 |
|
|
/* Get the READ_CAPACITY command Length. */ |
124 |
|
|
#ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT |
125 |
|
|
if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_STORAGE_SUBCLASS_UFI) |
126 |
|
|
command_length = UX_HOST_CLASS_STORAGE_READ_CAPACITY_COMMAND_LENGTH_UFI; |
127 |
|
|
else |
128 |
|
|
command_length = UX_HOST_CLASS_STORAGE_READ_CAPACITY_COMMAND_LENGTH_SBC; |
129 |
|
|
#else |
130 |
|
187 |
command_length = UX_HOST_CLASS_STORAGE_READ_CAPACITY_COMMAND_LENGTH_SBC; |
131 |
|
|
#endif |
132 |
|
|
|
133 |
|
|
/* Initialize the CBW for this command. */ |
134 |
|
187 |
_ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH, command_length); |
135 |
|
|
|
136 |
|
|
/* Prepare the READ_CAPACITY command block. */ |
137 |
|
187 |
*(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_READ_CAPACITY_OPERATION) = UX_HOST_CLASS_STORAGE_SCSI_READ_CAPACITY; |
138 |
|
|
|
139 |
|
|
/* Obtain a block of memory for the answer. */ |
140 |
|
187 |
capacity_response = _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH); |
141 |
✓✓ |
187 |
if (capacity_response == UX_NULL) |
142 |
|
2 |
return(UX_MEMORY_INSUFFICIENT); |
143 |
|
|
|
144 |
|
|
#if defined(UX_HOST_STANDALONE) |
145 |
|
|
|
146 |
|
|
/* Initialize state for transport. */ |
147 |
|
|
UX_HOST_CLASS_STORAGE_TRANS_STATE_RESET(storage); |
148 |
|
|
storage -> ux_host_class_storage_state_state = UX_HOST_CLASS_STORAGE_STATE_TRANSPORT; |
149 |
|
|
storage -> ux_host_class_storage_state_next = UX_HOST_CLASS_STORAGE_STATE_CAP_SAVE; |
150 |
|
|
storage -> ux_host_class_storage_trans_data = capacity_response; |
151 |
|
|
status = UX_SUCCESS; |
152 |
|
|
return(status); |
153 |
|
|
#else |
154 |
|
|
|
155 |
|
|
/* Send the command to transport layer. */ |
156 |
|
185 |
status = _ux_host_class_storage_transport(storage, capacity_response); |
157 |
|
|
|
158 |
|
|
/* Check for error during transfer. */ |
159 |
✓✓ |
185 |
if (status != UX_SUCCESS) |
160 |
|
|
{ |
161 |
|
|
|
162 |
|
|
/* Free the memory resource used for the command response. */ |
163 |
|
1 |
_ux_utility_memory_free(capacity_response); |
164 |
|
|
|
165 |
|
|
/* We return a sad status. */ |
166 |
|
1 |
return(status); |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
/* Check the sense code */ |
170 |
✓✓ |
184 |
if (storage -> ux_host_class_storage_sense_code == UX_SUCCESS) |
171 |
|
|
{ |
172 |
|
|
|
173 |
|
|
#if defined(UX_HOST_CLASS_STORAGE_NO_FILEX) |
174 |
|
|
/* Save the number of sectors. */ |
175 |
|
|
storage -> ux_host_class_storage_last_sector_number = _ux_utility_long_get_big_endian(capacity_response + UX_HOST_CLASS_STORAGE_READ_CAPACITY_DATA_LBA); |
176 |
|
|
#endif |
177 |
|
|
|
178 |
|
|
/* The data is valid, save the sector size. */ |
179 |
|
171 |
storage -> ux_host_class_storage_sector_size = _ux_utility_long_get_big_endian(capacity_response + UX_HOST_CLASS_STORAGE_READ_CAPACITY_DATA_SECTOR_SIZE); |
180 |
|
|
|
181 |
|
|
/* Free the memory resource used for the command response. */ |
182 |
|
171 |
_ux_utility_memory_free(capacity_response); |
183 |
|
|
|
184 |
|
|
/* We return a happy status. */ |
185 |
|
171 |
return(UX_SUCCESS); |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
/* Free the memory resource used for the command response. */ |
189 |
|
13 |
_ux_utility_memory_free(capacity_response); |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
/* We get here when we could not retrieve the sector size through the READ_CAPACITY command. |
193 |
|
|
It's OK, we still calculated the default based on the device type. */ |
194 |
|
1 |
return(UX_SUCCESS); |
195 |
|
|
#endif |
196 |
|
|
} |