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 <[email protected]> 8 * Varma Electronics Oy 9 * 10 * Copyright (C) 2008 Wolfgang Grandegger <[email protected]> 11 * 12 */ 13 14 #ifndef _CAN_DEV_H 15 #define _CAN_DEV_H 16 17 #include <linux/can.h> 18 #include <linux/can/bittiming.h> 19 #include <linux/can/error.h> 20 #include <linux/can/led.h> 21 #include <linux/can/length.h> 22 #include <linux/can/netlink.h> 23 #include <linux/can/skb.h> 24 #include <linux/netdevice.h> 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 35 /* 36 * CAN common private data 37 */ 38 struct can_priv { 39 struct net_device *dev; 40 struct can_device_stats can_stats; 41 42 struct can_bittiming bittiming, data_bittiming; 43 const struct can_bittiming_const *bittiming_const, 44 *data_bittiming_const; 45 const u16 *termination_const; 46 unsigned int termination_const_cnt; 47 u16 termination; 48 const u32 *bitrate_const; 49 unsigned int bitrate_const_cnt; 50 const u32 *data_bitrate_const; 51 unsigned int data_bitrate_const_cnt; 52 u32 bitrate_max; 53 struct can_clock clock; 54 55 enum can_state state; 56 57 /* CAN controller features - see include/uapi/linux/can/netlink.h */ 58 u32 ctrlmode; /* current options setting */ 59 u32 ctrlmode_supported; /* options that can be modified by netlink */ 60 u32 ctrlmode_static; /* static enabled options for driver/hardware */ 61 62 int restart_ms; 63 struct delayed_work restart_work; 64 65 int (*do_set_bittiming)(struct net_device *dev); 66 int (*do_set_data_bittiming)(struct net_device *dev); 67 int (*do_set_mode)(struct net_device *dev, enum can_mode mode); 68 int (*do_set_termination)(struct net_device *dev, u16 term); 69 int (*do_get_state)(const struct net_device *dev, 70 enum can_state *state); 71 int (*do_get_berr_counter)(const struct net_device *dev, 72 struct can_berr_counter *bec); 73 74 unsigned int echo_skb_max; 75 struct sk_buff **echo_skb; 76 77 #ifdef CONFIG_CAN_LEDS 78 struct led_trigger *tx_led_trig; 79 char tx_led_trig_name[CAN_LED_NAME_SZ]; 80 struct led_trigger *rx_led_trig; 81 char rx_led_trig_name[CAN_LED_NAME_SZ]; 82 struct led_trigger *rxtx_led_trig; 83 char rxtx_led_trig_name[CAN_LED_NAME_SZ]; 84 #endif 85 }; 86 87 /* Check for outgoing skbs that have not been created by the CAN subsystem */ 88 static inline bool can_skb_headroom_valid(struct net_device *dev, 89 struct sk_buff *skb) 90 { 91 /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */ 92 if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv))) 93 return false; 94 95 /* af_packet does not apply CAN skb specific settings */ 96 if (skb->ip_summed == CHECKSUM_NONE) { 97 /* init headroom */ 98 can_skb_prv(skb)->ifindex = dev->ifindex; 99 can_skb_prv(skb)->skbcnt = 0; 100 101 skb->ip_summed = CHECKSUM_UNNECESSARY; 102 103 /* perform proper loopback on capable devices */ 104 if (dev->flags & IFF_ECHO) 105 skb->pkt_type = PACKET_LOOPBACK; 106 else 107 skb->pkt_type = PACKET_HOST; 108 109 skb_reset_mac_header(skb); 110 skb_reset_network_header(skb); 111 skb_reset_transport_header(skb); 112 } 113 114 return true; 115 } 116 117 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */ 118 static inline bool can_dropped_invalid_skb(struct net_device *dev, 119 struct sk_buff *skb) 120 { 121 const struct canfd_frame *cfd = (struct canfd_frame *)skb->data; 122 123 if (skb->protocol == htons(ETH_P_CAN)) { 124 if (unlikely(skb->len != CAN_MTU || 125 cfd->len > CAN_MAX_DLEN)) 126 goto inval_skb; 127 } else if (skb->protocol == htons(ETH_P_CANFD)) { 128 if (unlikely(skb->len != CANFD_MTU || 129 cfd->len > CANFD_MAX_DLEN)) 130 goto inval_skb; 131 } else 132 goto inval_skb; 133 134 if (!can_skb_headroom_valid(dev, skb)) 135 goto inval_skb; 136 137 return false; 138 139 inval_skb: 140 kfree_skb(skb); 141 dev->stats.tx_dropped++; 142 return true; 143 } 144 145 static inline bool can_is_canfd_skb(const struct sk_buff *skb) 146 { 147 /* the CAN specific type of skb is identified by its data length */ 148 return skb->len == CANFD_MTU; 149 } 150 151 /* helper to define static CAN controller features at device creation time */ 152 static inline void can_set_static_ctrlmode(struct net_device *dev, 153 u32 static_mode) 154 { 155 struct can_priv *priv = netdev_priv(dev); 156 157 /* alloc_candev() succeeded => netdev_priv() is valid at this point */ 158 priv->ctrlmode = static_mode; 159 priv->ctrlmode_static = static_mode; 160 161 /* override MTU which was set by default in can_setup()? */ 162 if (static_mode & CAN_CTRLMODE_FD) 163 dev->mtu = CANFD_MTU; 164 } 165 166 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max, 167 unsigned int txqs, unsigned int rxqs); 168 #define alloc_candev(sizeof_priv, echo_skb_max) \ 169 alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1) 170 #define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \ 171 alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count) 172 void free_candev(struct net_device *dev); 173 174 /* a candev safe wrapper around netdev_priv */ 175 struct can_priv *safe_candev_priv(struct net_device *dev); 176 177 int open_candev(struct net_device *dev); 178 void close_candev(struct net_device *dev); 179 int can_change_mtu(struct net_device *dev, int new_mtu); 180 181 int register_candev(struct net_device *dev); 182 void unregister_candev(struct net_device *dev); 183 184 int can_restart_now(struct net_device *dev); 185 void can_bus_off(struct net_device *dev); 186 187 void can_change_state(struct net_device *dev, struct can_frame *cf, 188 enum can_state tx_state, enum can_state rx_state); 189 190 int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, 191 unsigned int idx); 192 struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx, 193 u8 *len_ptr); 194 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx); 195 void can_free_echo_skb(struct net_device *dev, unsigned int idx); 196 197 #ifdef CONFIG_OF 198 void of_can_transceiver(struct net_device *dev); 199 #else 200 static inline void of_can_transceiver(struct net_device *dev) { } 201 #endif 202 203 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf); 204 struct sk_buff *alloc_canfd_skb(struct net_device *dev, 205 struct canfd_frame **cfd); 206 struct sk_buff *alloc_can_err_skb(struct net_device *dev, 207 struct can_frame **cf); 208 209 #endif /* !_CAN_DEV_H */ 210