xref: /linux-6.15/include/linux/can/dev.h (revision 63dfe070)
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 <[email protected]>
8  *               Varma Electronics Oy
9  *
10  * Copyright (C) 2008 Wolfgang Grandegger <[email protected]>
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/led.h>
21 #include <linux/can/length.h>
22 #include <linux/can/netlink.h>
23 #include <linux/can/skb.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 		*data_bittiming_const;
50 	struct can_bittiming bittiming, data_bittiming;
51 	const struct can_tdc_const *tdc_const;
52 	struct can_tdc tdc;
53 
54 	unsigned int bitrate_const_cnt;
55 	const u32 *bitrate_const;
56 	const u32 *data_bitrate_const;
57 	unsigned int data_bitrate_const_cnt;
58 	u32 bitrate_max;
59 	struct can_clock clock;
60 
61 	unsigned int termination_const_cnt;
62 	const u16 *termination_const;
63 	u16 termination;
64 	struct gpio_desc *termination_gpio;
65 	u16 termination_gpio_ohms[CAN_TERMINATION_GPIO_MAX];
66 
67 	enum can_state state;
68 
69 	/* CAN controller features - see include/uapi/linux/can/netlink.h */
70 	u32 ctrlmode;		/* current options setting */
71 	u32 ctrlmode_supported;	/* options that can be modified by netlink */
72 	u32 ctrlmode_static;	/* static enabled options for driver/hardware */
73 
74 	int restart_ms;
75 	struct delayed_work restart_work;
76 
77 	int (*do_set_bittiming)(struct net_device *dev);
78 	int (*do_set_data_bittiming)(struct net_device *dev);
79 	int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
80 	int (*do_set_termination)(struct net_device *dev, u16 term);
81 	int (*do_get_state)(const struct net_device *dev,
82 			    enum can_state *state);
83 	int (*do_get_berr_counter)(const struct net_device *dev,
84 				   struct can_berr_counter *bec);
85 
86 	unsigned int echo_skb_max;
87 	struct sk_buff **echo_skb;
88 
89 #ifdef CONFIG_CAN_LEDS
90 	struct led_trigger *tx_led_trig;
91 	char tx_led_trig_name[CAN_LED_NAME_SZ];
92 	struct led_trigger *rx_led_trig;
93 	char rx_led_trig_name[CAN_LED_NAME_SZ];
94 	struct led_trigger *rxtx_led_trig;
95 	char rxtx_led_trig_name[CAN_LED_NAME_SZ];
96 #endif
97 };
98 
99 static inline bool can_tdc_is_enabled(const struct can_priv *priv)
100 {
101 	return !!(priv->ctrlmode & CAN_CTRLMODE_TDC_MASK);
102 }
103 
104 /* helper to define static CAN controller features at device creation time */
105 static inline void can_set_static_ctrlmode(struct net_device *dev,
106 					   u32 static_mode)
107 {
108 	struct can_priv *priv = netdev_priv(dev);
109 
110 	/* alloc_candev() succeeded => netdev_priv() is valid at this point */
111 	priv->ctrlmode = static_mode;
112 	priv->ctrlmode_static = static_mode;
113 
114 	/* override MTU which was set by default in can_setup()? */
115 	if (static_mode & CAN_CTRLMODE_FD)
116 		dev->mtu = CANFD_MTU;
117 }
118 
119 void can_setup(struct net_device *dev);
120 
121 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
122 				    unsigned int txqs, unsigned int rxqs);
123 #define alloc_candev(sizeof_priv, echo_skb_max) \
124 	alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1)
125 #define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \
126 	alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count)
127 void free_candev(struct net_device *dev);
128 
129 /* a candev safe wrapper around netdev_priv */
130 struct can_priv *safe_candev_priv(struct net_device *dev);
131 
132 int open_candev(struct net_device *dev);
133 void close_candev(struct net_device *dev);
134 int can_change_mtu(struct net_device *dev, int new_mtu);
135 
136 int register_candev(struct net_device *dev);
137 void unregister_candev(struct net_device *dev);
138 
139 int can_restart_now(struct net_device *dev);
140 void can_bus_off(struct net_device *dev);
141 
142 const char *can_get_state_str(const enum can_state state);
143 void can_change_state(struct net_device *dev, struct can_frame *cf,
144 		      enum can_state tx_state, enum can_state rx_state);
145 
146 #ifdef CONFIG_OF
147 void of_can_transceiver(struct net_device *dev);
148 #else
149 static inline void of_can_transceiver(struct net_device *dev) { }
150 #endif
151 
152 extern struct rtnl_link_ops can_link_ops;
153 int can_netlink_register(void);
154 void can_netlink_unregister(void);
155 
156 #endif /* !_CAN_DEV_H */
157