Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
39549eef WG |
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 | * | |
39549eef WG |
12 | */ |
13 | ||
42193e3e OH |
14 | #ifndef _CAN_DEV_H |
15 | #define _CAN_DEV_H | |
39549eef | 16 | |
829e0015 | 17 | #include <linux/can.h> |
5a9d5ecd | 18 | #include <linux/can/bittiming.h> |
39549eef | 19 | #include <linux/can/error.h> |
bdd2e413 | 20 | #include <linux/can/length.h> |
2785968c | 21 | #include <linux/can/netlink.h> |
e7153bf7 | 22 | #include <linux/can/skb.h> |
7fb48d25 | 23 | #include <linux/ethtool.h> |
2785968c | 24 | #include <linux/netdevice.h> |
39549eef WG |
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 | ||
6e86a154 OR |
35 | enum can_termination_gpio { |
36 | CAN_TERMINATION_GPIO_DISABLED = 0, | |
37 | CAN_TERMINATION_GPIO_ENABLED, | |
38 | CAN_TERMINATION_GPIO_MAX, | |
39 | }; | |
40 | ||
b803c4a4 VM |
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 | ||
39549eef WG |
52 | /* |
53 | * CAN common private data | |
54 | */ | |
39549eef | 55 | struct can_priv { |
9abefcb1 | 56 | struct net_device *dev; |
39549eef WG |
57 | struct can_device_stats can_stats; |
58 | ||
b803c4a4 VM |
59 | const struct can_bittiming_const *bittiming_const; |
60 | struct can_bittiming bittiming; | |
61 | struct data_bittiming_params fd; | |
431af779 | 62 | unsigned int bitrate_const_cnt; |
4c9258dd | 63 | const u32 *bitrate_const; |
2290aefa | 64 | u32 bitrate_max; |
39549eef WG |
65 | struct can_clock clock; |
66 | ||
4c9258dd VM |
67 | unsigned int termination_const_cnt; |
68 | const u16 *termination_const; | |
69 | u16 termination; | |
6e86a154 OR |
70 | struct gpio_desc *termination_gpio; |
71 | u16 termination_gpio_ohms[CAN_TERMINATION_GPIO_MAX]; | |
4c9258dd | 72 | |
5fe1be81 VM |
73 | unsigned int echo_skb_max; |
74 | struct sk_buff **echo_skb; | |
75 | ||
39549eef | 76 | enum can_state state; |
bb208f14 OH |
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 */ | |
39549eef WG |
81 | |
82 | int restart_ms; | |
9abefcb1 | 83 | struct delayed_work restart_work; |
39549eef | 84 | |
39549eef WG |
85 | int (*do_set_bittiming)(struct net_device *dev); |
86 | int (*do_set_mode)(struct net_device *dev, enum can_mode mode); | |
12a6075c | 87 | int (*do_set_termination)(struct net_device *dev, u16 term); |
39549eef WG |
88 | int (*do_get_state)(const struct net_device *dev, |
89 | enum can_state *state); | |
52c793f2 WG |
90 | int (*do_get_berr_counter)(const struct net_device *dev, |
91 | struct can_berr_counter *bec); | |
39549eef WG |
92 | }; |
93 | ||
63dfe070 VM |
94 | static inline bool can_tdc_is_enabled(const struct can_priv *priv) |
95 | { | |
96 | return !!(priv->ctrlmode & CAN_CTRLMODE_TDC_MASK); | |
97 | } | |
98e69016 | 98 | |
fa759a93 VM |
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 | */ | |
119 | static inline s32 can_get_relative_tdco(const struct can_priv *priv) | |
120 | { | |
b803c4a4 | 121 | const struct can_bittiming *dbt = &priv->fd.data_bittiming; |
fa759a93 VM |
122 | s32 sample_point_in_tc = (CAN_SYNC_SEG + dbt->prop_seg + |
123 | dbt->phase_seg1) * dbt->brp; | |
124 | ||
b803c4a4 | 125 | return (s32)priv->fd.tdc.tdco - sample_point_in_tc; |
fa759a93 VM |
126 | } |
127 | ||
bb208f14 | 128 | /* helper to define static CAN controller features at device creation time */ |
7d4a101c VM |
129 | static inline int __must_check can_set_static_ctrlmode(struct net_device *dev, |
130 | u32 static_mode) | |
bb208f14 OH |
131 | { |
132 | struct can_priv *priv = netdev_priv(dev); | |
133 | ||
134 | /* alloc_candev() succeeded => netdev_priv() is valid at this point */ | |
7d4a101c VM |
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 | } | |
bb208f14 | 140 | priv->ctrlmode = static_mode; |
bb208f14 OH |
141 | |
142 | /* override MTU which was set by default in can_setup()? */ | |
143 | if (static_mode & CAN_CTRLMODE_FD) | |
144 | dev->mtu = CANFD_MTU; | |
7d4a101c VM |
145 | |
146 | return 0; | |
bb208f14 OH |
147 | } |
148 | ||
c9e1d8ed VM |
149 | static inline u32 can_get_static_ctrlmode(struct can_priv *priv) |
150 | { | |
151 | return priv->ctrlmode & ~priv->ctrlmode_supported; | |
152 | } | |
153 | ||
ebf87fc7 OH |
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 | ||
ae64438b OH |
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) | |
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 | ||
0a042c6e MKB |
175 | void can_setup(struct net_device *dev); |
176 | ||
03870907 ZY |
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) | |
39549eef WG |
183 | void free_candev(struct net_device *dev); |
184 | ||
bf03a537 KVD |
185 | /* a candev safe wrapper around netdev_priv */ |
186 | struct can_priv *safe_candev_priv(struct net_device *dev); | |
187 | ||
39549eef WG |
188 | int open_candev(struct net_device *dev); |
189 | void close_candev(struct net_device *dev); | |
bc05a894 | 190 | int can_change_mtu(struct net_device *dev, int new_mtu); |
90f942c5 | 191 | int can_eth_ioctl_hwts(struct net_device *netdev, struct ifreq *ifr, int cmd); |
7fb48d25 | 192 | int can_ethtool_op_get_ts_info_hwts(struct net_device *dev, |
2111375b | 193 | struct kernel_ethtool_ts_info *info); |
39549eef WG |
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 | ||
6fe27d68 | 201 | const char *can_get_state_str(const enum can_state state); |
9beebc2b MKB |
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); | |
bac78aab AY |
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 | ||
2290aefa FCJ |
209 | #ifdef CONFIG_OF |
210 | void of_can_transceiver(struct net_device *dev); | |
211 | #else | |
212 | static inline void of_can_transceiver(struct net_device *dev) { } | |
213 | #endif | |
214 | ||
0a042c6e MKB |
215 | extern struct rtnl_link_ops can_link_ops; |
216 | int can_netlink_register(void); | |
217 | void can_netlink_unregister(void); | |
218 | ||
42193e3e | 219 | #endif /* !_CAN_DEV_H */ |