EVE 1.0
usbnet.h
Go to the documentation of this file.
1 #ifndef EVE_NET_USBNET_H_INCLUDED
2 #define EVE_NET_USBNET_H_INCLUDED
3 /**********************************************************************/
4 /*
5  * Copyright (c) 2014-2016, Jetro AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  * derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONRIBUTORS ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28  * OF SUCH DAMAGE.
29  *
30  * This file is part of the EVE platform.
31  */
32 
33 /**
34  * \file
35  * @brief Common USB network layer, used by virtually all supported network USB protocols
36 
37  * @author DT, Jetro AS
38  */ /******************************************************************/
39 
40 #include <lib/dlist.h>
41 #include <net/uip.h>
42 #include <net/common/ep.h>
43 
44 /**
45  * \addtogroup net_common
46  * \{
47  */
48 
49 /*** Typedef's and defines. ***/
50 #define USBNET_ETH_FRAME_LEN (UIP_BUFSIZE + 58 - UIP_LLH_LEN) /**< Size of ethernet frame buffer */
51 #define USBNET_RX_BUF_SIZE USBNET_ETH_FRAME_LEN /**< Packet size when receiving on USB */
52 #define USBNET_TX_BUF_SIZE USBNET_ETH_FRAME_LEN /**< Packet size when transmitting on USB */
53 
54 /** Ethernet frames with size <= USBNET_TX_TINY_BUF_SIZE bytes are sent over a dedicated tiny pool */
55 #define USBNET_TX_TINY_BUF_SIZE (248)
56 #define USBNET_RX_POOL_SIZE (1) /**< Number of frames in the RX pool */
57 #define USBNET_TX_POOL_SIZE (1) /**< Number of frames on full-size TX pool */
58 #define USBNET_TX_TINY_POOL_SIZE (2) /**< Number of frames in the tiny TX pool */
59 #define USBNET_MANAGED_TX_POOL_SIZE (2) /**< Number of frames in the managed (TCP data) TX pool */
60 
61 enum
62 {
63  /**
64  * The event is sent to usbnet process when a new frame is received.
65  * Event data points to an URB structure, containing the data received.
66  */
68 #if 0
69  /**
70  * Obsolete. Replaced by PROCESS_EVENT_NET_FLOW_ON
71  * The event is sent to usbnet process when a new buffer becomes available
72  * in the managed pool and UIP code can continue data sending
73  */
74  USBNET_PROCESS_EVENT_TX_FLOW_ON,
75 #endif
76  /**
77  * The constant is used to count number of common events related to usbnet.
78  */
80 };
81 
82 /**
83  * USB RX buffer.
84  *
85  * Here urb->buf points to the .data field of the structure.
86  */
88 {
89  struct process *device; /**< Pointer to the network device */
90  uint8_t data[USBNET_RX_BUF_SIZE]; /**< The actual data buffer */
91 } __attribute__ ((aligned(4)));
92 
93 /**
94  * USB TX buffer (general and managed pools).
95  *
96  * The TX buffers are more complicated than RX buffers, because they are used also
97  * to accelerate TCP retransmissions. When an URB with TCP payload is transmitted,
98  * usbnet does not free it automatically, but rather puts it to a retransmission list
99  * of the socket, so-called "backlog". Responsibility to free (or retransmit) the URB
100  * lays on UIP code in this case.
101  * Like in \ref usbnet_rx_buf_t, urb->buf points to the .data field of the structure.
102  */
104 {
105  struct dlist_t *backlog; /**< The backlog the buffer belongs to */
106  uint32_t seq; /**< Internally used for indexing in UIP */
107  struct process *device; /**< Pointer to the network device */
108  uint8_t data[USBNET_TX_BUF_SIZE]; /**< The actual data buffer */
109 } __attribute__ ((aligned(4)));
110 
111 /**
112  * USB TX buffer (tiny pool).
113  *
114  * The major difference between this structure and \ref usbnet_tx_buf_t is the size
115  * of the .data field. Tiny pool is used more to control traffic (like TCP ACKs) and
116  * small UDP messages.
117  */
119 {
120  struct dlist_t *backlog; /**< The backlog always points to usbnetTxTinyEp */
121  uint32_t seq; /**< Not used for usbnet_tx_tiny_buf_t */
122  struct process *device; /**< Pointer to the network device */
123  uint8_t data[USBNET_TX_TINY_BUF_SIZE]; /**< The actual data buffer */
124 } __attribute__ ((aligned(4)));
125 
126 
127 /**
128  * All the usbnet buffer space together
129  */
131  struct usbnet_rx_buf_t rx[USBNET_RX_POOL_SIZE]; /**< RX buffer space */
132  struct usbnet_tx_buf_t tx[USBNET_TX_POOL_SIZE]; /**< TX buffer space */
133  struct usbnet_tx_tiny_buf_t tiny[USBNET_TX_TINY_POOL_SIZE]; /**< TX tiny buffer space */
134  struct usbnet_tx_buf_t managed[USBNET_MANAGED_TX_POOL_SIZE]; /**< TX managed buffer space */
135 };
136 
137 /**
138  * USBNET statistics
139  */
141  uint32_t txed;
142  uint32_t rxed;
143 };
144 
145 /** Usbnet statistics */
146 extern struct usbnet_stat_h usbnetStat;
147 
148 /**
149  * Initialize the usbnet core.
150  */
151 void usbnet_init();
152 
153 /**
154  * Start the interface; begin listening.
155  * \param module Usbnet client module
156  * \param rx_backlog_len Amount of buffers in the RX backlog
157  */
158 void usbnet_start(struct process *module, uint8_t rx_backlog_len);
159 
160 /**
161  * Stop the interface; kill all URBs
162  * \param module Usbnet client module
163  */
164 void usbnet_stop(struct process *module);
165 
166 /**
167  * Allocate an URB for transmitting.
168  *
169  * The URB is allocated from the appropriate pool, depending on requested size
170  * and backlog.
171  *
172  * \param module Usbnet client module
173  * \param size Actual size of data to send
174  * \param backlog The backlog the URB will belong to, or NULL for unmanaged allocation
175  */
176 struct urb_t *usbnet_alloc_urb(struct process *module, uint32_t size, struct dlist_t *backlog);
177 
178 /**
179  * Schedule the URB for transmission
180  *
181  * \param urb The URB to be sent over the interface
182  */
183 void usbnet_schedule_tx(struct urb_t *urb);
184 
185 /**
186  * Schedule the URB for reception
187  *
188  * \param urb The URB to be received
189  */
190 void usbnet_schedule_rx(struct urb_t *urb);
191 
192 /**
193  * Check if there is at least one URB available in the managed pool
194  *
195  * Note that it the function returns false, the usbnet core will send
196  * USBNET_PROCESS_EVENT_TX_FLOW_ON signal to the usbnet_process when a
197  * new fresh URB appears in the managed pool.
198  *
199  * \return true if at least one URB is available
200  * \return false if no URB is available
201  */
202 bool usbnet_quote(void);
203 
204 /**
205  * Convert an URB structure to the usbnet_tx_buf_t it actually points to
206  *
207  * \param urb The urb
208  * \return Pointer to usbnet_tx_buf_t structure
209  */
210 static inline struct usbnet_tx_buf_t *usbnet_tx_buf(struct urb_t *urb)
211 {
212  return (struct usbnet_tx_buf_t *)&urb->buf[-offsetof(struct usbnet_tx_buf_t, data)];
213 }
214 
215 /**
216  * Convert an URB structure to the usbnet_rx_buf_t it actually points to
217  *
218  * \param urb The urb
219  * \return Pointer to usbnet_rx_buf_t structure
220  */
221 static inline struct usbnet_rx_buf_t *usbnet_rx_buf(struct urb_t *urb)
222 {
223  return (struct usbnet_rx_buf_t *)&urb->buf[-offsetof(struct usbnet_rx_buf_t, data)];
224 }
225 
226 /** @} */ /* net_common */
227 
228 #endif /* EVE_NET_USBNET_H_INCLUDED */
void usbnet_schedule_tx(struct urb_t *urb)
bool usbnet_quote(void)
#define USBNET_TX_POOL_SIZE
Definition: usbnet.h:57
int size
Definition: ep.h:105
#define USBNET_RX_BUF_SIZE
Definition: usbnet.h:51
uint8_t * buf
Definition: ep.h:104
void usbnet_schedule_rx(struct urb_t *urb)
struct process * device
Definition: usbnet.h:122
struct process * device
Definition: usbnet.h:89
#define USBNET_TX_TINY_POOL_SIZE
Definition: usbnet.h:58
#define USBNET_RX_POOL_SIZE
Definition: usbnet.h:56
The code implements Dummy Headed Doubly Linked Circularlist (DHDLC) primitive.
struct dlist_t * backlog
Definition: usbnet.h:120
static struct usbnet_tx_buf_t * usbnet_tx_buf(struct urb_t *urb)
Definition: usbnet.h:210
void usbnet_init()
static struct usbnet_rx_buf_t * usbnet_rx_buf(struct urb_t *urb)
Definition: usbnet.h:221
struct dlist_t * backlog
Definition: usbnet.h:139
__attribute__((always_inline)) static inline void swint_enable_indirect_adapter(swint_state_t *state)
Definition: work.h:245
#define USBNET_MANAGED_TX_POOL_SIZE
Definition: usbnet.h:59
#define USBNET_TX_TINY_BUF_SIZE
Definition: usbnet.h:55
void usbnet_stop(struct process *module)
struct urb_t * usbnet_alloc_urb(struct process *module, uint32_t size, struct dlist_t *backlog)
uint32_t seq
Definition: usbnet.h:106
Definition: dlist.h:66
Definition: ep.h:101
USB endpoint primitive.
struct process * device
Definition: usbnet.h:107
void usbnet_start(struct process *module, uint8_t rx_backlog_len)
struct dlist_t * backlog
Definition: usbnet.h:105
#define USBNET_TX_BUF_SIZE
Definition: usbnet.h:52
uint8_t data[USBNET_RX_BUF_SIZE]
Definition: usbnet.h:90