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_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 /* 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, used UX_ */
78 /* things instead of FX_ */
79 /* things directly, used host */
80 /* class extension pointer for */
81 /* class specific structured */
82 /* data, */
83 /* resulting in version 6.1 */
84 /* 10-31-2022 Chaoqiong Xiao Modified comment(s), */
85 /* added buffer size check, */
86 /* resulting in version 6.2.0 */
87 /* */
88 /**************************************************************************/
_ux_host_class_storage_media_open(UX_HOST_CLASS_STORAGE * storage,ULONG hidden_sectors)89 UINT _ux_host_class_storage_media_open(UX_HOST_CLASS_STORAGE *storage, ULONG hidden_sectors)
90 {
91
92 #if defined(UX_HOST_CLASS_STORAGE_NO_FILEX)
93 UX_PARAMETER_NOT_USED(storage);
94 UX_PARAMETER_NOT_USED(hidden_sectors);
95 return(UX_FUNCTION_NOT_SUPPORTED);
96 #else
97 UINT status;
98 UINT media_index;
99 UX_HOST_CLASS_STORAGE_MEDIA *storage_media;
100 UX_MEDIA *media;
101 UX_HOST_CLASS *class_inst;
102
103
104 /* We need the class container. */
105 class_inst = storage -> ux_host_class_storage_class;
106
107 /* Point the media structure to the first media in the container. */
108 storage_media = (UX_HOST_CLASS_STORAGE_MEDIA *) class_inst -> ux_host_class_media;
109
110 /* Locate a free partition in the storage instance. */
111 for (media_index = 0; media_index < UX_HOST_CLASS_STORAGE_MAX_MEDIA; media_index++)
112 {
113
114 /* Get the USBX Integrated Media pointer for that media. */
115 media = &storage_media -> ux_host_class_storage_media;
116
117 /* Is the media valid? */
118 if (ux_media_id_get(media) == 0)
119 {
120
121 /* Save the storage instance in the media instance. */
122 ux_media_driver_info_set(media, storage);
123
124 /* Save the number of hidden sectors in this partition. */
125 storage_media -> ux_host_class_storage_media_partition_start = hidden_sectors;
126
127 /* Save the LUN number in the storage media instance. */
128 storage_media -> ux_host_class_storage_media_lun = storage -> ux_host_class_storage_lun;
129
130 /* Save the Sector size in the storage media instance. */
131 storage_media -> ux_host_class_storage_media_sector_size = storage -> ux_host_class_storage_sector_size;
132
133 /* Check if media setting can support the sector size. */
134 if (storage -> ux_host_class_storage_sector_size > UX_HOST_CLASS_STORAGE_MEMORY_BUFFER_SIZE)
135 {
136 /* Error trap. */
137 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_MEMORY_ERROR);
138
139 /* Required memory is over system setting. */
140 return(UX_HOST_CLASS_MEMORY_ERROR);
141 }
142
143 /* Save the storage media instance in the user reserved area in the UX_MEDIA structure. */
144 ux_media_reserved_for_user_set(media, storage_media);
145
146 /* We now need to allocate a block of memory for UX_MEDIA (default FileX) to use when doing transfers
147 The default buffer size is 8K. The value used for the definition is UX_HOST_CLASS_STORAGE_MEMORY_BUFFER_SIZE.
148 This value can be changed to save on memory space but should not be smaller than
149 the media sector size (which should be 512 bytes). Because USB devices are SCSI
150 devices and there is a great deal of overhead when doing read/writes, it is better
151 to leave the default buffer size or even increase it. */
152 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);
153 if (storage_media -> ux_host_class_storage_media_memory == UX_NULL)
154 return(UX_MEMORY_INSUFFICIENT);
155
156 /* If trace is enabled, insert this event into the trace buffer. */
157 UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_STORAGE_MEDIA_OPEN, storage, media, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
158
159 /* Ask UX_MEDIA (default FileX) to mount the partition. */
160 status = ux_media_open(media, UX_HOST_CLASS_STORAGE_MEDIA_NAME, _ux_host_class_storage_driver_entry,
161 storage, storage_media -> ux_host_class_storage_media_memory,
162 UX_HOST_CLASS_STORAGE_MEMORY_BUFFER_SIZE);
163
164 /* If the media is mounted, update the status for the application. */
165 if (status == UX_SUCCESS)
166 storage_media -> ux_host_class_storage_media_status = UX_HOST_CLASS_STORAGE_MEDIA_MOUNTED;
167
168 else
169
170 /* Free the memory resources. */
171 _ux_utility_memory_free(storage_media -> ux_host_class_storage_media_memory);
172
173 /* Return completion status. */
174 return(status);
175 }
176
177 /* Move to next entry in the media array. */
178 storage_media++;
179 }
180
181 /* Error trap. */
182 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_MEMORY_ERROR);
183
184 /* If trace is enabled, insert this event into the trace buffer. */
185 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_MEMORY_ERROR, storage, 0, 0, UX_TRACE_ERRORS, 0, 0)
186
187 /* Return error. */
188 return(UX_HOST_CLASS_MEMORY_ERROR);
189 #endif /* !defined(UX_HOST_CLASS_STORAGE_NO_FILEX) */
190 }
191