1 /*
2 * linux/can/dev.h
3 *
4 * Definitions for the CAN network device driver interface
5 *
6 * Copyright (C) 2006 Andrey Volkov <avolkov@varma-el.com>
7 * Varma Electronics Oy
8 *
9 * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
10 *
11 */
12
13 #ifndef _CAN_DEV_H
14 #define _CAN_DEV_H
15
16 #include <linux/can.h>
17 #include <linux/can/error.h>
18 #include <linux/can/led.h>
19 #include <linux/can/netlink.h>
20 #include <linux/can/skb.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 struct can_clock clock;
43
44 enum can_state state;
45
46 /* CAN controller features - see include/uapi/linux/can/netlink.h */
47 u32 ctrlmode; /* current options setting */
48 u32 ctrlmode_supported; /* options that can be modified by netlink */
49 u32 ctrlmode_static; /* static enabled options for driver/hardware */
50
51 int restart_ms;
52 struct delayed_work restart_work;
53
54 int (*do_set_bittiming)(struct net_device *dev);
55 int (*do_set_data_bittiming)(struct net_device *dev);
56 int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
57 int (*do_get_state)(const struct net_device *dev,
58 enum can_state *state);
59 int (*do_get_berr_counter)(const struct net_device *dev,
60 struct can_berr_counter *bec);
61
62 unsigned int echo_skb_max;
63 struct sk_buff **echo_skb;
64
65 #ifdef CONFIG_CAN_LEDS
66 struct led_trigger *tx_led_trig;
67 char tx_led_trig_name[CAN_LED_NAME_SZ];
68 struct led_trigger *rx_led_trig;
69 char rx_led_trig_name[CAN_LED_NAME_SZ];
70 struct led_trigger *rxtx_led_trig;
71 char rxtx_led_trig_name[CAN_LED_NAME_SZ];
72 #endif
73 };
74
75 /*
76 * get_can_dlc(value) - helper macro to cast a given data length code (dlc)
77 * to __u8 and ensure the dlc value to be max. 8 bytes.
78 *
79 * To be used in the CAN netdriver receive path to ensure conformance with
80 * ISO 11898-1 Chapter 8.4.2.3 (DLC field)
81 */
82 #define get_can_dlc(i) (min_t(__u8, (i), CAN_MAX_DLC))
83 #define get_canfd_dlc(i) (min_t(__u8, (i), CANFD_MAX_DLC))
84
85 /* Check for outgoing skbs that have not been created by the CAN subsystem */
can_skb_headroom_valid(struct net_device * dev,struct sk_buff * skb)86 static inline bool can_skb_headroom_valid(struct net_device *dev,
87 struct sk_buff *skb)
88 {
89 /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
90 if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
91 return false;
92
93 /* af_packet does not apply CAN skb specific settings */
94 if (skb->ip_summed == CHECKSUM_NONE) {
95 /* init headroom */
96 can_skb_prv(skb)->ifindex = dev->ifindex;
97 can_skb_prv(skb)->skbcnt = 0;
98
99 skb->ip_summed = CHECKSUM_UNNECESSARY;
100
101 /* preform proper loopback on capable devices */
102 if (dev->flags & IFF_ECHO)
103 skb->pkt_type = PACKET_LOOPBACK;
104 else
105 skb->pkt_type = PACKET_HOST;
106
107 skb_reset_mac_header(skb);
108 skb_reset_network_header(skb);
109 skb_reset_transport_header(skb);
110 }
111
112 return true;
113 }
114
115 /* 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)116 static inline bool can_dropped_invalid_skb(struct net_device *dev,
117 struct sk_buff *skb)
118 {
119 const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
120
121 if (skb->protocol == htons(ETH_P_CAN)) {
122 if (unlikely(skb->len != CAN_MTU ||
123 cfd->len > CAN_MAX_DLEN))
124 goto inval_skb;
125 } else if (skb->protocol == htons(ETH_P_CANFD)) {
126 if (unlikely(skb->len != CANFD_MTU ||
127 cfd->len > CANFD_MAX_DLEN))
128 goto inval_skb;
129 } else
130 goto inval_skb;
131
132 if (!can_skb_headroom_valid(dev, skb))
133 goto inval_skb;
134
135 return false;
136
137 inval_skb:
138 kfree_skb(skb);
139 dev->stats.tx_dropped++;
140 return true;
141 }
142
can_is_canfd_skb(const struct sk_buff * skb)143 static inline bool can_is_canfd_skb(const struct sk_buff *skb)
144 {
145 /* the CAN specific type of skb is identified by its data length */
146 return skb->len == CANFD_MTU;
147 }
148
149 /* helper to define static CAN controller features at device creation time */
can_set_static_ctrlmode(struct net_device * dev,u32 static_mode)150 static inline void can_set_static_ctrlmode(struct net_device *dev,
151 u32 static_mode)
152 {
153 struct can_priv *priv = netdev_priv(dev);
154
155 /* alloc_candev() succeeded => netdev_priv() is valid at this point */
156 priv->ctrlmode = static_mode;
157 priv->ctrlmode_static = static_mode;
158
159 /* override MTU which was set by default in can_setup()? */
160 if (static_mode & CAN_CTRLMODE_FD)
161 dev->mtu = CANFD_MTU;
162 }
163
164 /* get data length from can_dlc with sanitized can_dlc */
165 u8 can_dlc2len(u8 can_dlc);
166
167 /* map the sanitized data length to an appropriate data length code */
168 u8 can_len2dlc(u8 len);
169
170 struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max);
171 void free_candev(struct net_device *dev);
172
173 /* a candev safe wrapper around netdev_priv */
174 struct can_priv *safe_candev_priv(struct net_device *dev);
175
176 int open_candev(struct net_device *dev);
177 void close_candev(struct net_device *dev);
178 int can_change_mtu(struct net_device *dev, int new_mtu);
179
180 int register_candev(struct net_device *dev);
181 void unregister_candev(struct net_device *dev);
182
183 int can_restart_now(struct net_device *dev);
184 void can_bus_off(struct net_device *dev);
185
186 void can_change_state(struct net_device *dev, struct can_frame *cf,
187 enum can_state tx_state, enum can_state rx_state);
188
189 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
190 unsigned int idx);
191 struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr);
192 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx);
193 void can_free_echo_skb(struct net_device *dev, unsigned int idx);
194
195 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
196 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
197 struct canfd_frame **cfd);
198 struct sk_buff *alloc_can_err_skb(struct net_device *dev,
199 struct can_frame **cf);
200
201 #endif /* !_CAN_DEV_H */
202