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/led.h>
21 #include <linux/can/length.h>
22 #include <linux/can/netlink.h>
23 #include <linux/can/skb.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,
42 * CAN common private data
45 struct net_device *dev;
46 struct can_device_stats can_stats;
48 const struct can_bittiming_const *bittiming_const,
49 *data_bittiming_const;
50 struct can_bittiming bittiming, data_bittiming;
51 const struct can_tdc_const *tdc_const;
54 unsigned int bitrate_const_cnt;
55 const u32 *bitrate_const;
56 const u32 *data_bitrate_const;
57 unsigned int data_bitrate_const_cnt;
59 struct can_clock clock;
61 unsigned int termination_const_cnt;
62 const u16 *termination_const;
64 struct gpio_desc *termination_gpio;
65 u16 termination_gpio_ohms[CAN_TERMINATION_GPIO_MAX];
67 unsigned int echo_skb_max;
68 struct sk_buff **echo_skb;
72 /* CAN controller features - see include/uapi/linux/can/netlink.h */
73 u32 ctrlmode; /* current options setting */
74 u32 ctrlmode_supported; /* options that can be modified by netlink */
77 struct delayed_work restart_work;
79 int (*do_set_bittiming)(struct net_device *dev);
80 int (*do_set_data_bittiming)(struct net_device *dev);
81 int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
82 int (*do_set_termination)(struct net_device *dev, u16 term);
83 int (*do_get_state)(const struct net_device *dev,
84 enum can_state *state);
85 int (*do_get_berr_counter)(const struct net_device *dev,
86 struct can_berr_counter *bec);
87 int (*do_get_auto_tdcv)(const struct net_device *dev, u32 *tdcv);
89 #ifdef CONFIG_CAN_LEDS
90 struct led_trigger *tx_led_trig;
91 char tx_led_trig_name[CAN_LED_NAME_SZ];
92 struct led_trigger *rx_led_trig;
93 char rx_led_trig_name[CAN_LED_NAME_SZ];
94 struct led_trigger *rxtx_led_trig;
95 char rxtx_led_trig_name[CAN_LED_NAME_SZ];
99 static inline bool can_tdc_is_enabled(const struct can_priv *priv)
101 return !!(priv->ctrlmode & CAN_CTRLMODE_TDC_MASK);
105 * can_get_relative_tdco() - TDCO relative to the sample point
107 * struct can_tdc::tdco represents the absolute offset from TDCV. Some
108 * controllers use instead an offset relative to the Sample Point (SP)
111 * SSP = TDCV + absolute TDCO
112 * = TDCV + SP + relative TDCO
114 * -+----------- one bit ----------+-- TX pin
115 * |<--- Sample Point --->|
117 * --+----------- one bit ----------+-- RX pin
118 * |<-------- TDCV -------->|
119 * |<------------------------>| absolute TDCO
120 * |<--- Sample Point --->|
121 * | |<->| relative TDCO
122 * |<------------- Secondary Sample Point ------------>|
124 static inline s32 can_get_relative_tdco(const struct can_priv *priv)
126 const struct can_bittiming *dbt = &priv->data_bittiming;
127 s32 sample_point_in_tc = (CAN_SYNC_SEG + dbt->prop_seg +
128 dbt->phase_seg1) * dbt->brp;
130 return (s32)priv->tdc.tdco - sample_point_in_tc;
133 /* helper to define static CAN controller features at device creation time */
134 static inline int __must_check can_set_static_ctrlmode(struct net_device *dev,
137 struct can_priv *priv = netdev_priv(dev);
139 /* alloc_candev() succeeded => netdev_priv() is valid at this point */
140 if (priv->ctrlmode_supported & static_mode) {
142 "Controller features can not be supported and static at the same time\n");
145 priv->ctrlmode = static_mode;
147 /* override MTU which was set by default in can_setup()? */
148 if (static_mode & CAN_CTRLMODE_FD)
149 dev->mtu = CANFD_MTU;
154 static inline u32 can_get_static_ctrlmode(struct can_priv *priv)
156 return priv->ctrlmode & ~priv->ctrlmode_supported;
159 void can_setup(struct net_device *dev);
161 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
162 unsigned int txqs, unsigned int rxqs);
163 #define alloc_candev(sizeof_priv, echo_skb_max) \
164 alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1)
165 #define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \
166 alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count)
167 void free_candev(struct net_device *dev);
169 /* a candev safe wrapper around netdev_priv */
170 struct can_priv *safe_candev_priv(struct net_device *dev);
172 int open_candev(struct net_device *dev);
173 void close_candev(struct net_device *dev);
174 int can_change_mtu(struct net_device *dev, int new_mtu);
176 int register_candev(struct net_device *dev);
177 void unregister_candev(struct net_device *dev);
179 int can_restart_now(struct net_device *dev);
180 void can_bus_off(struct net_device *dev);
182 const char *can_get_state_str(const enum can_state state);
183 void can_change_state(struct net_device *dev, struct can_frame *cf,
184 enum can_state tx_state, enum can_state rx_state);
187 void of_can_transceiver(struct net_device *dev);
189 static inline void of_can_transceiver(struct net_device *dev) { }
192 extern struct rtnl_link_ops can_link_ops;
193 int can_netlink_register(void);
194 void can_netlink_unregister(void);
196 #endif /* !_CAN_DEV_H */