Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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/error.h>
19#include <linux/can/led.h>
20#include <linux/can/netlink.h>
21#include <linux/can/skb.h>
22#include <linux/netdevice.h>
23
24/*
25 * CAN mode
26 */
27enum can_mode {
28 CAN_MODE_STOP = 0,
29 CAN_MODE_START,
30 CAN_MODE_SLEEP
31};
32
33/*
34 * CAN common private data
35 */
36struct can_priv {
37 struct net_device *dev;
38 struct can_device_stats can_stats;
39
40 struct can_bittiming bittiming, data_bittiming;
41 const struct can_bittiming_const *bittiming_const,
42 *data_bittiming_const;
43 const u16 *termination_const;
44 unsigned int termination_const_cnt;
45 u16 termination;
46 const u32 *bitrate_const;
47 unsigned int bitrate_const_cnt;
48 const u32 *data_bitrate_const;
49 unsigned int data_bitrate_const_cnt;
50 u32 bitrate_max;
51 struct can_clock clock;
52
53 enum can_state state;
54
55 /* CAN controller features - see include/uapi/linux/can/netlink.h */
56 u32 ctrlmode; /* current options setting */
57 u32 ctrlmode_supported; /* options that can be modified by netlink */
58 u32 ctrlmode_static; /* static enabled options for driver/hardware */
59
60 int restart_ms;
61 struct delayed_work restart_work;
62
63 int (*do_set_bittiming)(struct net_device *dev);
64 int (*do_set_data_bittiming)(struct net_device *dev);
65 int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
66 int (*do_set_termination)(struct net_device *dev, u16 term);
67 int (*do_get_state)(const struct net_device *dev,
68 enum can_state *state);
69 int (*do_get_berr_counter)(const struct net_device *dev,
70 struct can_berr_counter *bec);
71
72 unsigned int echo_skb_max;
73 struct sk_buff **echo_skb;
74
75#ifdef CONFIG_CAN_LEDS
76 struct led_trigger *tx_led_trig;
77 char tx_led_trig_name[CAN_LED_NAME_SZ];
78 struct led_trigger *rx_led_trig;
79 char rx_led_trig_name[CAN_LED_NAME_SZ];
80 struct led_trigger *rxtx_led_trig;
81 char rxtx_led_trig_name[CAN_LED_NAME_SZ];
82#endif
83};
84
85#define CAN_SYNC_SEG 1
86
87/*
88 * can_bit_time() - Duration of one bit
89 *
90 * Please refer to ISO 11898-1:2015, section 11.3.1.1 "Bit time" for
91 * additional information.
92 *
93 * Return: the number of time quanta in one bit.
94 */
95static inline unsigned int can_bit_time(const struct can_bittiming *bt)
96{
97 return CAN_SYNC_SEG + bt->prop_seg + bt->phase_seg1 + bt->phase_seg2;
98}
99
100/*
101 * can_cc_dlc2len(value) - convert a given data length code (dlc) of a
102 * Classical CAN frame into a valid data length of max. 8 bytes.
103 *
104 * To be used in the CAN netdriver receive path to ensure conformance with
105 * ISO 11898-1 Chapter 8.4.2.3 (DLC field)
106 */
107#define can_cc_dlc2len(dlc) (min_t(u8, (dlc), CAN_MAX_DLEN))
108
109/* Check for outgoing skbs that have not been created by the CAN subsystem */
110static inline bool can_skb_headroom_valid(struct net_device *dev,
111 struct sk_buff *skb)
112{
113 /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
114 if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
115 return false;
116
117 /* af_packet does not apply CAN skb specific settings */
118 if (skb->ip_summed == CHECKSUM_NONE) {
119 /* init headroom */
120 can_skb_prv(skb)->ifindex = dev->ifindex;
121 can_skb_prv(skb)->skbcnt = 0;
122
123 skb->ip_summed = CHECKSUM_UNNECESSARY;
124
125 /* perform proper loopback on capable devices */
126 if (dev->flags & IFF_ECHO)
127 skb->pkt_type = PACKET_LOOPBACK;
128 else
129 skb->pkt_type = PACKET_HOST;
130
131 skb_reset_mac_header(skb);
132 skb_reset_network_header(skb);
133 skb_reset_transport_header(skb);
134 }
135
136 return true;
137}
138
139/* Drop a given socketbuffer if it does not contain a valid CAN frame. */
140static inline bool can_dropped_invalid_skb(struct net_device *dev,
141 struct sk_buff *skb)
142{
143 const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
144
145 if (skb->protocol == htons(ETH_P_CAN)) {
146 if (unlikely(skb->len != CAN_MTU ||
147 cfd->len > CAN_MAX_DLEN))
148 goto inval_skb;
149 } else if (skb->protocol == htons(ETH_P_CANFD)) {
150 if (unlikely(skb->len != CANFD_MTU ||
151 cfd->len > CANFD_MAX_DLEN))
152 goto inval_skb;
153 } else
154 goto inval_skb;
155
156 if (!can_skb_headroom_valid(dev, skb))
157 goto inval_skb;
158
159 return false;
160
161inval_skb:
162 kfree_skb(skb);
163 dev->stats.tx_dropped++;
164 return true;
165}
166
167static inline bool can_is_canfd_skb(const struct sk_buff *skb)
168{
169 /* the CAN specific type of skb is identified by its data length */
170 return skb->len == CANFD_MTU;
171}
172
173/* helper to get the data length code (DLC) for Classical CAN raw DLC access */
174static inline u8 can_get_cc_dlc(const struct can_frame *cf, const u32 ctrlmode)
175{
176 /* return len8_dlc as dlc value only if all conditions apply */
177 if ((ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC) &&
178 (cf->len == CAN_MAX_DLEN) &&
179 (cf->len8_dlc > CAN_MAX_DLEN && cf->len8_dlc <= CAN_MAX_RAW_DLC))
180 return cf->len8_dlc;
181
182 /* return the payload length as dlc value */
183 return cf->len;
184}
185
186/* helper to set len and len8_dlc value for Classical CAN raw DLC access */
187static inline void can_frame_set_cc_len(struct can_frame *cf, const u8 dlc,
188 const u32 ctrlmode)
189{
190 /* the caller already ensured that dlc is a value from 0 .. 15 */
191 if (ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC && dlc > CAN_MAX_DLEN)
192 cf->len8_dlc = dlc;
193
194 /* limit the payload length 'len' to CAN_MAX_DLEN */
195 cf->len = can_cc_dlc2len(dlc);
196}
197
198/* helper to define static CAN controller features at device creation time */
199static inline void can_set_static_ctrlmode(struct net_device *dev,
200 u32 static_mode)
201{
202 struct can_priv *priv = netdev_priv(dev);
203
204 /* alloc_candev() succeeded => netdev_priv() is valid at this point */
205 priv->ctrlmode = static_mode;
206 priv->ctrlmode_static = static_mode;
207
208 /* override MTU which was set by default in can_setup()? */
209 if (static_mode & CAN_CTRLMODE_FD)
210 dev->mtu = CANFD_MTU;
211}
212
213/* get data length from raw data length code (DLC) */
214u8 can_fd_dlc2len(u8 dlc);
215
216/* map the sanitized data length to an appropriate data length code */
217u8 can_fd_len2dlc(u8 len);
218
219struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
220 unsigned int txqs, unsigned int rxqs);
221#define alloc_candev(sizeof_priv, echo_skb_max) \
222 alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1)
223#define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \
224 alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count)
225void free_candev(struct net_device *dev);
226
227/* a candev safe wrapper around netdev_priv */
228struct can_priv *safe_candev_priv(struct net_device *dev);
229
230int open_candev(struct net_device *dev);
231void close_candev(struct net_device *dev);
232int can_change_mtu(struct net_device *dev, int new_mtu);
233
234int register_candev(struct net_device *dev);
235void unregister_candev(struct net_device *dev);
236
237int can_restart_now(struct net_device *dev);
238void can_bus_off(struct net_device *dev);
239
240void can_change_state(struct net_device *dev, struct can_frame *cf,
241 enum can_state tx_state, enum can_state rx_state);
242
243int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
244 unsigned int idx);
245struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx,
246 u8 *len_ptr);
247unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx);
248void can_free_echo_skb(struct net_device *dev, unsigned int idx);
249
250#ifdef CONFIG_OF
251void of_can_transceiver(struct net_device *dev);
252#else
253static inline void of_can_transceiver(struct net_device *dev) { }
254#endif
255
256struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
257struct sk_buff *alloc_canfd_skb(struct net_device *dev,
258 struct canfd_frame **cfd);
259struct sk_buff *alloc_can_err_skb(struct net_device *dev,
260 struct can_frame **cf);
261
262#endif /* !_CAN_DEV_H */