Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 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 ( TUSB_OPT_DEVICE_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 | |
| 38 | void rndis_class_set_handler(uint8_t *data, int size); /* found in ./misc/networking/rndis_reports.c */ |
| 39 | |
| 40 | //--------------------------------------------------------------------+ |
| 41 | // MACRO CONSTANT TYPEDEF |
| 42 | //--------------------------------------------------------------------+ |
| 43 | typedef 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 | |
| 52 | bool ecm_mode; |
| 53 | |
| 54 | // Endpoint descriptor use to open/close when receving 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 | |
| 59 | } netd_interface_t; |
| 60 | |
| 61 | #define CFG_TUD_NET_PACKET_PREFIX_LEN sizeof(rndis_data_packet_t) |
| 62 | #define CFG_TUD_NET_PACKET_SUFFIX_LEN 0 |
| 63 | |
| 64 | CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN static uint8_t received[CFG_TUD_NET_PACKET_PREFIX_LEN + CFG_TUD_NET_MTU + CFG_TUD_NET_PACKET_PREFIX_LEN]; |
| 65 | CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN static uint8_t transmitted[CFG_TUD_NET_PACKET_PREFIX_LEN + CFG_TUD_NET_MTU + CFG_TUD_NET_PACKET_PREFIX_LEN]; |
| 66 | |
| 67 | struct ecm_notify_struct |
| 68 | { |
| 69 | tusb_control_request_t header; |
| 70 | uint32_t downlink, uplink; |
| 71 | }; |
| 72 | |
| 73 | static const struct ecm_notify_struct ecm_notify_nc = |
| 74 | { |
| 75 | .header = { |
| 76 | .bmRequestType = 0xA1, |
| 77 | .bRequest = 0 /* NETWORK_CONNECTION aka NetworkConnection */, |
| 78 | .wValue = 1 /* Connected */, |
| 79 | .wLength = 0, |
| 80 | }, |
| 81 | }; |
| 82 | |
| 83 | static const struct ecm_notify_struct ecm_notify_csc = |
| 84 | { |
| 85 | .header = { |
| 86 | .bmRequestType = 0xA1, |
| 87 | .bRequest = 0x2A /* CONNECTION_SPEED_CHANGE aka ConnectionSpeedChange */, |
| 88 | .wLength = 8, |
| 89 | }, |
| 90 | .downlink = 9728000, |
| 91 | .uplink = 9728000, |
| 92 | }; |
| 93 | |
| 94 | // TODO remove CFG_TUSB_MEM_SECTION, control internal buffer is already in this special section |
| 95 | CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN static union |
| 96 | { |
| 97 | uint8_t rndis_buf[120]; |
| 98 | struct ecm_notify_struct ecm_buf; |
| 99 | } notify; |
| 100 | |
| 101 | //--------------------------------------------------------------------+ |
| 102 | // INTERNAL OBJECT & FUNCTION DECLARATION |
| 103 | //--------------------------------------------------------------------+ |
| 104 | // TODO remove CFG_TUSB_MEM_SECTION |
| 105 | CFG_TUSB_MEM_SECTION static netd_interface_t _netd_itf; |
| 106 | |
| 107 | static bool can_xmit; |
| 108 | |
| 109 | void tud_network_recv_renew(void) |
| 110 | { |
| 111 | usbd_edpt_xfer(TUD_OPT_RHPORT, _netd_itf.ep_out, received, sizeof(received)); |
| 112 | } |
| 113 | |
| 114 | static void do_in_xfer(uint8_t *buf, uint16_t len) |
| 115 | { |
| 116 | can_xmit = false; |
| 117 | usbd_edpt_xfer(TUD_OPT_RHPORT, _netd_itf.ep_in, buf, len); |
| 118 | } |
| 119 | |
| 120 | void netd_report(uint8_t *buf, uint16_t len) |
| 121 | { |
| 122 | // skip if previous report not yet acknowledged by host |
| 123 | if ( usbd_edpt_busy(TUD_OPT_RHPORT, _netd_itf.ep_notif) ) return; |
| 124 | usbd_edpt_xfer(TUD_OPT_RHPORT, _netd_itf.ep_notif, buf, len); |
| 125 | } |
| 126 | |
| 127 | //--------------------------------------------------------------------+ |
| 128 | // USBD Driver API |
| 129 | //--------------------------------------------------------------------+ |
| 130 | void netd_init(void) |
| 131 | { |
| 132 | tu_memclr(&_netd_itf, sizeof(_netd_itf)); |
| 133 | } |
| 134 | |
| 135 | void netd_reset(uint8_t rhport) |
| 136 | { |
| 137 | (void) rhport; |
| 138 | |
| 139 | netd_init(); |
| 140 | } |
| 141 | |
| 142 | uint16_t netd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) |
| 143 | { |
| 144 | bool const is_rndis = (TUD_RNDIS_ITF_CLASS == itf_desc->bInterfaceClass && |
| 145 | TUD_RNDIS_ITF_SUBCLASS == itf_desc->bInterfaceSubClass && |
| 146 | TUD_RNDIS_ITF_PROTOCOL == itf_desc->bInterfaceProtocol); |
| 147 | |
| 148 | bool const is_ecm = (TUSB_CLASS_CDC == itf_desc->bInterfaceClass && |
| 149 | CDC_COMM_SUBCLASS_ETHERNET_CONTROL_MODEL == itf_desc->bInterfaceSubClass && |
| 150 | 0x00 == itf_desc->bInterfaceProtocol); |
| 151 | |
| 152 | TU_VERIFY(is_rndis || is_ecm, 0); |
| 153 | |
| 154 | // confirm interface hasn't already been allocated |
| 155 | TU_ASSERT(0 == _netd_itf.ep_notif, 0); |
| 156 | |
| 157 | // sanity check the descriptor |
| 158 | _netd_itf.ecm_mode = is_ecm; |
| 159 | |
| 160 | //------------- Management Interface -------------// |
| 161 | _netd_itf.itf_num = itf_desc->bInterfaceNumber; |
| 162 | |
| 163 | uint16_t drv_len = sizeof(tusb_desc_interface_t); |
| 164 | uint8_t const * p_desc = tu_desc_next( itf_desc ); |
| 165 | |
| 166 | // Communication Functional Descriptors |
| 167 | while ( TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len ) |
| 168 | { |
| 169 | drv_len += tu_desc_len(p_desc); |
| 170 | p_desc = tu_desc_next(p_desc); |
| 171 | } |
| 172 | |
| 173 | // notification endpoint (if any) |
| 174 | if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) ) |
| 175 | { |
| 176 | TU_ASSERT( usbd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc), 0 ); |
| 177 | |
| 178 | _netd_itf.ep_notif = ((tusb_desc_endpoint_t const *) p_desc)->bEndpointAddress; |
| 179 | |
| 180 | drv_len += tu_desc_len(p_desc); |
| 181 | p_desc = tu_desc_next(p_desc); |
| 182 | } |
| 183 | |
| 184 | //------------- Data Interface -------------// |
| 185 | // - RNDIS Data followed immediately by a pair of endpoints |
| 186 | // - CDC-ECM data interface has 2 alternate settings |
| 187 | // - 0 : zero endpoints for inactive (default) |
| 188 | // - 1 : IN & OUT endpoints for active networking |
| 189 | TU_ASSERT(TUSB_DESC_INTERFACE == tu_desc_type(p_desc), 0); |
| 190 | |
| 191 | do |
| 192 | { |
| 193 | tusb_desc_interface_t const * data_itf_desc = (tusb_desc_interface_t const *) p_desc; |
| 194 | TU_ASSERT(TUSB_CLASS_CDC_DATA == data_itf_desc->bInterfaceClass, 0); |
| 195 | |
| 196 | drv_len += tu_desc_len(p_desc); |
| 197 | p_desc = tu_desc_next(p_desc); |
| 198 | }while( _netd_itf.ecm_mode && (TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) && (drv_len <= max_len) ); |
| 199 | |
| 200 | // Pair of endpoints |
| 201 | TU_ASSERT(TUSB_DESC_ENDPOINT == tu_desc_type(p_desc), 0); |
| 202 | |
| 203 | if ( _netd_itf.ecm_mode ) |
| 204 | { |
| 205 | // ECM by default is in-active, save the endpoint attribute |
| 206 | // to open later when received setInterface |
| 207 | _netd_itf.ecm_desc_epdata = p_desc; |
| 208 | }else |
| 209 | { |
| 210 | // Open endpoint pair for RNDIS |
| 211 | TU_ASSERT( usbd_open_edpt_pair(rhport, p_desc, 2, TUSB_XFER_BULK, &_netd_itf.ep_out, &_netd_itf.ep_in), 0 ); |
| 212 | |
| 213 | tud_network_init_cb(); |
| 214 | |
| 215 | // we are ready to transmit a packet |
| 216 | can_xmit = true; |
| 217 | |
| 218 | // prepare for incoming packets |
| 219 | tud_network_recv_renew(); |
| 220 | } |
| 221 | |
| 222 | drv_len += 2*sizeof(tusb_desc_endpoint_t); |
| 223 | |
| 224 | return drv_len; |
| 225 | } |
| 226 | |
| 227 | static void ecm_report(bool nc) |
| 228 | { |
| 229 | notify.ecm_buf = (nc) ? ecm_notify_nc : ecm_notify_csc; |
| 230 | notify.ecm_buf.header.wIndex = _netd_itf.itf_num; |
| 231 | netd_report((uint8_t *)¬ify.ecm_buf, (nc) ? sizeof(notify.ecm_buf.header) : sizeof(notify.ecm_buf)); |
| 232 | } |
| 233 | |
| 234 | // Invoked when a control transfer occurred on an interface of this class |
| 235 | // Driver response accordingly to the request and the transfer stage (setup/data/ack) |
| 236 | // return false to stall control endpoint (e.g unsupported request) |
| 237 | bool netd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) |
| 238 | { |
| 239 | if ( stage == CONTROL_STAGE_SETUP ) |
| 240 | { |
| 241 | switch ( request->bmRequestType_bit.type ) |
| 242 | { |
| 243 | case TUSB_REQ_TYPE_STANDARD: |
| 244 | switch ( request->bRequest ) |
| 245 | { |
| 246 | case TUSB_REQ_GET_INTERFACE: |
| 247 | { |
| 248 | uint8_t const req_itfnum = (uint8_t) request->wIndex; |
| 249 | TU_VERIFY(_netd_itf.itf_num+1 == req_itfnum); |
| 250 | |
| 251 | tud_control_xfer(rhport, request, &_netd_itf.itf_data_alt, 1); |
| 252 | } |
| 253 | break; |
| 254 | |
| 255 | case TUSB_REQ_SET_INTERFACE: |
| 256 | { |
| 257 | uint8_t const req_itfnum = (uint8_t) request->wIndex; |
| 258 | uint8_t const req_alt = (uint8_t) request->wValue; |
| 259 | |
| 260 | // Only valid for Data Interface with Alternate is either 0 or 1 |
| 261 | TU_VERIFY(_netd_itf.itf_num+1 == req_itfnum && req_alt < 2); |
| 262 | |
| 263 | // ACM-ECM only: qequest to enable/disable network activities |
| 264 | TU_VERIFY(_netd_itf.ecm_mode); |
| 265 | |
| 266 | _netd_itf.itf_data_alt = req_alt; |
| 267 | |
| 268 | if ( _netd_itf.itf_data_alt ) |
| 269 | { |
| 270 | // TODO since we don't actually close endpoint |
| 271 | // hack here to not re-open it |
| 272 | if ( _netd_itf.ep_in == 0 && _netd_itf.ep_out == 0 ) |
| 273 | { |
| 274 | TU_ASSERT(_netd_itf.ecm_desc_epdata); |
| 275 | TU_ASSERT( usbd_open_edpt_pair(rhport, _netd_itf.ecm_desc_epdata, 2, TUSB_XFER_BULK, &_netd_itf.ep_out, &_netd_itf.ep_in) ); |
| 276 | |
| 277 | // TODO should be merge with RNDIS's after endpoint opened |
| 278 | // Also should have opposite callback for application to disable network !! |
| 279 | tud_network_init_cb(); |
| 280 | can_xmit = true; // we are ready to transmit a packet |
| 281 | tud_network_recv_renew(); // prepare for incoming packets |
| 282 | } |
| 283 | }else |
| 284 | { |
| 285 | // TODO close the endpoint pair |
| 286 | // For now pretend that we did, this should have no harm since host won't try to |
| 287 | // communicate with the endpoints again |
| 288 | // _netd_itf.ep_in = _netd_itf.ep_out = 0 |
| 289 | } |
| 290 | |
| 291 | tud_control_status(rhport, request); |
| 292 | } |
| 293 | break; |
| 294 | |
| 295 | // unsupported request |
| 296 | default: return false; |
| 297 | } |
| 298 | break; |
| 299 | |
| 300 | case TUSB_REQ_TYPE_CLASS: |
| 301 | TU_VERIFY (_netd_itf.itf_num == request->wIndex); |
| 302 | |
| 303 | if (_netd_itf.ecm_mode) |
| 304 | { |
| 305 | /* the only required CDC-ECM Management Element Request is SetEthernetPacketFilter */ |
| 306 | if (0x43 /* SET_ETHERNET_PACKET_FILTER */ == request->bRequest) |
| 307 | { |
| 308 | tud_control_xfer(rhport, request, NULL, 0); |
| 309 | ecm_report(true); |
| 310 | } |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | if (request->bmRequestType_bit.direction == TUSB_DIR_IN) |
| 315 | { |
| 316 | rndis_generic_msg_t *rndis_msg = (rndis_generic_msg_t *) ((void*) notify.rndis_buf); |
| 317 | uint32_t msglen = tu_le32toh(rndis_msg->MessageLength); |
| 318 | TU_ASSERT(msglen <= sizeof(notify.rndis_buf)); |
| 319 | tud_control_xfer(rhport, request, notify.rndis_buf, msglen); |
| 320 | } |
| 321 | else |
| 322 | { |
| 323 | tud_control_xfer(rhport, request, notify.rndis_buf, sizeof(notify.rndis_buf)); |
| 324 | } |
| 325 | } |
| 326 | break; |
| 327 | |
| 328 | // unsupported request |
| 329 | default: return false; |
| 330 | } |
| 331 | } |
| 332 | else if ( stage == CONTROL_STAGE_DATA ) |
| 333 | { |
| 334 | // Handle RNDIS class control OUT only |
| 335 | if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS && |
| 336 | request->bmRequestType_bit.direction == TUSB_DIR_OUT && |
| 337 | _netd_itf.itf_num == request->wIndex) |
| 338 | { |
| 339 | if ( !_netd_itf.ecm_mode ) |
| 340 | { |
| 341 | rndis_class_set_handler(notify.rndis_buf, request->wLength); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | return true; |
| 347 | } |
| 348 | |
| 349 | static void handle_incoming_packet(uint32_t len) |
| 350 | { |
| 351 | uint8_t *pnt = received; |
| 352 | uint32_t size = 0; |
| 353 | |
| 354 | if (_netd_itf.ecm_mode) |
| 355 | { |
| 356 | size = len; |
| 357 | } |
| 358 | else |
| 359 | { |
| 360 | rndis_data_packet_t *r = (rndis_data_packet_t *) ((void*) pnt); |
| 361 | if (len >= sizeof(rndis_data_packet_t)) |
| 362 | if ( (r->MessageType == REMOTE_NDIS_PACKET_MSG) && (r->MessageLength <= len)) |
| 363 | if ( (r->DataOffset + offsetof(rndis_data_packet_t, DataOffset) + r->DataLength) <= len) |
| 364 | { |
| 365 | pnt = &received[r->DataOffset + offsetof(rndis_data_packet_t, DataOffset)]; |
| 366 | size = r->DataLength; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if (!tud_network_recv_cb(pnt, size)) |
| 371 | { |
| 372 | /* if a buffer was never handled by user code, we must renew on the user's behalf */ |
| 373 | tud_network_recv_renew(); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | bool netd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) |
| 378 | { |
| 379 | (void) rhport; |
| 380 | (void) result; |
| 381 | |
| 382 | /* new packet received */ |
| 383 | if ( ep_addr == _netd_itf.ep_out ) |
| 384 | { |
| 385 | handle_incoming_packet(xferred_bytes); |
| 386 | } |
| 387 | |
| 388 | /* data transmission finished */ |
| 389 | if ( ep_addr == _netd_itf.ep_in ) |
| 390 | { |
| 391 | /* TinyUSB requires the class driver to implement ZLP (since ZLP usage is class-specific) */ |
| 392 | |
| 393 | if ( xferred_bytes && (0 == (xferred_bytes % CFG_TUD_NET_ENDPOINT_SIZE)) ) |
| 394 | { |
| 395 | do_in_xfer(NULL, 0); /* a ZLP is needed */ |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | /* we're finally finished */ |
| 400 | can_xmit = true; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | if ( _netd_itf.ecm_mode && (ep_addr == _netd_itf.ep_notif) ) |
| 405 | { |
| 406 | if (sizeof(notify.ecm_buf.header) == xferred_bytes) ecm_report(false); |
| 407 | } |
| 408 | |
| 409 | return true; |
| 410 | } |
| 411 | |
| 412 | bool tud_network_can_xmit(uint16_t size) |
| 413 | { |
| 414 | (void)size; |
| 415 | |
| 416 | return can_xmit; |
| 417 | } |
| 418 | |
| 419 | void tud_network_xmit(void *ref, uint16_t arg) |
| 420 | { |
| 421 | uint8_t *data; |
| 422 | uint16_t len; |
| 423 | |
| 424 | if (!can_xmit) |
| 425 | return; |
| 426 | |
| 427 | len = (_netd_itf.ecm_mode) ? 0 : CFG_TUD_NET_PACKET_PREFIX_LEN; |
| 428 | data = transmitted + len; |
| 429 | |
| 430 | len += tud_network_xmit_cb(data, ref, arg); |
| 431 | |
| 432 | if (!_netd_itf.ecm_mode) |
| 433 | { |
| 434 | rndis_data_packet_t *hdr = (rndis_data_packet_t *) ((void*) transmitted); |
| 435 | memset(hdr, 0, sizeof(rndis_data_packet_t)); |
| 436 | hdr->MessageType = REMOTE_NDIS_PACKET_MSG; |
| 437 | hdr->MessageLength = len; |
| 438 | hdr->DataOffset = sizeof(rndis_data_packet_t) - offsetof(rndis_data_packet_t, DataOffset); |
| 439 | hdr->DataLength = len - sizeof(rndis_data_packet_t); |
| 440 | } |
| 441 | |
| 442 | do_in_xfer(transmitted, len); |
| 443 | } |
| 444 | |
| 445 | #endif |