0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef _CAN_DEV_H
0015 #define _CAN_DEV_H
0016
0017 #include <linux/can.h>
0018 #include <linux/can/bittiming.h>
0019 #include <linux/can/error.h>
0020 #include <linux/can/length.h>
0021 #include <linux/can/netlink.h>
0022 #include <linux/can/skb.h>
0023 #include <linux/ethtool.h>
0024 #include <linux/netdevice.h>
0025
0026
0027
0028
0029 enum can_mode {
0030 CAN_MODE_STOP = 0,
0031 CAN_MODE_START,
0032 CAN_MODE_SLEEP
0033 };
0034
0035 enum can_termination_gpio {
0036 CAN_TERMINATION_GPIO_DISABLED = 0,
0037 CAN_TERMINATION_GPIO_ENABLED,
0038 CAN_TERMINATION_GPIO_MAX,
0039 };
0040
0041
0042
0043
0044 struct can_priv {
0045 struct net_device *dev;
0046 struct can_device_stats can_stats;
0047
0048 const struct can_bittiming_const *bittiming_const,
0049 *data_bittiming_const;
0050 struct can_bittiming bittiming, data_bittiming;
0051 const struct can_tdc_const *tdc_const;
0052 struct can_tdc tdc;
0053
0054 unsigned int bitrate_const_cnt;
0055 const u32 *bitrate_const;
0056 const u32 *data_bitrate_const;
0057 unsigned int data_bitrate_const_cnt;
0058 u32 bitrate_max;
0059 struct can_clock clock;
0060
0061 unsigned int termination_const_cnt;
0062 const u16 *termination_const;
0063 u16 termination;
0064 struct gpio_desc *termination_gpio;
0065 u16 termination_gpio_ohms[CAN_TERMINATION_GPIO_MAX];
0066
0067 unsigned int echo_skb_max;
0068 struct sk_buff **echo_skb;
0069
0070 enum can_state state;
0071
0072
0073 u32 ctrlmode;
0074 u32 ctrlmode_supported;
0075
0076 int restart_ms;
0077 struct delayed_work restart_work;
0078
0079 int (*do_set_bittiming)(struct net_device *dev);
0080 int (*do_set_data_bittiming)(struct net_device *dev);
0081 int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
0082 int (*do_set_termination)(struct net_device *dev, u16 term);
0083 int (*do_get_state)(const struct net_device *dev,
0084 enum can_state *state);
0085 int (*do_get_berr_counter)(const struct net_device *dev,
0086 struct can_berr_counter *bec);
0087 int (*do_get_auto_tdcv)(const struct net_device *dev, u32 *tdcv);
0088 };
0089
0090 static inline bool can_tdc_is_enabled(const struct can_priv *priv)
0091 {
0092 return !!(priv->ctrlmode & CAN_CTRLMODE_TDC_MASK);
0093 }
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115 static inline s32 can_get_relative_tdco(const struct can_priv *priv)
0116 {
0117 const struct can_bittiming *dbt = &priv->data_bittiming;
0118 s32 sample_point_in_tc = (CAN_SYNC_SEG + dbt->prop_seg +
0119 dbt->phase_seg1) * dbt->brp;
0120
0121 return (s32)priv->tdc.tdco - sample_point_in_tc;
0122 }
0123
0124
0125 static inline int __must_check can_set_static_ctrlmode(struct net_device *dev,
0126 u32 static_mode)
0127 {
0128 struct can_priv *priv = netdev_priv(dev);
0129
0130
0131 if (priv->ctrlmode_supported & static_mode) {
0132 netdev_warn(dev,
0133 "Controller features can not be supported and static at the same time\n");
0134 return -EINVAL;
0135 }
0136 priv->ctrlmode = static_mode;
0137
0138
0139 if (static_mode & CAN_CTRLMODE_FD)
0140 dev->mtu = CANFD_MTU;
0141
0142 return 0;
0143 }
0144
0145 static inline u32 can_get_static_ctrlmode(struct can_priv *priv)
0146 {
0147 return priv->ctrlmode & ~priv->ctrlmode_supported;
0148 }
0149
0150 void can_setup(struct net_device *dev);
0151
0152 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
0153 unsigned int txqs, unsigned int rxqs);
0154 #define alloc_candev(sizeof_priv, echo_skb_max) \
0155 alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1)
0156 #define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \
0157 alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count)
0158 void free_candev(struct net_device *dev);
0159
0160
0161 struct can_priv *safe_candev_priv(struct net_device *dev);
0162
0163 int open_candev(struct net_device *dev);
0164 void close_candev(struct net_device *dev);
0165 int can_change_mtu(struct net_device *dev, int new_mtu);
0166 int can_eth_ioctl_hwts(struct net_device *netdev, struct ifreq *ifr, int cmd);
0167 int can_ethtool_op_get_ts_info_hwts(struct net_device *dev,
0168 struct ethtool_ts_info *info);
0169
0170 int register_candev(struct net_device *dev);
0171 void unregister_candev(struct net_device *dev);
0172
0173 int can_restart_now(struct net_device *dev);
0174 void can_bus_off(struct net_device *dev);
0175
0176 const char *can_get_state_str(const enum can_state state);
0177 void can_change_state(struct net_device *dev, struct can_frame *cf,
0178 enum can_state tx_state, enum can_state rx_state);
0179
0180 #ifdef CONFIG_OF
0181 void of_can_transceiver(struct net_device *dev);
0182 #else
0183 static inline void of_can_transceiver(struct net_device *dev) { }
0184 #endif
0185
0186 extern struct rtnl_link_ops can_link_ops;
0187 int can_netlink_register(void);
0188 void can_netlink_unregister(void);
0189
0190 #endif