Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * linux/can/dev.h
0004  *
0005  * Definitions for the CAN network device driver interface
0006  *
0007  * Copyright (C) 2006 Andrey Volkov <avolkov@varma-el.com>
0008  *               Varma Electronics Oy
0009  *
0010  * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
0011  *
0012  */
0013 
0014 #ifndef _CAN_DEV_H
0015 #define _CAN_DEV_H
0016 
0017 #include <linux/can.h>
0018 #include <linux/can/bittiming.h>
0019 #include <linux/can/error.h>
0020 #include <linux/can/length.h>
0021 #include <linux/can/netlink.h>
0022 #include <linux/can/skb.h>
0023 #include <linux/ethtool.h>
0024 #include <linux/netdevice.h>
0025 
0026 /*
0027  * CAN mode
0028  */
0029 enum can_mode {
0030     CAN_MODE_STOP = 0,
0031     CAN_MODE_START,
0032     CAN_MODE_SLEEP
0033 };
0034 
0035 enum can_termination_gpio {
0036     CAN_TERMINATION_GPIO_DISABLED = 0,
0037     CAN_TERMINATION_GPIO_ENABLED,
0038     CAN_TERMINATION_GPIO_MAX,
0039 };
0040 
0041 /*
0042  * CAN common private data
0043  */
0044 struct can_priv {
0045     struct net_device *dev;
0046     struct can_device_stats can_stats;
0047 
0048     const struct can_bittiming_const *bittiming_const,
0049         *data_bittiming_const;
0050     struct can_bittiming bittiming, data_bittiming;
0051     const struct can_tdc_const *tdc_const;
0052     struct can_tdc tdc;
0053 
0054     unsigned int bitrate_const_cnt;
0055     const u32 *bitrate_const;
0056     const u32 *data_bitrate_const;
0057     unsigned int data_bitrate_const_cnt;
0058     u32 bitrate_max;
0059     struct can_clock clock;
0060 
0061     unsigned int termination_const_cnt;
0062     const u16 *termination_const;
0063     u16 termination;
0064     struct gpio_desc *termination_gpio;
0065     u16 termination_gpio_ohms[CAN_TERMINATION_GPIO_MAX];
0066 
0067     unsigned int echo_skb_max;
0068     struct sk_buff **echo_skb;
0069 
0070     enum can_state state;
0071 
0072     /* CAN controller features - see include/uapi/linux/can/netlink.h */
0073     u32 ctrlmode;       /* current options setting */
0074     u32 ctrlmode_supported; /* options that can be modified by netlink */
0075 
0076     int restart_ms;
0077     struct delayed_work restart_work;
0078 
0079     int (*do_set_bittiming)(struct net_device *dev);
0080     int (*do_set_data_bittiming)(struct net_device *dev);
0081     int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
0082     int (*do_set_termination)(struct net_device *dev, u16 term);
0083     int (*do_get_state)(const struct net_device *dev,
0084                 enum can_state *state);
0085     int (*do_get_berr_counter)(const struct net_device *dev,
0086                    struct can_berr_counter *bec);
0087     int (*do_get_auto_tdcv)(const struct net_device *dev, u32 *tdcv);
0088 };
0089 
0090 static inline bool can_tdc_is_enabled(const struct can_priv *priv)
0091 {
0092     return !!(priv->ctrlmode & CAN_CTRLMODE_TDC_MASK);
0093 }
0094 
0095 /*
0096  * can_get_relative_tdco() - TDCO relative to the sample point
0097  *
0098  * struct can_tdc::tdco represents the absolute offset from TDCV. Some
0099  * controllers use instead an offset relative to the Sample Point (SP)
0100  * such that:
0101  *
0102  * SSP = TDCV + absolute TDCO
0103  *     = TDCV + SP + relative TDCO
0104  *
0105  * -+----------- one bit ----------+-- TX pin
0106  *  |<--- Sample Point --->|
0107  *
0108  *                         --+----------- one bit ----------+-- RX pin
0109  *  |<-------- TDCV -------->|
0110  *                           |<------------------------>| absolute TDCO
0111  *                           |<--- Sample Point --->|
0112  *                           |                      |<->| relative TDCO
0113  *  |<------------- Secondary Sample Point ------------>|
0114  */
0115 static inline s32 can_get_relative_tdco(const struct can_priv *priv)
0116 {
0117     const struct can_bittiming *dbt = &priv->data_bittiming;
0118     s32 sample_point_in_tc = (CAN_SYNC_SEG + dbt->prop_seg +
0119                   dbt->phase_seg1) * dbt->brp;
0120 
0121     return (s32)priv->tdc.tdco - sample_point_in_tc;
0122 }
0123 
0124 /* helper to define static CAN controller features at device creation time */
0125 static inline int __must_check can_set_static_ctrlmode(struct net_device *dev,
0126                                u32 static_mode)
0127 {
0128     struct can_priv *priv = netdev_priv(dev);
0129 
0130     /* alloc_candev() succeeded => netdev_priv() is valid at this point */
0131     if (priv->ctrlmode_supported & static_mode) {
0132         netdev_warn(dev,
0133                 "Controller features can not be supported and static at the same time\n");
0134         return -EINVAL;
0135     }
0136     priv->ctrlmode = static_mode;
0137 
0138     /* override MTU which was set by default in can_setup()? */
0139     if (static_mode & CAN_CTRLMODE_FD)
0140         dev->mtu = CANFD_MTU;
0141 
0142     return 0;
0143 }
0144 
0145 static inline u32 can_get_static_ctrlmode(struct can_priv *priv)
0146 {
0147     return priv->ctrlmode & ~priv->ctrlmode_supported;
0148 }
0149 
0150 void can_setup(struct net_device *dev);
0151 
0152 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
0153                     unsigned int txqs, unsigned int rxqs);
0154 #define alloc_candev(sizeof_priv, echo_skb_max) \
0155     alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1)
0156 #define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \
0157     alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count)
0158 void free_candev(struct net_device *dev);
0159 
0160 /* a candev safe wrapper around netdev_priv */
0161 struct can_priv *safe_candev_priv(struct net_device *dev);
0162 
0163 int open_candev(struct net_device *dev);
0164 void close_candev(struct net_device *dev);
0165 int can_change_mtu(struct net_device *dev, int new_mtu);
0166 int can_eth_ioctl_hwts(struct net_device *netdev, struct ifreq *ifr, int cmd);
0167 int can_ethtool_op_get_ts_info_hwts(struct net_device *dev,
0168                     struct ethtool_ts_info *info);
0169 
0170 int register_candev(struct net_device *dev);
0171 void unregister_candev(struct net_device *dev);
0172 
0173 int can_restart_now(struct net_device *dev);
0174 void can_bus_off(struct net_device *dev);
0175 
0176 const char *can_get_state_str(const enum can_state state);
0177 void can_change_state(struct net_device *dev, struct can_frame *cf,
0178               enum can_state tx_state, enum can_state rx_state);
0179 
0180 #ifdef CONFIG_OF
0181 void of_can_transceiver(struct net_device *dev);
0182 #else
0183 static inline void of_can_transceiver(struct net_device *dev) { }
0184 #endif
0185 
0186 extern struct rtnl_link_ops can_link_ops;
0187 int can_netlink_register(void);
0188 void can_netlink_unregister(void);
0189 
0190 #endif /* !_CAN_DEV_H */