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