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