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/error.h>
19 #include <linux/can/led.h>
20 #include <linux/can/netlink.h>
21 #include <linux/netdevice.h>
22
23 /*
24 * CAN mode
25 */
26 enum can_mode {
27 CAN_MODE_STOP = 0,
28 CAN_MODE_START,
29 CAN_MODE_SLEEP
30 };
31
32 /*
33 * CAN common private data
34 */
35 struct can_priv {
36 struct net_device *dev;
37 struct can_device_stats can_stats;
38
39 struct can_bittiming bittiming, data_bittiming;
40 const struct can_bittiming_const *bittiming_const,
41 *data_bittiming_const;
42 const u16 *termination_const;
43 unsigned int termination_const_cnt;
44 u16 termination;
45 const u32 *bitrate_const;
46 unsigned int bitrate_const_cnt;
47 const u32 *data_bitrate_const;
48 unsigned int data_bitrate_const_cnt;
49 u32 bitrate_max;
50 struct can_clock clock;
51
52 enum can_state state;
53
54 /* CAN controller features - see include/uapi/linux/can/netlink.h */
55 u32 ctrlmode; /* current options setting */
56 u32 ctrlmode_supported; /* options that can be modified by netlink */
57 u32 ctrlmode_static; /* static enabled options for driver/hardware */
58
59 int restart_ms;
60 struct delayed_work restart_work;
61
62 int (*do_set_bittiming)(struct net_device *dev);
63 int (*do_set_data_bittiming)(struct net_device *dev);
64 int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
65 int (*do_set_termination)(struct net_device *dev, u16 term);
66 int (*do_get_state)(const struct net_device *dev,
67 enum can_state *state);
68 int (*do_get_berr_counter)(const struct net_device *dev,
69 struct can_berr_counter *bec);
70
71 unsigned int echo_skb_max;
72 struct sk_buff **echo_skb;
73
74 #ifdef CONFIG_CAN_LEDS
75 struct led_trigger *tx_led_trig;
76 char tx_led_trig_name[CAN_LED_NAME_SZ];
77 struct led_trigger *rx_led_trig;
78 char rx_led_trig_name[CAN_LED_NAME_SZ];
79 struct led_trigger *rxtx_led_trig;
80 char rxtx_led_trig_name[CAN_LED_NAME_SZ];
81 #endif
82 };
83
84 /*
85 * get_can_dlc(value) - helper macro to cast a given data length code (dlc)
86 * to __u8 and ensure the dlc value to be max. 8 bytes.
87 *
88 * To be used in the CAN netdriver receive path to ensure conformance with
89 * ISO 11898-1 Chapter 8.4.2.3 (DLC field)
90 */
91 #define get_can_dlc(i) (min_t(__u8, (i), CAN_MAX_DLC))
92 #define get_canfd_dlc(i) (min_t(__u8, (i), CANFD_MAX_DLC))
93
94 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
can_dropped_invalid_skb(struct net_device * dev,struct sk_buff * skb)95 static inline bool can_dropped_invalid_skb(struct net_device *dev,
96 struct sk_buff *skb)
97 {
98 const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
99
100 if (skb->protocol == htons(ETH_P_CAN)) {
101 if (unlikely(skb->len != CAN_MTU ||
102 cfd->len > CAN_MAX_DLEN))
103 goto inval_skb;
104 } else if (skb->protocol == htons(ETH_P_CANFD)) {
105 if (unlikely(skb->len != CANFD_MTU ||
106 cfd->len > CANFD_MAX_DLEN))
107 goto inval_skb;
108 } else
109 goto inval_skb;
110
111 return false;
112
113 inval_skb:
114 kfree_skb(skb);
115 dev->stats.tx_dropped++;
116 return true;
117 }
118
can_is_canfd_skb(const struct sk_buff * skb)119 static inline bool can_is_canfd_skb(const struct sk_buff *skb)
120 {
121 /* the CAN specific type of skb is identified by its data length */
122 return skb->len == CANFD_MTU;
123 }
124
125 /* helper to define static CAN controller features at device creation time */
can_set_static_ctrlmode(struct net_device * dev,u32 static_mode)126 static inline void can_set_static_ctrlmode(struct net_device *dev,
127 u32 static_mode)
128 {
129 struct can_priv *priv = netdev_priv(dev);
130
131 /* alloc_candev() succeeded => netdev_priv() is valid at this point */
132 priv->ctrlmode = static_mode;
133 priv->ctrlmode_static = static_mode;
134
135 /* override MTU which was set by default in can_setup()? */
136 if (static_mode & CAN_CTRLMODE_FD)
137 dev->mtu = CANFD_MTU;
138 }
139
140 /* get data length from can_dlc with sanitized can_dlc */
141 u8 can_dlc2len(u8 can_dlc);
142
143 /* map the sanitized data length to an appropriate data length code */
144 u8 can_len2dlc(u8 len);
145
146 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
147 unsigned int txqs, unsigned int rxqs);
148 #define alloc_candev(sizeof_priv, echo_skb_max) \
149 alloc_candev_mqs(sizeof_priv, echo_skb_max, 1, 1)
150 #define alloc_candev_mq(sizeof_priv, echo_skb_max, count) \
151 alloc_candev_mqs(sizeof_priv, echo_skb_max, count, count)
152 void free_candev(struct net_device *dev);
153
154 /* a candev safe wrapper around netdev_priv */
155 struct can_priv *safe_candev_priv(struct net_device *dev);
156
157 int open_candev(struct net_device *dev);
158 void close_candev(struct net_device *dev);
159 int can_change_mtu(struct net_device *dev, int new_mtu);
160
161 int register_candev(struct net_device *dev);
162 void unregister_candev(struct net_device *dev);
163
164 int can_restart_now(struct net_device *dev);
165 void can_bus_off(struct net_device *dev);
166
167 void can_change_state(struct net_device *dev, struct can_frame *cf,
168 enum can_state tx_state, enum can_state rx_state);
169
170 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
171 unsigned int idx);
172 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx);
173 void can_free_echo_skb(struct net_device *dev, unsigned int idx);
174
175 #ifdef CONFIG_OF
176 void of_can_transceiver(struct net_device *dev);
177 #else
of_can_transceiver(struct net_device * dev)178 static inline void of_can_transceiver(struct net_device *dev) { }
179 #endif
180
181 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
182 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
183 struct canfd_frame **cfd);
184 struct sk_buff *alloc_can_err_skb(struct net_device *dev,
185 struct can_frame **cf);
186
187 #endif /* !_CAN_DEV_H */
188