1 /***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10
11
12 /**************************************************************************/
13 /**************************************************************************/
14 /** */
15 /** USBX Component */
16 /** */
17 /** Device Storage Class */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22 #define UX_SOURCE_CODE
23
24
25 /* Include necessary system files. */
26
27 #include "ux_api.h"
28 #include "ux_device_class_storage.h"
29 #include "ux_device_stack.h"
30
31
32 /**************************************************************************/
33 /* */
34 /* FUNCTION RELEASE */
35 /* */
36 /* _ux_device_class_storage_control_request PORTABLE C */
37 /* 6.1.12 */
38 /* AUTHOR */
39 /* */
40 /* Chaoqiong Xiao, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function manages the based sent by the host on the control */
45 /* endpoints with a CLASS or VENDOR SPECIFIC type. */
46 /* */
47 /* INPUT */
48 /* */
49 /* storage Pointer to storage class */
50 /* */
51 /* OUTPUT */
52 /* */
53 /* None */
54 /* */
55 /* CALLS */
56 /* */
57 /* _ux_device_stack_transfer_request Transfer request */
58 /* _ux_device_stack_transfer_abort Abort Transfer */
59 /* */
60 /* CALLED BY */
61 /* */
62 /* Device Storage Class */
63 /* */
64 /* RELEASE HISTORY */
65 /* */
66 /* DATE NAME DESCRIPTION */
67 /* */
68 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
69 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
70 /* optimized command logic, */
71 /* resulting in version 6.1 */
72 /* 12-31-2020 Chaoqiong Xiao Modified comment(s), */
73 /* fixed USB CV test issues, */
74 /* resulting in version 6.1.3 */
75 /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */
76 /* added standalone support, */
77 /* resulting in version 6.1.10 */
78 /* 07-29-2022 Chaoqiong Xiao Modified comment(s), */
79 /* fixed parameter/variable */
80 /* names conflict C++ keyword, */
81 /* resulting in version 6.1.12 */
82 /* */
83 /**************************************************************************/
_ux_device_class_storage_control_request(UX_SLAVE_CLASS_COMMAND * command)84 UINT _ux_device_class_storage_control_request(UX_SLAVE_CLASS_COMMAND *command)
85 {
86
87 UX_SLAVE_TRANSFER *transfer_request;
88 UX_SLAVE_DEVICE *device;
89 UX_SLAVE_CLASS *class_ptr;
90 ULONG request;
91 ULONG request_value;
92 ULONG request_length;
93 UX_SLAVE_CLASS_STORAGE *storage;
94 #if !defined(UX_DEVICE_STANDALONE)
95 UX_SLAVE_INTERFACE *interface_ptr;
96 #endif
97 UX_SLAVE_ENDPOINT *endpoint_in;
98 UX_SLAVE_ENDPOINT *endpoint_out;
99
100
101 /* Get the pointer to the device. */
102 device = &_ux_system_slave -> ux_system_slave_device;
103
104 /* Get the pointer to the transfer request associated with the control endpoint. */
105 transfer_request = &device -> ux_slave_device_control_endpoint.ux_slave_endpoint_transfer_request;
106
107 /* Extract the request type from the SETUP packet.. */
108 request = *(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_REQUEST);
109 request_value = _ux_utility_short_get(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_VALUE);
110 request_length = _ux_utility_short_get(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_LENGTH);
111
112 /* Check if wValue is valid. */
113 if (request_value != 0)
114 return(UX_ERROR);
115
116 /* Get the class container. */
117 class_ptr = command -> ux_slave_class_command_class_ptr;
118
119 /* Get the storage instance from this class container. */
120 storage = (UX_SLAVE_CLASS_STORAGE *) class_ptr -> ux_slave_class_instance;
121
122 /* Here we proceed only the standard request we know of at the device level. */
123 switch (request)
124 {
125
126 case UX_SLAVE_CLASS_STORAGE_RESET:
127
128 /* Check if wLength is valid. */
129 if (request_length != 0)
130 return(UX_ERROR);
131
132 #if defined(UX_DEVICE_STANDALONE)
133 endpoint_in = storage -> ux_device_class_storage_ep_in;
134 endpoint_out = storage -> ux_device_class_storage_ep_out;
135 #else
136
137 /* We need the interface to the class. */
138 interface_ptr = storage -> ux_slave_class_storage_interface;
139
140 /* Locate the endpoints. */
141 endpoint_in = interface_ptr -> ux_slave_interface_first_endpoint;
142
143 /* Check the endpoint direction, if IN we have the correct endpoint. */
144 if ((endpoint_in -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_IN)
145 {
146
147 /* Wrong direction, we found the OUT endpoint first. */
148 endpoint_out = endpoint_in;
149
150 /* So the next endpoint has to be the IN endpoint. */
151 endpoint_in = endpoint_out -> ux_slave_endpoint_next_endpoint;
152 }
153 else
154 {
155
156 /* We found the endpoint IN first, so next endpoint is OUT. */
157 endpoint_out = endpoint_in -> ux_slave_endpoint_next_endpoint;
158 }
159 #endif
160
161 /* First cancel any transfer on the endpoint OUT, from the host. */
162 transfer_request = &endpoint_out -> ux_slave_endpoint_transfer_request;
163 _ux_device_stack_transfer_abort(transfer_request, UX_TRANSFER_APPLICATION_RESET);
164
165 /* Then cancel any transfer on the endpoint IN, from the host. */
166 transfer_request = &endpoint_in -> ux_slave_endpoint_transfer_request;
167 _ux_device_stack_transfer_abort(transfer_request, UX_TRANSFER_APPLICATION_RESET);
168
169 /* Reset phase error. */
170 storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PASSED;
171
172 break;
173
174 case UX_SLAVE_CLASS_STORAGE_GET_MAX_LUN:
175
176 /* Check if wLength is valid. */
177 if (request_length < 1)
178 return(UX_ERROR);
179
180 /* Set the value of the number of LUN in the buffer. The max number of LUN is the
181 number of declared LUN - 1. */
182 *transfer_request -> ux_slave_transfer_request_data_pointer = (UCHAR)(storage -> ux_slave_class_storage_number_lun -1);
183
184 /* Set the phase of the transfer to data out. */
185 transfer_request -> ux_slave_transfer_request_phase = UX_TRANSFER_PHASE_DATA_OUT;
186
187 /* We can return the LUN number. */
188 _ux_device_stack_transfer_request(transfer_request, 1, 1);
189 break;
190
191 default:
192
193 /* Unknown function. It's not handled. */
194 return(UX_ERROR);
195 }
196
197 /* It's handled. */
198 return(UX_SUCCESS);
199 }
200