dev.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <[email protected]>
  8. * Varma Electronics Oy
  9. *
  10. * Copyright (C) 2008 Wolfgang Grandegger <[email protected]>
  11. *
  12. */
  13. #ifndef _CAN_DEV_H
  14. #define _CAN_DEV_H
  15. #include <linux/can.h>
  16. #include <linux/can/bittiming.h>
  17. #include <linux/can/error.h>
  18. #include <linux/can/length.h>
  19. #include <linux/can/netlink.h>
  20. #include <linux/can/skb.h>
  21. #include <linux/ethtool.h>
  22. #include <linux/netdevice.h>
  23. /*
  24. * CAN mode
  25. */
  26. enum can_mode {
  27. CAN_MODE_STOP = 0,
  28. CAN_MODE_START,
  29. CAN_MODE_SLEEP
  30. };
  31. enum can_termination_gpio {
  32. CAN_TERMINATION_GPIO_DISABLED = 0,
  33. CAN_TERMINATION_GPIO_ENABLED,
  34. CAN_TERMINATION_GPIO_MAX,
  35. };
  36. /*
  37. * CAN common private data
  38. */
  39. struct can_priv {
  40. struct net_device *dev;
  41. struct can_device_stats can_stats;
  42. const struct can_bittiming_const *bittiming_const,
  43. *data_bittiming_const;
  44. struct can_bittiming bittiming, data_bittiming;
  45. const struct can_tdc_const *tdc_const;
  46. struct can_tdc tdc;
  47. unsigned int bitrate_const_cnt;
  48. const u32 *bitrate_const;
  49. const u32 *data_bitrate_const;
  50. unsigned int data_bitrate_const_cnt;
  51. u32 bitrate_max;
  52. struct can_clock clock;
  53. unsigned int termination_const_cnt;
  54. const u16 *termination_const;
  55. u16 termination;
  56. struct gpio_desc *termination_gpio;
  57. u16 termination_gpio_ohms[CAN_TERMINATION_GPIO_MAX];
  58. unsigned int echo_skb_max;
  59. struct sk_buff **echo_skb;
  60. enum can_state state;
  61. /* CAN controller features - see include/uapi/linux/can/netlink.h */
  62. u32 ctrlmode; /* current options setting */
  63. u32 ctrlmode_supported; /* options that can be modified by netlink */
  64. int restart_ms;
  65. struct delayed_work restart_work;
  66. int (*do_set_bittiming)(struct net_device *dev);
  67. int (*do_set_data_bittiming)(struct net_device *dev);
  68. int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
  69. int (*do_set_termination)(struct net_device *dev, u16 term);
  70. int (*do_get_state)(const struct net_device *dev,
  71. enum can_state *state);
  72. int (*do_get_berr_counter)(const struct net_device *dev,
  73. struct can_berr_counter *bec);
  74. int (*do_get_auto_tdcv)(const struct net_device *dev, u32 *tdcv);
  75. };
  76. static inline bool can_tdc_is_enabled(const struct can_priv *priv)
  77. {
  78. return !!(priv->ctrlmode & CAN_CTRLMODE_TDC_MASK);
  79. }
  80. /*
  81. * can_get_relative_tdco() - TDCO relative to the sample point
  82. *
  83. * struct can_tdc::tdco represents the absolute offset from TDCV. Some
  84. * controllers use instead an offset relative to the Sample Point (SP)
  85. * such that:
  86. *
  87. * SSP = TDCV + absolute TDCO
  88. * = TDCV + SP + relative TDCO
  89. *
  90. * -+----------- one bit ----------+-- TX pin
  91. * |<--- Sample Point --->|
  92. *
  93. * --+----------- one bit ----------+-- RX pin
  94. * |<-------- TDCV -------->|
  95. * |<------------------------>| absolute TDCO
  96. * |<--- Sample Point --->|
  97. * | |<->| relative TDCO
  98. * |<------------- Secondary Sample Point ------------>|
  99. */
  100. static inline s32 can_get_relative_tdco(const struct can_priv *priv)
  101. {
  102. const struct can_bittiming *dbt = &priv->data_bittiming;
  103. s32 sample_point_in_tc = (CAN_SYNC_SEG + dbt->prop_seg +
  104. dbt->phase_seg1) * dbt->brp;
  105. return (s32)priv->tdc.tdco - sample_point_in_tc;
  106. }
  107. /* helper to define static CAN controller features at device creation time */
  108. static inline int __must_check 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. if (priv->ctrlmode_supported & static_mode) {
  114. netdev_warn(dev,
  115. "Controller features can not be supported and static at the same time\n");
  116. return -EINVAL;
  117. }
  118. priv->ctrlmode = static_mode;
  119. /* override MTU which was set by default in can_setup()? */
  120. if (static_mode & CAN_CTRLMODE_FD)
  121. dev->mtu = CANFD_MTU;
  122. return 0;
  123. }
  124. static inline u32 can_get_static_ctrlmode(struct can_priv *priv)
  125. {
  126. return priv->ctrlmode & ~priv->ctrlmode_supported;
  127. }
  128. static inline bool can_is_canxl_dev_mtu(unsigned int mtu)
  129. {
  130. return (mtu >= CANXL_MIN_MTU && mtu <= CANXL_MAX_MTU);
  131. }
  132. /* drop skb if it does not contain a valid CAN frame for sending */
  133. static inline bool can_dev_dropped_skb(struct net_device *dev, struct sk_buff *skb)
  134. {
  135. struct can_priv *priv = netdev_priv(dev);
  136. if (priv->ctrlmode & CAN_CTRLMODE_LISTENONLY) {
  137. netdev_info_once(dev,
  138. "interface in listen only mode, dropping skb\n");
  139. kfree_skb(skb);
  140. dev->stats.tx_dropped++;
  141. return true;
  142. }
  143. return can_dropped_invalid_skb(dev, skb);
  144. }
  145. void can_setup(struct net_device *dev);
  146. struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
  147. unsigned int txqs, unsigned int rxqs);
  148. #define alloc_candev(sizeof_priv, echo_skb_max) \
  149. alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1)
  150. #define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \
  151. alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count)
  152. void free_candev(struct net_device *dev);
  153. /* a candev safe wrapper around netdev_priv */
  154. struct can_priv *safe_candev_priv(struct net_device *dev);
  155. int open_candev(struct net_device *dev);
  156. void close_candev(struct net_device *dev);
  157. int can_change_mtu(struct net_device *dev, int new_mtu);
  158. int can_eth_ioctl_hwts(struct net_device *netdev, struct ifreq *ifr, int cmd);
  159. int can_ethtool_op_get_ts_info_hwts(struct net_device *dev,
  160. struct ethtool_ts_info *info);
  161. int register_candev(struct net_device *dev);
  162. void unregister_candev(struct net_device *dev);
  163. int can_restart_now(struct net_device *dev);
  164. void can_bus_off(struct net_device *dev);
  165. const char *can_get_state_str(const enum can_state state);
  166. void can_change_state(struct net_device *dev, struct can_frame *cf,
  167. enum can_state tx_state, enum can_state rx_state);
  168. #ifdef CONFIG_OF
  169. void of_can_transceiver(struct net_device *dev);
  170. #else
  171. static inline void of_can_transceiver(struct net_device *dev) { }
  172. #endif
  173. extern struct rtnl_link_ops can_link_ops;
  174. int can_netlink_register(void);
  175. void can_netlink_unregister(void);
  176. #endif /* !_CAN_DEV_H */