at master 6.0 kB view raw
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 */ 29enum can_mode { 30 CAN_MODE_STOP = 0, 31 CAN_MODE_START, 32 CAN_MODE_SLEEP 33}; 34 35enum 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 */ 44struct 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 83static inline bool can_fd_tdc_is_enabled(const struct can_priv *priv) 84{ 85 return !!(priv->ctrlmode & CAN_CTRLMODE_FD_TDC_MASK); 86} 87 88static inline bool can_xl_tdc_is_enabled(const struct can_priv *priv) 89{ 90 return !!(priv->ctrlmode & CAN_CTRLMODE_XL_TDC_MASK); 91} 92 93static inline u32 can_get_static_ctrlmode(struct can_priv *priv) 94{ 95 return priv->ctrlmode & ~priv->ctrlmode_supported; 96} 97 98static inline bool can_is_canxl_dev_mtu(unsigned int mtu) 99{ 100 return (mtu >= CANXL_MIN_MTU && mtu <= CANXL_MAX_MTU); 101} 102 103void can_setup(struct net_device *dev); 104 105struct 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) 111void free_candev(struct net_device *dev); 112 113/* a candev safe wrapper around netdev_priv */ 114#if IS_ENABLED(CONFIG_CAN_NETLINK) 115struct can_priv *safe_candev_priv(struct net_device *dev); 116#else 117static inline struct can_priv *safe_candev_priv(struct net_device *dev) 118{ 119 return NULL; 120} 121#endif 122 123int open_candev(struct net_device *dev); 124void close_candev(struct net_device *dev); 125void can_set_default_mtu(struct net_device *dev); 126int __must_check can_set_static_ctrlmode(struct net_device *dev, 127 u32 static_mode); 128int can_hwtstamp_get(struct net_device *netdev, 129 struct kernel_hwtstamp_config *cfg); 130int can_hwtstamp_set(struct net_device *netdev, 131 struct kernel_hwtstamp_config *cfg, 132 struct netlink_ext_ack *extack); 133int can_ethtool_op_get_ts_info_hwts(struct net_device *dev, 134 struct kernel_ethtool_ts_info *info); 135 136int register_candev(struct net_device *dev); 137void unregister_candev(struct net_device *dev); 138 139int can_restart_now(struct net_device *dev); 140void can_bus_off(struct net_device *dev); 141 142const char *can_get_state_str(const enum can_state state); 143const char *can_get_ctrlmode_str(u32 ctrlmode); 144 145static inline bool can_dev_in_xl_only_mode(struct can_priv *priv) 146{ 147 const u32 mixed_mode = CAN_CTRLMODE_FD | CAN_CTRLMODE_XL; 148 149 /* When CAN XL is enabled but FD is disabled we are running in 150 * the so-called 'CANXL-only mode' where the error signalling is 151 * disabled. This helper function determines the required value 152 * to disable error signalling in the CAN XL controller. 153 * The so-called CC/FD/XL 'mixed mode' requires error signalling. 154 */ 155 return ((priv->ctrlmode & mixed_mode) == CAN_CTRLMODE_XL); 156} 157 158/* drop skb if it does not contain a valid CAN frame for sending */ 159static inline bool can_dev_dropped_skb(struct net_device *dev, struct sk_buff *skb) 160{ 161 struct can_priv *priv = netdev_priv(dev); 162 u32 silent_mode = priv->ctrlmode & (CAN_CTRLMODE_LISTENONLY | 163 CAN_CTRLMODE_RESTRICTED); 164 165 if (silent_mode) { 166 netdev_info_once(dev, "interface in %s mode, dropping skb\n", 167 can_get_ctrlmode_str(silent_mode)); 168 goto invalid_skb; 169 } 170 171 if (!(priv->ctrlmode & CAN_CTRLMODE_FD) && can_is_canfd_skb(skb)) { 172 netdev_info_once(dev, "CAN FD is disabled, dropping skb\n"); 173 goto invalid_skb; 174 } 175 176 if (can_dev_in_xl_only_mode(priv) && !can_is_canxl_skb(skb)) { 177 netdev_info_once(dev, 178 "Error signaling is disabled, dropping skb\n"); 179 goto invalid_skb; 180 } 181 182 return can_dropped_invalid_skb(dev, skb); 183 184invalid_skb: 185 kfree_skb(skb); 186 dev->stats.tx_dropped++; 187 return true; 188} 189 190void can_state_get_by_berr_counter(const struct net_device *dev, 191 const struct can_berr_counter *bec, 192 enum can_state *tx_state, 193 enum can_state *rx_state); 194void can_change_state(struct net_device *dev, struct can_frame *cf, 195 enum can_state tx_state, enum can_state rx_state); 196 197#ifdef CONFIG_OF 198void of_can_transceiver(struct net_device *dev); 199#else 200static inline void of_can_transceiver(struct net_device *dev) { } 201#endif 202 203extern struct rtnl_link_ops can_link_ops; 204int can_netlink_register(void); 205void can_netlink_unregister(void); 206 207#endif /* !_CAN_DEV_H */