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_activate                   PORTABLE C      */
37 /*                                                           6.3.0        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function activates the USB storage device.                     */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    command                               Pointer to storage command    */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    Completion Status                                                   */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _ux_device_thread_resume              Resume thread                 */
57 /*                                                                        */
58 /*  CALLED BY                                                             */
59 /*                                                                        */
60 /*    Device Storage Class                                                */
61 /*                                                                        */
62 /*  RELEASE HISTORY                                                       */
63 /*                                                                        */
64 /*    DATE              NAME                      DESCRIPTION             */
65 /*                                                                        */
66 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
67 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
68 /*                                            resulting in version 6.1    */
69 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
70 /*                                            added standalone support,   */
71 /*                                            resulting in version 6.1.10 */
72 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            fixed parameter/variable    */
74 /*                                            names conflict C++ keyword, */
75 /*                                            resulting in version 6.1.12 */
76 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            added a new mode to manage  */
78 /*                                            endpoint buffer in classes, */
79 /*                                            resulting in version 6.3.0  */
80 /*                                                                        */
81 /**************************************************************************/
_ux_device_class_storage_activate(UX_SLAVE_CLASS_COMMAND * command)82 UINT  _ux_device_class_storage_activate(UX_SLAVE_CLASS_COMMAND *command)
83 {
84 
85 UINT                                    status = UX_SUCCESS;
86 UX_SLAVE_INTERFACE                      *interface_ptr;
87 UX_SLAVE_CLASS                          *class_ptr;
88 UX_SLAVE_CLASS_STORAGE                  *storage;
89 #if defined(UX_DEVICE_STANDALONE)
90 UX_SLAVE_ENDPOINT                       *endpoint;
91 #endif
92 
93 
94     /* Get the class container.  */
95     class_ptr =  command -> ux_slave_class_command_class_ptr;
96 
97     /* Get the class instance in the container.  */
98     storage = (UX_SLAVE_CLASS_STORAGE *)class_ptr -> ux_slave_class_instance;
99 
100     /* Get the interface that owns this instance.  */
101     interface_ptr =  (UX_SLAVE_INTERFACE  *) command -> ux_slave_class_command_interface;
102 
103     /* Store the class instance into the interface.  */
104     interface_ptr -> ux_slave_interface_class_instance =  (VOID *)storage;
105 
106     /* Now the opposite, store the interface in the class instance.  */
107     storage -> ux_slave_class_storage_interface =  interface_ptr;
108 
109 #if !defined(UX_DEVICE_STANDALONE)
110 
111     /* Resume thread.  */
112     _ux_device_thread_resume(&class_ptr -> ux_slave_class_thread);
113 
114 #else
115 
116     /* Locate the endpoints.  */
117     /* Check the first endpoint direction, if IN we have the correct endpoint.  */
118     endpoint = interface_ptr -> ux_slave_interface_first_endpoint;
119     if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_IN)
120     {
121 
122         /* Wrong direction, we found the OUT endpoint first.  */
123         storage -> ux_device_class_storage_ep_out = endpoint;
124 
125         /* So the next endpoint has to be the IN endpoint.  */
126         storage -> ux_device_class_storage_ep_in = endpoint -> ux_slave_endpoint_next_endpoint;
127     }
128     else
129     {
130 
131         /* We found the IN endpoint first.  */
132         storage -> ux_device_class_storage_ep_in = endpoint;
133 
134         /* So the next endpoint has to be the OUT endpoint.  */
135         storage -> ux_device_class_storage_ep_out = endpoint -> ux_slave_endpoint_next_endpoint;
136     }
137 
138 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
139 
140     /* Assign endpoint buffers.  */
141     storage -> ux_device_class_storage_ep_in -> ux_slave_endpoint_transfer_request.
142         ux_slave_transfer_request_data_pointer = UX_DEVICE_CLASS_STORAGE_BULKIN_BUFFER(storage);
143     storage -> ux_device_class_storage_ep_out -> ux_slave_endpoint_transfer_request.
144         ux_slave_transfer_request_data_pointer = UX_DEVICE_CLASS_STORAGE_BULKOUT_BUFFER(storage);
145 #endif
146 
147     /* Reset states.  */
148     storage -> ux_device_class_storage_buffer[0] = storage -> ux_device_class_storage_ep_out ->
149                     ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer;
150     storage -> ux_device_class_storage_buffer[1] = storage -> ux_device_class_storage_ep_in ->
151                     ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer;
152     storage -> ux_device_class_storage_data_buffer = UX_NULL;
153     storage -> ux_device_class_storage_state = UX_STATE_RESET;
154     storage -> ux_device_class_storage_disk_state = UX_DEVICE_CLASS_STORAGE_DISK_IDLE;
155     storage -> ux_device_class_storage_buffer_state[0] = UX_DEVICE_CLASS_STORAGE_BUFFER_IDLE;
156     storage -> ux_device_class_storage_buffer_state[1] = UX_DEVICE_CLASS_STORAGE_BUFFER_IDLE;
157     storage -> ux_device_class_storage_buffer_usb = 0;
158     storage -> ux_device_class_storage_buffer_disk = 0;
159     UX_SLAVE_TRANSFER_STATE_RESET(&storage -> ux_device_class_storage_ep_out -> ux_slave_endpoint_transfer_request);
160     UX_SLAVE_TRANSFER_STATE_RESET(&storage -> ux_device_class_storage_ep_in -> ux_slave_endpoint_transfer_request);
161 
162     status = UX_SUCCESS;
163 #endif
164 
165     /* If there is a activate function call it.  */
166     if (storage -> ux_slave_class_storage_instance_activate != UX_NULL)
167     {
168         /* Invoke the application.  */
169         storage -> ux_slave_class_storage_instance_activate(storage);
170     }
171 
172     /* If trace is enabled, insert this event into the trace buffer.  */
173     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_ACTIVATE, storage, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
174 
175     /* If trace is enabled, register this object.  */
176     UX_TRACE_OBJECT_REGISTER(UX_TRACE_DEVICE_OBJECT_TYPE_INTERFACE, storage, 0, 0, 0)
177 
178     /* Return completion status.  */
179     return(status);
180 }
181 
182