dev.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * linux/can/dev.h
  4. *
  5. * Definitions for the CAN network device driver interface
  6. *
  7. * Copyright (C) 2006 Andrey Volkov <avolkov@varma-el.com>
  8. * Varma Electronics Oy
  9. *
  10. * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
  11. *
  12. */
  13. #ifndef _CAN_DEV_H
  14. #define _CAN_DEV_H
  15. #include <linux/can.h>
  16. #include <linux/can/error.h>
  17. #include <linux/can/led.h>
  18. #include <linux/can/netlink.h>
  19. #include <linux/netdevice.h>
  20. /*
  21. * CAN mode
  22. */
  23. enum can_mode {
  24. CAN_MODE_STOP = 0,
  25. CAN_MODE_START,
  26. CAN_MODE_SLEEP
  27. };
  28. /*
  29. * CAN common private data
  30. */
  31. struct can_priv {
  32. struct net_device *dev;
  33. struct can_device_stats can_stats;
  34. struct can_bittiming bittiming, data_bittiming;
  35. const struct can_bittiming_const *bittiming_const,
  36. *data_bittiming_const;
  37. const u16 *termination_const;
  38. unsigned int termination_const_cnt;
  39. u16 termination;
  40. const u32 *bitrate_const;
  41. unsigned int bitrate_const_cnt;
  42. const u32 *data_bitrate_const;
  43. unsigned int data_bitrate_const_cnt;
  44. u32 bitrate_max;
  45. struct can_clock clock;
  46. enum can_state state;
  47. /* CAN controller features - see include/uapi/linux/can/netlink.h */
  48. u32 ctrlmode; /* current options setting */
  49. u32 ctrlmode_supported; /* options that can be modified by netlink */
  50. u32 ctrlmode_static; /* static enabled options for driver/hardware */
  51. int restart_ms;
  52. struct delayed_work restart_work;
  53. int (*do_set_bittiming)(struct net_device *dev);
  54. int (*do_set_data_bittiming)(struct net_device *dev);
  55. int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
  56. int (*do_set_termination)(struct net_device *dev, u16 term);
  57. int (*do_get_state)(const struct net_device *dev,
  58. enum can_state *state);
  59. int (*do_get_berr_counter)(const struct net_device *dev,
  60. struct can_berr_counter *bec);
  61. unsigned int echo_skb_max;
  62. struct sk_buff **echo_skb;
  63. #ifdef CONFIG_CAN_LEDS
  64. struct led_trigger *tx_led_trig;
  65. char tx_led_trig_name[CAN_LED_NAME_SZ];
  66. struct led_trigger *rx_led_trig;
  67. char rx_led_trig_name[CAN_LED_NAME_SZ];
  68. struct led_trigger *rxtx_led_trig;
  69. char rxtx_led_trig_name[CAN_LED_NAME_SZ];
  70. #endif
  71. };
  72. /*
  73. * get_can_dlc(value) - helper macro to cast a given data length code (dlc)
  74. * to __u8 and ensure the dlc value to be max. 8 bytes.
  75. *
  76. * To be used in the CAN netdriver receive path to ensure conformance with
  77. * ISO 11898-1 Chapter 8.4.2.3 (DLC field)
  78. */
  79. #define get_can_dlc(i) (min_t(__u8, (i), CAN_MAX_DLC))
  80. #define get_canfd_dlc(i) (min_t(__u8, (i), CANFD_MAX_DLC))
  81. /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
  82. static inline bool can_dropped_invalid_skb(struct net_device *dev,
  83. struct sk_buff *skb)
  84. {
  85. const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
  86. if (skb->protocol == htons(ETH_P_CAN)) {
  87. if (unlikely(skb->len != CAN_MTU ||
  88. cfd->len > CAN_MAX_DLEN))
  89. goto inval_skb;
  90. } else if (skb->protocol == htons(ETH_P_CANFD)) {
  91. if (unlikely(skb->len != CANFD_MTU ||
  92. cfd->len > CANFD_MAX_DLEN))
  93. goto inval_skb;
  94. } else
  95. goto inval_skb;
  96. return false;
  97. inval_skb:
  98. kfree_skb(skb);
  99. dev->stats.tx_dropped++;
  100. return true;
  101. }
  102. static inline bool can_is_canfd_skb(const struct sk_buff *skb)
  103. {
  104. /* the CAN specific type of skb is identified by its data length */
  105. return skb->len == CANFD_MTU;
  106. }
  107. /* helper to define static CAN controller features at device creation time */
  108. static inline void can_set_static_ctrlmode(struct net_device *dev,
  109. u32 static_mode)
  110. {
  111. struct can_priv *priv = netdev_priv(dev);
  112. /* alloc_candev() succeeded => netdev_priv() is valid at this point */
  113. priv->ctrlmode = static_mode;
  114. priv->ctrlmode_static = static_mode;
  115. /* override MTU which was set by default in can_setup()? */
  116. if (static_mode & CAN_CTRLMODE_FD)
  117. dev->mtu = CANFD_MTU;
  118. }
  119. /* get data length from can_dlc with sanitized can_dlc */
  120. u8 can_dlc2len(u8 can_dlc);
  121. /* map the sanitized data length to an appropriate data length code */
  122. u8 can_len2dlc(u8 len);
  123. struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
  124. unsigned int txqs, unsigned int rxqs);
  125. #define alloc_candev(sizeof_priv, echo_skb_max) \
  126. alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1)
  127. #define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \
  128. alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count)
  129. void free_candev(struct net_device *dev);
  130. /* a candev safe wrapper around netdev_priv */
  131. struct can_priv *safe_candev_priv(struct net_device *dev);
  132. int open_candev(struct net_device *dev);
  133. void close_candev(struct net_device *dev);
  134. int can_change_mtu(struct net_device *dev, int new_mtu);
  135. int register_candev(struct net_device *dev);
  136. void unregister_candev(struct net_device *dev);
  137. int can_restart_now(struct net_device *dev);
  138. void can_bus_off(struct net_device *dev);
  139. void can_change_state(struct net_device *dev, struct can_frame *cf,
  140. enum can_state tx_state, enum can_state rx_state);
  141. void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
  142. unsigned int idx);
  143. struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr);
  144. unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx);
  145. void can_free_echo_skb(struct net_device *dev, unsigned int idx);
  146. #ifdef CONFIG_OF
  147. void of_can_transceiver(struct net_device *dev);
  148. #else
  149. static inline void of_can_transceiver(struct net_device *dev) { }
  150. #endif
  151. struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
  152. struct sk_buff *alloc_canfd_skb(struct net_device *dev,
  153. struct canfd_frame **cfd);
  154. struct sk_buff *alloc_can_err_skb(struct net_device *dev,
  155. struct can_frame **cf);
  156. #endif /* !_CAN_DEV_H */