blob: f01623aef2f77945514d4b2ef0045f0735e11851 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00002/*
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 *
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000012 */
13
Oliver Hartkopp42193e32014-05-15 20:31:56 +020014#ifndef _CAN_DEV_H
15#define _CAN_DEV_H
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000016
Hans J. Koch829e0012010-04-13 00:03:25 +000017#include <linux/can.h>
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000018#include <linux/can/error.h>
Fabio Baltieri996a9532012-12-18 18:50:55 +010019#include <linux/can/led.h>
Marc Kleine-Budde27859682015-05-09 17:47:52 +020020#include <linux/can/netlink.h>
21#include <linux/netdevice.h>
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000022
23/*
24 * CAN mode
25 */
26enum can_mode {
27 CAN_MODE_STOP = 0,
28 CAN_MODE_START,
29 CAN_MODE_SLEEP
30};
31
32/*
33 * CAN common private data
34 */
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000035struct can_priv {
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +030036 struct net_device *dev;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000037 struct can_device_stats can_stats;
38
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +010039 struct can_bittiming bittiming, data_bittiming;
40 const struct can_bittiming_const *bittiming_const,
41 *data_bittiming_const;
Oliver Hartkopp12a60752017-01-10 18:52:06 +010042 const u16 *termination_const;
43 unsigned int termination_const_cnt;
44 u16 termination;
Marc Kleine-Budde431af772017-01-11 17:05:35 +010045 const u32 *bitrate_const;
46 unsigned int bitrate_const_cnt;
47 const u32 *data_bitrate_const;
48 unsigned int data_bitrate_const_cnt;
Franklin S Cooper Jr2290aef2018-01-10 16:25:18 +053049 u32 bitrate_max;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000050 struct can_clock clock;
51
52 enum can_state state;
Oliver Hartkoppbb208f12016-03-21 20:18:21 +010053
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 */
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000058
59 int restart_ms;
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +030060 struct delayed_work restart_work;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000061
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000062 int (*do_set_bittiming)(struct net_device *dev);
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +010063 int (*do_set_data_bittiming)(struct net_device *dev);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000064 int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
Oliver Hartkopp12a60752017-01-10 18:52:06 +010065 int (*do_set_termination)(struct net_device *dev, u16 term);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000066 int (*do_get_state)(const struct net_device *dev,
67 enum can_state *state);
Wolfgang Grandegger52c793f2010-02-22 22:21:17 +000068 int (*do_get_berr_counter)(const struct net_device *dev,
69 struct can_berr_counter *bec);
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +000070
71 unsigned int echo_skb_max;
72 struct sk_buff **echo_skb;
Fabio Baltieri996a9532012-12-18 18:50:55 +010073
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];
Yegor Yefremovc54eb702015-03-16 09:38:13 +010079 struct led_trigger *rxtx_led_trig;
80 char rxtx_led_trig_name[CAN_LED_NAME_SZ];
Fabio Baltieri996a9532012-12-18 18:50:55 +010081#endif
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000082};
83
Oliver Hartkoppc7cd6062009-12-12 04:13:21 +000084/*
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 */
Oliver Hartkopp1e0625f2012-06-13 20:48:21 +020091#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))
Oliver Hartkoppc7cd6062009-12-12 04:13:21 +000093
Oliver Hartkopp3ccd4c62010-01-12 02:00:46 -080094/* Drop a given socketbuffer if it does not contain a valid CAN frame. */
Yaowei Baid6fbaea2015-10-08 21:28:57 +080095static inline bool can_dropped_invalid_skb(struct net_device *dev,
Oliver Hartkopp3ccd4c62010-01-12 02:00:46 -080096 struct sk_buff *skb)
97{
Oliver Hartkopp1e0625f2012-06-13 20:48:21 +020098 const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
Oliver Hartkopp3ccd4c62010-01-12 02:00:46 -080099
Oliver Hartkopp1e0625f2012-06-13 20:48:21 +0200100 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;
Oliver Hartkopp3ccd4c62010-01-12 02:00:46 -0800110
Yaowei Baid6fbaea2015-10-08 21:28:57 +0800111 return false;
Oliver Hartkopp1e0625f2012-06-13 20:48:21 +0200112
113inval_skb:
114 kfree_skb(skb);
115 dev->stats.tx_dropped++;
Yaowei Baid6fbaea2015-10-08 21:28:57 +0800116 return true;
Oliver Hartkopp3ccd4c62010-01-12 02:00:46 -0800117}
118
Dong Aisheng98e69012014-11-07 16:45:12 +0800119static 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
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100125/* helper to define static CAN controller features at device creation time */
126static 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
Oliver Hartkopp1e0625f2012-06-13 20:48:21 +0200140/* get data length from can_dlc with sanitized can_dlc */
141u8 can_dlc2len(u8 can_dlc);
142
143/* map the sanitized data length to an appropriate data length code */
144u8 can_len2dlc(u8 len);
145
Zhu Yi03870902018-06-13 16:37:17 +0200146struct 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)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000152void free_candev(struct net_device *dev);
153
Kurt Van Dijckbf03a532012-12-18 18:50:56 +0100154/* a candev safe wrapper around netdev_priv */
155struct can_priv *safe_candev_priv(struct net_device *dev);
156
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000157int open_candev(struct net_device *dev);
158void close_candev(struct net_device *dev);
Oliver Hartkoppbc05a892014-02-28 16:36:24 +0100159int can_change_mtu(struct net_device *dev, int new_mtu);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000160
161int register_candev(struct net_device *dev);
162void unregister_candev(struct net_device *dev);
163
164int can_restart_now(struct net_device *dev);
165void can_bus_off(struct net_device *dev);
166
Andri Yngvasonbac78aa2014-12-03 17:54:13 +0000167void can_change_state(struct net_device *dev, struct can_frame *cf,
168 enum can_state tx_state, enum can_state rx_state);
169
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000170void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
171 unsigned int idx);
Marc Kleine-Buddea4310fa2018-10-31 10:37:46 +0100172struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr);
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200173unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx);
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000174void can_free_echo_skb(struct net_device *dev, unsigned int idx);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000175
Franklin S Cooper Jr2290aef2018-01-10 16:25:18 +0530176#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
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700182struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
Stephane Grosjeancb2518c2014-01-15 09:50:13 +0100183struct sk_buff *alloc_canfd_skb(struct net_device *dev,
184 struct canfd_frame **cfd);
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700185struct sk_buff *alloc_can_err_skb(struct net_device *dev,
186 struct can_frame **cf);
187
Oliver Hartkopp42193e32014-05-15 20:31:56 +0200188#endif /* !_CAN_DEV_H */