// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
* Copyright (C) 2006 Andrey Volkov, Varma Electronics
* Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/workqueue.h>
#include <linux/can.h>
#include <linux/can/can-ml.h>
#include <linux/can/dev.h>
#include <linux/can/skb.h>
#include <linux/can/netlink.h>
#include <linux/can/led.h>
#include <linux/of.h>
#include <net/rtnetlink.h>
#define MOD_DESC "CAN device driver interface"
MODULE_DESCRIPTION(MOD_DESC);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
/* CAN DLC to real data length conversion helpers */
static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
8, 12, 16, 20, 24, 32, 48, 64};
/* get data length from raw data length code (DLC) */
u8 can_fd_dlc2len(u8 dlc)
{
return dlc2len[dlc & 0x0F];
}
EXPORT_SYMBOL_GPL(can_fd_dlc2len);
static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
9, 9, 9, 9, /* 9 - 12 */
10, 10, 10, 10, /* 13 - 16 */
11, 11, 11, 11, /* 17 - 20 */
12, 12, 12, 12, /* 21 - 24 */
13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
/* map the sanitized data length to an appropriate data length code */
u8 can_fd_len2dlc(u8 len)
{
if (unlikely(len > 64))
return 0xF;
return len2dlc[len];
}
EXPORT_SYMBOL_GPL(can_fd_len2dlc);
static void can_update_state_error_stats(struct net_device *dev,
enum can_state new_state)
{
struct can_priv *priv = netdev_priv(dev);
if (new_state <= priv->state)
return;
switch (new_state) {
case CAN_STATE_ERROR_WARNING:
priv->can_stats.error_warning++;
break;
case CAN_STATE_ERROR_PASSIVE:
priv->can_stats.error_passive++;
break;
case CAN_STATE_BUS_OFF:
priv->can_stats.bus_off++;
break;
default:
break;
}
}
static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
{
switch (state) {
case CAN_STATE_ERROR_ACTIVE:
return CAN_ERR_CRTL_ACTIVE;
case CAN_STATE_ERROR_WARNING:
return CAN_ERR_CRTL_TX_WARNING;
case CAN_STATE_ERROR_PASSIVE:
return CAN_ERR_CRTL_TX_PASSIVE;
default:
return 0;
}
}
static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
{
switch (state) {
case CAN_STATE_ERROR_ACTIVE:
return CAN_ERR_CRTL_ACTIVE;
case CAN_STATE_ERROR_WARNING:
return CAN_ERR_CRTL_RX_WARNING;
case CAN_STATE_ERROR_PASSIVE:
return CAN_ERR_CRTL_RX_PASSIVE;
default:
return 0;
}
}
static const char *can_get_state_str(const enum can_state state)
{
switch (state) {
case CAN_STATE_ERROR_ACTIVE:
return "Error Active";
case CAN_STATE_ERROR_WARNING:
return "Error Warning";
case CAN_STATE_ERROR_PASSIVE:
return "Error Passive";
case CAN_STATE_BUS_OFF:
return "Bus Off";
case CAN_STATE_STOPPED:
return "Stopped";
case CAN_STATE_SLEEPING:
return "Sleeping";
default:
return "<unknown>";
}
return "<unknown>";
}
void can_change_state(struct net_device *dev, struct can_frame *cf,
enum can_state tx_state, enum can_state rx_state)
{
struct can_priv *priv = netdev_priv(dev);
enum can_state new_state = max(tx_state, rx_state);
if (unlikely(new_state == priv->state)) {
netdev_warn(dev, "%s: oops, state did not change", __func__);
return;
}
netdev_dbg(dev, "Controller changed from %s State (%d) into %s State (%d).\n",
can_get_state_str(priv->state), priv->state,
can_get_state_str(new_state), new_state);
can_update_state_error_stats(dev, new_state);
priv->state = new_state;
if (!cf)
return;
if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
cf->can_id |= CAN_ERR_BUSOFF;
return;
}
cf->can_id |=