xref: /linux/include/linux/can/dev.h (revision ec2e0fb07d789976c601bec19ecced7a501c3705)
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;
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 
can_fd_tdc_is_enabled(const struct can_priv * priv)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 
can_get_static_ctrlmode(struct can_priv * priv)88 static inline u32 can_get_static_ctrlmode(struct can_priv *priv)
89 {
90 	return priv->ctrlmode & ~priv->ctrlmode_supported;
91 }
92 
can_is_canxl_dev_mtu(unsigned int mtu)93 static inline bool can_is_canxl_dev_mtu(unsigned int mtu)
94 {
95 	return (mtu >= CANXL_MIN_MTU && mtu <= CANXL_MAX_MTU);
96 }
97 
98 /* drop skb if it does not contain a valid CAN frame for sending */
can_dev_dropped_skb(struct net_device * dev,struct sk_buff * skb)99 static inline bool can_dev_dropped_skb(struct net_device *dev, struct sk_buff *skb)
100 {
101 	struct can_priv *priv = netdev_priv(dev);
102 
103 	if (priv->ctrlmode & CAN_CTRLMODE_LISTENONLY) {
104 		netdev_info_once(dev,
105 				 "interface in listen only mode, dropping skb\n");
106 		kfree_skb(skb);
107 		dev->stats.tx_dropped++;
108 		return true;
109 	}
110 
111 	return can_dropped_invalid_skb(dev, skb);
112 }
113 
114 void can_setup(struct net_device *dev);
115 
116 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
117 				    unsigned int txqs, unsigned int rxqs);
118 #define alloc_candev(sizeof_priv, echo_skb_max) \
119 	alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1)
120 #define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \
121 	alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count)
122 void free_candev(struct net_device *dev);
123 
124 /* a candev safe wrapper around netdev_priv */
125 struct can_priv *safe_candev_priv(struct net_device *dev);
126 
127 int open_candev(struct net_device *dev);
128 void close_candev(struct net_device *dev);
129 void can_set_default_mtu(struct net_device *dev);
130 int can_change_mtu(struct net_device *dev, int new_mtu);
131 int __must_check can_set_static_ctrlmode(struct net_device *dev,
132 					 u32 static_mode);
133 int can_eth_ioctl_hwts(struct net_device *netdev, struct ifreq *ifr, int cmd);
134 int can_ethtool_op_get_ts_info_hwts(struct net_device *dev,
135 				    struct kernel_ethtool_ts_info *info);
136 
137 int register_candev(struct net_device *dev);
138 void unregister_candev(struct net_device *dev);
139 
140 int can_restart_now(struct net_device *dev);
141 void can_bus_off(struct net_device *dev);
142 
143 const char *can_get_state_str(const enum can_state state);
144 const char *can_get_ctrlmode_str(u32 ctrlmode);
145 
146 void can_state_get_by_berr_counter(const struct net_device *dev,
147 				   const struct can_berr_counter *bec,
148 				   enum can_state *tx_state,
149 				   enum can_state *rx_state);
150 void can_change_state(struct net_device *dev, struct can_frame *cf,
151 		      enum can_state tx_state, enum can_state rx_state);
152 
153 #ifdef CONFIG_OF
154 void of_can_transceiver(struct net_device *dev);
155 #else
of_can_transceiver(struct net_device * dev)156 static inline void of_can_transceiver(struct net_device *dev) { }
157 #endif
158 
159 extern struct rtnl_link_ops can_link_ops;
160 int can_netlink_register(void);
161 void can_netlink_unregister(void);
162 
163 #endif /* !_CAN_DEV_H */
164