drv_ether.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* Copyright (C) 2019 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _DRV_ETHER_H_
  13. #define _DRV_ETHER_H_
  14. #include "osi_compiler.h"
  15. OSI_EXTERN_C_BEGIN
  16. #include <stdbool.h>
  17. #include "drv_if_ether.h"
  18. typedef struct drv_ether drvEther_t;
  19. typedef struct
  20. {
  21. ethHdr_t ethhd;
  22. uint8_t data[0];
  23. } drvEthPacket_t;
  24. typedef struct
  25. {
  26. drvEthPacket_t *payload;
  27. union {
  28. uint32_t actual_size;
  29. uint32_t buffer_size;
  30. };
  31. } drvEthReq_t;
  32. typedef enum
  33. {
  34. DRV_ETHER_EVENT_CONNECT = (1 << 0), ///< ethernet connected
  35. DRV_ETHER_EVENT_DISCONNECT = (1 << 1), ///< ethernet disconnected
  36. DRV_ETHER_EVENT_RESUME_CONNECT = (1 << 2), ///< ethernet resume connected
  37. DRV_ETHER_EVENT_SUSPEND_DISCONNECT = (1 << 3), ///< ethernet suspend disconnected
  38. } drvEthEvent_t;
  39. typedef void (*drvEthEventCB_t)(uint32_t evt, void *priv);
  40. typedef void (*drvEthULDataCB_t)(drvEthPacket_t *pkt, uint32_t len, void *priv);
  41. typedef struct
  42. {
  43. unsigned long rx_packets;
  44. unsigned long tx_packets;
  45. unsigned long rx_bytes;
  46. unsigned long tx_bytes;
  47. unsigned long rx_errors;
  48. unsigned long tx_errors;
  49. unsigned long rx_dropped;
  50. unsigned long tx_dropped;
  51. unsigned long multicast;
  52. unsigned long collisions;
  53. unsigned long rx_length_errors;
  54. unsigned long rx_over_errors;
  55. unsigned long rx_crc_errors;
  56. unsigned long rx_frame_errors;
  57. unsigned long rx_fifo_errors;
  58. unsigned long rx_missed_errors;
  59. unsigned long tx_aborted_errors;
  60. unsigned long tx_carrier_errors;
  61. unsigned long tx_fifo_errors;
  62. unsigned long tx_heartbeat_errors;
  63. unsigned long tx_window_errors;
  64. unsigned long rx_compressed;
  65. unsigned long tx_compressed;
  66. } drvEthStats_t;
  67. typedef struct
  68. {
  69. bool (*netup)(drvEther_t *ether);
  70. void (*netdown)(drvEther_t *ether);
  71. drvEthReq_t *(*tx_req_alloc)(drvEther_t *ether);
  72. void (*tx_req_free)(drvEther_t *ether, drvEthReq_t *req);
  73. bool (*tx_req_submit)(drvEther_t *ether, drvEthReq_t *req, size_t size);
  74. void (*set_event_cb)(drvEther_t *ether, drvEthEventCB_t cb, void *priv);
  75. void (*set_uldata_cb)(drvEther_t *ether, drvEthULDataCB_t cb, void *priv);
  76. void (*set_host_mac)(drvEther_t *ether, uint8_t host_mac[ETH_ALEN]);
  77. } drvEthImpl_t;
  78. struct drv_ether
  79. {
  80. drvEthImpl_t impl;
  81. drvEthStats_t stats;
  82. void *impl_ctx;
  83. const uint8_t *host_mac;
  84. const uint8_t *dev_mac;
  85. };
  86. /**
  87. * \brief netlink notfy the actual device net up
  88. *
  89. * \param eth the net device
  90. * \return
  91. * - true on succeed else fail
  92. */
  93. static inline bool drvEtherNetUp(drvEther_t *eth)
  94. {
  95. return (eth ? eth->impl.netup(eth) : false);
  96. }
  97. /**
  98. * \brief netlink notify the actual device net down
  99. *
  100. * \param eth the net device
  101. */
  102. static inline void drvEtherNetDown(drvEther_t *eth)
  103. {
  104. if (eth)
  105. eth->impl.netdown(eth);
  106. }
  107. /**
  108. * \brief allocate a tx request from net device
  109. * \note besure call `drvEtherTxReqFree` if the request not use
  110. *
  111. * \param eth the net device
  112. * \return
  113. * - (non-null) the tx request else fail
  114. */
  115. static inline drvEthReq_t *drvEtherTxReqAlloc(drvEther_t *eth)
  116. {
  117. return (eth ? eth->impl.tx_req_alloc(eth) : NULL);
  118. }
  119. /**
  120. * \brief release a tx transfer request
  121. * \note do not call me if a request had been submitted (even submit fail)
  122. *
  123. * \param eth the net device
  124. * \param req the transfer request
  125. */
  126. static inline void drvEtherTxReqFree(drvEther_t *eth, drvEthReq_t *req)
  127. {
  128. if (eth && req)
  129. eth->impl.tx_req_free(eth, req);
  130. }
  131. /**
  132. * \brief submit a tx transfer request (and recall the request)
  133. *
  134. * \param eth the net device
  135. * \param req the request
  136. * \param size data size
  137. * \return
  138. * - true on succeed else fail
  139. */
  140. static inline bool drvEtherTxReqSubmit(drvEther_t *eth, drvEthReq_t *req, size_t size)
  141. {
  142. return ((eth && req) ? eth->impl.tx_req_submit(eth, req, size) : false);
  143. }
  144. /**
  145. * \brief register event callback to be notified net device state change
  146. * like connect/disconnected
  147. *
  148. * \param eth the net device
  149. * \param cb the callback
  150. * \param priv caller private context
  151. */
  152. static inline void drvEtherSetEventCB(drvEther_t *eth, drvEthEventCB_t cb, void *priv)
  153. {
  154. if (eth)
  155. eth->impl.set_event_cb(eth, cb, priv);
  156. }
  157. /**
  158. * \brief register data upload callback, the callbcak will be call every time
  159. * data coming from net device
  160. *
  161. * \param eth the net device
  162. * \param cb the callback
  163. * \param priv caller private context
  164. */
  165. static inline void drvEtherSetULDataCB(drvEther_t *eth, drvEthULDataCB_t cb, void *priv)
  166. {
  167. if (eth)
  168. eth->impl.set_uldata_cb(eth, cb, priv);
  169. }
  170. /**
  171. * \brief set host mac address
  172. *
  173. * \param eth the net device
  174. * \param host_mac host mac address
  175. */
  176. static inline void drvEtherSetHostMac(drvEther_t *eth, uint8_t host_mac[ETH_ALEN])
  177. {
  178. if (eth)
  179. eth->impl.set_host_mac(eth, host_mac);
  180. }
  181. OSI_EXTERN_C_END
  182. #endif