1 /* SPDX-License-Identifier: GPL-2.0 */ 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 2 /* 3 * linux/can/dev.h 3 * linux/can/dev.h 4 * 4 * 5 * Definitions for the CAN network device driv 5 * Definitions for the CAN network device driver interface 6 * 6 * 7 * Copyright (C) 2006 Andrey Volkov <avolkov@v 7 * Copyright (C) 2006 Andrey Volkov <avolkov@varma-el.com> 8 * Varma Electronics Oy 8 * Varma Electronics Oy 9 * 9 * 10 * Copyright (C) 2008 Wolfgang Grandegger <wg@ 10 * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com> 11 * 11 * 12 */ 12 */ 13 13 14 #ifndef _CAN_DEV_H 14 #ifndef _CAN_DEV_H 15 #define _CAN_DEV_H 15 #define _CAN_DEV_H 16 16 17 #include <linux/can.h> 17 #include <linux/can.h> 18 #include <linux/can/bittiming.h> 18 #include <linux/can/bittiming.h> 19 #include <linux/can/error.h> 19 #include <linux/can/error.h> 20 #include <linux/can/length.h> 20 #include <linux/can/length.h> 21 #include <linux/can/netlink.h> 21 #include <linux/can/netlink.h> 22 #include <linux/can/skb.h> 22 #include <linux/can/skb.h> 23 #include <linux/ethtool.h> 23 #include <linux/ethtool.h> 24 #include <linux/netdevice.h> 24 #include <linux/netdevice.h> 25 25 26 /* 26 /* 27 * CAN mode 27 * CAN mode 28 */ 28 */ 29 enum can_mode { 29 enum can_mode { 30 CAN_MODE_STOP = 0, 30 CAN_MODE_STOP = 0, 31 CAN_MODE_START, 31 CAN_MODE_START, 32 CAN_MODE_SLEEP 32 CAN_MODE_SLEEP 33 }; 33 }; 34 34 35 enum can_termination_gpio { 35 enum can_termination_gpio { 36 CAN_TERMINATION_GPIO_DISABLED = 0, 36 CAN_TERMINATION_GPIO_DISABLED = 0, 37 CAN_TERMINATION_GPIO_ENABLED, 37 CAN_TERMINATION_GPIO_ENABLED, 38 CAN_TERMINATION_GPIO_MAX, 38 CAN_TERMINATION_GPIO_MAX, 39 }; 39 }; 40 40 41 /* 41 /* 42 * CAN common private data 42 * CAN common private data 43 */ 43 */ 44 struct can_priv { 44 struct can_priv { 45 struct net_device *dev; 45 struct net_device *dev; 46 struct can_device_stats can_stats; 46 struct can_device_stats can_stats; 47 47 48 const struct can_bittiming_const *bitt 48 const struct can_bittiming_const *bittiming_const, 49 *data_bittiming_const; 49 *data_bittiming_const; 50 struct can_bittiming bittiming, data_b 50 struct can_bittiming bittiming, data_bittiming; 51 const struct can_tdc_const *tdc_const; 51 const struct can_tdc_const *tdc_const; 52 struct can_tdc tdc; 52 struct can_tdc tdc; 53 53 54 unsigned int bitrate_const_cnt; 54 unsigned int bitrate_const_cnt; 55 const u32 *bitrate_const; 55 const u32 *bitrate_const; 56 const u32 *data_bitrate_const; 56 const u32 *data_bitrate_const; 57 unsigned int data_bitrate_const_cnt; 57 unsigned int data_bitrate_const_cnt; 58 u32 bitrate_max; 58 u32 bitrate_max; 59 struct can_clock clock; 59 struct can_clock clock; 60 60 61 unsigned int termination_const_cnt; 61 unsigned int termination_const_cnt; 62 const u16 *termination_const; 62 const u16 *termination_const; 63 u16 termination; 63 u16 termination; 64 struct gpio_desc *termination_gpio; 64 struct gpio_desc *termination_gpio; 65 u16 termination_gpio_ohms[CAN_TERMINAT 65 u16 termination_gpio_ohms[CAN_TERMINATION_GPIO_MAX]; 66 66 67 unsigned int echo_skb_max; 67 unsigned int echo_skb_max; 68 struct sk_buff **echo_skb; 68 struct sk_buff **echo_skb; 69 69 70 enum can_state state; 70 enum can_state state; 71 71 72 /* CAN controller features - see inclu 72 /* CAN controller features - see include/uapi/linux/can/netlink.h */ 73 u32 ctrlmode; /* current opt 73 u32 ctrlmode; /* current options setting */ 74 u32 ctrlmode_supported; /* options tha 74 u32 ctrlmode_supported; /* options that can be modified by netlink */ 75 75 76 int restart_ms; 76 int restart_ms; 77 struct delayed_work restart_work; 77 struct delayed_work restart_work; 78 78 79 int (*do_set_bittiming)(struct net_dev 79 int (*do_set_bittiming)(struct net_device *dev); 80 int (*do_set_data_bittiming)(struct ne 80 int (*do_set_data_bittiming)(struct net_device *dev); 81 int (*do_set_mode)(struct net_device * 81 int (*do_set_mode)(struct net_device *dev, enum can_mode mode); 82 int (*do_set_termination)(struct net_d 82 int (*do_set_termination)(struct net_device *dev, u16 term); 83 int (*do_get_state)(const struct net_d 83 int (*do_get_state)(const struct net_device *dev, 84 enum can_state *st 84 enum can_state *state); 85 int (*do_get_berr_counter)(const struc 85 int (*do_get_berr_counter)(const struct net_device *dev, 86 struct can_ 86 struct can_berr_counter *bec); 87 int (*do_get_auto_tdcv)(const struct n 87 int (*do_get_auto_tdcv)(const struct net_device *dev, u32 *tdcv); 88 }; 88 }; 89 89 90 static inline bool can_tdc_is_enabled(const st 90 static inline bool can_tdc_is_enabled(const struct can_priv *priv) 91 { 91 { 92 return !!(priv->ctrlmode & CAN_CTRLMOD 92 return !!(priv->ctrlmode & CAN_CTRLMODE_TDC_MASK); 93 } 93 } 94 94 95 /* 95 /* 96 * can_get_relative_tdco() - TDCO relative to 96 * can_get_relative_tdco() - TDCO relative to the sample point 97 * 97 * 98 * struct can_tdc::tdco represents the absolut 98 * struct can_tdc::tdco represents the absolute offset from TDCV. Some 99 * controllers use instead an offset relative 99 * controllers use instead an offset relative to the Sample Point (SP) 100 * such that: 100 * such that: 101 * 101 * 102 * SSP = TDCV + absolute TDCO 102 * SSP = TDCV + absolute TDCO 103 * = TDCV + SP + relative TDCO 103 * = TDCV + SP + relative TDCO 104 * 104 * 105 * -+----------- one bit ----------+-- TX pin 105 * -+----------- one bit ----------+-- TX pin 106 * |<--- Sample Point --->| 106 * |<--- Sample Point --->| 107 * 107 * 108 * --+----------- one 108 * --+----------- one bit ----------+-- RX pin 109 * |<-------- TDCV -------->| 109 * |<-------- TDCV -------->| 110 * |<--------------- 110 * |<------------------------>| absolute TDCO 111 * |<--- Sample Poin 111 * |<--- Sample Point --->| 112 * | 112 * | |<->| relative TDCO 113 * |<------------- Secondary Sample Point --- 113 * |<------------- Secondary Sample Point ------------>| 114 */ 114 */ 115 static inline s32 can_get_relative_tdco(const 115 static inline s32 can_get_relative_tdco(const struct can_priv *priv) 116 { 116 { 117 const struct can_bittiming *dbt = &pri 117 const struct can_bittiming *dbt = &priv->data_bittiming; 118 s32 sample_point_in_tc = (CAN_SYNC_SEG 118 s32 sample_point_in_tc = (CAN_SYNC_SEG + dbt->prop_seg + 119 dbt->phase_s 119 dbt->phase_seg1) * dbt->brp; 120 120 121 return (s32)priv->tdc.tdco - sample_po 121 return (s32)priv->tdc.tdco - sample_point_in_tc; 122 } 122 } 123 123 124 /* helper to define static CAN controller feat 124 /* helper to define static CAN controller features at device creation time */ 125 static inline int __must_check can_set_static_ 125 static inline int __must_check can_set_static_ctrlmode(struct net_device *dev, 126 126 u32 static_mode) 127 { 127 { 128 struct can_priv *priv = netdev_priv(de 128 struct can_priv *priv = netdev_priv(dev); 129 129 130 /* alloc_candev() succeeded => netdev_ 130 /* alloc_candev() succeeded => netdev_priv() is valid at this point */ 131 if (priv->ctrlmode_supported & static_ 131 if (priv->ctrlmode_supported & static_mode) { 132 netdev_warn(dev, 132 netdev_warn(dev, 133 "Controller featur 133 "Controller features can not be supported and static at the same time\n"); 134 return -EINVAL; 134 return -EINVAL; 135 } 135 } 136 priv->ctrlmode = static_mode; 136 priv->ctrlmode = static_mode; 137 137 138 /* override MTU which was set by defau 138 /* override MTU which was set by default in can_setup()? */ 139 if (static_mode & CAN_CTRLMODE_FD) 139 if (static_mode & CAN_CTRLMODE_FD) 140 dev->mtu = CANFD_MTU; 140 dev->mtu = CANFD_MTU; 141 141 142 return 0; 142 return 0; 143 } 143 } 144 144 145 static inline u32 can_get_static_ctrlmode(stru 145 static inline u32 can_get_static_ctrlmode(struct can_priv *priv) 146 { 146 { 147 return priv->ctrlmode & ~priv->ctrlmod 147 return priv->ctrlmode & ~priv->ctrlmode_supported; 148 } 148 } 149 149 150 static inline bool can_is_canxl_dev_mtu(unsign 150 static inline bool can_is_canxl_dev_mtu(unsigned int mtu) 151 { 151 { 152 return (mtu >= CANXL_MIN_MTU && mtu <= 152 return (mtu >= CANXL_MIN_MTU && mtu <= CANXL_MAX_MTU); 153 } 153 } 154 154 155 /* drop skb if it does not contain a valid CAN 155 /* drop skb if it does not contain a valid CAN frame for sending */ 156 static inline bool can_dev_dropped_skb(struct 156 static inline bool can_dev_dropped_skb(struct net_device *dev, struct sk_buff *skb) 157 { 157 { 158 struct can_priv *priv = netdev_priv(de 158 struct can_priv *priv = netdev_priv(dev); 159 159 160 if (priv->ctrlmode & CAN_CTRLMODE_LIST 160 if (priv->ctrlmode & CAN_CTRLMODE_LISTENONLY) { 161 netdev_info_once(dev, 161 netdev_info_once(dev, 162 "interface in 162 "interface in listen only mode, dropping skb\n"); 163 kfree_skb(skb); 163 kfree_skb(skb); 164 dev->stats.tx_dropped++; 164 dev->stats.tx_dropped++; 165 return true; 165 return true; 166 } 166 } 167 167 168 return can_dropped_invalid_skb(dev, sk 168 return can_dropped_invalid_skb(dev, skb); 169 } 169 } 170 170 171 void can_setup(struct net_device *dev); 171 void can_setup(struct net_device *dev); 172 172 173 struct net_device *alloc_candev_mqs(int sizeof 173 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max, 174 unsigned i 174 unsigned int txqs, unsigned int rxqs); 175 #define alloc_candev(sizeof_priv, echo_skb_max 175 #define alloc_candev(sizeof_priv, echo_skb_max) \ 176 alloc_candev_mqs(sizeof_priv, echo_skb 176 alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1) 177 #define alloc_candev_mq(sizeof_priv, echo_skb_ 177 #define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \ 178 alloc_candev_mqs(sizeof_priv, echo_skb 178 alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count) 179 void free_candev(struct net_device *dev); 179 void free_candev(struct net_device *dev); 180 180 181 /* a candev safe wrapper around netdev_priv */ 181 /* a candev safe wrapper around netdev_priv */ 182 struct can_priv *safe_candev_priv(struct net_d 182 struct can_priv *safe_candev_priv(struct net_device *dev); 183 183 184 int open_candev(struct net_device *dev); 184 int open_candev(struct net_device *dev); 185 void close_candev(struct net_device *dev); 185 void close_candev(struct net_device *dev); 186 int can_change_mtu(struct net_device *dev, int 186 int can_change_mtu(struct net_device *dev, int new_mtu); 187 int can_eth_ioctl_hwts(struct net_device *netd 187 int can_eth_ioctl_hwts(struct net_device *netdev, struct ifreq *ifr, int cmd); 188 int can_ethtool_op_get_ts_info_hwts(struct net 188 int can_ethtool_op_get_ts_info_hwts(struct net_device *dev, 189 struct ker 189 struct kernel_ethtool_ts_info *info); 190 190 191 int register_candev(struct net_device *dev); 191 int register_candev(struct net_device *dev); 192 void unregister_candev(struct net_device *dev) 192 void unregister_candev(struct net_device *dev); 193 193 194 int can_restart_now(struct net_device *dev); 194 int can_restart_now(struct net_device *dev); 195 void can_bus_off(struct net_device *dev); 195 void can_bus_off(struct net_device *dev); 196 196 197 const char *can_get_state_str(const enum can_s 197 const char *can_get_state_str(const enum can_state state); 198 void can_state_get_by_berr_counter(const struc 198 void can_state_get_by_berr_counter(const struct net_device *dev, 199 const struc 199 const struct can_berr_counter *bec, 200 enum can_st 200 enum can_state *tx_state, 201 enum can_st 201 enum can_state *rx_state); 202 void can_change_state(struct net_device *dev, 202 void can_change_state(struct net_device *dev, struct can_frame *cf, 203 enum can_state tx_state, 203 enum can_state tx_state, enum can_state rx_state); 204 204 205 #ifdef CONFIG_OF 205 #ifdef CONFIG_OF 206 void of_can_transceiver(struct net_device *dev 206 void of_can_transceiver(struct net_device *dev); 207 #else 207 #else 208 static inline void of_can_transceiver(struct n 208 static inline void of_can_transceiver(struct net_device *dev) { } 209 #endif 209 #endif 210 210 211 extern struct rtnl_link_ops can_link_ops; 211 extern struct rtnl_link_ops can_link_ops; 212 int can_netlink_register(void); 212 int can_netlink_register(void); 213 void can_netlink_unregister(void); 213 void can_netlink_unregister(void); 214 214 215 #endif /* !_CAN_DEV_H */ 215 #endif /* !_CAN_DEV_H */ 216 216
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.