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 #if defined(UX_HOST_STANDALONE)
33
34 extern VOID _ux_host_class_storage_read_initialize(UX_HOST_CLASS_STORAGE *storage,
35 ULONG sector_start, ULONG sector_count);
36
37 extern VOID _ux_host_class_storage_write_initialize(UX_HOST_CLASS_STORAGE *storage,
38 ULONG sector_start, ULONG sector_count);
39
40
41 /**************************************************************************/
42 /* */
43 /* FUNCTION RELEASE */
44 /* */
45 /* _ux_host_class_storage_read_write_run PORTABLE C */
46 /* 6.2.1 */
47 /* AUTHOR */
48 /* */
49 /* Chaoqiong Xiao, Microsoft Corporation */
50 /* */
51 /* DESCRIPTION */
52 /* */
53 /* This function runs state machine to read or write one or more */
54 /* logical sector on a selected media of a storage function. */
55 /* */
56 /* It's valid only in standalone mode. */
57 /* */
58 /* Note it must be used in case following conditions are true: */
59 /* - storage insstance is live */
60 /* - storage is locked for specific LUN */
61 /* - the main storage state must be idle */
62 /* */
63 /* INPUT */
64 /* */
65 /* storage Pointer to storage class */
66 /* read_write Set to UX_TRUE to read */
67 /* sector_start Starting sector */
68 /* sector_count Number of sectors to read */
69 /* data_pointer Pointer to data to read */
70 /* */
71 /* OUTPUT */
72 /* */
73 /* Completion Status */
74 /* UX_STATE_WAIT States started/in progress */
75 /* UX_STATE_NEXT Read/write is done, next call */
76 /* starts read/write again. */
77 /* */
78 /* CALLS */
79 /* */
80 /* _ux_host_class_storage_read_initialize */
81 /* Initialize the CBW */
82 /* _ux_host_class_storage_write_initialize */
83 /* Initialize the CBW */
84 /* _ux_system_error_handler The error trap */
85 /* _ux_system_tasks_run Run USB system tasks */
86 /* */
87 /* CALLED BY */
88 /* */
89 /* Storage Class */
90 /* */
91 /* RELEASE HISTORY */
92 /* */
93 /* DATE NAME DESCRIPTION */
94 /* */
95 /* 01-31-2022 Chaoqiong Xiao Initial Version 6.1.10 */
96 /* 03-08-2023 Chaoqiong Xiao Modified comment(s), */
97 /* checked device state, */
98 /* resulting in version 6.2.1 */
99 /* */
100 /**************************************************************************/
_ux_host_class_storage_read_write_run(UX_HOST_CLASS_STORAGE * storage,ULONG read_write,ULONG sector_start,ULONG sector_count,UCHAR * data_pointer)101 UINT _ux_host_class_storage_read_write_run(UX_HOST_CLASS_STORAGE *storage,
102 ULONG read_write,
103 ULONG sector_start, ULONG sector_count, UCHAR *data_pointer)
104 {
105
106 UX_DEVICE *device;
107
108 #if defined UX_HOST_CLASS_STORAGE_STATE_CHECK_ENABLE
109 UX_INTERRUPT_SAVE_AREA
110
111 /* Check states - storage must be live, locked and main state machine idle. */
112 UX_DISABLE
113 if (storage -> ux_host_class_storage_state != UX_HOST_CLASS_INSTANCE_LIVE ||
114 (storage -> ux_host_class_storage_flags & UX_HOST_CLASS_STORAGE_FLAG_LOCK) == 0 ||
115 storage -> ux_host_class_storage_state_state != UX_STATE_IDLE)
116 {
117 UX_RESTORE
118
119 /* Error trap. */
120 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_NOT_READY);
121
122 /* The state is not ready for read. */
123 storage -> ux_host_class_storage_status = UX_TRANSFER_NOT_READY;
124 storage -> ux_host_class_storage_op_state = UX_STATE_RESET;
125 return(UX_STATE_ERROR);
126 }
127 UX_RESTORE
128 #endif
129
130 /* If trace is enabled, insert this event into the trace buffer. */
131 UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_STORAGE_MEDIA_READ, storage, sector_start, sector_count, data_pointer, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
132
133 /* Get device. */
134 device = storage -> ux_host_class_storage_device;
135
136 switch(storage -> ux_host_class_storage_op_state)
137 {
138 case UX_STATE_IDLE: /* Fall through. */
139 case UX_STATE_ERROR: /* Fall through. */
140 case UX_STATE_RESET:
141
142 /* Initialize CBW. */
143 if (read_write)
144 _ux_host_class_storage_read_initialize(storage, sector_start, sector_count);
145 else
146 _ux_host_class_storage_write_initialize(storage, sector_start, sector_count);
147
148 /* Initialize for transport. */
149 UX_HOST_CLASS_STORAGE_TRANS_STATE_RESET(storage);
150 storage -> ux_host_class_storage_trans_data = data_pointer;
151
152 /* Next : wait */
153 storage -> ux_host_class_storage_op_state = UX_STATE_WAIT;
154
155 /* Fall through. */
156 case UX_STATE_WAIT:
157
158 /* Run tasks, including transport task. */
159 _ux_system_host_tasks_run();
160
161 /* Check if device is still available. */
162 if (device -> ux_device_state != UX_DEVICE_CONFIGURED)
163 {
164
165 /* Instance should have been destroyed, just return. */
166 return(UX_STATE_EXIT);
167 }
168
169 /* Fatal error. */
170 if (storage -> ux_host_class_storage_op_state < UX_STATE_IDLE)
171 return(UX_STATE_EXIT);
172
173 /* It's done with/without error. */
174 if (storage -> ux_host_class_storage_op_state <= UX_STATE_NEXT)
175 return(UX_STATE_NEXT);
176
177 /* Wait. */
178 return(UX_STATE_WAIT);
179
180 /* Unexpected states. */
181 default:
182 storage -> ux_host_class_storage_op_state = UX_STATE_RESET;
183 break;
184 }
185
186 /* Return fatal exit state status. */
187 return(UX_STATE_EXIT);
188 }
189 #endif
190