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