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 | ||
39549eef WG |
41 | /* |
42 | * CAN common private data | |
43 | */ | |
39549eef | 44 | struct can_priv { |
9abefcb1 | 45 | struct net_device *dev; |
39549eef WG |
46 | struct can_device_stats can_stats; |
47 | ||
9859ccd2 OH |
48 | const struct can_bittiming_const *bittiming_const, |
49 | *data_bittiming_const; | |
4c9258dd | 50 | struct can_bittiming bittiming, data_bittiming; |
289ea9e4 | 51 | const struct can_tdc_const *tdc_const; |
4c9258dd | 52 | struct can_tdc tdc; |
289ea9e4 | 53 | |
431af779 | 54 | unsigned int bitrate_const_cnt; |
4c9258dd | 55 | const u32 *bitrate_const; |
431af779 MKB |
56 | const u32 *data_bitrate_const; |
57 | unsigned int data_bitrate_const_cnt; | |
2290aefa | 58 | u32 bitrate_max; |
39549eef WG |
59 | struct can_clock clock; |
60 | ||
4c9258dd VM |
61 | unsigned int termination_const_cnt; |
62 | const u16 *termination_const; | |
63 | u16 termination; | |
6e86a154 OR |
64 | struct gpio_desc *termination_gpio; |
65 | u16 termination_gpio_ohms[CAN_TERMINATION_GPIO_MAX]; | |
4c9258dd | 66 | |
5fe1be81 VM |
67 | unsigned int echo_skb_max; |
68 | struct sk_buff **echo_skb; | |
69 | ||
39549eef | 70 | enum can_state state; |
bb208f14 OH |
71 | |
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 */ | |
39549eef WG |
75 | |
76 | int restart_ms; | |
9abefcb1 | 77 | struct delayed_work restart_work; |
39549eef | 78 | |
39549eef | 79 | int (*do_set_bittiming)(struct net_device *dev); |
9859ccd2 | 80 | int (*do_set_data_bittiming)(struct net_device *dev); |
39549eef | 81 | int (*do_set_mode)(struct net_device *dev, enum can_mode mode); |
12a6075c | 82 | int (*do_set_termination)(struct net_device *dev, u16 term); |
39549eef WG |
83 | int (*do_get_state)(const struct net_device *dev, |
84 | enum can_state *state); | |
52c793f2 WG |
85 | int (*do_get_berr_counter)(const struct net_device *dev, |
86 | struct can_berr_counter *bec); | |
e8060f08 | 87 | int (*do_get_auto_tdcv)(const struct net_device *dev, u32 *tdcv); |
39549eef WG |
88 | }; |
89 | ||
63dfe070 VM |
90 | static inline bool can_tdc_is_enabled(const struct can_priv *priv) |
91 | { | |
92 | return !!(priv->ctrlmode & CAN_CTRLMODE_TDC_MASK); | |
93 | } | |
98e69016 | 94 | |
fa759a93 VM |
95 | /* |
96 | * can_get_relative_tdco() - TDCO relative to the sample point | |
97 | * | |
98 | * struct can_tdc::tdco represents the absolute offset from TDCV. Some | |
99 | * controllers use instead an offset relative to the Sample Point (SP) | |
100 | * such that: | |
101 | * | |
102 | * SSP = TDCV + absolute TDCO | |
103 | * = TDCV + SP + relative TDCO | |
104 | * | |
105 | * -+----------- one bit ----------+-- TX pin | |
106 | * |<--- Sample Point --->| | |
107 | * | |
108 | * --+----------- one bit ----------+-- RX pin | |
109 | * |<-------- TDCV -------->| | |
110 | * |<------------------------>| absolute TDCO | |
111 | * |<--- Sample Point --->| | |
112 | * | |<->| relative TDCO | |
113 | * |<------------- Secondary Sample Point ------------>| | |
114 | */ | |
115 | static inline s32 can_get_relative_tdco(const struct can_priv *priv) | |
116 | { | |
117 | const struct can_bittiming *dbt = &priv->data_bittiming; | |
118 | s32 sample_point_in_tc = (CAN_SYNC_SEG + dbt->prop_seg + | |
119 | dbt->phase_seg1) * dbt->brp; | |
120 | ||
121 | return (s32)priv->tdc.tdco - sample_point_in_tc; | |
122 | } | |
123 | ||
bb208f14 | 124 | /* helper to define static CAN controller features at device creation time */ |
7d4a101c VM |
125 | static inline int __must_check can_set_static_ctrlmode(struct net_device *dev, |
126 | u32 static_mode) | |
bb208f14 OH |
127 | { |
128 | struct can_priv *priv = netdev_priv(dev); | |
129 | ||
130 | /* alloc_candev() succeeded => netdev_priv() is valid at this point */ | |
7d4a101c VM |
131 | if (priv->ctrlmode_supported & static_mode) { |
132 | netdev_warn(dev, | |
133 | "Controller features can not be supported and static at the same time\n"); | |
134 | return -EINVAL; | |
135 | } | |
bb208f14 | 136 | priv->ctrlmode = static_mode; |
bb208f14 OH |
137 | |
138 | /* override MTU which was set by default in can_setup()? */ | |
139 | if (static_mode & CAN_CTRLMODE_FD) | |
140 | dev->mtu = CANFD_MTU; | |
7d4a101c VM |
141 | |
142 | return 0; | |
bb208f14 OH |
143 | } |
144 | ||
c9e1d8ed VM |
145 | static inline u32 can_get_static_ctrlmode(struct can_priv *priv) |
146 | { | |
147 | return priv->ctrlmode & ~priv->ctrlmode_supported; | |
148 | } | |
149 | ||
ebf87fc7 OH |
150 | static inline bool can_is_canxl_dev_mtu(unsigned int mtu) |
151 | { | |
152 | return (mtu >= CANXL_MIN_MTU && mtu <= CANXL_MAX_MTU); | |
153 | } | |
154 | ||
ae64438b OH |
155 | /* drop skb if it does not contain a valid CAN frame for sending */ |
156 | static inline bool can_dev_dropped_skb(struct net_device *dev, struct sk_buff *skb) | |
157 | { | |
158 | struct can_priv *priv = netdev_priv(dev); | |
159 | ||
160 | if (priv->ctrlmode & CAN_CTRLMODE_LISTENONLY) { | |
161 | netdev_info_once(dev, | |
162 | "interface in listen only mode, dropping skb\n"); | |
163 | kfree_skb(skb); | |
164 | dev->stats.tx_dropped++; | |
165 | return true; | |
166 | } | |
167 | ||
168 | return can_dropped_invalid_skb(dev, skb); | |
169 | } | |
170 | ||
0a042c6e MKB |
171 | void can_setup(struct net_device *dev); |
172 | ||
03870907 ZY |
173 | struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max, |
174 | unsigned int txqs, unsigned int rxqs); | |
175 | #define alloc_candev(sizeof_priv, echo_skb_max) \ | |
176 | alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1) | |
177 | #define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \ | |
178 | alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count) | |
39549eef WG |
179 | void free_candev(struct net_device *dev); |
180 | ||
bf03a537 KVD |
181 | /* a candev safe wrapper around netdev_priv */ |
182 | struct can_priv *safe_candev_priv(struct net_device *dev); | |
183 | ||
39549eef WG |
184 | int open_candev(struct net_device *dev); |
185 | void close_candev(struct net_device *dev); | |
bc05a894 | 186 | int can_change_mtu(struct net_device *dev, int new_mtu); |
90f942c5 | 187 | int can_eth_ioctl_hwts(struct net_device *netdev, struct ifreq *ifr, int cmd); |
7fb48d25 VM |
188 | int can_ethtool_op_get_ts_info_hwts(struct net_device *dev, |
189 | struct ethtool_ts_info *info); | |
39549eef WG |
190 | |
191 | int register_candev(struct net_device *dev); | |
192 | void unregister_candev(struct net_device *dev); | |
193 | ||
194 | int can_restart_now(struct net_device *dev); | |
195 | void can_bus_off(struct net_device *dev); | |
196 | ||
6fe27d68 | 197 | const char *can_get_state_str(const enum can_state state); |
bac78aab AY |
198 | void can_change_state(struct net_device *dev, struct can_frame *cf, |
199 | enum can_state tx_state, enum can_state rx_state); | |
200 | ||
2290aefa FCJ |
201 | #ifdef CONFIG_OF |
202 | void of_can_transceiver(struct net_device *dev); | |
203 | #else | |
204 | static inline void of_can_transceiver(struct net_device *dev) { } | |
205 | #endif | |
206 | ||
0a042c6e MKB |
207 | extern struct rtnl_link_ops can_link_ops; |
208 | int can_netlink_register(void); | |
209 | void can_netlink_unregister(void); | |
210 | ||
42193e3e | 211 | #endif /* !_CAN_DEV_H */ |