4 * Definitions for the CAN network device driver interface
6 * Copyright (C) 2006 Andrey Volkov <avolkov@varma-el.com>
9 * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
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/can/skb.h>
21 #include <linux/netdevice.h>
33 * CAN common private data
36 struct net_device *dev;
37 struct can_device_stats can_stats;
39 struct can_bittiming bittiming, data_bittiming;
40 const struct can_bittiming_const *bittiming_const,
41 *data_bittiming_const;
42 struct can_clock clock;
46 /* CAN controller features - see include/uapi/linux/can/netlink.h */
47 u32 ctrlmode; /* current options setting */
48 u32 ctrlmode_supported; /* options that can be modified by netlink */
49 u32 ctrlmode_static; /* static enabled options for driver/hardware */
52 struct delayed_work restart_work;
54 int (*do_set_bittiming)(struct net_device *dev);
55 int (*do_set_data_bittiming)(struct net_device *dev);
56 int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
57 int (*do_get_state)(const struct net_device *dev,
58 enum can_state *state);
59 int (*do_get_berr_counter)(const struct net_device *dev,
60 struct can_berr_counter *bec);
62 unsigned int echo_skb_max;
63 struct sk_buff **echo_skb;
65 #ifdef CONFIG_CAN_LEDS
66 struct led_trigger *tx_led_trig;
67 char tx_led_trig_name[CAN_LED_NAME_SZ];
68 struct led_trigger *rx_led_trig;
69 char rx_led_trig_name[CAN_LED_NAME_SZ];
70 struct led_trigger *rxtx_led_trig;
71 char rxtx_led_trig_name[CAN_LED_NAME_SZ];
76 * get_can_dlc(value) - helper macro to cast a given data length code (dlc)
77 * to __u8 and ensure the dlc value to be max. 8 bytes.
79 * To be used in the CAN netdriver receive path to ensure conformance with
80 * ISO 11898-1 Chapter 8.4.2.3 (DLC field)
82 #define get_can_dlc(i) (min_t(__u8, (i), CAN_MAX_DLC))
83 #define get_canfd_dlc(i) (min_t(__u8, (i), CANFD_MAX_DLC))
85 /* Check for outgoing skbs that have not been created by the CAN subsystem */
86 static inline bool can_skb_headroom_valid(struct net_device *dev,
89 /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
90 if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
93 /* af_packet does not apply CAN skb specific settings */
94 if (skb->ip_summed == CHECKSUM_NONE) {
96 can_skb_prv(skb)->ifindex = dev->ifindex;
97 can_skb_prv(skb)->skbcnt = 0;
99 skb->ip_summed = CHECKSUM_UNNECESSARY;
101 /* preform proper loopback on capable devices */
102 if (dev->flags & IFF_ECHO)
103 skb->pkt_type = PACKET_LOOPBACK;
105 skb->pkt_type = PACKET_HOST;
107 skb_reset_mac_header(skb);
108 skb_reset_network_header(skb);
109 skb_reset_transport_header(skb);
115 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
116 static inline bool can_dropped_invalid_skb(struct net_device *dev,
119 const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
121 if (skb->protocol == htons(ETH_P_CAN)) {
122 if (unlikely(skb->len != CAN_MTU ||
123 cfd->len > CAN_MAX_DLEN))
125 } else if (skb->protocol == htons(ETH_P_CANFD)) {
126 if (unlikely(skb->len != CANFD_MTU ||
127 cfd->len > CANFD_MAX_DLEN))
132 if (!can_skb_headroom_valid(dev, skb))
139 dev->stats.tx_dropped++;
143 static inline bool can_is_canfd_skb(const struct sk_buff *skb)
145 /* the CAN specific type of skb is identified by its data length */
146 return skb->len == CANFD_MTU;
149 /* helper to define static CAN controller features at device creation time */
150 static inline void can_set_static_ctrlmode(struct net_device *dev,
153 struct can_priv *priv = netdev_priv(dev);
155 /* alloc_candev() succeeded => netdev_priv() is valid at this point */
156 priv->ctrlmode = static_mode;
157 priv->ctrlmode_static = static_mode;
159 /* override MTU which was set by default in can_setup()? */
160 if (static_mode & CAN_CTRLMODE_FD)
161 dev->mtu = CANFD_MTU;
164 /* get data length from can_dlc with sanitized can_dlc */
165 u8 can_dlc2len(u8 can_dlc);
167 /* map the sanitized data length to an appropriate data length code */
168 u8 can_len2dlc(u8 len);
170 struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max);
171 void free_candev(struct net_device *dev);
173 /* a candev safe wrapper around netdev_priv */
174 struct can_priv *safe_candev_priv(struct net_device *dev);
176 int open_candev(struct net_device *dev);
177 void close_candev(struct net_device *dev);
178 int can_change_mtu(struct net_device *dev, int new_mtu);
180 int register_candev(struct net_device *dev);
181 void unregister_candev(struct net_device *dev);
183 int can_restart_now(struct net_device *dev);
184 void can_bus_off(struct net_device *dev);
186 void can_change_state(struct net_device *dev, struct can_frame *cf,
187 enum can_state tx_state, enum can_state rx_state);
189 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
191 struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr);
192 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx);
193 void can_free_echo_skb(struct net_device *dev, unsigned int idx);
195 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
196 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
197 struct canfd_frame **cfd);
198 struct sk_buff *alloc_can_err_skb(struct net_device *dev,
199 struct can_frame **cf);
201 #endif /* !_CAN_DEV_H */