Open FFBoard
Open source force feedback firmware
ncm_device.c File Reference

Go to the source code of this file.

Classes

struct  ncm_interface_t
 

Functions

static void notification_xmit (uint8_t rhport, bool force_next)
 
static void xmit_put_ntb_into_free_list (xmit_ntb_t *free_ntb)
 
static xmit_ntb_txmit_get_free_ntb (void)
 
static void xmit_put_ntb_into_ready_list (xmit_ntb_t *ready_ntb)
 
static xmit_ntb_txmit_get_next_ready_ntb (void)
 
static bool xmit_insert_required_zlp (uint8_t rhport, uint32_t xferred_bytes)
 
static void xmit_start_if_possible (uint8_t rhport)
 
static bool xmit_requested_datagram_fits_into_current_ntb (uint16_t datagram_size)
 
static bool xmit_setup_next_glue_ntb (void)
 
static recv_ntb_trecv_get_free_ntb (void)
 
static recv_ntb_trecv_get_next_ready_ntb (void)
 
static void recv_put_ntb_into_free_list (recv_ntb_t *free_ntb)
 
static void recv_put_ntb_into_ready_list (recv_ntb_t *ready_ntb)
 
static void recv_try_to_start_new_reception (uint8_t rhport)
 
static bool recv_validate_datagram (const recv_ntb_t *ntb, uint32_t len)
 
static void recv_transfer_datagram_to_glue_logic (void)
 
bool tud_network_can_xmit (uint16_t size)
 
void tud_network_xmit (void *ref, uint16_t arg)
 
void tud_network_recv_renew (void)
 
void tud_network_recv_renew_r (uint8_t rhport)
 
void netd_init (void)
 
bool netd_deinit (void)
 
void netd_reset (uint8_t rhport)
 
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)
 
bool netd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const *request)
 

Variables

CFG_TUD_MEM_SECTION CFG_TUD_MEM_ALIGN tu_static ncm_interface_t ncm_interface
 
CFG_TUD_MEM_SECTION CFG_TUD_MEM_ALIGN tu_static const ntb_parameters_t ntb_parameters
 
tu_static struct ncm_notify_t ncm_notify_connected
 
tu_static struct ncm_notify_t ncm_notify_speed_change
 

Function Documentation

◆ netd_control_xfer_cb()

bool netd_control_xfer_cb ( uint8_t  rhport,
uint8_t  stage,
tusb_control_request_t const *  request 
)

Respond to TinyUSB control requests. At startup transmission of notification packets are done here.

Definition at line 863 of file ncm_device.c.

◆ netd_deinit()

bool netd_deinit ( void  )

Deinit driver

Definition at line 756 of file ncm_device.c.

◆ netd_init()

void netd_init ( void  )

Initialize the driver data structures. Might be called several times.

Definition at line 740 of file ncm_device.c.

◆ netd_open()

uint16_t netd_open ( uint8_t  rhport,
tusb_desc_interface_t const *  itf_desc,
uint16_t  max_len 
)

Open the USB interface.

  • parse the USB descriptor TUD_CDC_NCM_DESCRIPTOR for itfnum and endpoints
  • a specific order of elements in the descriptor is tested.
Note
Actually all of the information could be read directly from itf_desc, because the structure and the values are well known. But we do it this way.
Postcondition
  • itf_num set
  • ep_notif, ep_in and ep_out are set
  • USB interface is open

Definition at line 784 of file ncm_device.c.

◆ netd_reset()

void netd_reset ( uint8_t  rhport)

Resets the port. In this driver this is the same as netd_init()

Definition at line 764 of file ncm_device.c.

◆ netd_xfer_cb()

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

Handle TinyUSB requests to process transfer events.

Definition at line 824 of file ncm_device.c.

◆ notification_xmit()

static void notification_xmit ( uint8_t  rhport,
bool  force_next 
)
static

Transmit next notification to the host (if appropriate). Notifications are transferred to the host once during connection setup.

Definition at line 188 of file ncm_device.c.

◆ recv_get_free_ntb()

static recv_ntb_t * recv_get_free_ntb ( void  )
static

Return pointer to an available receive buffer or NULL. Returned buffer (if any) has the size CFG_TUD_NCM_OUT_NTB_MAX_SIZE.

Definition at line 419 of file ncm_device.c.

◆ recv_get_next_ready_ntb()

static recv_ntb_t * recv_get_next_ready_ntb ( void  )
static

Get the next NTB from the ready list (and remove it from the list). If the ready list is empty, return NULL.

Definition at line 436 of file ncm_device.c.

◆ recv_put_ntb_into_free_list()

static void recv_put_ntb_into_free_list ( recv_ntb_t free_ntb)
static

Put NTB into the receiver free list.

Definition at line 450 of file ncm_device.c.

◆ recv_put_ntb_into_ready_list()

static void recv_put_ntb_into_ready_list ( recv_ntb_t ready_ntb)
static

ready_ntb holds a validated NTB, put this buffer into the waiting list.

Definition at line 466 of file ncm_device.c.

◆ recv_transfer_datagram_to_glue_logic()

static void recv_transfer_datagram_to_glue_logic ( void  )
static

Transfer the next (pending) datagram to the glue logic and return receive buffer if empty.

Definition at line 598 of file ncm_device.c.

◆ recv_try_to_start_new_reception()

static void recv_try_to_start_new_reception ( uint8_t  rhport)
static

If possible, start a new reception TinyUSB -> driver.

Definition at line 481 of file ncm_device.c.

◆ recv_validate_datagram()

static bool recv_validate_datagram ( const recv_ntb_t ntb,
uint32_t  len 
)
static

Validate incoming datagram.

Returns
true if valid
Note
ndp16->wNextNdpIndex != 0 is not supported

Definition at line 515 of file ncm_device.c.

◆ tud_network_can_xmit()

bool tud_network_can_xmit ( uint16_t  size)

Check if the glue logic is allowed to call tud_network_xmit(). This function also fetches a next buffer if required, so that tud_network_xmit() is ready for copy and transmission operation.

Definition at line 648 of file ncm_device.c.

◆ tud_network_recv_renew()

void tud_network_recv_renew ( void  )

Keep the receive logic busy and transfer pending packets to the glue logic. Avoid recursive calls due to wrong expectations of the net glue logic, see https://github.com/hathach/tinyusb/issues/2711

Definition at line 699 of file ncm_device.c.

◆ tud_network_recv_renew_r()

void tud_network_recv_renew_r ( uint8_t  rhport)

Same as tud_network_recv_renew() but knows rhport

Definition at line 725 of file ncm_device.c.

◆ tud_network_xmit()

void tud_network_xmit ( void *  ref,
uint16_t  arg 
)

Put a datagram into a waiting NTB. If currently no transmission is started, then initiate transmission.

Definition at line 666 of file ncm_device.c.

◆ xmit_get_free_ntb()

static xmit_ntb_t * xmit_get_free_ntb ( void  )
static

Get an NTB from the free list

Definition at line 239 of file ncm_device.c.

◆ xmit_get_next_ready_ntb()

static xmit_ntb_t * xmit_get_next_ready_ntb ( void  )
static

Get the next NTB from the ready list (and remove it from the list). If the ready list is empty, return NULL.

Definition at line 271 of file ncm_device.c.

◆ xmit_insert_required_zlp()

static bool xmit_insert_required_zlp ( uint8_t  rhport,
uint32_t  xferred_bytes 
)
static

Transmit a ZLP if required

Note
Insertion of the ZLPs is a little bit different then described in the spec. But the below implementation actually works. Don't know if this is a spec or TinyUSB issue.
Precondition
This must be called from netd_xfer_cb() so that ep_in is ready

Definition at line 293 of file ncm_device.c.

◆ xmit_put_ntb_into_free_list()

static void xmit_put_ntb_into_free_list ( xmit_ntb_t free_ntb)
static

Put NTB into the transmitter free list.

Definition at line 220 of file ncm_device.c.

◆ xmit_put_ntb_into_ready_list()

static void xmit_put_ntb_into_ready_list ( xmit_ntb_t ready_ntb)
static

Put a filled NTB into the ready list

Definition at line 255 of file ncm_device.c.

◆ xmit_requested_datagram_fits_into_current_ntb()

static bool xmit_requested_datagram_fits_into_current_ntb ( uint16_t  datagram_size)
static

check if a new datagram fits into the current NTB

Definition at line 358 of file ncm_device.c.

◆ xmit_setup_next_glue_ntb()

static bool xmit_setup_next_glue_ntb ( void  )
static

Setup an NTB for the glue logic

Definition at line 376 of file ncm_device.c.

◆ xmit_start_if_possible()

static void xmit_start_if_possible ( uint8_t  rhport)
static

Start transmission if it there is a waiting packet and if can be done from interface side.

Definition at line 314 of file ncm_device.c.

Variable Documentation

◆ ncm_interface

CFG_TUD_MEM_SECTION CFG_TUD_MEM_ALIGN tu_static ncm_interface_t ncm_interface

Definition at line 121 of file ncm_device.c.

◆ ncm_notify_connected

tu_static struct ncm_notify_t ncm_notify_connected
Initial value:
= {
.header = {
.bmRequestType_bit = {
.direction = TUSB_DIR_IN},
.wValue = 1 ,
.wLength = 0,
},
}
@ CDC_NOTIF_NETWORK_CONNECTION
This notification allows the device to notify the host about network connection status.
Definition: cdc.h:214
@ TUSB_DIR_IN
Definition: tusb_types.h:67
@ TUSB_REQ_RCPT_INTERFACE
Definition: tusb_types.h:152
@ TUSB_REQ_TYPE_CLASS
Definition: tusb_types.h:145

Definition at line 159 of file ncm_device.c.

◆ ncm_notify_speed_change

tu_static struct ncm_notify_t ncm_notify_speed_change
Initial value:
= {
.header = {
.bmRequestType_bit = {
.direction = TUSB_DIR_IN},
.wLength = 8,
},
.downlink = TUD_OPT_HIGH_SPEED ? 480000000 : 12000000,
.uplink = TUD_OPT_HIGH_SPEED ? 480000000 : 12000000,
}
@ CDC_NOTIF_CONNECTION_SPEED_CHANGE
This notification allows the device to inform the host-networking driver that a change in either the ...
Definition: cdc.h:221

Definition at line 171 of file ncm_device.c.

◆ ntb_parameters

CFG_TUD_MEM_SECTION CFG_TUD_MEM_ALIGN tu_static const ntb_parameters_t ntb_parameters
Initial value:
= {
.wLength = sizeof(ntb_parameters_t),
.bmNtbFormatsSupported = 0x01,
.dwNtbInMaxSize = CFG_TUD_NCM_IN_NTB_MAX_SIZE,
.wNdbInDivisor = 1,
.wNdbInPayloadRemainder = 0,
.wNdbInAlignment = TUD_NCM_ALIGNMENT,
.wReserved = 0,
.dwNtbOutMaxSize = CFG_TUD_NCM_OUT_NTB_MAX_SIZE,
.wNdbOutDivisor = 1,
.wNdbOutPayloadRemainder = 0,
.wNdbOutAlignment = TUD_NCM_ALIGNMENT,
.wNtbOutMaxDatagrams = CFG_TUD_NCM_OUT_MAX_DATAGRAMS_PER_NTB,
}
struct TU_ATTR_PACKED ntb_parameters_t

This is the NTB parameter structure

Attention
We are lucky, that byte order is correct

Definition at line 129 of file ncm_device.c.