1 /* SPDX-License-Identifier: GPL-2.0 */ << 2 /* 1 /* 3 * linux/can/dev.h 2 * linux/can/dev.h 4 * 3 * 5 * Definitions for the CAN network device driv 4 * Definitions for the CAN network device driver interface 6 * 5 * 7 * Copyright (C) 2006 Andrey Volkov <avolkov@v 6 * Copyright (C) 2006 Andrey Volkov <avolkov@varma-el.com> 8 * Varma Electronics Oy 7 * Varma Electronics Oy 9 * 8 * 10 * Copyright (C) 2008 Wolfgang Grandegger <wg@ 9 * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com> 11 * 10 * 12 */ 11 */ 13 12 14 #ifndef _CAN_DEV_H 13 #ifndef _CAN_DEV_H 15 #define _CAN_DEV_H 14 #define _CAN_DEV_H 16 15 17 #include <linux/can.h> 16 #include <linux/can.h> 18 #include <linux/can/bittiming.h> << 19 #include <linux/can/error.h> 17 #include <linux/can/error.h> 20 #include <linux/can/length.h> !! 18 #include <linux/can/led.h> 21 #include <linux/can/netlink.h> 19 #include <linux/can/netlink.h> 22 #include <linux/can/skb.h> << 23 #include <linux/ethtool.h> << 24 #include <linux/netdevice.h> 20 #include <linux/netdevice.h> 25 21 26 /* 22 /* 27 * CAN mode 23 * CAN mode 28 */ 24 */ 29 enum can_mode { 25 enum can_mode { 30 CAN_MODE_STOP = 0, 26 CAN_MODE_STOP = 0, 31 CAN_MODE_START, 27 CAN_MODE_START, 32 CAN_MODE_SLEEP 28 CAN_MODE_SLEEP 33 }; 29 }; 34 30 35 enum can_termination_gpio { << 36 CAN_TERMINATION_GPIO_DISABLED = 0, << 37 CAN_TERMINATION_GPIO_ENABLED, << 38 CAN_TERMINATION_GPIO_MAX, << 39 }; << 40 << 41 /* 31 /* 42 * CAN common private data 32 * CAN common private data 43 */ 33 */ 44 struct can_priv { 34 struct can_priv { 45 struct net_device *dev; 35 struct net_device *dev; 46 struct can_device_stats can_stats; 36 struct can_device_stats can_stats; 47 37 >> 38 struct can_bittiming bittiming, data_bittiming; 48 const struct can_bittiming_const *bitt 39 const struct can_bittiming_const *bittiming_const, 49 *data_bittiming_const; 40 *data_bittiming_const; 50 struct can_bittiming bittiming, data_b !! 41 const u16 *termination_const; 51 const struct can_tdc_const *tdc_const; !! 42 unsigned int termination_const_cnt; 52 struct can_tdc tdc; !! 43 u16 termination; 53 << 54 unsigned int bitrate_const_cnt; << 55 const u32 *bitrate_const; 44 const u32 *bitrate_const; >> 45 unsigned int bitrate_const_cnt; 56 const u32 *data_bitrate_const; 46 const u32 *data_bitrate_const; 57 unsigned int data_bitrate_const_cnt; 47 unsigned int data_bitrate_const_cnt; 58 u32 bitrate_max; << 59 struct can_clock clock; 48 struct can_clock clock; 60 49 61 unsigned int termination_const_cnt; << 62 const u16 *termination_const; << 63 u16 termination; << 64 struct gpio_desc *termination_gpio; << 65 u16 termination_gpio_ohms[CAN_TERMINAT << 66 << 67 unsigned int echo_skb_max; << 68 struct sk_buff **echo_skb; << 69 << 70 enum can_state state; 50 enum can_state state; 71 51 72 /* CAN controller features - see inclu 52 /* CAN controller features - see include/uapi/linux/can/netlink.h */ 73 u32 ctrlmode; /* current opt 53 u32 ctrlmode; /* current options setting */ 74 u32 ctrlmode_supported; /* options tha 54 u32 ctrlmode_supported; /* options that can be modified by netlink */ >> 55 u32 ctrlmode_static; /* static enabled options for driver/hardware */ 75 56 76 int restart_ms; 57 int restart_ms; 77 struct delayed_work restart_work; 58 struct delayed_work restart_work; 78 59 79 int (*do_set_bittiming)(struct net_dev 60 int (*do_set_bittiming)(struct net_device *dev); 80 int (*do_set_data_bittiming)(struct ne 61 int (*do_set_data_bittiming)(struct net_device *dev); 81 int (*do_set_mode)(struct net_device * 62 int (*do_set_mode)(struct net_device *dev, enum can_mode mode); 82 int (*do_set_termination)(struct net_d 63 int (*do_set_termination)(struct net_device *dev, u16 term); 83 int (*do_get_state)(const struct net_d 64 int (*do_get_state)(const struct net_device *dev, 84 enum can_state *st 65 enum can_state *state); 85 int (*do_get_berr_counter)(const struc 66 int (*do_get_berr_counter)(const struct net_device *dev, 86 struct can_ 67 struct can_berr_counter *bec); 87 int (*do_get_auto_tdcv)(const struct n << 88 }; << 89 68 90 static inline bool can_tdc_is_enabled(const st !! 69 unsigned int echo_skb_max; 91 { !! 70 struct sk_buff **echo_skb; 92 return !!(priv->ctrlmode & CAN_CTRLMOD !! 71 93 } !! 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 }; 94 81 95 /* 82 /* 96 * can_get_relative_tdco() - TDCO relative to !! 83 * get_can_dlc(value) - helper macro to cast a given data length code (dlc) 97 * !! 84 * to __u8 and ensure the dlc value to be max. 8 bytes. 98 * struct can_tdc::tdco represents the absolut << 99 * controllers use instead an offset relative << 100 * such that: << 101 * 85 * 102 * SSP = TDCV + absolute TDCO !! 86 * To be used in the CAN netdriver receive path to ensure conformance with 103 * = TDCV + SP + relative TDCO !! 87 * ISO 11898-1 Chapter 8.4.2.3 (DLC field) 104 * << 105 * -+----------- one bit ----------+-- TX pin << 106 * |<--- Sample Point --->| << 107 * << 108 * --+----------- one << 109 * |<-------- TDCV -------->| << 110 * |<--------------- << 111 * |<--- Sample Poin << 112 * | << 113 * |<------------- Secondary Sample Point --- << 114 */ 88 */ 115 static inline s32 can_get_relative_tdco(const !! 89 #define get_can_dlc(i) (min_t(__u8, (i), CAN_MAX_DLC)) 116 { !! 90 #define get_canfd_dlc(i) (min_t(__u8, (i), CANFD_MAX_DLC)) 117 const struct can_bittiming *dbt = &pri << 118 s32 sample_point_in_tc = (CAN_SYNC_SEG << 119 dbt->phase_s << 120 91 121 return (s32)priv->tdc.tdco - sample_po !! 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; 122 } 121 } 123 122 124 /* helper to define static CAN controller feat 123 /* helper to define static CAN controller features at device creation time */ 125 static inline int __must_check can_set_static_ !! 124 static inline void can_set_static_ctrlmode(struct net_device *dev, 126 !! 125 u32 static_mode) 127 { 126 { 128 struct can_priv *priv = netdev_priv(de 127 struct can_priv *priv = netdev_priv(dev); 129 128 130 /* alloc_candev() succeeded => netdev_ 129 /* alloc_candev() succeeded => netdev_priv() is valid at this point */ 131 if (priv->ctrlmode_supported & static_ << 132 netdev_warn(dev, << 133 "Controller featur << 134 return -EINVAL; << 135 } << 136 priv->ctrlmode = static_mode; 130 priv->ctrlmode = static_mode; >> 131 priv->ctrlmode_static = static_mode; 137 132 138 /* override MTU which was set by defau 133 /* override MTU which was set by default in can_setup()? */ 139 if (static_mode & CAN_CTRLMODE_FD) 134 if (static_mode & CAN_CTRLMODE_FD) 140 dev->mtu = CANFD_MTU; 135 dev->mtu = CANFD_MTU; 141 << 142 return 0; << 143 } 136 } 144 137 145 static inline u32 can_get_static_ctrlmode(stru !! 138 /* get data length from can_dlc with sanitized can_dlc */ 146 { !! 139 u8 can_dlc2len(u8 can_dlc); 147 return priv->ctrlmode & ~priv->ctrlmod << 148 } << 149 140 150 static inline bool can_is_canxl_dev_mtu(unsign !! 141 /* map the sanitized data length to an appropriate data length code */ 151 { !! 142 u8 can_len2dlc(u8 len); 152 return (mtu >= CANXL_MIN_MTU && mtu <= << 153 } << 154 << 155 /* drop skb if it does not contain a valid CAN << 156 static inline bool can_dev_dropped_skb(struct << 157 { << 158 struct can_priv *priv = netdev_priv(de << 159 << 160 if (priv->ctrlmode & CAN_CTRLMODE_LIST << 161 netdev_info_once(dev, << 162 "interface in << 163 kfree_skb(skb); << 164 dev->stats.tx_dropped++; << 165 return true; << 166 } << 167 143 168 return can_dropped_invalid_skb(dev, sk !! 144 struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max); 169 } << 170 << 171 void can_setup(struct net_device *dev); << 172 << 173 struct net_device *alloc_candev_mqs(int sizeof << 174 unsigned i << 175 #define alloc_candev(sizeof_priv, echo_skb_max << 176 alloc_candev_mqs(sizeof_priv, echo_skb << 177 #define alloc_candev_mq(sizeof_priv, echo_skb_ << 178 alloc_candev_mqs(sizeof_priv, echo_skb << 179 void free_candev(struct net_device *dev); 145 void free_candev(struct net_device *dev); 180 146 181 /* a candev safe wrapper around netdev_priv */ 147 /* a candev safe wrapper around netdev_priv */ 182 struct can_priv *safe_candev_priv(struct net_d 148 struct can_priv *safe_candev_priv(struct net_device *dev); 183 149 184 int open_candev(struct net_device *dev); 150 int open_candev(struct net_device *dev); 185 void close_candev(struct net_device *dev); 151 void close_candev(struct net_device *dev); 186 int can_change_mtu(struct net_device *dev, int 152 int can_change_mtu(struct net_device *dev, int new_mtu); 187 int can_eth_ioctl_hwts(struct net_device *netd << 188 int can_ethtool_op_get_ts_info_hwts(struct net << 189 struct ker << 190 153 191 int register_candev(struct net_device *dev); 154 int register_candev(struct net_device *dev); 192 void unregister_candev(struct net_device *dev) 155 void unregister_candev(struct net_device *dev); 193 156 194 int can_restart_now(struct net_device *dev); 157 int can_restart_now(struct net_device *dev); 195 void can_bus_off(struct net_device *dev); 158 void can_bus_off(struct net_device *dev); 196 159 197 const char *can_get_state_str(const enum can_s << 198 void can_state_get_by_berr_counter(const struc << 199 const struc << 200 enum can_st << 201 enum can_st << 202 void can_change_state(struct net_device *dev, 160 void can_change_state(struct net_device *dev, struct can_frame *cf, 203 enum can_state tx_state, 161 enum can_state tx_state, enum can_state rx_state); 204 162 205 #ifdef CONFIG_OF !! 163 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, 206 void of_can_transceiver(struct net_device *dev !! 164 unsigned int idx); 207 #else !! 165 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx); 208 static inline void of_can_transceiver(struct n !! 166 void can_free_echo_skb(struct net_device *dev, unsigned int idx); 209 #endif !! 167 210 !! 168 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf); 211 extern struct rtnl_link_ops can_link_ops; !! 169 struct sk_buff *alloc_canfd_skb(struct net_device *dev, 212 int can_netlink_register(void); !! 170 struct canfd_frame **cfd); 213 void can_netlink_unregister(void); !! 171 struct sk_buff *alloc_can_err_skb(struct net_device *dev, >> 172 struct can_frame **cf); 214 173 215 #endif /* !_CAN_DEV_H */ 174 #endif /* !_CAN_DEV_H */ 216 175
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.