1 /*
2  * Copyright 2018, 2019 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "usb_host_config.h"
9 #if ((defined USB_HOST_CONFIG_CDC_RNDIS) && (USB_HOST_CONFIG_CDC_RNDIS))
10 #include "usb_host.h"
11 #include "usb_host_cdc.h"
12 #include "usb_host_cdc_rndis.h"
13 
14 #include "usb_host_devices.h"
15 /*******************************************************************************
16  * Definitions
17  ******************************************************************************/
18 
19 /*******************************************************************************
20  * Prototypes
21  ******************************************************************************/
22 
23 /*******************************************************************************
24  * Variables
25  ******************************************************************************/
26 /*A value, generated by the host, used to match the host's sent request to the response
27 from the device. It is the responsibility of the host to ensure the uniqueness of this
28 value among all outstanding request messages sent to the device. */
29 static uint32_t messageID = 1U;
30 /*******************************************************************************
31  * Code
32  ******************************************************************************/
USB_HostRndisInitMsg(usb_host_class_handle classHandle,uint8_t * messageBuffer,uint32_t messageBufferLength,transfer_callback_t callbackFn,void * callbackParam)33 usb_status_t USB_HostRndisInitMsg(usb_host_class_handle classHandle,
34                                   uint8_t *messageBuffer,
35                                   uint32_t messageBufferLength,
36                                   transfer_callback_t callbackFn,
37                                   void *callbackParam)
38 {
39     void *temp = (void *)messageBuffer;
40 
41     rndis_init_msg_struct_t *message = (rndis_init_msg_struct_t *)temp;
42 
43     message->messageType     = REMOTE_NDIS_INITIALIZE_MSG;
44     message->messageLength   = sizeof(rndis_init_msg_struct_t);
45     messageID                = messageID % 1024U;
46     message->requestID       = messageID++;
47     message->majorVersion    = RNDIS_MAJOR_VERSION;
48     message->minorVersion    = RNDIS_MINOR_VERSION;
49     message->maxTransferSize = RNDIS_MAX_TRANSFER_PACKET_SIZE;
50 
51     return USB_HostCdcSendEncapsulatedCommand(classHandle, messageBuffer, (uint16_t)message->messageLength, callbackFn,
52                                               callbackParam);
53 }
54 
USB_HostRndisQueryMsg(usb_host_class_handle classHandle,uint32_t Oid,uint8_t * messageBuffer,uint32_t messageBufferLength,uint32_t informationOffset,uint32_t informationLength,uint8_t * OIDInputBuffer,transfer_callback_t callbackFn,void * callbackParam)55 usb_status_t USB_HostRndisQueryMsg(usb_host_class_handle classHandle,
56                                    uint32_t Oid,
57                                    uint8_t *messageBuffer,
58                                    uint32_t messageBufferLength,
59                                    uint32_t informationOffset,
60                                    uint32_t informationLength,
61                                    uint8_t *OIDInputBuffer,
62                                    transfer_callback_t callbackFn,
63                                    void *callbackParam)
64 {
65     void *temp                        = (void *)messageBuffer;
66     rndis_query_msg_struct_t *message = (rndis_query_msg_struct_t *)temp;
67 
68     message->messageType             = REMOTE_NDIS_QUERY_MSG;
69     message->messageLength           = sizeof(rndis_query_msg_struct_t) + informationLength;
70     messageID                        = messageID % 1024U;
71     message->requestID               = messageID++;
72     message->oid                     = Oid;
73     message->informationBufferLength = informationLength;
74     message->informationBufferOffset = informationOffset;
75     message->deviceVcHandle          = 0U;
76     if (0U != informationLength)
77     {
78         if (informationLength > (messageBufferLength - RNDIS_QUERY_MSG_SIZE))
79         {
80 #ifdef HOST_ECHO
81             usb_echo("buffer is not enough\r\n");
82 #endif
83             message->messageLength           = messageBufferLength;
84             message->informationBufferLength = (messageBufferLength - RNDIS_QUERY_MSG_SIZE);
85         }
86         /*fixed misra 18.1*/
87         temp = (void *)(messageBuffer + sizeof(rndis_query_msg_struct_t));
88         (void)memcpy(temp, (void *)OIDInputBuffer, message->informationBufferLength);
89     }
90 
91     return USB_HostCdcSendEncapsulatedCommand(classHandle, messageBuffer, (uint16_t)message->messageLength, callbackFn,
92                                               callbackParam);
93 }
USB_HostRndisHaltMsg(usb_host_class_handle classHandle,uint8_t * messageBuffer,uint32_t messageBufferLength,transfer_callback_t callbackFn,void * callbackParam)94 usb_status_t USB_HostRndisHaltMsg(usb_host_class_handle classHandle,
95                                   uint8_t *messageBuffer,
96                                   uint32_t messageBufferLength,
97                                   transfer_callback_t callbackFn,
98                                   void *callbackParam)
99 {
100     void *temp                       = (void *)messageBuffer;
101     rndis_halt_msg_struct_t *message = (rndis_halt_msg_struct_t *)temp;
102 
103     message->messageType   = REMOTE_NDIS_HALT_MSG;
104     message->messageLength = sizeof(rndis_halt_msg_struct_t);
105     messageID              = messageID % 1024U;
106     message->requestID     = messageID++;
107 
108     return USB_HostCdcSendEncapsulatedCommand(classHandle, NULL, 0U, callbackFn, callbackParam);
109 }
USB_HostRndisSetMsg(usb_host_class_handle classHandle,uint32_t Oid,uint8_t * messageBuffer,uint32_t messageBufferLength,uint32_t informationOffset,uint32_t informationLength,uint32_t * OIDInputBuffer,transfer_callback_t callbackFn,void * callbackParam)110 usb_status_t USB_HostRndisSetMsg(usb_host_class_handle classHandle,
111                                  uint32_t Oid,
112                                  uint8_t *messageBuffer,
113                                  uint32_t messageBufferLength,
114                                  uint32_t informationOffset,
115                                  uint32_t informationLength,
116                                  uint32_t *OIDInputBuffer,
117                                  transfer_callback_t callbackFn,
118                                  void *callbackParam)
119 
120 {
121     void *temp                      = (void *)messageBuffer;
122     rndis_set_msg_struct_t *message = (rndis_set_msg_struct_t *)temp;
123 
124     message->messageType             = REMOTE_NDIS_SET_MSG;
125     message->messageLength           = sizeof(rndis_set_msg_struct_t) + informationLength;
126     messageID                        = messageID % 1024U;
127     message->requestID               = messageID++;
128     message->oid                     = Oid;
129     message->informationBufferLength = informationLength;
130     message->informationBufferOffset = informationOffset;
131     message->deviceVcHandle          = 0U;
132     if (0U != informationLength)
133     {
134         if (informationLength > (messageBufferLength - RNDIS_SET_MSG_SIZE))
135         {
136 #ifdef HOST_ECHO
137             usb_echo("buffer is not enough\r\n");
138 #endif
139             message->informationBufferLength = messageBufferLength - RNDIS_SET_MSG_SIZE;
140         }
141         /*fixed misra 18.1*/
142         temp = (void *)(messageBuffer + sizeof(rndis_set_msg_struct_t));
143         (void)memcpy((temp), (void *)OIDInputBuffer, message->informationBufferLength);
144     }
145     return USB_HostCdcSendEncapsulatedCommand(classHandle, messageBuffer, (uint16_t)message->messageLength, callbackFn,
146                                               callbackParam);
147 }
USB_HostRndisResetMsg(usb_host_class_handle classHandle,uint8_t * messageBuffer,uint32_t messageBufferLength,transfer_callback_t callbackFn,void * callbackParam)148 usb_status_t USB_HostRndisResetMsg(usb_host_class_handle classHandle,
149                                    uint8_t *messageBuffer,
150                                    uint32_t messageBufferLength,
151                                    transfer_callback_t callbackFn,
152                                    void *callbackParam)
153 {
154     void *temp                        = (void *)messageBuffer;
155     rndis_reset_msg_struct_t *message = (rndis_reset_msg_struct_t *)temp;
156 
157     message->messageType   = REMOTE_NDIS_QUERY_MSG;
158     message->messageLength = sizeof(rndis_reset_msg_struct_t);
159     message->Reserved      = 0U;
160 
161     return USB_HostCdcSendEncapsulatedCommand(classHandle, NULL, 0U, callbackFn, callbackParam);
162 }
USB_HostRndisSendDataMsg(usb_host_class_handle classHandle,uint8_t * messageBuffer,uint32_t messageBufferLength,uint32_t oobDataOffset,uint32_t oobDataLength,uint32_t numOOBDataElements,uint32_t perPacketInfoOffset,uint32_t perPacketInfoLength,uint8_t * dataBuffer,uint32_t dataLength,transfer_callback_t callbackFn,void * callbackParam)163 usb_status_t USB_HostRndisSendDataMsg(usb_host_class_handle classHandle,
164                                       uint8_t *messageBuffer,
165                                       uint32_t messageBufferLength,
166                                       uint32_t oobDataOffset,
167                                       uint32_t oobDataLength,
168                                       uint32_t numOOBDataElements,
169                                       uint32_t perPacketInfoOffset,
170                                       uint32_t perPacketInfoLength,
171                                       uint8_t *dataBuffer,
172                                       uint32_t dataLength,
173                                       transfer_callback_t callbackFn,
174                                       void *callbackParam)
175 {
176     void *temp                         = (void *)messageBuffer;
177     rndis_packet_msg_struct_t *message = (rndis_packet_msg_struct_t *)temp;
178 
179     message->messageType   = REMOTE_NDIS_PACKET_MSG;
180     message->messageLength = dataLength + RNDIS_DAT_MSG_HEADER_SIZE;
181 
182     message->dataOffset          = 36U;
183     message->dataLength          = dataLength;
184     message->oobDataOffset       = oobDataOffset;
185     message->oobDataLength       = oobDataLength;
186     message->numOOBDataElements  = numOOBDataElements;
187     message->perPacketInfoOffset = perPacketInfoOffset;
188     message->perPacketInfoLength = perPacketInfoLength;
189     message->vcHandle            = 0U;
190     message->reserved            = 0U;
191     if (dataLength > RNDIS_FRAME_MAX_FRAMELEN)
192     {
193 #ifdef HOST_ECHO
194         usb_echo("buffer is not enough\r\n");
195 #endif
196         message->dataLength = RNDIS_FRAME_MAX_FRAMELEN;
197     }
198     temp = (void *)&message->dataBuffer[0];
199     (void)memcpy(temp, (void *)dataBuffer, message->dataLength);
200 
201     return USB_HostCdcDataSend(classHandle, messageBuffer, message->messageLength, callbackFn, callbackParam);
202 }
USB_HostRndisRecvDataMsg(usb_host_class_handle classHandle,uint8_t * buffer,uint32_t bufferLength,transfer_callback_t callbackFn,void * callbackParam)203 usb_status_t USB_HostRndisRecvDataMsg(usb_host_class_handle classHandle,
204                                       uint8_t *buffer,
205                                       uint32_t bufferLength,
206                                       transfer_callback_t callbackFn,
207                                       void *callbackParam)
208 {
209     return USB_HostCdcDataRecv(classHandle, buffer, bufferLength, callbackFn, callbackParam);
210 }
211 
212 #endif
213