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 <avolkov@varma-el.com>
8 * Varma Electronics Oy
9 *
10 * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
11 *
12 */
13
14 #ifndef _CAN_DEV_H
15 #define _CAN_DEV_H
16
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>
25
26 /*
27 * CAN mode
28 */
29 enum can_mode {
30 CAN_MODE_STOP = 0,
31 CAN_MODE_START,
32 CAN_MODE_SLEEP
33 };
34
35 enum can_termination_gpio {
36 CAN_TERMINATION_GPIO_DISABLED = 0,
37 CAN_TERMINATION_GPIO_ENABLED,
38 CAN_TERMINATION_GPIO_MAX,
39 };
40
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;
45 struct can_tdc tdc;
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);
50 };
51
52 /*
53 * CAN common private data
54 */
55 struct can_priv {
56 struct net_device *dev;
57 struct can_device_stats can_stats;
58
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;
64 u32 bitrate_max;
65 struct can_clock clock;
66
67 unsigned int termination_const_cnt;
68 const u16 *termination_const;
69 u16 termination;
70 struct gpio_desc *termination_gpio;
71 u16 termination_gpio_ohms[CAN_TERMINATION_GPIO_MAX];
72
73 unsigned int echo_skb_max;
74 struct sk_buff **echo_skb;
75
76 enum can_state state;
77
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 */
81
82 int restart_ms;
83 struct delayed_work restart_work;
84
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);
92 };
93
can_fd_tdc_is_enabled(const struct can_priv * priv)94 static inline bool can_fd_tdc_is_enabled(const struct can_priv *priv)
95 {
96 return !!(priv->ctrlmode & CAN_CTRLMODE_FD_TDC_MASK);
97 }
98
99 /*
100 * can_get_relative_tdco() - TDCO relative to the sample point
101 *
102 * struct can_tdc::tdco represents the absolute offset from TDCV. Some
103 * controllers use instead an offset relative to the Sample Point (SP)
104 * such that:
105 *
106 * SSP = TDCV + absolute TDCO
107 * = TDCV + SP + relative TDCO
108 *
109 * -+----------- one bit ----------+-- TX pin
110 * |<--- Sample Point --->|
111 *
112 * --+----------- one bit ----------+-- RX pin
113 * |<-------- TDCV -------->|
114 * |<------------------------>| absolute TDCO
115 * |<--- Sample Point --->|
116 * | |<->| relative TDCO
117 * |<------------- Secondary Sample Point ------------>|
118 */
can_get_relative_tdco(const struct can_priv * priv)119 static inline s32 can_get_relative_tdco(const struct can_priv *priv)
120 {
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;
124
125 return (s32)priv->fd.tdc.tdco - sample_point_in_tc;
126 }
127
128 /* helper to define static CAN controller features at device creation time */
can_set_static_ctrlmode(struct net_device * dev,u32 static_mode)129 static inline int __must_check can_set_static_ctrlmode(struct net_device *dev,
130 u32 static_mode)
131 {
132 struct can_priv *priv = netdev_priv(dev);
133
134 /* alloc_candev() succeeded => netdev_priv() is valid at this point */
135 if (priv->ctrlmode_supported & static_mode) {
136 netdev_warn(dev,
137 "Controller features can not be supported and static at the same time\n");
138 return -EINVAL;
139 }
140 priv->ctrlmode = static_mode;
141
142 /* override MTU which was set by default in can_setup()? */
143 if (static_mode & CAN_CTRLMODE_FD)
144 dev->mtu = CANFD_MTU;
145
146 return 0;
147 }
148
can_get_static_ctrlmode(struct can_priv * priv)149 static inline u32 can_get_static_ctrlmode(struct can_priv *priv)
150 {
151 return priv->ctrlmode & ~priv->ctrlmode_supported;
152 }
153
can_is_canxl_dev_mtu(unsigned int mtu)154 static inline bool can_is_canxl_dev_mtu(unsigned int mtu)
155 {
156 return (mtu >= CANXL_MIN_MTU && mtu <= CANXL_MAX_MTU);
157 }
158
159 /* drop skb if it does not contain a valid CAN frame for sending */
can_dev_dropped_skb(struct net_device * dev,struct sk_buff * skb)160 static inline bool can_dev_dropped_skb(struct net_device *dev, struct sk_buff *skb)
161 {
162 struct can_priv *priv = netdev_priv(dev);
163
164 if (priv->ctrlmode & CAN_CTRLMODE_LISTENONLY) {
165 netdev_info_once(dev,
166 "interface in listen only mode, dropping skb\n");
167 kfree_skb(skb);
168 dev->stats.tx_dropped++;
169 return true;
170 }
171
172 return can_dropped_invalid_skb(dev, skb);
173 }
174
175 void can_setup(struct net_device *dev);
176
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);
184
185 /* a candev safe wrapper around netdev_priv */
186 struct can_priv *safe_candev_priv(struct net_device *dev);
187
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);
194
195 int register_candev(struct net_device *dev);
196 void unregister_candev(struct net_device *dev);
197
198 int can_restart_now(struct net_device *dev);
199 void can_bus_off(struct net_device *dev);
200
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);
208
209 #ifdef CONFIG_OF
210 void of_can_transceiver(struct net_device *dev);
211 #else
of_can_transceiver(struct net_device * dev)212 static inline void of_can_transceiver(struct net_device *dev) { }
213 #endif
214
215 extern struct rtnl_link_ops can_link_ops;
216 int can_netlink_register(void);
217 void can_netlink_unregister(void);
218
219 #endif /* !_CAN_DEV_H */
220