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 /** USBX Component                                                        */
15 /**                                                                       */
16 /**   Device DPUMP Class                                                  */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 #define UX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "ux_api.h"
27 #include "ux_device_class_dpump.h"
28 #include "ux_device_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_class_dpump_read                         PORTABLE C      */
36 /*                                                           6.3.0        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function reads from the DPUMP class.                           */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    dpump                                   Address of dpump class      */
48 /*                                                instance                */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    None                                                                */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _ux_device_stack_transfer_request     Request transfer              */
57 /*    _ux_utility_memory_copy               Copy memory                   */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    ThreadX                                                             */
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 /*                                            verified memset and memcpy  */
70 /*                                            cases,                      */
71 /*                                            resulting in version 6.1    */
72 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            added a new mode to manage  */
74 /*                                            endpoint buffer in classes, */
75 /*                                            resulting in version 6.3.0  */
76 /*                                                                        */
77 /**************************************************************************/
_ux_device_class_dpump_read(UX_SLAVE_CLASS_DPUMP * dpump,UCHAR * buffer,ULONG requested_length,ULONG * actual_length)78 UINT _ux_device_class_dpump_read(UX_SLAVE_CLASS_DPUMP *dpump, UCHAR *buffer,
79                                 ULONG requested_length, ULONG *actual_length)
80 {
81 
82 UX_SLAVE_ENDPOINT           *endpoint;
83 UX_SLAVE_DEVICE             *device;
84 UX_SLAVE_TRANSFER           *transfer_request;
85 UINT                        status;
86 ULONG                       local_requested_length;
87 
88     /* If trace is enabled, insert this event into the trace buffer.  */
89     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_DPUMP_READ, dpump, buffer, requested_length, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
90 
91     /* Get the pointer to the device.  */
92     device =  &_ux_system_slave -> ux_system_slave_device;
93 
94     /* As long as the device is in the CONFIGURED state.  */
95     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
96     {
97 
98         /* Error trap. */
99         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
100 
101         /* If trace is enabled, insert this event into the trace buffer.  */
102         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, device, 0, 0, UX_TRACE_ERRORS, 0, 0)
103 
104         /* Cannot proceed with command, the interface is down.  */
105         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
106     }
107 
108     /* Locate the OUT endpoint.  */
109     endpoint =  dpump -> ux_slave_class_dpump_bulkout_endpoint;
110 
111     /* Check endpoint. If NULL, we have not yet received the proper SET_INTERFACE command.  */
112     if (endpoint == UX_NULL)
113     {
114         /* Error trap. */
115         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
116 
117         return(UX_ENDPOINT_HANDLE_UNKNOWN);
118     }
119 
120     /* All DPUMP reading  are on the endpoint OUT, from the host.  */
121     transfer_request =  &endpoint -> ux_slave_endpoint_transfer_request;
122 
123     /* Reset the actual length.  */
124     *actual_length =  0;
125 
126     /* Set return status to SUCCESS to make certain compilers happy.  */
127     status =  UX_SUCCESS;
128 
129     /* Check if we need more transactions.  */
130     while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED && requested_length != 0)
131     {
132 
133         /* Check if we have enough in the local buffer.  */
134         if (requested_length > UX_DEVICE_CLASS_DPUMP_READ_BUFFER_SIZE)
135 
136             /* We have too much to transfer.  */
137             local_requested_length = UX_DEVICE_CLASS_DPUMP_READ_BUFFER_SIZE;
138 
139         else
140 
141             /* We can proceed with the demanded length.  */
142             local_requested_length = requested_length;
143 
144         /* Send the request to the device controller.  */
145         status =  _ux_device_stack_transfer_request(transfer_request, local_requested_length, local_requested_length);
146 
147         /* Check the status */
148         if (status == UX_SUCCESS)
149         {
150 
151             /* We need to copy the buffer locally.  */
152             _ux_utility_memory_copy(buffer, transfer_request -> ux_slave_transfer_request_data_pointer,
153                             local_requested_length); /* Use case of memcpy is verified. */
154 
155             /* Next buffer address.  */
156             buffer += transfer_request -> ux_slave_transfer_request_actual_length;
157 
158             /* Set the length actually received. */
159             *actual_length += transfer_request -> ux_slave_transfer_request_actual_length;
160 
161             /* Decrement what left has to be done.  */
162             requested_length -= transfer_request -> ux_slave_transfer_request_actual_length;
163 
164         }
165         else
166 
167             /* We got an error.  */
168             return(status);
169     }
170 
171     /* Check why we got here, either completion or device was extracted.  */
172     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
173     {
174 
175         /* Error trap. */
176         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_NO_ANSWER);
177 
178         /* If trace is enabled, insert this event into the trace buffer.  */
179         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_TRANSFER_NO_ANSWER, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0)
180 
181         /* Device must have been extracted.  */
182         return (UX_TRANSFER_NO_ANSWER);
183     }
184     else
185 
186         /* Simply return the last transaction result.  */
187         return(status);
188 }
189 
190