1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Definitions for the CAN network device driver interface
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>
34 * CAN common private data
37 struct net_device *dev;
38 struct can_device_stats can_stats;
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;
46 const u32 *bitrate_const;
47 unsigned int bitrate_const_cnt;
48 const u32 *data_bitrate_const;
49 unsigned int data_bitrate_const_cnt;
51 struct can_clock clock;
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 */
61 struct delayed_work restart_work;
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);
72 unsigned int echo_skb_max;
73 struct sk_buff **echo_skb;
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];
86 * get_can_dlc(value) - helper macro to cast a given data length code (dlc)
87 * to __u8 and ensure the dlc value to be max. 8 bytes.
89 * To be used in the CAN netdriver receive path to ensure conformance with
90 * ISO 11898-1 Chapter 8.4.2.3 (DLC field)
92 #define get_can_dlc(i) (min_t(__u8, (i), CAN_MAX_DLC))
93 #define get_canfd_dlc(i) (min_t(__u8, (i), CANFD_MAX_DLC))
95 /* Check for outgoing skbs that have not been created by the CAN subsystem */
96 static inline bool can_skb_headroom_valid(struct net_device *dev,
99 /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
100 if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
103 /* af_packet does not apply CAN skb specific settings */
104 if (skb->ip_summed == CHECKSUM_NONE) {
106 can_skb_prv(skb)->ifindex = dev->ifindex;
107 can_skb_prv(skb)->skbcnt = 0;
109 skb->ip_summed = CHECKSUM_UNNECESSARY;
111 /* perform proper loopback on capable devices */
112 if (dev->flags & IFF_ECHO)
113 skb->pkt_type = PACKET_LOOPBACK;
115 skb->pkt_type = PACKET_HOST;
117 skb_reset_mac_header(skb);
118 skb_reset_network_header(skb);
119 skb_reset_transport_header(skb);
125 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
126 static inline bool can_dropped_invalid_skb(struct net_device *dev,
129 const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
131 if (skb->protocol == htons(ETH_P_CAN)) {
132 if (unlikely(skb->len != CAN_MTU ||
133 cfd->len > CAN_MAX_DLEN))
135 } else if (skb->protocol == htons(ETH_P_CANFD)) {
136 if (unlikely(skb->len != CANFD_MTU ||
137 cfd->len > CANFD_MAX_DLEN))
142 if (!can_skb_headroom_valid(dev, skb))
149 dev->stats.tx_dropped++;
153 static inline bool can_is_canfd_skb(const struct sk_buff *skb)
155 /* the CAN specific type of skb is identified by its data length */
156 return skb->len == CANFD_MTU;
159 /* helper to define static CAN controller features at device creation time */
160 static inline void can_set_static_ctrlmode(struct net_device *dev,
163 struct can_priv *priv = netdev_priv(dev);
165 /* alloc_candev() succeeded => netdev_priv() is valid at this point */
166 priv->ctrlmode = static_mode;
167 priv->ctrlmode_static = static_mode;
169 /* override MTU which was set by default in can_setup()? */
170 if (static_mode & CAN_CTRLMODE_FD)
171 dev->mtu = CANFD_MTU;
174 /* get data length from can_dlc with sanitized can_dlc */
175 u8 can_dlc2len(u8 can_dlc);
177 /* map the sanitized data length to an appropriate data length code */
178 u8 can_len2dlc(u8 len);
180 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
181 unsigned int txqs, unsigned int rxqs);
182 #define alloc_candev(sizeof_priv, echo_skb_max) \
183 alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1)
184 #define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \
185 alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count)
186 void free_candev(struct net_device *dev);
188 /* a candev safe wrapper around netdev_priv */
189 struct can_priv *safe_candev_priv(struct net_device *dev);
191 int open_candev(struct net_device *dev);
192 void close_candev(struct net_device *dev);
193 int can_change_mtu(struct net_device *dev, int new_mtu);
195 int register_candev(struct net_device *dev);
196 void unregister_candev(struct net_device *dev);
198 int can_restart_now(struct net_device *dev);
199 void can_bus_off(struct net_device *dev);
201 void can_change_state(struct net_device *dev, struct can_frame *cf,
202 enum can_state tx_state, enum can_state rx_state);
204 int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
206 struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx,
208 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx);
209 void can_free_echo_skb(struct net_device *dev, unsigned int idx);
212 void of_can_transceiver(struct net_device *dev);
214 static inline void of_can_transceiver(struct net_device *dev) { }
217 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
218 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
219 struct canfd_frame **cfd);
220 struct sk_buff *alloc_can_err_skb(struct net_device *dev,
221 struct can_frame **cf);
223 #endif /* !_CAN_DEV_H */