| 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 <avolkov@varma-el.com> |
| 8 | * Varma Electronics Oy |
| 9 | * |
| 10 | * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com> |
| 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/length.h> |
| 21 | #include <linux/can/netlink.h> |
| 22 | #include <linux/can/skb.h> |
| 23 | #include <linux/ethtool.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 | enum can_termination_gpio { |
| 36 | CAN_TERMINATION_GPIO_DISABLED = 0, |
| 37 | CAN_TERMINATION_GPIO_ENABLED, |
| 38 | CAN_TERMINATION_GPIO_MAX, |
| 39 | }; |
| 40 | |
| 41 | /* |
| 42 | * CAN common private data |
| 43 | */ |
| 44 | struct can_priv { |
| 45 | struct net_device *dev; |
| 46 | struct can_device_stats can_stats; |
| 47 | |
| 48 | const struct can_bittiming_const *bittiming_const; |
| 49 | struct can_bittiming bittiming; |
| 50 | struct data_bittiming_params fd, xl; |
| 51 | unsigned int bitrate_const_cnt; |
| 52 | const u32 *bitrate_const; |
| 53 | u32 bitrate_max; |
| 54 | struct can_clock clock; |
| 55 | |
| 56 | unsigned int termination_const_cnt; |
| 57 | const u16 *termination_const; |
| 58 | u16 termination; |
| 59 | struct gpio_desc *termination_gpio; |
| 60 | u16 termination_gpio_ohms[CAN_TERMINATION_GPIO_MAX]; |
| 61 | |
| 62 | unsigned int echo_skb_max; |
| 63 | struct sk_buff **echo_skb; |
| 64 | |
| 65 | enum can_state state; |
| 66 | |
| 67 | /* CAN controller features - see include/uapi/linux/can/netlink.h */ |
| 68 | u32 ctrlmode; /* current options setting */ |
| 69 | u32 ctrlmode_supported; /* options that can be modified by netlink */ |
| 70 | |
| 71 | int restart_ms; |
| 72 | struct delayed_work restart_work; |
| 73 | |
| 74 | int (*do_set_bittiming)(struct net_device *dev); |
| 75 | int (*do_set_mode)(struct net_device *dev, enum can_mode mode); |
| 76 | int (*do_set_termination)(struct net_device *dev, u16 term); |
| 77 | int (*do_get_state)(const struct net_device *dev, |
| 78 | enum can_state *state); |
| 79 | int (*do_get_berr_counter)(const struct net_device *dev, |
| 80 | struct can_berr_counter *bec); |
| 81 | }; |
| 82 | |
| 83 | static inline bool can_fd_tdc_is_enabled(const struct can_priv *priv) |
| 84 | { |
| 85 | return !!(priv->ctrlmode & CAN_CTRLMODE_FD_TDC_MASK); |
| 86 | } |
| 87 | |
| 88 | static inline bool can_xl_tdc_is_enabled(const struct can_priv *priv) |
| 89 | { |
| 90 | return !!(priv->ctrlmode & CAN_CTRLMODE_XL_TDC_MASK); |
| 91 | } |
| 92 | |
| 93 | static inline u32 can_get_static_ctrlmode(struct can_priv *priv) |
| 94 | { |
| 95 | return priv->ctrlmode & ~priv->ctrlmode_supported; |
| 96 | } |
| 97 | |
| 98 | static inline bool can_is_canxl_dev_mtu(unsigned int mtu) |
| 99 | { |
| 100 | return (mtu >= CANXL_MIN_MTU && mtu <= CANXL_MAX_MTU); |
| 101 | } |
| 102 | |
| 103 | void can_setup(struct net_device *dev); |
| 104 | |
| 105 | struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max, |
| 106 | unsigned int txqs, unsigned int rxqs); |
| 107 | #define alloc_candev(sizeof_priv, echo_skb_max) \ |
| 108 | alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1) |
| 109 | #define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \ |
| 110 | alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count) |
| 111 | void free_candev(struct net_device *dev); |
| 112 | |
| 113 | /* a candev safe wrapper around netdev_priv */ |
| 114 | struct can_priv *safe_candev_priv(struct net_device *dev); |
| 115 | |
| 116 | int open_candev(struct net_device *dev); |
| 117 | void close_candev(struct net_device *dev); |
| 118 | void can_set_default_mtu(struct net_device *dev); |
| 119 | void can_set_cap_info(struct net_device *dev); |
| 120 | int __must_check can_set_static_ctrlmode(struct net_device *dev, |
| 121 | u32 static_mode); |
| 122 | int can_hwtstamp_get(struct net_device *netdev, |
| 123 | struct kernel_hwtstamp_config *cfg); |
| 124 | int can_hwtstamp_set(struct net_device *netdev, |
| 125 | struct kernel_hwtstamp_config *cfg, |
| 126 | struct netlink_ext_ack *extack); |
| 127 | int can_ethtool_op_get_ts_info_hwts(struct net_device *dev, |
| 128 | struct kernel_ethtool_ts_info *info); |
| 129 | |
| 130 | int register_candev(struct net_device *dev); |
| 131 | void unregister_candev(struct net_device *dev); |
| 132 | |
| 133 | int can_restart_now(struct net_device *dev); |
| 134 | void can_bus_off(struct net_device *dev); |
| 135 | |
| 136 | const char *can_get_state_str(const enum can_state state); |
| 137 | const char *can_get_ctrlmode_str(u32 ctrlmode); |
| 138 | |
| 139 | static inline bool can_dev_in_xl_only_mode(struct can_priv *priv) |
| 140 | { |
| 141 | const u32 mixed_mode = CAN_CTRLMODE_FD | CAN_CTRLMODE_XL; |
| 142 | |
| 143 | /* When CAN XL is enabled but FD is disabled we are running in |
| 144 | * the so-called 'CANXL-only mode' where the error signalling is |
| 145 | * disabled. This helper function determines the required value |
| 146 | * to disable error signalling in the CAN XL controller. |
| 147 | * The so-called CC/FD/XL 'mixed mode' requires error signalling. |
| 148 | */ |
| 149 | return ((priv->ctrlmode & mixed_mode) == CAN_CTRLMODE_XL); |
| 150 | } |
| 151 | |
| 152 | /* drop skb if it does not contain a valid CAN frame for sending */ |
| 153 | static inline bool can_dev_dropped_skb(struct net_device *dev, struct sk_buff *skb) |
| 154 | { |
| 155 | struct can_priv *priv = netdev_priv(dev); |
| 156 | u32 silent_mode = priv->ctrlmode & (CAN_CTRLMODE_LISTENONLY | |
| 157 | CAN_CTRLMODE_RESTRICTED); |
| 158 | |
| 159 | if (silent_mode) { |
| 160 | netdev_info_once(dev, "interface in %s mode, dropping skb\n" , |
| 161 | can_get_ctrlmode_str(silent_mode)); |
| 162 | goto invalid_skb; |
| 163 | } |
| 164 | |
| 165 | if (!(priv->ctrlmode & CAN_CTRLMODE_FD) && can_is_canfd_skb(skb)) { |
| 166 | netdev_info_once(dev, "CAN FD is disabled, dropping skb\n" ); |
| 167 | goto invalid_skb; |
| 168 | } |
| 169 | |
| 170 | if (can_dev_in_xl_only_mode(priv) && !can_is_canxl_skb(skb)) { |
| 171 | netdev_info_once(dev, |
| 172 | "Error signaling is disabled, dropping skb\n" ); |
| 173 | goto invalid_skb; |
| 174 | } |
| 175 | |
| 176 | return can_dropped_invalid_skb(dev, skb); |
| 177 | |
| 178 | invalid_skb: |
| 179 | kfree_skb(skb); |
| 180 | dev->stats.tx_dropped++; |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | void can_state_get_by_berr_counter(const struct net_device *dev, |
| 185 | const struct can_berr_counter *bec, |
| 186 | enum can_state *tx_state, |
| 187 | enum can_state *rx_state); |
| 188 | void can_change_state(struct net_device *dev, struct can_frame *cf, |
| 189 | enum can_state tx_state, enum can_state rx_state); |
| 190 | |
| 191 | #ifdef CONFIG_OF |
| 192 | void of_can_transceiver(struct net_device *dev); |
| 193 | #else |
| 194 | static inline void of_can_transceiver(struct net_device *dev) { } |
| 195 | #endif |
| 196 | |
| 197 | extern struct rtnl_link_ops can_link_ops; |
| 198 | int can_netlink_register(void); |
| 199 | void can_netlink_unregister(void); |
| 200 | |
| 201 | #endif /* !_CAN_DEV_H */ |
| 202 | |