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_open PORTABLE C */ |
38 |
|
|
/* 6.2.0 */ |
39 |
|
|
/* AUTHOR */ |
40 |
|
|
/* */ |
41 |
|
|
/* Chaoqiong Xiao, Microsoft Corporation */ |
42 |
|
|
/* */ |
43 |
|
|
/* DESCRIPTION */ |
44 |
|
|
/* */ |
45 |
|
|
/* This function will ask UX_MEDIA (default FileX) to mount a new */ |
46 |
|
|
/* partition for this device. This function has some */ |
47 |
|
|
/* UX_MEDIA (default FileX) dependencies. */ |
48 |
|
|
/* */ |
49 |
|
|
/* INPUT */ |
50 |
|
|
/* */ |
51 |
|
|
/* storage Pointer to storage class */ |
52 |
|
|
/* hidden_sectors Number of hidden sectors */ |
53 |
|
|
/* */ |
54 |
|
|
/* OUTPUT */ |
55 |
|
|
/* */ |
56 |
|
|
/* Completion Status */ |
57 |
|
|
/* */ |
58 |
|
|
/* CALLS */ |
59 |
|
|
/* */ |
60 |
|
|
/* ux_media_open Media open */ |
61 |
|
|
/* _ux_host_class_storage_media_protection_check */ |
62 |
|
|
/* Check for protection */ |
63 |
|
|
/* _ux_utility_memory_allocate Allocate memory block */ |
64 |
|
|
/* _ux_utility_memory_free Free memory block */ |
65 |
|
|
/* */ |
66 |
|
|
/* CALLED BY */ |
67 |
|
|
/* */ |
68 |
|
|
/* _ux_host_class_storage_media_mount Media open */ |
69 |
|
|
/* */ |
70 |
|
|
/**************************************************************************/ |
71 |
|
162 |
UINT _ux_host_class_storage_media_open(UX_HOST_CLASS_STORAGE *storage, ULONG hidden_sectors) |
72 |
|
|
{ |
73 |
|
|
|
74 |
|
|
#if defined(UX_HOST_CLASS_STORAGE_NO_FILEX) |
75 |
|
|
UX_PARAMETER_NOT_USED(storage); |
76 |
|
|
UX_PARAMETER_NOT_USED(hidden_sectors); |
77 |
|
|
return(UX_FUNCTION_NOT_SUPPORTED); |
78 |
|
|
#else |
79 |
|
|
UINT status; |
80 |
|
|
UINT media_index; |
81 |
|
|
UX_HOST_CLASS_STORAGE_MEDIA *storage_media; |
82 |
|
|
UX_MEDIA *media; |
83 |
|
|
UX_HOST_CLASS *class_inst; |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
/* We need the class container. */ |
87 |
|
162 |
class_inst = storage -> ux_host_class_storage_class; |
88 |
|
|
|
89 |
|
|
/* Point the media structure to the first media in the container. */ |
90 |
|
162 |
storage_media = (UX_HOST_CLASS_STORAGE_MEDIA *) class_inst -> ux_host_class_media; |
91 |
|
|
|
92 |
|
|
/* Locate a free partition in the storage instance. */ |
93 |
✓✓ |
186 |
for (media_index = 0; media_index < UX_HOST_CLASS_STORAGE_MAX_MEDIA; media_index++) |
94 |
|
|
{ |
95 |
|
|
|
96 |
|
|
/* Get the USBX Integrated Media pointer for that media. */ |
97 |
|
185 |
media = &storage_media -> ux_host_class_storage_media; |
98 |
|
|
|
99 |
|
|
/* Is the media valid? */ |
100 |
✓✓ |
185 |
if (ux_media_id_get(media) == 0) |
101 |
|
|
{ |
102 |
|
|
|
103 |
|
|
/* Save the storage instance in the media instance. */ |
104 |
|
161 |
ux_media_driver_info_set(media, storage); |
105 |
|
|
|
106 |
|
|
/* Save the number of hidden sectors in this partition. */ |
107 |
|
161 |
storage_media -> ux_host_class_storage_media_partition_start = hidden_sectors; |
108 |
|
|
|
109 |
|
|
/* Save the LUN number in the storage media instance. */ |
110 |
|
161 |
storage_media -> ux_host_class_storage_media_lun = storage -> ux_host_class_storage_lun; |
111 |
|
|
|
112 |
|
|
/* Save the Sector size in the storage media instance. */ |
113 |
|
161 |
storage_media -> ux_host_class_storage_media_sector_size = storage -> ux_host_class_storage_sector_size; |
114 |
|
|
|
115 |
|
|
/* Check if media setting can support the sector size. */ |
116 |
✗✓ |
161 |
if (storage -> ux_host_class_storage_sector_size > UX_HOST_CLASS_STORAGE_MEMORY_BUFFER_SIZE) |
117 |
|
|
{ |
118 |
|
|
/* Error trap. */ |
119 |
|
|
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_MEMORY_ERROR); |
120 |
|
|
|
121 |
|
|
/* Required memory is over system setting. */ |
122 |
|
|
return(UX_HOST_CLASS_MEMORY_ERROR); |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
/* Save the storage media instance in the user reserved area in the UX_MEDIA structure. */ |
126 |
|
161 |
ux_media_reserved_for_user_set(media, storage_media); |
127 |
|
|
|
128 |
|
|
/* We now need to allocate a block of memory for UX_MEDIA (default FileX) to use when doing transfers |
129 |
|
|
The default buffer size is 8K. The value used for the definition is UX_HOST_CLASS_STORAGE_MEMORY_BUFFER_SIZE. |
130 |
|
|
This value can be changed to save on memory space but should not be smaller than |
131 |
|
|
the media sector size (which should be 512 bytes). Because USB devices are SCSI |
132 |
|
|
devices and there is a great deal of overhead when doing read/writes, it is better |
133 |
|
|
to leave the default buffer size or even increase it. */ |
134 |
|
161 |
storage_media -> ux_host_class_storage_media_memory = _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_MEMORY_BUFFER_SIZE); |
135 |
✓✓ |
161 |
if (storage_media -> ux_host_class_storage_media_memory == UX_NULL) |
136 |
|
3 |
return(UX_MEMORY_INSUFFICIENT); |
137 |
|
|
|
138 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
139 |
|
|
UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_STORAGE_MEDIA_OPEN, storage, media, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0) |
140 |
|
|
|
141 |
|
|
/* Ask UX_MEDIA (default FileX) to mount the partition. */ |
142 |
|
158 |
status = ux_media_open(media, UX_HOST_CLASS_STORAGE_MEDIA_NAME, _ux_host_class_storage_driver_entry, |
143 |
|
|
storage, storage_media -> ux_host_class_storage_media_memory, |
144 |
|
|
UX_HOST_CLASS_STORAGE_MEMORY_BUFFER_SIZE); |
145 |
|
|
|
146 |
|
|
/* If the media is mounted, update the status for the application. */ |
147 |
✓✓ |
158 |
if (status == UX_SUCCESS) |
148 |
|
151 |
storage_media -> ux_host_class_storage_media_status = UX_HOST_CLASS_STORAGE_MEDIA_MOUNTED; |
149 |
|
|
|
150 |
|
|
else |
151 |
|
|
|
152 |
|
|
/* Free the memory resources. */ |
153 |
|
7 |
_ux_utility_memory_free(storage_media -> ux_host_class_storage_media_memory); |
154 |
|
|
|
155 |
|
|
/* Return completion status. */ |
156 |
|
158 |
return(status); |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
/* Move to next entry in the media array. */ |
160 |
|
24 |
storage_media++; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
/* Error trap. */ |
164 |
|
1 |
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_MEMORY_ERROR); |
165 |
|
|
|
166 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
167 |
|
|
UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_MEMORY_ERROR, storage, 0, 0, UX_TRACE_ERRORS, 0, 0) |
168 |
|
|
|
169 |
|
|
/* Return error. */ |
170 |
|
1 |
return(UX_HOST_CLASS_MEMORY_ERROR); |
171 |
|
|
#endif /* !defined(UX_HOST_CLASS_STORAGE_NO_FILEX) */ |
172 |
|
|
} |