1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Definitions for the CAN network device driver interface
7 * Copyright (C) 2006 Andrey Volkov <avolkov@varma-el.com>
10 * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
17 #include <linux/can.h>
18 #include <linux/can/bittiming.h>
19 #include <linux/can/error.h>
20 #include <linux/can/length.h>
21 #include <linux/can/netlink.h>
22 #include <linux/can/skb.h>
23 #include <linux/ethtool.h>
24 #include <linux/netdevice.h>
35 enum can_termination_gpio {
36 CAN_TERMINATION_GPIO_DISABLED = 0,
37 CAN_TERMINATION_GPIO_ENABLED,
38 CAN_TERMINATION_GPIO_MAX,
41 struct data_bittiming_params {
42 const struct can_bittiming_const *data_bittiming_const;
43 struct can_bittiming data_bittiming;
44 const struct can_tdc_const *tdc_const;
46 const u32 *data_bitrate_const;
47 unsigned int data_bitrate_const_cnt;
48 int (*do_set_data_bittiming)(struct net_device *dev);
49 int (*do_get_auto_tdcv)(const struct net_device *dev, u32 *tdcv);
53 * CAN common private data
56 struct net_device *dev;
57 struct can_device_stats can_stats;
59 const struct can_bittiming_const *bittiming_const;
60 struct can_bittiming bittiming;
61 struct data_bittiming_params fd;
62 unsigned int bitrate_const_cnt;
63 const u32 *bitrate_const;
65 struct can_clock clock;
67 unsigned int termination_const_cnt;
68 const u16 *termination_const;
70 struct gpio_desc *termination_gpio;
71 u16 termination_gpio_ohms[CAN_TERMINATION_GPIO_MAX];
73 unsigned int echo_skb_max;
74 struct sk_buff **echo_skb;
78 /* CAN controller features - see include/uapi/linux/can/netlink.h */
79 u32 ctrlmode; /* current options setting */
80 u32 ctrlmode_supported; /* options that can be modified by netlink */
83 struct delayed_work restart_work;
85 int (*do_set_bittiming)(struct net_device *dev);
86 int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
87 int (*do_set_termination)(struct net_device *dev, u16 term);
88 int (*do_get_state)(const struct net_device *dev,
89 enum can_state *state);
90 int (*do_get_berr_counter)(const struct net_device *dev,
91 struct can_berr_counter *bec);
94 static inline bool can_tdc_is_enabled(const struct can_priv *priv)
96 return !!(priv->ctrlmode & CAN_CTRLMODE_TDC_MASK);
100 * can_get_relative_tdco() - TDCO relative to the sample point
102 * struct can_tdc::tdco represents the absolute offset from TDCV. Some
103 * controllers use instead an offset relative to the Sample Point (SP)
106 * SSP = TDCV + absolute TDCO
107 * = TDCV + SP + relative TDCO
109 * -+----------- one bit ----------+-- TX pin
110 * |<--- Sample Point --->|
112 * --+----------- one bit ----------+-- RX pin
113 * |<-------- TDCV -------->|
114 * |<------------------------>| absolute TDCO
115 * |<--- Sample Point --->|
116 * | |<->| relative TDCO
117 * |<------------- Secondary Sample Point ------------>|
119 static inline s32 can_get_relative_tdco(const struct can_priv *priv)
121 const struct can_bittiming *dbt = &priv->fd.data_bittiming;
122 s32 sample_point_in_tc = (CAN_SYNC_SEG + dbt->prop_seg +
123 dbt->phase_seg1) * dbt->brp;
125 return (s32)priv->fd.tdc.tdco - sample_point_in_tc;
128 /* helper to define static CAN controller features at device creation time */
129 static inline int __must_check can_set_static_ctrlmode(struct net_device *dev,
132 struct can_priv *priv = netdev_priv(dev);
134 /* alloc_candev() succeeded => netdev_priv() is valid at this point */
135 if (priv->ctrlmode_supported & static_mode) {
137 "Controller features can not be supported and static at the same time\n");
140 priv->ctrlmode = static_mode;
142 /* override MTU which was set by default in can_setup()? */
143 if (static_mode & CAN_CTRLMODE_FD)
144 dev->mtu = CANFD_MTU;
149 static inline u32 can_get_static_ctrlmode(struct can_priv *priv)
151 return priv->ctrlmode & ~priv->ctrlmode_supported;
154 static inline bool can_is_canxl_dev_mtu(unsigned int mtu)
156 return (mtu >= CANXL_MIN_MTU && mtu <= CANXL_MAX_MTU);
159 /* drop skb if it does not contain a valid CAN frame for sending */
160 static inline bool can_dev_dropped_skb(struct net_device *dev, struct sk_buff *skb)
162 struct can_priv *priv = netdev_priv(dev);
164 if (priv->ctrlmode & CAN_CTRLMODE_LISTENONLY) {
165 netdev_info_once(dev,
166 "interface in listen only mode, dropping skb\n");
168 dev->stats.tx_dropped++;
172 return can_dropped_invalid_skb(dev, skb);
175 void can_setup(struct net_device *dev);
177 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
178 unsigned int txqs, unsigned int rxqs);
179 #define alloc_candev(sizeof_priv, echo_skb_max) \
180 alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1)
181 #define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \
182 alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count)
183 void free_candev(struct net_device *dev);
185 /* a candev safe wrapper around netdev_priv */
186 struct can_priv *safe_candev_priv(struct net_device *dev);
188 int open_candev(struct net_device *dev);
189 void close_candev(struct net_device *dev);
190 int can_change_mtu(struct net_device *dev, int new_mtu);
191 int can_eth_ioctl_hwts(struct net_device *netdev, struct ifreq *ifr, int cmd);
192 int can_ethtool_op_get_ts_info_hwts(struct net_device *dev,
193 struct kernel_ethtool_ts_info *info);
195 int register_candev(struct net_device *dev);
196 void unregister_candev(struct net_device *dev);
198 int can_restart_now(struct net_device *dev);
199 void can_bus_off(struct net_device *dev);
201 const char *can_get_state_str(const enum can_state state);
202 void can_state_get_by_berr_counter(const struct net_device *dev,
203 const struct can_berr_counter *bec,
204 enum can_state *tx_state,
205 enum can_state *rx_state);
206 void can_change_state(struct net_device *dev, struct can_frame *cf,
207 enum can_state tx_state, enum can_state rx_state);
210 void of_can_transceiver(struct net_device *dev);
212 static inline void of_can_transceiver(struct net_device *dev) { }
215 extern struct rtnl_link_ops can_link_ops;
216 int can_netlink_register(void);
217 void can_netlink_unregister(void);
219 #endif /* !_CAN_DEV_H */