summaryrefslogtreecommitdiff
path: root/src/class/net/ecm_rndis_device.c
blob: b1ac2e8fd3cd40d54d7f410789fcdb1c4eb96435 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * SPDX-FileCopyrightText: Copyright (c) 2020 Peter Lawrence
 * SPDX-FileCopyrightText: Copyright (c) 2019 Ha Thach (tinyusb.org)
 * SPDX-License-Identifier: MIT
 *
 * This file is part of the TinyUSB stack.
 */

#include "tusb_option.h"

#if ( CFG_TUD_ENABLED && CFG_TUD_ECM_RNDIS )

#include "device/usbd.h"
#include "device/usbd_pvt.h"

#include "net_device.h"
#include "rndis_protocol.h"

#define CFG_TUD_NET_PACKET_PREFIX_LEN sizeof(rndis_data_packet_t)
#define CFG_TUD_NET_PACKET_SUFFIX_LEN 0

#define NETD_PACKET_SIZE  (CFG_TUD_NET_PACKET_PREFIX_LEN + CFG_TUD_NET_MTU + CFG_TUD_NET_PACKET_PREFIX_LEN)
#define NETD_CONTROL_SIZE 120

//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
typedef struct {
  uint8_t itf_num;      // Index number of Management Interface, +1 for Data Interface
  uint8_t itf_data_alt; // Alternate setting of Data Interface. 0 : inactive, 1 : active

  uint8_t ep_in;
  uint8_t ep_out;
  uint16_t ep_size;     // bulk endpoint max packet size (IN and OUT assumed equal)
  uint8_t ep_notif;

  bool ecm_mode;

  // Endpoint descriptor use to open/close when receiving SetInterface
  // TODO since configuration descriptor may not be long-lived memory, we should
  // keep a copy of endpoint attribute instead
  uint8_t const * ecm_desc_epdata;
} netd_interface_t;

typedef struct ecm_notify_struct {
  tusb_control_request_t header;
  uint32_t downlink, uplink;
} ecm_notify_t;

typedef struct {
  TUD_EPBUF_DEF(rx, NETD_PACKET_SIZE);
  TUD_EPBUF_DEF(tx, NETD_PACKET_SIZE);

  TUD_EPBUF_DEF(notify, sizeof(ecm_notify_t));
  TUD_EPBUF_DEF(ctrl, NETD_CONTROL_SIZE);
} netd_epbuf_t;

//--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+
static netd_interface_t _netd_itf;
CFG_TUD_MEM_SECTION static netd_epbuf_t _netd_epbuf;
static bool can_xmit;
static bool ecm_link_is_up = true;  // Store link state for ECM mode

//--------------------------------------------------------------------+
// Weak stubs: invoked if no strong implementation is available
//--------------------------------------------------------------------+
TU_ATTR_WEAK void tud_network_set_packet_filter_cb(uint16_t packet_filter) {
  (void) packet_filter;
}

void tud_network_recv_renew(void) {
  usbd_edpt_xfer(0, _netd_itf.ep_out, _netd_epbuf.rx, NETD_PACKET_SIZE, false);
}

static void do_in_xfer(uint8_t *buf, uint16_t len) {
  can_xmit = false;
  usbd_edpt_xfer(0, _netd_itf.ep_in, buf, len, false);
}

void netd_report(uint8_t *buf, uint16_t len) {
  const uint8_t rhport = 0;
  len = tu_min16(len, sizeof(ecm_notify_t));

  if (!usbd_edpt_claim(rhport, _netd_itf.ep_notif)) {
    TU_LOG1("ECM: Failed to claim notification endpoint\n");
    return;
  }

  memcpy(_netd_epbuf.notify, buf, len);
  usbd_edpt_xfer(rhport, _netd_itf.ep_notif, _netd_epbuf.notify, len, false);
}

//--------------------------------------------------------------------+
// USBD Driver API
//--------------------------------------------------------------------+
void netd_init(void) {
  tu_memclr(&_netd_itf, sizeof(_netd_itf));
}

bool netd_deinit(void) {
  return true;
}

void netd_reset(uint8_t rhport) {
  (void) rhport;
  netd_init();
}

uint16_t netd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) {
  bool const is_rndis = (TUD_RNDIS_ITF_CLASS    == itf_desc->bInterfaceClass    &&
                         TUD_RNDIS_ITF_SUBCLASS == itf_desc->bInterfaceSubClass &&
                         TUD_RNDIS_ITF_PROTOCOL == itf_desc->bInterfaceProtocol);

  bool const is_ecm = (TUSB_CLASS_CDC                           == itf_desc->bInterfaceClass &&
                       CDC_COMM_SUBCLASS_ETHERNET_CONTROL_MODEL == itf_desc->bInterfaceSubClass &&
                       0x00                                     == itf_desc->bInterfaceProtocol);

  TU_VERIFY(is_rndis || is_ecm, 0);

  // confirm interface hasn't already been allocated
  TU_ASSERT(0 == _netd_itf.ep_notif, 0);

  // sanity check the descriptor
  _netd_itf.ecm_mode = is_ecm;

  //------------- Management Interface -------------//
  _netd_itf.itf_num = itf_desc->bInterfaceNumber;

  uint16_t drv_len = sizeof(tusb_desc_interface_t);
  uint8_t const * p_desc = tu_desc_next( itf_desc );

  // Communication Functional Descriptors
  while (TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len) {
    drv_len += tu_desc_len(p_desc);
    p_desc   = tu_desc_next(p_desc);
  }

  // notification endpoint (if any)
  if (TUSB_DESC_ENDPOINT == tu_desc_type(p_desc)) {
    TU_ASSERT(usbd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc), 0);

    _netd_itf.ep_notif = ((tusb_desc_endpoint_t const*)p_desc)->bEndpointAddress;

    drv_len += tu_desc_len(p_desc);
    p_desc = tu_desc_next(p_desc);
  }

  //------------- Data Interface -------------//
  // - RNDIS Data followed immediately by a pair of endpoints
  // - CDC-ECM data interface has 2 alternate settings
  //   - 0 : zero endpoints for inactive (default)
  //   - 1 : IN & OUT endpoints for active networking
  TU_ASSERT(TUSB_DESC_INTERFACE == tu_desc_type(p_desc), 0);

  do {
    tusb_desc_interface_t const * data_itf_desc = (tusb_desc_interface_t const *) p_desc;
    TU_ASSERT(TUSB_CLASS_CDC_DATA == data_itf_desc->bInterfaceClass, 0);

    drv_len += tu_desc_len(p_desc);
    p_desc   = tu_desc_next(p_desc);
  } while (_netd_itf.ecm_mode && (TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) && (drv_len <= max_len));

  // Pair of endpoints
  TU_ASSERT(TUSB_DESC_ENDPOINT == tu_desc_type(p_desc), 0);

  // Save the actual bulk endpoint size (IN and OUT assumed equal)
  _netd_itf.ep_size = tu_edpt_packet_size((tusb_desc_endpoint_t const *) p_desc);

  if (_netd_itf.ecm_mode) {
    // ECM by default is in-active, save the endpoint attribute
    // to open later when received setInterface
    _netd_itf.ecm_desc_epdata = p_desc;
  } else {
    // Open endpoint pair for RNDIS
    TU_ASSERT(usbd_open_edpt_pair(rhport, p_desc, 2, TUSB_XFER_BULK, &_netd_itf.ep_out, &_netd_itf.ep_in), 0);

    // we are ready to transmit a packet
    can_xmit = true;

    // prepare for incoming packets
    tud_network_recv_renew();
  }

  drv_len += 2*sizeof(tusb_desc_endpoint_t);

  return drv_len;
}

static void ecm_report(bool nc) {
  ecm_notify_t ecm_notify_nc = {
    .header = {
      .bmRequestType = 0xA1,
      .bRequest = 0, /* NETWORK_CONNECTION aka NetworkConnection */
      .wValue = ecm_link_is_up ? 1 : 0,   /* Use current link state */
      .wLength = 0,
    },
  };

  const uint32_t link_bps = (tud_speed_get() == TUSB_SPEED_HIGH) ? 480000000U : 12000000U;
  const ecm_notify_t ecm_notify_csc = {
    .header = {
      .bmRequestType = 0xA1,
      .bRequest = 0x2A, /* CONNECTION_SPEED_CHANGE aka ConnectionSpeedChange */
      .wLength = 8,
    },
    .downlink = link_bps,
    .uplink = link_bps,
  };

  ecm_notify_t notify = (nc) ? ecm_notify_nc : ecm_notify_csc;
  notify.header.wIndex = _netd_itf.itf_num;
  netd_report((uint8_t *)&notify, (nc) ? sizeof(notify.header) : sizeof(notify));
}

// Invoked when a control transfer occurred on an interface of this class
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
// return false to stall control endpoint (e.g unsupported request)
bool netd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) {
  if (stage == CONTROL_STAGE_SETUP) {
    switch (request->bmRequestType_bit.type) {
      case TUSB_REQ_TYPE_STANDARD:
        switch (request->bRequest) {
          case TUSB_REQ_GET_INTERFACE: {
            uint8_t const req_itfnum = (uint8_t)request->wIndex;
            TU_VERIFY(_netd_itf.itf_num+1 == req_itfnum);

            tud_control_xfer(rhport, request, &_netd_itf.itf_data_alt, 1);
          }
          break;

          case TUSB_REQ_SET_INTERFACE: {
            uint8_t const req_itfnum = (uint8_t)request->wIndex;
            uint8_t const req_alt = (uint8_t)request->wValue;

            // Only valid for Data Interface with Alternate is either 0 or 1
            TU_VERIFY(_netd_itf.itf_num+1 == req_itfnum && req_alt < 2);

            // ACM-ECM only: qequest to enable/disable network activities
            TU_VERIFY(_netd_itf.ecm_mode);

            _netd_itf.itf_data_alt = req_alt;

            if (_netd_itf.itf_data_alt) {
              // TODO since we don't actually close endpoint
              // hack here to not re-open it
              if (_netd_itf.ep_in == 0 && _netd_itf.ep_out == 0) {
                TU_ASSERT(_netd_itf.ecm_desc_epdata);
                TU_ASSERT(
                  usbd_open_edpt_pair(rhport, _netd_itf.ecm_desc_epdata, 2, TUSB_XFER_BULK, &_netd_itf.ep_out, &
                    _netd_itf.ep_in));

                // TODO should be merge with RNDIS's after endpoint opened
                // Also should have opposite callback for application to disable network !!
                can_xmit = true; // we are ready to transmit a packet
                tud_network_recv_renew(); // prepare for incoming packets
              }
            } else {
              // TODO close the endpoint pair
              // For now pretend that we did, this should have no harm since host won't try to
              // communicate with the endpoints again
              // _netd_itf.ep_in = _netd_itf.ep_out = 0
            }

            tud_control_status(rhport, request);
          }
          break;

          // unsupported request
          default: return false;
        }
        break;

      case TUSB_REQ_TYPE_CLASS:
        TU_VERIFY(_netd_itf.itf_num == request->wIndex);

        if (_netd_itf.ecm_mode) {
          /* the only required CDC-ECM Management Element Request is SetEthernetPacketFilter */
          if (0x43 /* SET_ETHERNET_PACKET_FILTER */ == request->bRequest) {
            tud_network_set_packet_filter_cb(request->wValue);
            tud_control_xfer(rhport, request, NULL, 0);
            // Only send connection notification if link is up
            if (ecm_link_is_up) {
              ecm_report(true);
            }
          }
        } else {
          if (request->bmRequestType_bit.direction == TUSB_DIR_IN) {
            rndis_generic_msg_t* rndis_msg = (rndis_generic_msg_t*)((void*)_netd_epbuf.ctrl);
            uint32_t msglen = tu_le32toh(rndis_msg->MessageLength);
            TU_ASSERT(msglen <= NETD_CONTROL_SIZE);
            tud_control_xfer(rhport, request, _netd_epbuf.ctrl, (uint16_t)msglen);
          } else {
            tud_control_xfer(rhport, request, _netd_epbuf.ctrl, NETD_CONTROL_SIZE);
          }
        }
        break;

      // unsupported request
      default: return false;
    }
  } else if (stage == CONTROL_STAGE_DATA) {
    // Handle RNDIS class control OUT only
    if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS &&
        request->bmRequestType_bit.direction == TUSB_DIR_OUT &&
        _netd_itf.itf_num == request->wIndex) {
      if (!_netd_itf.ecm_mode) {
        rndis_class_set_handler(_netd_epbuf.ctrl, request->wLength);
      }
    }
  }

  return true;
}

static void handle_incoming_packet(uint32_t len) {
  uint8_t* pnt = _netd_epbuf.rx;
  uint32_t size = 0;

  if (_netd_itf.ecm_mode) {
    size = len;
  } else {
    rndis_data_packet_t* r = (rndis_data_packet_t*)((void*)pnt);
    if (len >= sizeof(rndis_data_packet_t)) {
      if ((r->MessageType == REMOTE_NDIS_PACKET_MSG) && (r->MessageLength <= len)) {
        // DataOffset and DataLength are host-controlled; validate the payload window fits
        // within the received data without overflowing the uint32 addition (len >= header)
        const uint32_t hdr = offsetof(rndis_data_packet_t, DataOffset);
        if ((r->DataOffset <= len - hdr) && (r->DataLength <= len - hdr - r->DataOffset)) {
          pnt = &_netd_epbuf.rx[hdr + r->DataOffset];
          size = r->DataLength;
        }
      }
    }
  }

  if (!tud_network_recv_cb(pnt, (uint16_t)size)) {
    /* if a buffer was never handled by user code, we must renew on the user's behalf */
    tud_network_recv_renew();
  }
}

bool netd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
  (void)rhport;
  (void)result;

  /* new packet received */
  if (ep_addr == _netd_itf.ep_out) {
    handle_incoming_packet(xferred_bytes);
  }

  /* data transmission finished */
  if (ep_addr == _netd_itf.ep_in) {
    /* TinyUSB requires the class driver to implement ZLP (since ZLP usage is class-specific) */
    if (xferred_bytes > 0 && 0 == (xferred_bytes & (_netd_itf.ep_size-1))) {
      do_in_xfer(NULL, 0); /* a ZLP is needed */
    } else {
      /* we're finally finished */
      can_xmit = true;
    }
  }

  if (_netd_itf.ecm_mode && (ep_addr == _netd_itf.ep_notif)) {
    // Notification transfer complete - endpoint is now free
    // Don't automatically send speed change notification after link state changes
  }

  return true;
}

bool tud_network_can_xmit(uint16_t size) {
  (void)size;
  return can_xmit;
}

void tud_network_xmit(void *ref, uint16_t arg) {
  if (!can_xmit) {
    return;
  }

  uint16_t len = (_netd_itf.ecm_mode) ? 0 : CFG_TUD_NET_PACKET_PREFIX_LEN;
  uint8_t* data = _netd_epbuf.tx + len;

  len += tud_network_xmit_cb(data, ref, arg);

  if (!_netd_itf.ecm_mode) {
    rndis_data_packet_t *hdr = (rndis_data_packet_t *) ((void*) _netd_epbuf.tx);
    memset(hdr, 0, sizeof(rndis_data_packet_t));
    hdr->MessageType = REMOTE_NDIS_PACKET_MSG;
    hdr->MessageLength = len;
    hdr->DataOffset = sizeof(rndis_data_packet_t) - offsetof(rndis_data_packet_t, DataOffset);
    hdr->DataLength = len - sizeof(rndis_data_packet_t);
  }

  do_in_xfer(_netd_epbuf.tx, len);
}

// Set the network link state (up/down) and notify the host
void tud_network_link_state(uint8_t rhport, bool is_up) {
  (void)rhport;

  if (_netd_itf.ecm_mode) {
    ecm_link_is_up = is_up;

    // For ECM mode, send network connection notification only
    // Don't trigger speed change notification for link state changes
    ecm_notify_t notify = {
      .header = {
        .bmRequestType = 0xA1,
        .bRequest = 0,        /* NETWORK_CONNECTION */
        .wValue = is_up ? 1 : 0,  /* 0 = disconnected, 1 = connected */
        .wLength = 0,
      },
    };
    notify.header.wIndex = _netd_itf.itf_num;
    netd_report((uint8_t *)&notify, sizeof(notify.header));
  } else {
    // For RNDIS mode, we would need to implement RNDIS status indication
    // This is more complex and requires RNDIS_INDICATE_STATUS_MSG
    // For now, RNDIS doesn't support dynamic link state changes
    (void)is_up;
  }
}

#endif