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 |
|
|
#if !defined(UX_HOST_STANDALONE) |
34 |
|
|
/**************************************************************************/ |
35 |
|
|
/* */ |
36 |
|
|
/* FUNCTION RELEASE */ |
37 |
|
|
/* */ |
38 |
|
|
/* _ux_host_class_storage_device_initialize PORTABLE C */ |
39 |
|
|
/* 6.3.0 */ |
40 |
|
|
/* AUTHOR */ |
41 |
|
|
/* */ |
42 |
|
|
/* Chaoqiong Xiao, Microsoft Corporation */ |
43 |
|
|
/* */ |
44 |
|
|
/* DESCRIPTION */ |
45 |
|
|
/* */ |
46 |
|
|
/* This function initializes the USB storage device. */ |
47 |
|
|
/* */ |
48 |
|
|
/* This function is for RTOS mode. */ |
49 |
|
|
/* */ |
50 |
|
|
/* INPUT */ |
51 |
|
|
/* */ |
52 |
|
|
/* storage Pointer to storage class */ |
53 |
|
|
/* */ |
54 |
|
|
/* OUTPUT */ |
55 |
|
|
/* */ |
56 |
|
|
/* Completion Status */ |
57 |
|
|
/* */ |
58 |
|
|
/* CALLS */ |
59 |
|
|
/* */ |
60 |
|
|
/* _ux_host_class_storage_device_reset Reset device */ |
61 |
|
|
/* _ux_host_class_storage_max_lun_get Get maximum number of LUNs */ |
62 |
|
|
/* _ux_host_class_storage_media_characteristics_get */ |
63 |
|
|
/* Get media characteristics */ |
64 |
|
|
/* _ux_host_class_storage_media_format_capacity_get */ |
65 |
|
|
/* Get format capacity */ |
66 |
|
|
/* _ux_host_class_storage_media_mount Mount the media */ |
67 |
|
|
/* _ux_utility_delay_ms Delay ms */ |
68 |
|
|
/* */ |
69 |
|
|
/* CALLED BY */ |
70 |
|
|
/* */ |
71 |
|
|
/* Storage Class */ |
72 |
|
|
/* */ |
73 |
|
|
/**************************************************************************/ |
74 |
|
127 |
UINT _ux_host_class_storage_device_initialize(UX_HOST_CLASS_STORAGE *storage) |
75 |
|
|
{ |
76 |
|
|
|
77 |
|
|
UINT status; |
78 |
|
|
ULONG lun_index; |
79 |
|
|
#if defined(UX_HOST_CLASS_STORAGE_NO_FILEX) |
80 |
|
|
UX_HOST_CLASS_STORAGE_MEDIA *storage_media; |
81 |
|
|
UX_HOST_CLASS *class_inst; |
82 |
|
|
UINT inst_index; |
83 |
|
|
#endif |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
/* Get the maximum number of LUN (Bulk Only device only, other device |
87 |
|
|
will set the LUN number to 0). */ |
88 |
|
127 |
status = _ux_host_class_storage_max_lun_get(storage); |
89 |
✓✓ |
127 |
if (status != UX_SUCCESS) |
90 |
|
1 |
return(status); |
91 |
|
|
|
92 |
|
|
/* We need to wait for some device to settle. The INTUIX Flash disk is an example of |
93 |
|
|
these device who fail the first Inquiry command if sent too quickly. |
94 |
|
|
The timing does not have to be precise so we use the thread sleep function. |
95 |
|
|
The default sleep value is 2 seconds. */ |
96 |
|
126 |
_ux_utility_delay_ms(UX_HOST_CLASS_STORAGE_DEVICE_INIT_DELAY); |
97 |
|
|
|
98 |
|
|
#if defined(UX_HOST_CLASS_STORAGE_NO_FILEX) |
99 |
|
|
/* We need the class container. */ |
100 |
|
|
class_inst = storage -> ux_host_class_storage_class; |
101 |
|
|
#endif |
102 |
|
|
|
103 |
|
|
/* Each LUN must be parsed and mounted. */ |
104 |
✓✓ |
268 |
for (lun_index = 0; lun_index <= storage -> ux_host_class_storage_max_lun; lun_index++) |
105 |
|
|
{ |
106 |
|
|
|
107 |
|
|
/* Set the LUN into the storage instance. */ |
108 |
|
155 |
storage -> ux_host_class_storage_lun = lun_index; |
109 |
|
|
|
110 |
|
|
/* Get the media type supported by this storage device. */ |
111 |
|
155 |
status = _ux_host_class_storage_media_characteristics_get(storage); |
112 |
✓✓ |
155 |
if (status == UX_HOST_CLASS_MEDIA_NOT_SUPPORTED) |
113 |
|
|
{ |
114 |
|
|
/* Unsupported device. */ |
115 |
|
|
|
116 |
|
|
/* Error trap. */ |
117 |
|
2 |
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_MEDIA_NOT_SUPPORTED); |
118 |
|
|
|
119 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
120 |
|
|
UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_MEDIA_NOT_SUPPORTED, storage, 0, 0, UX_TRACE_ERRORS, 0, 0) |
121 |
|
|
|
122 |
|
2 |
continue; |
123 |
|
|
} |
124 |
✓✓ |
153 |
if (status != UX_SUCCESS) |
125 |
|
11 |
return(status); |
126 |
|
|
|
127 |
|
|
/* Get the format capacity of this storage device. */ |
128 |
|
142 |
status = _ux_host_class_storage_media_format_capacity_get(storage); |
129 |
✓✓ |
142 |
if (status != UX_SUCCESS) |
130 |
|
2 |
return(status); |
131 |
|
|
|
132 |
|
|
/* Store the LUN type in the LUN type array. */ |
133 |
|
140 |
storage -> ux_host_class_storage_lun_types[lun_index] = storage -> ux_host_class_storage_media_type; |
134 |
|
|
|
135 |
|
|
/* Check the media type. We support regular FAT drives and optical drives. |
136 |
|
|
No CD-ROM support in this release. */ |
137 |
✓✓ |
140 |
switch (storage -> ux_host_class_storage_media_type) |
138 |
|
|
{ |
139 |
|
|
|
140 |
|
138 |
case UX_HOST_CLASS_STORAGE_MEDIA_FAT_DISK: |
141 |
|
|
/* Fall through. */ |
142 |
|
|
case UX_HOST_CLASS_STORAGE_MEDIA_OPTICAL_DISK: |
143 |
|
|
/* Fall through. */ |
144 |
|
|
case UX_HOST_CLASS_STORAGE_MEDIA_IOMEGA_CLICK: |
145 |
|
|
|
146 |
|
|
#if !defined(UX_HOST_CLASS_STORAGE_NO_FILEX) |
147 |
|
|
/* the ux_host_class_storage_mounted_partitions_count is needed to avoid |
148 |
|
|
infinite recursive loops when mounting extended partions. |
149 |
|
|
The value is checked against the UX_HOST_CLASS_STORAGE_MAX_PARTITIONS_COUNT |
150 |
|
|
*/ |
151 |
|
138 |
storage -> ux_host_class_storage_mounted_partitions_count = 0; |
152 |
|
|
|
153 |
|
|
/* Try to read the device media in search for a partition table or boot sector. |
154 |
|
|
We are at the root of the disk, so use sector 0 as the starting point. */ |
155 |
|
138 |
_ux_host_class_storage_media_mount(storage, 0); |
156 |
|
|
#endif |
157 |
|
138 |
break; |
158 |
|
|
|
159 |
|
2 |
case UX_HOST_CLASS_STORAGE_MEDIA_CDROM: |
160 |
|
|
default: |
161 |
|
|
|
162 |
|
|
/* In the case of CD-ROM, we do no need to mount any file system yet. The application |
163 |
|
|
can read sectors directly. */ |
164 |
|
|
|
165 |
|
2 |
break; |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
#if defined(UX_HOST_CLASS_STORAGE_NO_FILEX) |
169 |
|
|
|
170 |
|
|
/* Find a free media slot for inserted media. */ |
171 |
|
|
storage_media = (UX_HOST_CLASS_STORAGE_MEDIA *) class_inst -> ux_host_class_media; |
172 |
|
|
for (inst_index = 0; inst_index < UX_HOST_CLASS_STORAGE_MAX_MEDIA; |
173 |
|
|
storage_media ++, inst_index++) |
174 |
|
|
{ |
175 |
|
|
|
176 |
|
|
/* Find an unused storage media slot. */ |
177 |
|
|
if (storage_media -> ux_host_class_storage_media_status != UX_USED) |
178 |
|
|
{ |
179 |
|
|
|
180 |
|
|
/* Use this free storage media slot. */ |
181 |
|
|
storage_media -> ux_host_class_storage_media_status = UX_USED; |
182 |
|
|
storage_media -> ux_host_class_storage_media_storage = storage; |
183 |
|
|
|
184 |
|
|
/* Save media information. */ |
185 |
|
|
storage_media -> ux_host_class_storage_media_lun = (UCHAR)storage -> ux_host_class_storage_lun; |
186 |
|
|
storage_media -> ux_host_class_storage_media_sector_size = (USHORT)storage -> ux_host_class_storage_sector_size; |
187 |
|
|
storage_media -> ux_host_class_storage_media_number_sectors = storage -> ux_host_class_storage_last_sector_number + 1; |
188 |
|
|
|
189 |
|
|
/* Invoke callback for media insertion. */ |
190 |
|
|
if (_ux_system_host -> ux_system_host_change_function != UX_NULL) |
191 |
|
|
{ |
192 |
|
|
|
193 |
|
|
/* Call system change function. */ |
194 |
|
|
_ux_system_host -> ux_system_host_change_function(UX_STORAGE_MEDIA_INSERTION, |
195 |
|
|
storage -> ux_host_class_storage_class, (VOID *) storage_media); |
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
/* Media inserted in slot, done. */ |
199 |
|
|
break; |
200 |
|
|
} |
201 |
|
|
} |
202 |
|
|
#endif |
203 |
|
|
} |
204 |
|
|
|
205 |
|
|
/* Some LUNs may succeed and some may fail. For simplicity's sake, we just |
206 |
|
|
return success. The storage thread will try to remount the ones that failed. */ |
207 |
|
113 |
return(UX_SUCCESS); |
208 |
|
|
} |
209 |
|
|
#endif |