Open FFBoard
Open source force feedback firmware
ecm_rndis_device.c
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2020 Peter Lawrence
5 * Copyright (c) 2019 Ha Thach (tinyusb.org)
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 *
25 * This file is part of the TinyUSB stack.
26 */
27
28#include "tusb_option.h"
29
30#if ( CFG_TUD_ENABLED && CFG_TUD_ECM_RNDIS )
31
32#include "device/usbd.h"
33#include "device/usbd_pvt.h"
34
35#include "net_device.h"
36#include "rndis_protocol.h"
37
38void rndis_class_set_handler(uint8_t *data, int size); /* found in ./misc/networking/rndis_reports.c */
39
40//--------------------------------------------------------------------+
41// MACRO CONSTANT TYPEDEF
42//--------------------------------------------------------------------+
43typedef struct
44{
45 uint8_t itf_num; // Index number of Management Interface, +1 for Data Interface
46 uint8_t itf_data_alt; // Alternate setting of Data Interface. 0 : inactive, 1 : active
47
48 uint8_t ep_notif;
49 uint8_t ep_in;
50 uint8_t ep_out;
51
53
54 // Endpoint descriptor use to open/close when receiving SetInterface
55 // TODO since configuration descriptor may not be long-lived memory, we should
56 // keep a copy of endpoint attribute instead
57 uint8_t const * ecm_desc_epdata;
58
60
61#define CFG_TUD_NET_PACKET_PREFIX_LEN sizeof(rndis_data_packet_t)
62#define CFG_TUD_NET_PACKET_SUFFIX_LEN 0
63
64CFG_TUD_MEM_SECTION CFG_TUSB_MEM_ALIGN tu_static
65uint8_t received[CFG_TUD_NET_PACKET_PREFIX_LEN + CFG_TUD_NET_MTU + CFG_TUD_NET_PACKET_PREFIX_LEN];
66
67CFG_TUD_MEM_SECTION CFG_TUSB_MEM_ALIGN tu_static
68uint8_t transmitted[CFG_TUD_NET_PACKET_PREFIX_LEN + CFG_TUD_NET_MTU + CFG_TUD_NET_PACKET_PREFIX_LEN];
69
71{
73 uint32_t downlink, uplink;
74};
75
76tu_static const struct ecm_notify_struct ecm_notify_nc =
77{
78 .header = {
79 .bmRequestType = 0xA1,
80 .bRequest = 0 /* NETWORK_CONNECTION aka NetworkConnection */,
81 .wValue = 1 /* Connected */,
82 .wLength = 0,
83 },
84};
85
86tu_static const struct ecm_notify_struct ecm_notify_csc =
87{
88 .header = {
89 .bmRequestType = 0xA1,
90 .bRequest = 0x2A /* CONNECTION_SPEED_CHANGE aka ConnectionSpeedChange */,
91 .wLength = 8,
92 },
93 .downlink = 9728000,
94 .uplink = 9728000,
95};
96
97// TODO remove CFG_TUD_MEM_SECTION, control internal buffer is already in this special section
98CFG_TUD_MEM_SECTION CFG_TUSB_MEM_ALIGN tu_static union
99{
100 uint8_t rndis_buf[120];
103
104//--------------------------------------------------------------------+
105// INTERNAL OBJECT & FUNCTION DECLARATION
106//--------------------------------------------------------------------+
107// TODO remove CFG_TUD_MEM_SECTION
108CFG_TUD_MEM_SECTION tu_static netd_interface_t _netd_itf;
109
110tu_static bool can_xmit;
111
113{
115}
116
117static void do_in_xfer(uint8_t *buf, uint16_t len)
118{
119 can_xmit = false;
120 usbd_edpt_xfer(0, _netd_itf.ep_in, buf, len);
121}
122
123void netd_report(uint8_t *buf, uint16_t len)
124{
125 uint8_t const rhport = 0;
126
127 // skip if previous report not yet acknowledged by host
128 if ( usbd_edpt_busy(rhport, _netd_itf.ep_notif) ) return;
129 usbd_edpt_xfer(rhport, _netd_itf.ep_notif, buf, len);
130}
131
132//--------------------------------------------------------------------+
133// USBD Driver API
134//--------------------------------------------------------------------+
135void netd_init(void) {
136 tu_memclr(&_netd_itf, sizeof(_netd_itf));
137}
138
139bool netd_deinit(void) {
140 return true;
141}
142
143void netd_reset(uint8_t rhport)
144{
145 (void) rhport;
146
147 netd_init();
148}
149
150uint16_t netd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
151{
152 bool const is_rndis = (TUD_RNDIS_ITF_CLASS == itf_desc->bInterfaceClass &&
153 TUD_RNDIS_ITF_SUBCLASS == itf_desc->bInterfaceSubClass &&
154 TUD_RNDIS_ITF_PROTOCOL == itf_desc->bInterfaceProtocol);
155
156 bool const is_ecm = (TUSB_CLASS_CDC == itf_desc->bInterfaceClass &&
157 CDC_COMM_SUBCLASS_ETHERNET_CONTROL_MODEL == itf_desc->bInterfaceSubClass &&
158 0x00 == itf_desc->bInterfaceProtocol);
159
160 TU_VERIFY(is_rndis || is_ecm, 0);
161
162 // confirm interface hasn't already been allocated
163 TU_ASSERT(0 == _netd_itf.ep_notif, 0);
164
165 // sanity check the descriptor
166 _netd_itf.ecm_mode = is_ecm;
167
168 //------------- Management Interface -------------//
170
171 uint16_t drv_len = sizeof(tusb_desc_interface_t);
172 uint8_t const * p_desc = tu_desc_next( itf_desc );
173
174 // Communication Functional Descriptors
175 while ( TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len )
176 {
177 drv_len += tu_desc_len(p_desc);
178 p_desc = tu_desc_next(p_desc);
179 }
180
181 // notification endpoint (if any)
182 if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) )
183 {
184 TU_ASSERT( usbd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc), 0 );
185
186 _netd_itf.ep_notif = ((tusb_desc_endpoint_t const *) p_desc)->bEndpointAddress;
187
188 drv_len += tu_desc_len(p_desc);
189 p_desc = tu_desc_next(p_desc);
190 }
191
192 //------------- Data Interface -------------//
193 // - RNDIS Data followed immediately by a pair of endpoints
194 // - CDC-ECM data interface has 2 alternate settings
195 // - 0 : zero endpoints for inactive (default)
196 // - 1 : IN & OUT endpoints for active networking
197 TU_ASSERT(TUSB_DESC_INTERFACE == tu_desc_type(p_desc), 0);
198
199 do
200 {
201 tusb_desc_interface_t const * data_itf_desc = (tusb_desc_interface_t const *) p_desc;
202 TU_ASSERT(TUSB_CLASS_CDC_DATA == data_itf_desc->bInterfaceClass, 0);
203
204 drv_len += tu_desc_len(p_desc);
205 p_desc = tu_desc_next(p_desc);
206 }while( _netd_itf.ecm_mode && (TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) && (drv_len <= max_len) );
207
208 // Pair of endpoints
209 TU_ASSERT(TUSB_DESC_ENDPOINT == tu_desc_type(p_desc), 0);
210
211 if ( _netd_itf.ecm_mode )
212 {
213 // ECM by default is in-active, save the endpoint attribute
214 // to open later when received setInterface
215 _netd_itf.ecm_desc_epdata = p_desc;
216 }else
217 {
218 // Open endpoint pair for RNDIS
219 TU_ASSERT( usbd_open_edpt_pair(rhport, p_desc, 2, TUSB_XFER_BULK, &_netd_itf.ep_out, &_netd_itf.ep_in), 0 );
220
222
223 // we are ready to transmit a packet
224 can_xmit = true;
225
226 // prepare for incoming packets
228 }
229
230 drv_len += 2*sizeof(tusb_desc_endpoint_t);
231
232 return drv_len;
233}
234
235static void ecm_report(bool nc)
236{
237 notify.ecm_buf = (nc) ? ecm_notify_nc : ecm_notify_csc;
238 notify.ecm_buf.header.wIndex = _netd_itf.itf_num;
239 netd_report((uint8_t *)&notify.ecm_buf, (nc) ? sizeof(notify.ecm_buf.header) : sizeof(notify.ecm_buf));
240}
241
242// Invoked when a control transfer occurred on an interface of this class
243// Driver response accordingly to the request and the transfer stage (setup/data/ack)
244// return false to stall control endpoint (e.g unsupported request)
245bool netd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
246{
247 if ( stage == CONTROL_STAGE_SETUP )
248 {
249 switch ( request->bmRequestType_bit.type )
250 {
252 switch ( request->bRequest )
253 {
255 {
256 uint8_t const req_itfnum = (uint8_t) request->wIndex;
257 TU_VERIFY(_netd_itf.itf_num+1 == req_itfnum);
258
260 }
261 break;
262
264 {
265 uint8_t const req_itfnum = (uint8_t) request->wIndex;
266 uint8_t const req_alt = (uint8_t) request->wValue;
267
268 // Only valid for Data Interface with Alternate is either 0 or 1
269 TU_VERIFY(_netd_itf.itf_num+1 == req_itfnum && req_alt < 2);
270
271 // ACM-ECM only: qequest to enable/disable network activities
272 TU_VERIFY(_netd_itf.ecm_mode);
273
274 _netd_itf.itf_data_alt = req_alt;
275
277 {
278 // TODO since we don't actually close endpoint
279 // hack here to not re-open it
280 if ( _netd_itf.ep_in == 0 && _netd_itf.ep_out == 0 )
281 {
282 TU_ASSERT(_netd_itf.ecm_desc_epdata);
284
285 // TODO should be merge with RNDIS's after endpoint opened
286 // Also should have opposite callback for application to disable network !!
288 can_xmit = true; // we are ready to transmit a packet
289 tud_network_recv_renew(); // prepare for incoming packets
290 }
291 }else
292 {
293 // TODO close the endpoint pair
294 // For now pretend that we did, this should have no harm since host won't try to
295 // communicate with the endpoints again
296 // _netd_itf.ep_in = _netd_itf.ep_out = 0
297 }
298
300 }
301 break;
302
303 // unsupported request
304 default: return false;
305 }
306 break;
307
309 TU_VERIFY (_netd_itf.itf_num == request->wIndex);
310
312 {
313 /* the only required CDC-ECM Management Element Request is SetEthernetPacketFilter */
314 if (0x43 /* SET_ETHERNET_PACKET_FILTER */ == request->bRequest)
315 {
316 tud_control_xfer(rhport, request, NULL, 0);
317 ecm_report(true);
318 }
319 }
320 else
321 {
322 if (request->bmRequestType_bit.direction == TUSB_DIR_IN)
323 {
324 rndis_generic_msg_t *rndis_msg = (rndis_generic_msg_t *) ((void*) notify.rndis_buf);
325 uint32_t msglen = tu_le32toh(rndis_msg->MessageLength);
326 TU_ASSERT(msglen <= sizeof(notify.rndis_buf));
327 tud_control_xfer(rhport, request, notify.rndis_buf, (uint16_t) msglen);
328 }
329 else
330 {
331 tud_control_xfer(rhport, request, notify.rndis_buf, (uint16_t) sizeof(notify.rndis_buf));
332 }
333 }
334 break;
335
336 // unsupported request
337 default: return false;
338 }
339 }
340 else if ( stage == CONTROL_STAGE_DATA )
341 {
342 // Handle RNDIS class control OUT only
344 request->bmRequestType_bit.direction == TUSB_DIR_OUT &&
346 {
347 if ( !_netd_itf.ecm_mode )
348 {
350 }
351 }
352 }
353
354 return true;
355}
356
357static void handle_incoming_packet(uint32_t len)
358{
359 uint8_t *pnt = received;
360 uint32_t size = 0;
361
363 {
364 size = len;
365 }
366 else
367 {
368 rndis_data_packet_t *r = (rndis_data_packet_t *) ((void*) pnt);
369 if (len >= sizeof(rndis_data_packet_t))
370 if ( (r->MessageType == REMOTE_NDIS_PACKET_MSG) && (r->MessageLength <= len))
371 if ( (r->DataOffset + offsetof(rndis_data_packet_t, DataOffset) + r->DataLength) <= len)
372 {
373 pnt = &received[r->DataOffset + offsetof(rndis_data_packet_t, DataOffset)];
374 size = r->DataLength;
375 }
376 }
377
378 if (!tud_network_recv_cb(pnt, (uint16_t) size))
379 {
380 /* if a buffer was never handled by user code, we must renew on the user's behalf */
382 }
383}
384
385bool netd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
386{
387 (void) rhport;
388 (void) result;
389
390 /* new packet received */
391 if ( ep_addr == _netd_itf.ep_out )
392 {
393 handle_incoming_packet(xferred_bytes);
394 }
395
396 /* data transmission finished */
397 if ( ep_addr == _netd_itf.ep_in )
398 {
399 /* TinyUSB requires the class driver to implement ZLP (since ZLP usage is class-specific) */
400
401 if ( xferred_bytes && (0 == (xferred_bytes % CFG_TUD_NET_ENDPOINT_SIZE)) )
402 {
403 do_in_xfer(NULL, 0); /* a ZLP is needed */
404 }
405 else
406 {
407 /* we're finally finished */
408 can_xmit = true;
409 }
410 }
411
412 if ( _netd_itf.ecm_mode && (ep_addr == _netd_itf.ep_notif) )
413 {
414 if (sizeof(notify.ecm_buf.header) == xferred_bytes) ecm_report(false);
415 }
416
417 return true;
418}
419
420bool tud_network_can_xmit(uint16_t size)
421{
422 (void)size;
423
424 return can_xmit;
425}
426
427void tud_network_xmit(void *ref, uint16_t arg)
428{
429 uint8_t *data;
430 uint16_t len;
431
432 if (!can_xmit)
433 return;
434
435 len = (_netd_itf.ecm_mode) ? 0 : CFG_TUD_NET_PACKET_PREFIX_LEN;
436 data = transmitted + len;
437
438 len += tud_network_xmit_cb(data, ref, arg);
439
440 if (!_netd_itf.ecm_mode)
441 {
442 rndis_data_packet_t *hdr = (rndis_data_packet_t *) ((void*) transmitted);
443 memset(hdr, 0, sizeof(rndis_data_packet_t));
444 hdr->MessageType = REMOTE_NDIS_PACKET_MSG;
445 hdr->MessageLength = len;
446 hdr->DataOffset = sizeof(rndis_data_packet_t) - offsetof(rndis_data_packet_t, DataOffset);
447 hdr->DataLength = len - sizeof(rndis_data_packet_t);
448 }
449
451}
452
453#endif
static struct @612 data
void netd_reset(uint8_t rhport)
CFG_TUD_MEM_SECTION CFG_TUSB_MEM_ALIGN tu_static uint8_t transmitted[CFG_TUD_NET_PACKET_PREFIX_LEN+CFG_TUD_NET_MTU+CFG_TUD_NET_PACKET_PREFIX_LEN]
void netd_report(uint8_t *buf, uint16_t len)
CFG_TUD_MEM_SECTION tu_static netd_interface_t _netd_itf
uint16_t netd_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len)
bool netd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
static void ecm_report(bool nc)
CFG_TUD_MEM_SECTION CFG_TUSB_MEM_ALIGN tu_static union @45 notify
tu_static const struct ecm_notify_struct ecm_notify_csc
void tud_network_xmit(void *ref, uint16_t arg)
void rndis_class_set_handler(uint8_t *data, int size)
static void handle_incoming_packet(uint32_t len)
void tud_network_recv_renew(void)
bool netd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const *request)
struct ecm_notify_struct ecm_buf
bool netd_deinit(void)
static void do_in_xfer(uint8_t *buf, uint16_t len)
uint8_t rndis_buf[120]
CFG_TUD_MEM_SECTION CFG_TUSB_MEM_ALIGN tu_static uint8_t received[CFG_TUD_NET_PACKET_PREFIX_LEN+CFG_TUD_NET_MTU+CFG_TUD_NET_PACKET_PREFIX_LEN]
void netd_init(void)
tu_static bool can_xmit
tu_static const struct ecm_notify_struct ecm_notify_nc
bool tud_network_can_xmit(uint16_t size)
bool tud_network_recv_cb(const uint8_t *src, uint16_t size)
void tud_network_init_cb(void)
uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg)
AUDIO Channel Cluster Descriptor (4.1)
Definition: audio.h:647
uint16_t wValue
Definition: audio.h:934
struct TU_ATTR_PACKED::@16::TU_ATTR_PACKED bmRequestType_bit
uint16_t wLength
Definition: audio.h:840
uint16_t wIndex
Definition: audio.h:943
uint8_t bInterfaceClass
Class code (assigned by the USB-IF).
Definition: tusb_types.h:349
uint8_t bInterfaceSubClass
Subclass code (assigned by the USB-IF). These codes are qualified by the value of the bInterfaceCla...
Definition: tusb_types.h:350
uint8_t bmRequestType
Definition: audio.h:828
uint8_t bRequest
Request type audio_cs_req_t.
Definition: audio.h:831
uint8_t bInterfaceProtocol
Protocol code (assigned by the USB). These codes are qualified by the value of the bInterfaceClass ...
Definition: tusb_types.h:351
uint8_t bInterfaceNumber
Number of this interface. Zero-based value identifying the index in the array of concurrent interface...
Definition: tusb_types.h:346
tusb_control_request_t header
uint8_t const * ecm_desc_epdata
@ TUSB_DIR_IN
Definition: tusb_types.h:67
@ TUSB_DIR_OUT
Definition: tusb_types.h:66
@ TUSB_REQ_SET_INTERFACE
Definition: tusb_types.h:133
@ TUSB_REQ_GET_INTERFACE
Definition: tusb_types.h:132
@ TUSB_CLASS_CDC
Definition: tusb_types.h:161
@ TUSB_CLASS_CDC_DATA
Definition: tusb_types.h:169
static TU_ATTR_ALWAYS_INLINE uint8_t tu_desc_len(void const *desc)
Definition: tusb_types.h:542
xfer_result_t
Definition: tusb_types.h:236
@ TUSB_XFER_BULK
Definition: tusb_types.h:61
struct TU_ATTR_PACKED tusb_desc_endpoint_t
USB Endpoint Descriptor.
@ CONTROL_STAGE_DATA
Definition: tusb_types.h:269
@ CONTROL_STAGE_SETUP
Definition: tusb_types.h:268
static TU_ATTR_ALWAYS_INLINE uint8_t tu_desc_type(void const *desc)
Definition: tusb_types.h:537
@ TUSB_REQ_TYPE_STANDARD
Definition: tusb_types.h:144
@ TUSB_REQ_TYPE_CLASS
Definition: tusb_types.h:145
struct TU_ATTR_PACKED tusb_desc_interface_t
USB Interface Descriptor.
static TU_ATTR_ALWAYS_INLINE uint8_t const * tu_desc_next(void const *desc)
Definition: tusb_types.h:531
@ TUSB_DESC_CS_INTERFACE
Definition: tusb_types.h:114
@ TUSB_DESC_ENDPOINT
Definition: tusb_types.h:97
@ TUSB_DESC_INTERFACE
Definition: tusb_types.h:96
bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t *buffer, uint16_t total_bytes)
Definition: usbd.c:1309
bool usbd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const *desc_ep)
Definition: usbd.c:1277
bool usbd_edpt_busy(uint8_t rhport, uint8_t ep_addr)
Definition: usbd.c:1376
bool usbd_open_edpt_pair(uint8_t rhport, uint8_t const *p_desc, uint8_t ep_count, uint8_t xfer_type, uint8_t *ep_out, uint8_t *ep_in)
Definition: usbd.c:1238
bool tud_control_xfer(uint8_t rhport, tusb_control_request_t const *request, void *buffer, uint16_t len)
Definition: usbd_control.c:111
bool tud_control_status(uint8_t rhport, tusb_control_request_t const *request)
Definition: usbd_control.c:81
CFG_TUH_MEM_ALIGN tusb_control_request_t request
Definition: usbh.c:259
volatile uint8_t stage
Definition: usbh.c:265