dev.h (5443B)
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/netdevice.h> 24 25/* 26 * CAN mode 27 */ 28enum can_mode { 29 CAN_MODE_STOP = 0, 30 CAN_MODE_START, 31 CAN_MODE_SLEEP 32}; 33 34enum can_termination_gpio { 35 CAN_TERMINATION_GPIO_DISABLED = 0, 36 CAN_TERMINATION_GPIO_ENABLED, 37 CAN_TERMINATION_GPIO_MAX, 38}; 39 40/* 41 * CAN common private data 42 */ 43struct can_priv { 44 struct net_device *dev; 45 struct can_device_stats can_stats; 46 47 const struct can_bittiming_const *bittiming_const, 48 *data_bittiming_const; 49 struct can_bittiming bittiming, data_bittiming; 50 const struct can_tdc_const *tdc_const; 51 struct can_tdc tdc; 52 53 unsigned int bitrate_const_cnt; 54 const u32 *bitrate_const; 55 const u32 *data_bitrate_const; 56 unsigned int data_bitrate_const_cnt; 57 u32 bitrate_max; 58 struct can_clock clock; 59 60 unsigned int termination_const_cnt; 61 const u16 *termination_const; 62 u16 termination; 63 struct gpio_desc *termination_gpio; 64 u16 termination_gpio_ohms[CAN_TERMINATION_GPIO_MAX]; 65 66 unsigned int echo_skb_max; 67 struct sk_buff **echo_skb; 68 69 enum can_state state; 70 71 /* CAN controller features - see include/uapi/linux/can/netlink.h */ 72 u32 ctrlmode; /* current options setting */ 73 u32 ctrlmode_supported; /* options that can be modified by netlink */ 74 75 int restart_ms; 76 struct delayed_work restart_work; 77 78 int (*do_set_bittiming)(struct net_device *dev); 79 int (*do_set_data_bittiming)(struct net_device *dev); 80 int (*do_set_mode)(struct net_device *dev, enum can_mode mode); 81 int (*do_set_termination)(struct net_device *dev, u16 term); 82 int (*do_get_state)(const struct net_device *dev, 83 enum can_state *state); 84 int (*do_get_berr_counter)(const struct net_device *dev, 85 struct can_berr_counter *bec); 86 int (*do_get_auto_tdcv)(const struct net_device *dev, u32 *tdcv); 87}; 88 89static inline bool can_tdc_is_enabled(const struct can_priv *priv) 90{ 91 return !!(priv->ctrlmode & CAN_CTRLMODE_TDC_MASK); 92} 93 94/* 95 * can_get_relative_tdco() - TDCO relative to the sample point 96 * 97 * struct can_tdc::tdco represents the absolute offset from TDCV. Some 98 * controllers use instead an offset relative to the Sample Point (SP) 99 * such that: 100 * 101 * SSP = TDCV + absolute TDCO 102 * = TDCV + SP + relative TDCO 103 * 104 * -+----------- one bit ----------+-- TX pin 105 * |<--- Sample Point --->| 106 * 107 * --+----------- one bit ----------+-- RX pin 108 * |<-------- TDCV -------->| 109 * |<------------------------>| absolute TDCO 110 * |<--- Sample Point --->| 111 * | |<->| relative TDCO 112 * |<------------- Secondary Sample Point ------------>| 113 */ 114static inline s32 can_get_relative_tdco(const struct can_priv *priv) 115{ 116 const struct can_bittiming *dbt = &priv->data_bittiming; 117 s32 sample_point_in_tc = (CAN_SYNC_SEG + dbt->prop_seg + 118 dbt->phase_seg1) * dbt->brp; 119 120 return (s32)priv->tdc.tdco - sample_point_in_tc; 121} 122 123/* helper to define static CAN controller features at device creation time */ 124static inline int __must_check can_set_static_ctrlmode(struct net_device *dev, 125 u32 static_mode) 126{ 127 struct can_priv *priv = netdev_priv(dev); 128 129 /* alloc_candev() succeeded => netdev_priv() is valid at this point */ 130 if (priv->ctrlmode_supported & static_mode) { 131 netdev_warn(dev, 132 "Controller features can not be supported and static at the same time\n"); 133 return -EINVAL; 134 } 135 priv->ctrlmode = static_mode; 136 137 /* override MTU which was set by default in can_setup()? */ 138 if (static_mode & CAN_CTRLMODE_FD) 139 dev->mtu = CANFD_MTU; 140 141 return 0; 142} 143 144static inline u32 can_get_static_ctrlmode(struct can_priv *priv) 145{ 146 return priv->ctrlmode & ~priv->ctrlmode_supported; 147} 148 149void can_setup(struct net_device *dev); 150 151struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max, 152 unsigned int txqs, unsigned int rxqs); 153#define alloc_candev(sizeof_priv, echo_skb_max) \ 154 alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1) 155#define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \ 156 alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count) 157void free_candev(struct net_device *dev); 158 159/* a candev safe wrapper around netdev_priv */ 160struct can_priv *safe_candev_priv(struct net_device *dev); 161 162int open_candev(struct net_device *dev); 163void close_candev(struct net_device *dev); 164int can_change_mtu(struct net_device *dev, int new_mtu); 165 166int register_candev(struct net_device *dev); 167void unregister_candev(struct net_device *dev); 168 169int can_restart_now(struct net_device *dev); 170void can_bus_off(struct net_device *dev); 171 172const char *can_get_state_str(const enum can_state state); 173void can_change_state(struct net_device *dev, struct can_frame *cf, 174 enum can_state tx_state, enum can_state rx_state); 175 176#ifdef CONFIG_OF 177void of_can_transceiver(struct net_device *dev); 178#else 179static inline void of_can_transceiver(struct net_device *dev) { } 180#endif 181 182extern struct rtnl_link_ops can_link_ops; 183int can_netlink_register(void); 184void can_netlink_unregister(void); 185 186#endif /* !_CAN_DEV_H */