xref: /linux-6.15/include/linux/can/dev.h (revision 9d64fc08)
1 /*
2  * linux/can/dev.h
3  *
4  * Definitions for the CAN network device driver interface
5  *
6  * Copyright (C) 2006 Andrey Volkov <[email protected]>
7  *               Varma Electronics Oy
8  *
9  * Copyright (C) 2008 Wolfgang Grandegger <[email protected]>
10  *
11  */
12 
13 #ifndef _CAN_DEV_H
14 #define _CAN_DEV_H
15 
16 #include <linux/can.h>
17 #include <linux/can/error.h>
18 #include <linux/can/led.h>
19 #include <linux/can/netlink.h>
20 #include <linux/netdevice.h>
21 
22 /*
23  * CAN mode
24  */
25 enum can_mode {
26 	CAN_MODE_STOP = 0,
27 	CAN_MODE_START,
28 	CAN_MODE_SLEEP
29 };
30 
31 /*
32  * CAN common private data
33  */
34 struct can_priv {
35 	struct net_device *dev;
36 	struct can_device_stats can_stats;
37 
38 	struct can_bittiming bittiming, data_bittiming;
39 	const struct can_bittiming_const *bittiming_const,
40 		*data_bittiming_const;
41 	const u16 *termination_const;
42 	unsigned int termination_const_cnt;
43 	u16 termination;
44 	const u32 *bitrate_const;
45 	unsigned int bitrate_const_cnt;
46 	const u32 *data_bitrate_const;
47 	unsigned int data_bitrate_const_cnt;
48 	struct can_clock clock;
49 
50 	enum can_state state;
51 
52 	/* CAN controller features - see include/uapi/linux/can/netlink.h */
53 	u32 ctrlmode;		/* current options setting */
54 	u32 ctrlmode_supported;	/* options that can be modified by netlink */
55 	u32 ctrlmode_static;	/* static enabled options for driver/hardware */
56 
57 	int restart_ms;
58 	struct delayed_work restart_work;
59 
60 	int (*do_set_bittiming)(struct net_device *dev);
61 	int (*do_set_data_bittiming)(struct net_device *dev);
62 	int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
63 	int (*do_set_termination)(struct net_device *dev, u16 term);
64 	int (*do_get_state)(const struct net_device *dev,
65 			    enum can_state *state);
66 	int (*do_get_berr_counter)(const struct net_device *dev,
67 				   struct can_berr_counter *bec);
68 
69 	unsigned int echo_skb_max;
70 	struct sk_buff **echo_skb;
71 
72 #ifdef CONFIG_CAN_LEDS
73 	struct led_trigger *tx_led_trig;
74 	char tx_led_trig_name[CAN_LED_NAME_SZ];
75 	struct led_trigger *rx_led_trig;
76 	char rx_led_trig_name[CAN_LED_NAME_SZ];
77 	struct led_trigger *rxtx_led_trig;
78 	char rxtx_led_trig_name[CAN_LED_NAME_SZ];
79 #endif
80 };
81 
82 /*
83  * get_can_dlc(value) - helper macro to cast a given data length code (dlc)
84  * to __u8 and ensure the dlc value to be max. 8 bytes.
85  *
86  * To be used in the CAN netdriver receive path to ensure conformance with
87  * ISO 11898-1 Chapter 8.4.2.3 (DLC field)
88  */
89 #define get_can_dlc(i)		(min_t(__u8, (i), CAN_MAX_DLC))
90 #define get_canfd_dlc(i)	(min_t(__u8, (i), CANFD_MAX_DLC))
91 
92 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
93 static inline bool can_dropped_invalid_skb(struct net_device *dev,
94 					  struct sk_buff *skb)
95 {
96 	const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
97 
98 	if (skb->protocol == htons(ETH_P_CAN)) {
99 		if (unlikely(skb->len != CAN_MTU ||
100 			     cfd->len > CAN_MAX_DLEN))
101 			goto inval_skb;
102 	} else if (skb->protocol == htons(ETH_P_CANFD)) {
103 		if (unlikely(skb->len != CANFD_MTU ||
104 			     cfd->len > CANFD_MAX_DLEN))
105 			goto inval_skb;
106 	} else
107 		goto inval_skb;
108 
109 	return false;
110 
111 inval_skb:
112 	kfree_skb(skb);
113 	dev->stats.tx_dropped++;
114 	return true;
115 }
116 
117 static inline bool can_is_canfd_skb(const struct sk_buff *skb)
118 {
119 	/* the CAN specific type of skb is identified by its data length */
120 	return skb->len == CANFD_MTU;
121 }
122 
123 /* helper to define static CAN controller features at device creation time */
124 static inline void can_set_static_ctrlmode(struct net_device *dev,
125 					   u32 static_mode)
126 {
127 	struct can_priv *priv = netdev_priv(dev);
128 
129 	/* alloc_candev() succeeded => netdev_priv() is valid at this point */
130 	priv->ctrlmode = static_mode;
131 	priv->ctrlmode_static = static_mode;
132 
133 	/* override MTU which was set by default in can_setup()? */
134 	if (static_mode & CAN_CTRLMODE_FD)
135 		dev->mtu = CANFD_MTU;
136 }
137 
138 /* get data length from can_dlc with sanitized can_dlc */
139 u8 can_dlc2len(u8 can_dlc);
140 
141 /* map the sanitized data length to an appropriate data length code */
142 u8 can_len2dlc(u8 len);
143 
144 struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max);
145 void free_candev(struct net_device *dev);
146 
147 /* a candev safe wrapper around netdev_priv */
148 struct can_priv *safe_candev_priv(struct net_device *dev);
149 
150 int open_candev(struct net_device *dev);
151 void close_candev(struct net_device *dev);
152 int can_change_mtu(struct net_device *dev, int new_mtu);
153 
154 int register_candev(struct net_device *dev);
155 void unregister_candev(struct net_device *dev);
156 
157 int can_restart_now(struct net_device *dev);
158 void can_bus_off(struct net_device *dev);
159 
160 void can_change_state(struct net_device *dev, struct can_frame *cf,
161 		      enum can_state tx_state, enum can_state rx_state);
162 
163 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
164 		      unsigned int idx);
165 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx);
166 void can_free_echo_skb(struct net_device *dev, unsigned int idx);
167 
168 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
169 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
170 				struct canfd_frame **cfd);
171 struct sk_buff *alloc_can_err_skb(struct net_device *dev,
172 				  struct can_frame **cf);
173 
174 #endif /* !_CAN_DEV_H */
175