Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
+180
View File
@@ -0,0 +1,180 @@
# SPDX-License-Identifier: GPL-2.0-only
menu "CAN USB interfaces"
depends on USB
config CAN_8DEV_USB
tristate "8 devices USB2CAN interface"
help
This driver supports the USB2CAN interface
from 8 devices (http://www.8devices.com).
config CAN_EMS_USB
tristate "EMS CPC-USB/ARM7 CAN/USB interface"
help
This driver is for the one channel CPC-USB/ARM7 CAN/USB interface
from EMS Dr. Thomas Wuensche (http://www.ems-wuensche.de).
config CAN_ESD_USB
tristate "esd electronics gmbh CAN/USB interfaces"
help
This driver adds support for several CAN/USB interfaces
from esd electronics gmbh (https://www.esd.eu).
The drivers supports the following devices:
- esd CAN-USB/2
- esd CAN-USB/3-FD
- esd CAN-USB/Micro
To compile this driver as a module, choose M here: the module
will be called esd_usb.
config CAN_ETAS_ES58X
tristate "ETAS ES58X CAN/USB interfaces"
select CRC16
select NET_DEVLINK
help
This driver supports the ES581.4, ES582.1 and ES584.1 interfaces
from ETAS GmbH (https://www.etas.com/en/products/es58x.php).
To compile this driver as a module, choose M here: the module
will be called etas_es58x.
config CAN_F81604
tristate "Fintek F81604 USB to 2CAN interface"
help
This driver supports the Fintek F81604 USB to 2CAN interface.
The device can support CAN2.0A/B protocol and also support
2 output pins to control external terminator (optional).
To compile this driver as a module, choose M here: the module will
be called f81604.
(see also https://www.fintek.com.tw).
config CAN_GS_USB
tristate "Geschwister Schneider UG and candleLight compatible interfaces"
select CAN_RX_OFFLOAD
help
This driver supports the Geschwister Schneider and
bytewerk.org candleLight compatible
(https://github.com/candle-usb/candleLight_fw) USB/CAN
interfaces.
If unsure choose N,
choose Y for built in support,
M to compile as module (module will be named: gs_usb).
config CAN_KVASER_USB
tristate "Kvaser CAN/USB interface"
select NET_DEVLINK
help
This driver adds support for Kvaser CAN/USB devices like Kvaser
Leaf Light, Kvaser USBcan II and Kvaser Memorator Pro 5xHS.
The driver provides support for the following devices:
- Kvaser Leaf Light
- Kvaser Leaf Professional HS
- Kvaser Leaf SemiPro HS
- Kvaser Leaf Professional LS
- Kvaser Leaf Professional SWC
- Kvaser Leaf Professional LIN
- Kvaser Leaf SemiPro LS
- Kvaser Leaf SemiPro SWC
- Kvaser Memorator II HS/HS
- Kvaser USBcan Professional HS/HS
- Kvaser Leaf Light GI
- Kvaser Leaf Professional HS (OBD-II connector)
- Kvaser Memorator Professional HS/LS
- Kvaser Leaf Light "China"
- Kvaser BlackBird SemiPro
- Kvaser USBcan R
- Kvaser USBcan R v2
- Kvaser Leaf Light v2
- Kvaser Leaf Light R v2
- Kvaser Mini PCI Express HS
- Kvaser Mini PCI Express 2xHS
- Kvaser Mini PCIe 1xCAN
- Kvaser USBcan Light 2xHS
- Kvaser USBcan II HS/HS
- Kvaser USBcan II HS/LS
- Kvaser USBcan Rugged ("USBcan Rev B")
- Kvaser Memorator HS/HS
- Kvaser Memorator HS/LS
- Scania VCI2 (if you have the Kvaser logo on top)
- Kvaser BlackBird v2
- Kvaser Leaf Pro HS v2
- Kvaser Leaf v3
- Kvaser Hybrid CAN/LIN
- Kvaser Hybrid 2xCAN/LIN
- Kvaser Hybrid Pro CAN/LIN
- Kvaser Hybrid Pro 2xCAN/LIN
- Kvaser Memorator 2xHS v2
- Kvaser Memorator Pro 2xHS v2
- Kvaser Memorator Pro 5xHS
- Kvaser USBcan Light 4xHS
- Kvaser USBcan Pro 2xHS v2
- Kvaser USBcan Pro 4xHS
- Kvaser USBcan Pro 5xCAN
- Kvaser USBcan Pro 5xHS
- Kvaser U100
- Kvaser U100P
- Kvaser U100S
- ATI Memorator Pro 2xHS v2
- ATI USBcan Pro 2xHS v2
- Vining 800
If unsure, say N.
To compile this driver as a module, choose M here: the
module will be called kvaser_usb.
config CAN_MCBA_USB
tristate "Microchip CAN BUS Analyzer interface"
help
This driver supports the CAN BUS Analyzer interface
from Microchip (http://www.microchip.com/development-tools/).
config CAN_NCT6694
tristate "Nuvoton NCT6694 Socket CANfd support"
depends on MFD_NCT6694
select CAN_RX_OFFLOAD
help
If you say yes to this option, support will be included for Nuvoton
NCT6694, a USB device to socket CANfd controller.
This driver can also be built as a module. If so, the module will
be called nct6694_canfd.
config CAN_PEAK_USB
tristate "PEAK PCAN-USB/USB Pro interfaces for CAN 2.0b/CAN-FD"
help
This driver supports the PEAK-System Technik USB adapters that enable
access to the CAN bus, with respect to the CAN 2.0b and/or CAN-FD
standards, that is:
PCAN-USB single CAN 2.0b channel USB adapter
PCAN-USB Pro dual CAN 2.0b channels USB adapter
PCAN-USB FD single CAN-FD channel USB adapter
PCAN-USB Pro FD dual CAN-FD channels USB adapter
PCAN-Chip USB CAN-FD to USB stamp module
PCAN-USB X6 6 CAN-FD channels USB adapter
(see also http://www.peak-system.com).
config CAN_UCAN
tristate "Theobroma Systems UCAN interface"
help
This driver supports the Theobroma Systems
UCAN USB-CAN interface.
The UCAN driver supports the microcontroller-based USB/CAN
adapters from Theobroma Systems. There are two form-factors
that run essentially the same firmware:
* Seal: standalone USB stick
https://www.theobroma-systems.com/seal)
* Mule: integrated on the PCB of various System-on-Modules
from Theobroma Systems like the A31-µQ7 and the RK3399-Q7
(https://www.theobroma-systems.com/rk3399-q7)
endmenu
+16
View File
@@ -0,0 +1,16 @@
# SPDX-License-Identifier: GPL-2.0
#
# Makefile for the Linux Controller Area Network USB drivers.
#
obj-$(CONFIG_CAN_8DEV_USB) += usb_8dev.o
obj-$(CONFIG_CAN_EMS_USB) += ems_usb.o
obj-$(CONFIG_CAN_ESD_USB) += esd_usb.o
obj-$(CONFIG_CAN_ETAS_ES58X) += etas_es58x/
obj-$(CONFIG_CAN_F81604) += f81604.o
obj-$(CONFIG_CAN_GS_USB) += gs_usb.o
obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb/
obj-$(CONFIG_CAN_MCBA_USB) += mcba_usb.o
obj-$(CONFIG_CAN_NCT6694) += nct6694_canfd.o
obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/
obj-$(CONFIG_CAN_UCAN) += ucan.o
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,3 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_CAN_ETAS_ES58X) += etas_es58x.o
etas_es58x-y = es58x_core.o es58x_devlink.o es581_4.o es58x_fd.o
@@ -0,0 +1,507 @@
// SPDX-License-Identifier: GPL-2.0
/* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.
*
* File es581_4.c: Adds support to ETAS ES581.4.
*
* Copyright (c) 2019 Robert Bosch Engineering and Business Solutions. All rights reserved.
* Copyright (c) 2020 ETAS K.K.. All rights reserved.
* Copyright (c) 2020-2022 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
*/
#include <linux/unaligned.h>
#include <linux/kernel.h>
#include <linux/units.h>
#include "es58x_core.h"
#include "es581_4.h"
/**
* es581_4_sizeof_rx_tx_msg() - Calculate the actual length of the
* structure of a rx or tx message.
* @msg: message of variable length, must have a dlc field.
*
* Even if RTR frames have actually no payload, the ES58X devices
* still expect it. Must be a macro in order to accept several types
* (struct es581_4_tx_can_msg and struct es581_4_rx_can_msg) as an
* input.
*
* Return: length of the message.
*/
#define es581_4_sizeof_rx_tx_msg(msg) \
offsetof(typeof(msg), data[can_cc_dlc2len((msg).dlc)])
static u16 es581_4_get_msg_len(const union es58x_urb_cmd *urb_cmd)
{
return get_unaligned_le16(&urb_cmd->es581_4_urb_cmd.msg_len);
}
static int es581_4_echo_msg(struct es58x_device *es58x_dev,
const struct es581_4_urb_cmd *es581_4_urb_cmd)
{
struct net_device *netdev;
const struct es581_4_bulk_echo_msg *bulk_echo_msg;
const struct es581_4_echo_msg *echo_msg;
u64 *tstamps = es58x_dev->timestamps;
u16 msg_len;
u32 first_packet_idx, packet_idx;
unsigned int dropped = 0;
int i, num_element, ret;
bulk_echo_msg = &es581_4_urb_cmd->bulk_echo_msg;
msg_len = get_unaligned_le16(&es581_4_urb_cmd->msg_len) -
sizeof(bulk_echo_msg->channel_no);
num_element = es58x_msg_num_element(es58x_dev->dev,
bulk_echo_msg->echo_msg, msg_len);
if (num_element <= 0)
return num_element;
ret = es58x_get_netdev(es58x_dev, bulk_echo_msg->channel_no,
ES581_4_CHANNEL_IDX_OFFSET, &netdev);
if (ret)
return ret;
echo_msg = &bulk_echo_msg->echo_msg[0];
first_packet_idx = get_unaligned_le32(&echo_msg->packet_idx);
packet_idx = first_packet_idx;
for (i = 0; i < num_element; i++) {
u32 tmp_idx;
echo_msg = &bulk_echo_msg->echo_msg[i];
tmp_idx = get_unaligned_le32(&echo_msg->packet_idx);
if (tmp_idx == packet_idx - 1) {
if (net_ratelimit())
netdev_warn(netdev,
"Received echo packet idx %u twice\n",
packet_idx - 1);
dropped++;
continue;
}
if (tmp_idx != packet_idx) {
netdev_err(netdev, "Echo packet idx jumped from %u to %u\n",
packet_idx - 1, echo_msg->packet_idx);
return -EBADMSG;
}
tstamps[i] = get_unaligned_le64(&echo_msg->timestamp);
packet_idx++;
}
netdev->stats.tx_dropped += dropped;
return es58x_can_get_echo_skb(netdev, first_packet_idx,
tstamps, num_element - dropped);
}
static int es581_4_rx_can_msg(struct es58x_device *es58x_dev,
const struct es581_4_urb_cmd *es581_4_urb_cmd,
u16 msg_len)
{
const struct device *dev = es58x_dev->dev;
struct net_device *netdev;
int pkts, num_element, channel_no, ret;
num_element = es58x_msg_num_element(dev, es581_4_urb_cmd->rx_can_msg,
msg_len);
if (num_element <= 0)
return num_element;
channel_no = es581_4_urb_cmd->rx_can_msg[0].channel_no;
ret = es58x_get_netdev(es58x_dev, channel_no,
ES581_4_CHANNEL_IDX_OFFSET, &netdev);
if (ret)
return ret;
if (!netif_running(netdev)) {
if (net_ratelimit())
netdev_info(netdev,
"%s: %s is down, dropping %d rx packets\n",
__func__, netdev->name, num_element);
netdev->stats.rx_dropped += num_element;
return 0;
}
for (pkts = 0; pkts < num_element; pkts++) {
const struct es581_4_rx_can_msg *rx_can_msg =
&es581_4_urb_cmd->rx_can_msg[pkts];
u64 tstamp = get_unaligned_le64(&rx_can_msg->timestamp);
canid_t can_id = get_unaligned_le32(&rx_can_msg->can_id);
if (channel_no != rx_can_msg->channel_no)
return -EBADMSG;
ret = es58x_rx_can_msg(netdev, tstamp, rx_can_msg->data,
can_id, rx_can_msg->flags,
rx_can_msg->dlc);
if (ret)
break;
}
return ret;
}
static int es581_4_rx_err_msg(struct es58x_device *es58x_dev,
const struct es581_4_rx_err_msg *rx_err_msg)
{
struct net_device *netdev;
enum es58x_err error = get_unaligned_le32(&rx_err_msg->error);
int ret;
ret = es58x_get_netdev(es58x_dev, rx_err_msg->channel_no,
ES581_4_CHANNEL_IDX_OFFSET, &netdev);
if (ret)
return ret;
return es58x_rx_err_msg(netdev, error, 0,
get_unaligned_le64(&rx_err_msg->timestamp));
}
static int es581_4_rx_event_msg(struct es58x_device *es58x_dev,
const struct es581_4_rx_event_msg *rx_event_msg)
{
struct net_device *netdev;
enum es58x_event event = get_unaligned_le32(&rx_event_msg->event);
int ret;
ret = es58x_get_netdev(es58x_dev, rx_event_msg->channel_no,
ES581_4_CHANNEL_IDX_OFFSET, &netdev);
if (ret)
return ret;
return es58x_rx_err_msg(netdev, 0, event,
get_unaligned_le64(&rx_event_msg->timestamp));
}
static int es581_4_rx_cmd_ret_u32(struct es58x_device *es58x_dev,
const struct es581_4_urb_cmd *es581_4_urb_cmd,
enum es58x_ret_type ret_type)
{
struct net_device *netdev;
const struct es581_4_rx_cmd_ret *rx_cmd_ret;
u16 msg_len = get_unaligned_le16(&es581_4_urb_cmd->msg_len);
int ret;
ret = es58x_check_msg_len(es58x_dev->dev,
es581_4_urb_cmd->rx_cmd_ret, msg_len);
if (ret)
return ret;
rx_cmd_ret = &es581_4_urb_cmd->rx_cmd_ret;
ret = es58x_get_netdev(es58x_dev, rx_cmd_ret->channel_no,
ES581_4_CHANNEL_IDX_OFFSET, &netdev);
if (ret)
return ret;
return es58x_rx_cmd_ret_u32(netdev, ret_type,
get_unaligned_le32(&rx_cmd_ret->rx_cmd_ret_le32));
}
static int es581_4_tx_ack_msg(struct es58x_device *es58x_dev,
const struct es581_4_urb_cmd *es581_4_urb_cmd)
{
struct net_device *netdev;
const struct es581_4_tx_ack_msg *tx_ack_msg;
u16 msg_len = get_unaligned_le16(&es581_4_urb_cmd->msg_len);
int ret;
tx_ack_msg = &es581_4_urb_cmd->tx_ack_msg;
ret = es58x_check_msg_len(es58x_dev->dev, *tx_ack_msg, msg_len);
if (ret)
return ret;
if (tx_ack_msg->rx_cmd_ret_u8 != ES58X_RET_U8_OK)
return es58x_rx_cmd_ret_u8(es58x_dev->dev,
ES58X_RET_TYPE_TX_MSG,
tx_ack_msg->rx_cmd_ret_u8);
ret = es58x_get_netdev(es58x_dev, tx_ack_msg->channel_no,
ES581_4_CHANNEL_IDX_OFFSET, &netdev);
if (ret)
return ret;
return es58x_tx_ack_msg(netdev,
get_unaligned_le16(&tx_ack_msg->tx_free_entries),
ES58X_RET_U32_OK);
}
static int es581_4_dispatch_rx_cmd(struct es58x_device *es58x_dev,
const struct es581_4_urb_cmd *es581_4_urb_cmd)
{
const struct device *dev = es58x_dev->dev;
u16 msg_len = get_unaligned_le16(&es581_4_urb_cmd->msg_len);
enum es581_4_rx_type rx_type = es581_4_urb_cmd->rx_can_msg[0].rx_type;
int ret = 0;
switch (rx_type) {
case ES581_4_RX_TYPE_MESSAGE:
return es581_4_rx_can_msg(es58x_dev, es581_4_urb_cmd, msg_len);
case ES581_4_RX_TYPE_ERROR:
ret = es58x_check_msg_len(dev, es581_4_urb_cmd->rx_err_msg,
msg_len);
if (ret < 0)
return ret;
return es581_4_rx_err_msg(es58x_dev,
&es581_4_urb_cmd->rx_err_msg);
case ES581_4_RX_TYPE_EVENT:
ret = es58x_check_msg_len(dev, es581_4_urb_cmd->rx_event_msg,
msg_len);
if (ret < 0)
return ret;
return es581_4_rx_event_msg(es58x_dev,
&es581_4_urb_cmd->rx_event_msg);
default:
dev_err(dev, "%s: Unknown rx_type 0x%02X\n", __func__, rx_type);
return -EBADRQC;
}
}
static int es581_4_handle_urb_cmd(struct es58x_device *es58x_dev,
const union es58x_urb_cmd *urb_cmd)
{
const struct es581_4_urb_cmd *es581_4_urb_cmd;
struct device *dev = es58x_dev->dev;
u16 msg_len = es581_4_get_msg_len(urb_cmd);
int ret;
es581_4_urb_cmd = &urb_cmd->es581_4_urb_cmd;
if (es581_4_urb_cmd->cmd_type != ES581_4_CAN_COMMAND_TYPE) {
dev_err(dev, "%s: Unknown command type (0x%02X)\n",
__func__, es581_4_urb_cmd->cmd_type);
return -EBADRQC;
}
switch ((enum es581_4_cmd_id)es581_4_urb_cmd->cmd_id) {
case ES581_4_CMD_ID_SET_BITTIMING:
return es581_4_rx_cmd_ret_u32(es58x_dev, es581_4_urb_cmd,
ES58X_RET_TYPE_SET_BITTIMING);
case ES581_4_CMD_ID_ENABLE_CHANNEL:
return es581_4_rx_cmd_ret_u32(es58x_dev, es581_4_urb_cmd,
ES58X_RET_TYPE_ENABLE_CHANNEL);
case ES581_4_CMD_ID_TX_MSG:
return es581_4_tx_ack_msg(es58x_dev, es581_4_urb_cmd);
case ES581_4_CMD_ID_RX_MSG:
return es581_4_dispatch_rx_cmd(es58x_dev, es581_4_urb_cmd);
case ES581_4_CMD_ID_RESET_RX:
ret = es581_4_rx_cmd_ret_u32(es58x_dev, es581_4_urb_cmd,
ES58X_RET_TYPE_RESET_RX);
return ret;
case ES581_4_CMD_ID_RESET_TX:
ret = es581_4_rx_cmd_ret_u32(es58x_dev, es581_4_urb_cmd,
ES58X_RET_TYPE_RESET_TX);
return ret;
case ES581_4_CMD_ID_DISABLE_CHANNEL:
return es581_4_rx_cmd_ret_u32(es58x_dev, es581_4_urb_cmd,
ES58X_RET_TYPE_DISABLE_CHANNEL);
case ES581_4_CMD_ID_TIMESTAMP:
ret = es58x_check_msg_len(dev, es581_4_urb_cmd->timestamp,
msg_len);
if (ret < 0)
return ret;
es58x_rx_timestamp(es58x_dev,
get_unaligned_le64(&es581_4_urb_cmd->timestamp));
return 0;
case ES581_4_CMD_ID_ECHO:
return es581_4_echo_msg(es58x_dev, es581_4_urb_cmd);
case ES581_4_CMD_ID_DEVICE_ERR:
ret = es58x_check_msg_len(dev, es581_4_urb_cmd->rx_cmd_ret_u8,
msg_len);
if (ret)
return ret;
return es58x_rx_cmd_ret_u8(dev, ES58X_RET_TYPE_DEVICE_ERR,
es581_4_urb_cmd->rx_cmd_ret_u8);
default:
dev_warn(dev, "%s: Unexpected command ID: 0x%02X\n",
__func__, es581_4_urb_cmd->cmd_id);
return -EBADRQC;
}
}
static void es581_4_fill_urb_header(union es58x_urb_cmd *urb_cmd, u8 cmd_type,
u8 cmd_id, u8 channel_idx, u16 msg_len)
{
struct es581_4_urb_cmd *es581_4_urb_cmd = &urb_cmd->es581_4_urb_cmd;
es581_4_urb_cmd->SOF = cpu_to_le16(es581_4_param.tx_start_of_frame);
es581_4_urb_cmd->cmd_type = cmd_type;
es581_4_urb_cmd->cmd_id = cmd_id;
es581_4_urb_cmd->msg_len = cpu_to_le16(msg_len);
}
static int es581_4_tx_can_msg(struct es58x_priv *priv,
const struct sk_buff *skb)
{
struct es58x_device *es58x_dev = priv->es58x_dev;
union es58x_urb_cmd *urb_cmd = priv->tx_urb->transfer_buffer;
struct es581_4_urb_cmd *es581_4_urb_cmd = &urb_cmd->es581_4_urb_cmd;
struct can_frame *cf = (struct can_frame *)skb->data;
struct es581_4_tx_can_msg *tx_can_msg;
u16 msg_len;
int ret;
if (can_is_canfd_skb(skb))
return -EMSGSIZE;
if (priv->tx_can_msg_cnt == 0) {
msg_len = sizeof(es581_4_urb_cmd->bulk_tx_can_msg.num_can_msg);
es581_4_fill_urb_header(urb_cmd, ES581_4_CAN_COMMAND_TYPE,
ES581_4_CMD_ID_TX_MSG,
priv->channel_idx, msg_len);
es581_4_urb_cmd->bulk_tx_can_msg.num_can_msg = 0;
} else {
msg_len = es581_4_get_msg_len(urb_cmd);
}
ret = es58x_check_msg_max_len(es58x_dev->dev,
es581_4_urb_cmd->bulk_tx_can_msg,
msg_len + sizeof(*tx_can_msg));
if (ret)
return ret;
/* Fill message contents. */
tx_can_msg = (typeof(tx_can_msg))&es581_4_urb_cmd->raw_msg[msg_len];
put_unaligned_le32(es58x_get_raw_can_id(cf), &tx_can_msg->can_id);
put_unaligned_le32(priv->tx_head, &tx_can_msg->packet_idx);
put_unaligned_le16((u16)es58x_get_flags(skb), &tx_can_msg->flags);
tx_can_msg->channel_no = priv->channel_idx + ES581_4_CHANNEL_IDX_OFFSET;
tx_can_msg->dlc = can_get_cc_dlc(cf, priv->can.ctrlmode);
memcpy(tx_can_msg->data, cf->data, cf->len);
/* Calculate new sizes. */
es581_4_urb_cmd->bulk_tx_can_msg.num_can_msg++;
msg_len += es581_4_sizeof_rx_tx_msg(*tx_can_msg);
priv->tx_urb->transfer_buffer_length = es58x_get_urb_cmd_len(es58x_dev,
msg_len);
es581_4_urb_cmd->msg_len = cpu_to_le16(msg_len);
return 0;
}
static int es581_4_set_bittiming(struct es58x_priv *priv)
{
struct es581_4_tx_conf_msg tx_conf_msg = { 0 };
struct can_bittiming *bt = &priv->can.bittiming;
tx_conf_msg.bitrate = cpu_to_le32(bt->bitrate);
/* bt->sample_point is in tenth of percent. Convert it to percent. */
tx_conf_msg.sample_point = cpu_to_le32(bt->sample_point / 10U);
tx_conf_msg.samples_per_bit = cpu_to_le32(ES58X_SAMPLES_PER_BIT_ONE);
tx_conf_msg.bit_time = cpu_to_le32(can_bit_time(bt));
tx_conf_msg.sjw = cpu_to_le32(bt->sjw);
tx_conf_msg.sync_edge = cpu_to_le32(ES58X_SYNC_EDGE_SINGLE);
tx_conf_msg.physical_layer =
cpu_to_le32(ES58X_PHYSICAL_LAYER_HIGH_SPEED);
tx_conf_msg.echo_mode = cpu_to_le32(ES58X_ECHO_ON);
tx_conf_msg.channel_no = priv->channel_idx + ES581_4_CHANNEL_IDX_OFFSET;
return es58x_send_msg(priv->es58x_dev, ES581_4_CAN_COMMAND_TYPE,
ES581_4_CMD_ID_SET_BITTIMING, &tx_conf_msg,
sizeof(tx_conf_msg), priv->channel_idx);
}
static int es581_4_enable_channel(struct es58x_priv *priv)
{
int ret;
u8 msg = priv->channel_idx + ES581_4_CHANNEL_IDX_OFFSET;
ret = es581_4_set_bittiming(priv);
if (ret)
return ret;
return es58x_send_msg(priv->es58x_dev, ES581_4_CAN_COMMAND_TYPE,
ES581_4_CMD_ID_ENABLE_CHANNEL, &msg, sizeof(msg),
priv->channel_idx);
}
static int es581_4_disable_channel(struct es58x_priv *priv)
{
u8 msg = priv->channel_idx + ES581_4_CHANNEL_IDX_OFFSET;
return es58x_send_msg(priv->es58x_dev, ES581_4_CAN_COMMAND_TYPE,
ES581_4_CMD_ID_DISABLE_CHANNEL, &msg, sizeof(msg),
priv->channel_idx);
}
static int es581_4_reset_device(struct es58x_device *es58x_dev)
{
return es58x_send_msg(es58x_dev, ES581_4_CAN_COMMAND_TYPE,
ES581_4_CMD_ID_RESET_DEVICE,
ES58X_EMPTY_MSG, 0, ES58X_CHANNEL_IDX_NA);
}
static int es581_4_get_timestamp(struct es58x_device *es58x_dev)
{
return es58x_send_msg(es58x_dev, ES581_4_CAN_COMMAND_TYPE,
ES581_4_CMD_ID_TIMESTAMP,
ES58X_EMPTY_MSG, 0, ES58X_CHANNEL_IDX_NA);
}
/* Nominal bittiming constants for ES581.4 as specified in the
* microcontroller datasheet: "Stellaris(R) LM3S5B91 Microcontroller"
* table 17-4 "CAN Protocol Ranges" from Texas Instruments.
*/
static const struct can_bittiming_const es581_4_bittiming_const = {
.name = "ES581.4",
.tseg1_min = 1,
.tseg1_max = 8,
.tseg2_min = 1,
.tseg2_max = 8,
.sjw_max = 4,
.brp_min = 1,
.brp_max = 128,
.brp_inc = 1
};
const struct es58x_parameters es581_4_param = {
.bittiming_const = &es581_4_bittiming_const,
.data_bittiming_const = NULL,
.tdc_const = NULL,
.bitrate_max = 1 * MEGA /* BPS */,
.clock = {.freq = 50 * MEGA /* Hz */},
.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC,
.tx_start_of_frame = 0xAFAF,
.rx_start_of_frame = 0xFAFA,
.tx_urb_cmd_max_len = ES581_4_TX_URB_CMD_MAX_LEN,
.rx_urb_cmd_max_len = ES581_4_RX_URB_CMD_MAX_LEN,
/* Size of internal device TX queue is 330.
*
* However, we witnessed some ES58X_ERR_PROT_CRC errors from
* the device and thus, echo_skb_max was lowered to the
* empirical value of 75 which seems stable and then rounded
* down to become a power of two.
*
* Root cause of those ES58X_ERR_PROT_CRC errors is still
* unclear.
*/
.fifo_mask = 63, /* echo_skb_max = 64 */
.dql_min_limit = CAN_FRAME_LEN_MAX * 50, /* Empirical value. */
.tx_bulk_max = ES581_4_TX_BULK_MAX,
.urb_cmd_header_len = ES581_4_URB_CMD_HEADER_LEN,
.rx_urb_max = ES58X_RX_URBS_MAX,
.tx_urb_max = ES58X_TX_URBS_MAX
};
const struct es58x_operators es581_4_ops = {
.get_msg_len = es581_4_get_msg_len,
.handle_urb_cmd = es581_4_handle_urb_cmd,
.fill_urb_header = es581_4_fill_urb_header,
.tx_can_msg = es581_4_tx_can_msg,
.enable_channel = es581_4_enable_channel,
.disable_channel = es581_4_disable_channel,
.reset_device = es581_4_reset_device,
.get_timestamp = es581_4_get_timestamp
};
@@ -0,0 +1,207 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.
*
* File es581_4.h: Definitions and declarations specific to ETAS
* ES581.4.
*
* Copyright (c) 2019 Robert Bosch Engineering and Business Solutions. All rights reserved.
* Copyright (c) 2020 ETAS K.K.. All rights reserved.
* Copyright (c) 2020, 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
*/
#ifndef __ES581_4_H__
#define __ES581_4_H__
#include <linux/types.h>
#define ES581_4_NUM_CAN_CH 2
#define ES581_4_CHANNEL_IDX_OFFSET 1
#define ES581_4_TX_BULK_MAX 25
#define ES581_4_RX_BULK_MAX 30
#define ES581_4_ECHO_BULK_MAX 30
enum es581_4_cmd_type {
ES581_4_CAN_COMMAND_TYPE = 0x45
};
enum es581_4_cmd_id {
ES581_4_CMD_ID_OPEN_CHANNEL = 0x01,
ES581_4_CMD_ID_CLOSE_CHANNEL = 0x02,
ES581_4_CMD_ID_SET_BITTIMING = 0x03,
ES581_4_CMD_ID_ENABLE_CHANNEL = 0x04,
ES581_4_CMD_ID_TX_MSG = 0x05,
ES581_4_CMD_ID_RX_MSG = 0x06,
ES581_4_CMD_ID_RESET_RX = 0x0A,
ES581_4_CMD_ID_RESET_TX = 0x0B,
ES581_4_CMD_ID_DISABLE_CHANNEL = 0x0C,
ES581_4_CMD_ID_TIMESTAMP = 0x0E,
ES581_4_CMD_ID_RESET_DEVICE = 0x28,
ES581_4_CMD_ID_ECHO = 0x71,
ES581_4_CMD_ID_DEVICE_ERR = 0x72
};
enum es581_4_rx_type {
ES581_4_RX_TYPE_MESSAGE = 1,
ES581_4_RX_TYPE_ERROR = 3,
ES581_4_RX_TYPE_EVENT = 4
};
/**
* struct es581_4_tx_conf_msg - Channel configuration.
* @bitrate: Bitrate.
* @sample_point: Sample point is in percent [0..100].
* @samples_per_bit: type enum es58x_samples_per_bit.
* @bit_time: Number of time quanta in one bit.
* @sjw: Synchronization Jump Width.
* @sync_edge: type enum es58x_sync_edge.
* @physical_layer: type enum es58x_physical_layer.
* @echo_mode: type enum es58x_echo_mode.
* @channel_no: Channel number, starting from 1. Not to be confused
* with channed_idx of the ES58X FD which starts from 0.
*/
struct es581_4_tx_conf_msg {
__le32 bitrate;
__le32 sample_point;
__le32 samples_per_bit;
__le32 bit_time;
__le32 sjw;
__le32 sync_edge;
__le32 physical_layer;
__le32 echo_mode;
u8 channel_no;
} __packed;
struct es581_4_tx_can_msg {
__le32 can_id;
__le32 packet_idx;
__le16 flags;
u8 channel_no;
u8 dlc;
u8 data[CAN_MAX_DLEN];
} __packed;
/* The ES581.4 allows bulk transfer. */
struct es581_4_bulk_tx_can_msg {
u8 num_can_msg;
/* Using type "u8[]" instead of "struct es581_4_tx_can_msg[]"
* for tx_msg_buf because each member has a flexible size.
*/
u8 tx_can_msg_buf[ES581_4_TX_BULK_MAX *
sizeof(struct es581_4_tx_can_msg)];
} __packed;
struct es581_4_echo_msg {
__le64 timestamp;
__le32 packet_idx;
} __packed;
struct es581_4_bulk_echo_msg {
u8 channel_no;
struct es581_4_echo_msg echo_msg[ES581_4_ECHO_BULK_MAX];
} __packed;
/* Normal Rx CAN Message */
struct es581_4_rx_can_msg {
__le64 timestamp;
u8 rx_type; /* type enum es581_4_rx_type */
u8 flags; /* type enum es58x_flag */
u8 channel_no;
u8 dlc;
__le32 can_id;
u8 data[CAN_MAX_DLEN];
} __packed;
struct es581_4_rx_err_msg {
__le64 timestamp;
__le16 rx_type; /* type enum es581_4_rx_type */
__le16 flags; /* type enum es58x_flag */
u8 channel_no;
u8 __padding[2];
u8 dlc;
__le32 tag; /* Related to the CAN filtering. Unused in this module */
__le32 can_id;
__le32 error; /* type enum es58x_error */
__le32 destination; /* Unused in this module */
} __packed;
struct es581_4_rx_event_msg {
__le64 timestamp;
__le16 rx_type; /* type enum es581_4_rx_type */
u8 channel_no;
u8 __padding;
__le32 tag; /* Related to the CAN filtering. Unused in this module */
__le32 event; /* type enum es58x_event */
__le32 destination; /* Unused in this module */
} __packed;
struct es581_4_tx_ack_msg {
__le16 tx_free_entries; /* Number of remaining free entries in the device TX queue */
u8 channel_no;
u8 rx_cmd_ret_u8; /* type enum es58x_cmd_ret_code_u8 */
} __packed;
struct es581_4_rx_cmd_ret {
__le32 rx_cmd_ret_le32;
u8 channel_no;
u8 __padding[3];
} __packed;
/**
* struct es581_4_urb_cmd - Commands received from or sent to the
* ES581.4 device.
* @SOF: Start of Frame.
* @cmd_type: Command Type (type: enum es581_4_cmd_type). The CRC
* calculation starts at this position.
* @cmd_id: Command ID (type: enum es581_4_cmd_id).
* @msg_len: Length of the message, excluding CRC (i.e. length of the
* union).
* @tx_conf_msg: Channel configuration.
* @bulk_tx_can_msg: Tx messages.
* @rx_can_msg: Array of Rx messages.
* @bulk_echo_msg: Tx message being looped back.
* @rx_err_msg: Error message.
* @rx_event_msg: Event message.
* @tx_ack_msg: Tx acknowledgment message.
* @rx_cmd_ret: Command return code.
* @timestamp: Timestamp reply.
* @rx_cmd_ret_u8: Rx 8 bits return code (type: enum
* es58x_cmd_ret_code_u8).
* @raw_msg: Message raw payload.
* @reserved_for_crc16_do_not_use: The structure ends with a
* CRC16. Because the structures in above union are of variable
* lengths, we can not predict the offset of the CRC in
* advance. Use functions es58x_get_crc() and es58x_set_crc() to
* manipulate it.
*/
struct es581_4_urb_cmd {
__le16 SOF;
u8 cmd_type;
u8 cmd_id;
__le16 msg_len;
union {
struct es581_4_tx_conf_msg tx_conf_msg;
struct es581_4_bulk_tx_can_msg bulk_tx_can_msg;
struct es581_4_rx_can_msg rx_can_msg[ES581_4_RX_BULK_MAX];
struct es581_4_bulk_echo_msg bulk_echo_msg;
struct es581_4_rx_err_msg rx_err_msg;
struct es581_4_rx_event_msg rx_event_msg;
struct es581_4_tx_ack_msg tx_ack_msg;
struct es581_4_rx_cmd_ret rx_cmd_ret;
__le64 timestamp;
u8 rx_cmd_ret_u8;
DECLARE_FLEX_ARRAY(u8, raw_msg);
} __packed;
__le16 reserved_for_crc16_do_not_use;
} __packed;
#define ES581_4_URB_CMD_HEADER_LEN (offsetof(struct es581_4_urb_cmd, raw_msg))
#define ES581_4_TX_URB_CMD_MAX_LEN \
ES58X_SIZEOF_URB_CMD(struct es581_4_urb_cmd, bulk_tx_can_msg)
#define ES581_4_RX_URB_CMD_MAX_LEN \
ES58X_SIZEOF_URB_CMD(struct es581_4_urb_cmd, rx_can_msg)
#endif /* __ES581_4_H__ */
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,750 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.
*
* File es58x_core.h: All common definitions and declarations.
*
* Copyright (c) 2019 Robert Bosch Engineering and Business Solutions. All rights reserved.
* Copyright (c) 2020 ETAS K.K.. All rights reserved.
* Copyright (c) 2020-2022 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
*/
#ifndef __ES58X_COMMON_H__
#define __ES58X_COMMON_H__
#include <linux/can.h>
#include <linux/can/dev.h>
#include <linux/netdevice.h>
#include <linux/types.h>
#include <linux/usb.h>
#include <net/devlink.h>
#include "es581_4.h"
#include "es58x_fd.h"
/* Driver constants */
#define ES58X_RX_URBS_MAX 5 /* Empirical value */
#define ES58X_TX_URBS_MAX 6 /* Empirical value */
#define ES58X_MAX(param) \
(ES581_4_##param > ES58X_FD_##param ? \
ES581_4_##param : ES58X_FD_##param)
#define ES58X_TX_BULK_MAX ES58X_MAX(TX_BULK_MAX)
#define ES58X_RX_BULK_MAX ES58X_MAX(RX_BULK_MAX)
#define ES58X_ECHO_BULK_MAX ES58X_MAX(ECHO_BULK_MAX)
#define ES58X_NUM_CAN_CH_MAX ES58X_MAX(NUM_CAN_CH)
/* Use this when channel index is irrelevant (e.g. device
* timestamp).
*/
#define ES58X_CHANNEL_IDX_NA 0xFF
#define ES58X_EMPTY_MSG NULL
/* Threshold on consecutive CAN_STATE_ERROR_PASSIVE. If we receive
* ES58X_CONSECUTIVE_ERR_PASSIVE_MAX times the event
* ES58X_ERR_CRTL_PASSIVE in a row without any successful RX or TX,
* we force the device to switch to CAN_STATE_BUS_OFF state.
*/
#define ES58X_CONSECUTIVE_ERR_PASSIVE_MAX 254
/* A magic number sent by the ES581.4 to inform it is alive. */
#define ES58X_HEARTBEAT 0x11
/**
* enum es58x_driver_info - Quirks of the device.
* @ES58X_DUAL_CHANNEL: Device has two CAN channels. If this flag is
* not set, it is implied that the device has only one CAN
* channel.
* @ES58X_FD_FAMILY: Device is CAN-FD capable. If this flag is not
* set, the device only supports classical CAN.
*/
enum es58x_driver_info {
ES58X_DUAL_CHANNEL = BIT(0),
ES58X_FD_FAMILY = BIT(1)
};
enum es58x_echo {
ES58X_ECHO_OFF = 0,
ES58X_ECHO_ON = 1
};
/**
* enum es58x_physical_layer - Type of the physical layer.
* @ES58X_PHYSICAL_LAYER_HIGH_SPEED: High-speed CAN (c.f. ISO
* 11898-2).
*
* Some products of the ETAS portfolio also support low-speed CAN
* (c.f. ISO 11898-3). However, all the devices in scope of this
* driver do not support the option, thus, the enum has only one
* member.
*/
enum es58x_physical_layer {
ES58X_PHYSICAL_LAYER_HIGH_SPEED = 1
};
enum es58x_samples_per_bit {
ES58X_SAMPLES_PER_BIT_ONE = 1,
ES58X_SAMPLES_PER_BIT_THREE = 2
};
/**
* enum es58x_sync_edge - Synchronization method.
* @ES58X_SYNC_EDGE_SINGLE: ISO CAN specification defines the use of a
* single edge synchronization. The synchronization should be
* done on recessive to dominant level change.
*
* For information, ES582.1 and ES584.1 also support a double
* synchronization, requiring both recessive to dominant then dominant
* to recessive level change. However, this is not supported in
* SocketCAN framework, thus, the enum has only one member.
*/
enum es58x_sync_edge {
ES58X_SYNC_EDGE_SINGLE = 1
};
/**
* enum es58x_flag - CAN flags for RX/TX messages.
* @ES58X_FLAG_EFF: Extended Frame Format (EFF).
* @ES58X_FLAG_RTR: Remote Transmission Request (RTR).
* @ES58X_FLAG_FD_BRS: Bit rate switch (BRS): second bitrate for
* payload data.
* @ES58X_FLAG_FD_ESI: Error State Indicator (ESI): tell if the
* transmitting node is in error passive mode.
* @ES58X_FLAG_FD_DATA: CAN FD frame.
*/
enum es58x_flag {
ES58X_FLAG_EFF = BIT(0),
ES58X_FLAG_RTR = BIT(1),
ES58X_FLAG_FD_BRS = BIT(3),
ES58X_FLAG_FD_ESI = BIT(5),
ES58X_FLAG_FD_DATA = BIT(6)
};
/**
* enum es58x_err - CAN error detection.
* @ES58X_ERR_OK: No errors.
* @ES58X_ERR_PROT_STUFF: Bit stuffing error: more than 5 consecutive
* equal bits.
* @ES58X_ERR_PROT_FORM: Frame format error.
* @ES58X_ERR_ACK: Received no ACK on transmission.
* @ES58X_ERR_PROT_BIT: Single bit error.
* @ES58X_ERR_PROT_CRC: Incorrect 15, 17 or 21 bits CRC.
* @ES58X_ERR_PROT_BIT1: Unable to send recessive bit: tried to send
* recessive bit 1 but monitored dominant bit 0.
* @ES58X_ERR_PROT_BIT0: Unable to send dominant bit: tried to send
* dominant bit 0 but monitored recessive bit 1.
* @ES58X_ERR_PROT_OVERLOAD: Bus overload.
* @ES58X_ERR_PROT_UNSPEC: Unspecified.
*
* Please refer to ISO 11898-1:2015, section 10.11 "Error detection"
* and section 10.13 "Overload signaling" for additional details.
*/
enum es58x_err {
ES58X_ERR_OK = 0,
ES58X_ERR_PROT_STUFF = BIT(0),
ES58X_ERR_PROT_FORM = BIT(1),
ES58X_ERR_ACK = BIT(2),
ES58X_ERR_PROT_BIT = BIT(3),
ES58X_ERR_PROT_CRC = BIT(4),
ES58X_ERR_PROT_BIT1 = BIT(5),
ES58X_ERR_PROT_BIT0 = BIT(6),
ES58X_ERR_PROT_OVERLOAD = BIT(7),
ES58X_ERR_PROT_UNSPEC = BIT(31)
};
/**
* enum es58x_event - CAN error codes returned by the device.
* @ES58X_EVENT_OK: No errors.
* @ES58X_EVENT_CRTL_ACTIVE: Active state: both TR and RX error count
* is less than 128.
* @ES58X_EVENT_CRTL_PASSIVE: Passive state: either TX or RX error
* count is greater than 127.
* @ES58X_EVENT_CRTL_WARNING: Warning state: either TX or RX error
* count is greater than 96.
* @ES58X_EVENT_BUSOFF: Bus off.
* @ES58X_EVENT_SINGLE_WIRE: Lost connection on either CAN high or CAN
* low.
*
* Please refer to ISO 11898-1:2015, section 12.1.4 "Rules of fault
* confinement" for additional details.
*/
enum es58x_event {
ES58X_EVENT_OK = 0,
ES58X_EVENT_CRTL_ACTIVE = BIT(0),
ES58X_EVENT_CRTL_PASSIVE = BIT(1),
ES58X_EVENT_CRTL_WARNING = BIT(2),
ES58X_EVENT_BUSOFF = BIT(3),
ES58X_EVENT_SINGLE_WIRE = BIT(4)
};
/* enum es58x_ret_u8 - Device return error codes, 8 bit format.
*
* Specific to ES581.4.
*/
enum es58x_ret_u8 {
ES58X_RET_U8_OK = 0x00,
ES58X_RET_U8_ERR_UNSPECIFIED_FAILURE = 0x80,
ES58X_RET_U8_ERR_NO_MEM = 0x81,
ES58X_RET_U8_ERR_BAD_CRC = 0x99
};
/* enum es58x_ret_u32 - Device return error codes, 32 bit format.
*/
enum es58x_ret_u32 {
ES58X_RET_U32_OK = 0x00000000UL,
ES58X_RET_U32_ERR_UNSPECIFIED_FAILURE = 0x80000000UL,
ES58X_RET_U32_ERR_NO_MEM = 0x80004001UL,
ES58X_RET_U32_WARN_PARAM_ADJUSTED = 0x40004000UL,
ES58X_RET_U32_WARN_TX_MAYBE_REORDER = 0x40004001UL,
ES58X_RET_U32_ERR_TIMEDOUT = 0x80000008UL,
ES58X_RET_U32_ERR_FIFO_FULL = 0x80003002UL,
ES58X_RET_U32_ERR_BAD_CONFIG = 0x80004000UL,
ES58X_RET_U32_ERR_NO_RESOURCE = 0x80004002UL
};
/* enum es58x_ret_type - Type of the command returned by the ES58X
* device.
*/
enum es58x_ret_type {
ES58X_RET_TYPE_SET_BITTIMING,
ES58X_RET_TYPE_ENABLE_CHANNEL,
ES58X_RET_TYPE_DISABLE_CHANNEL,
ES58X_RET_TYPE_TX_MSG,
ES58X_RET_TYPE_RESET_RX,
ES58X_RET_TYPE_RESET_TX,
ES58X_RET_TYPE_DEVICE_ERR
};
union es58x_urb_cmd {
struct es581_4_urb_cmd es581_4_urb_cmd;
struct es58x_fd_urb_cmd es58x_fd_urb_cmd;
struct { /* Common header parts of all variants */
__le16 sof;
u8 cmd_type;
u8 cmd_id;
} __packed;
DECLARE_FLEX_ARRAY(u8, raw_cmd);
};
/**
* struct es58x_priv - All information specific to a CAN channel.
* @can: struct can_priv must be the first member (Socket CAN relies
* on the fact that function netdev_priv() returns a pointer to
* a struct can_priv).
* @devlink_port: devlink instance for the network interface.
* @es58x_dev: pointer to the corresponding ES58X device.
* @tx_urb: Used as a buffer to concatenate the TX messages and to do
* a bulk send. Please refer to es58x_start_xmit() for more
* details.
* @tx_tail: Index of the oldest packet still pending for
* completion. @tx_tail & echo_skb_mask represents the beginning
* of the echo skb FIFO, i.e. index of the first element.
* @tx_head: Index of the next packet to be sent to the
* device. @tx_head & echo_skb_mask represents the end of the
* echo skb FIFO plus one, i.e. the first free index.
* @tx_can_msg_cnt: Number of messages in @tx_urb.
* @tx_can_msg_is_fd: false: all messages in @tx_urb are Classical
* CAN, true: all messages in @tx_urb are CAN FD. Rationale:
* ES58X FD devices do not allow to mix Classical CAN and FD CAN
* frames in one single bulk transmission.
* @err_passive_before_rtx_success: The ES58X device might enter in a
* state in which it keeps alternating between error passive
* and active states. This counter keeps track of the number of
* error passive and if it gets bigger than
* ES58X_CONSECUTIVE_ERR_PASSIVE_MAX, es58x_rx_err_msg() will
* force the status to bus-off.
* @channel_idx: Channel index, starts at zero.
*/
struct es58x_priv {
struct can_priv can;
struct devlink_port devlink_port;
struct es58x_device *es58x_dev;
struct urb *tx_urb;
u32 tx_tail;
u32 tx_head;
u8 tx_can_msg_cnt;
bool tx_can_msg_is_fd;
u8 err_passive_before_rtx_success;
u8 channel_idx;
};
/**
* struct es58x_parameters - Constant parameters of a given hardware
* variant.
* @bittiming_const: Nominal bittimming constant parameters.
* @data_bittiming_const: Data bittiming constant parameters.
* @tdc_const: Transmission Delay Compensation constant parameters.
* @bitrate_max: Maximum bitrate supported by the device.
* @clock: CAN clock parameters.
* @ctrlmode_supported: List of supported modes. Please refer to
* can/netlink.h file for additional details.
* @tx_start_of_frame: Magic number at the beginning of each TX URB
* command.
* @rx_start_of_frame: Magic number at the beginning of each RX URB
* command.
* @tx_urb_cmd_max_len: Maximum length of a TX URB command.
* @rx_urb_cmd_max_len: Maximum length of a RX URB command.
* @fifo_mask: Bit mask to quickly convert the tx_tail and tx_head
* field of the struct es58x_priv into echo_skb
* indexes. Properties: @fifo_mask = echo_skb_max - 1 where
* echo_skb_max must be a power of two. Also, echo_skb_max must
* not exceed the maximum size of the device internal TX FIFO
* length. This parameter is used to control the network queue
* wake/stop logic.
* @dql_min_limit: Dynamic Queue Limits (DQL) absolute minimum limit
* of bytes allowed to be queued on this network device transmit
* queue. Used by the Byte Queue Limits (BQL) to determine how
* frequently the xmit_more flag will be set to true in
* es58x_start_xmit(). Set this value higher to optimize for
* throughput but be aware that it might have a negative impact
* on the latency! This value can also be set dynamically. Please
* refer to Documentation/ABI/testing/sysfs-class-net-queues for
* more details.
* @tx_bulk_max: Maximum number of TX messages that can be sent in one
* single URB packet.
* @urb_cmd_header_len: Length of the URB command header.
* @rx_urb_max: Number of RX URB to be allocated during device probe.
* @tx_urb_max: Number of TX URB to be allocated during device probe.
*/
struct es58x_parameters {
const struct can_bittiming_const *bittiming_const;
const struct can_bittiming_const *data_bittiming_const;
const struct can_tdc_const *tdc_const;
u32 bitrate_max;
struct can_clock clock;
u32 ctrlmode_supported;
u16 tx_start_of_frame;
u16 rx_start_of_frame;
u16 tx_urb_cmd_max_len;
u16 rx_urb_cmd_max_len;
u16 fifo_mask;
u16 dql_min_limit;
u8 tx_bulk_max;
u8 urb_cmd_header_len;
u8 rx_urb_max;
u8 tx_urb_max;
};
/**
* struct es58x_operators - Function pointers used to encode/decode
* the TX/RX messages.
* @get_msg_len: Get field msg_len of the urb_cmd. The offset of
* msg_len inside urb_cmd depends of the device model.
* @handle_urb_cmd: Decode the URB command received from the device
* and dispatch it to the relevant sub function.
* @fill_urb_header: Fill the header of urb_cmd.
* @tx_can_msg: Encode a TX CAN message and add it to the bulk buffer
* cmd_buf of es58x_dev.
* @enable_channel: Start the CAN channel.
* @disable_channel: Stop the CAN channel.
* @reset_device: Full reset of the device. N.B: this feature is only
* present on the ES581.4. For ES58X FD devices, this field is
* set to NULL.
* @get_timestamp: Request a timestamp from the ES58X device.
*/
struct es58x_operators {
u16 (*get_msg_len)(const union es58x_urb_cmd *urb_cmd);
int (*handle_urb_cmd)(struct es58x_device *es58x_dev,
const union es58x_urb_cmd *urb_cmd);
void (*fill_urb_header)(union es58x_urb_cmd *urb_cmd, u8 cmd_type,
u8 cmd_id, u8 channel_idx, u16 cmd_len);
int (*tx_can_msg)(struct es58x_priv *priv, const struct sk_buff *skb);
int (*enable_channel)(struct es58x_priv *priv);
int (*disable_channel)(struct es58x_priv *priv);
int (*reset_device)(struct es58x_device *es58x_dev);
int (*get_timestamp)(struct es58x_device *es58x_dev);
};
/**
* struct es58x_sw_version - Version number of the firmware or the
* bootloader.
* @major: Version major number, represented on two digits.
* @minor: Version minor number, represented on two digits.
* @revision: Version revision number, represented on two digits.
*
* The firmware and the bootloader share the same format: "xx.xx.xx"
* where 'x' is a digit. Both can be retrieved from the product
* information string.
*/
struct es58x_sw_version {
u8 major;
u8 minor;
u8 revision;
};
/**
* struct es58x_hw_revision - Hardware revision number.
* @letter: Revision letter, an alphanumeric character.
* @major: Version major number, represented on three digits.
* @minor: Version minor number, represented on three digits.
*
* The hardware revision uses its own format: "axxx/xxx" where 'a' is
* an alphanumeric character and 'x' a digit. It can be retrieved from
* the product information string.
*/
struct es58x_hw_revision {
char letter;
u16 major;
u16 minor;
};
/**
* struct es58x_device - All information specific to an ES58X device.
* @dev: Device information.
* @udev: USB device information.
* @netdev: Array of our CAN channels.
* @param: The constant parameters.
* @ops: Operators.
* @rx_pipe: USB reception pipe.
* @tx_pipe: USB transmission pipe.
* @rx_urbs: Anchor for received URBs.
* @tx_urbs_busy: Anchor for TX URBs which were send to the device.
* @tx_urbs_idle: Anchor for TX USB which are idle. This driver
* allocates the memory for the URBs during the probe. When a TX
* URB is needed, it can be taken from this anchor. The network
* queue wake/stop logic should prevent this URB from getting
* empty. Please refer to es58x_get_tx_urb() for more details.
* @tx_urbs_idle_cnt: number of urbs in @tx_urbs_idle.
* @firmware_version: The firmware version number.
* @bootloader_version: The bootloader version number.
* @hardware_revision: The hardware revision number.
* @ktime_req_ns: kernel timestamp when es58x_set_realtime_diff_ns()
* was called.
* @realtime_diff_ns: difference in nanoseconds between the clocks of
* the ES58X device and the kernel.
* @timestamps: a temporary buffer to store the time stamps before
* feeding them to es58x_can_get_echo_skb(). Can only be used
* in RX branches.
* @num_can_ch: Number of CAN channel (i.e. number of elements of @netdev).
* @opened_channel_cnt: number of channels opened. Free of race
* conditions because its two users (net_device_ops:ndo_open()
* and net_device_ops:ndo_close()) guarantee that the network
* stack big kernel lock (a.k.a. rtnl_mutex) is being hold.
* @rx_cmd_buf_len: Length of @rx_cmd_buf.
* @rx_cmd_buf: The device might split the URB commands in an
* arbitrary amount of pieces. This buffer is used to concatenate
* all those pieces. Can only be used in RX branches. This field
* has to be the last one of the structure because it is has a
* flexible size (c.f. es58x_sizeof_es58x_device() function).
*/
struct es58x_device {
struct device *dev;
struct usb_device *udev;
struct net_device *netdev[ES58X_NUM_CAN_CH_MAX];
const struct es58x_parameters *param;
const struct es58x_operators *ops;
unsigned int rx_pipe;
unsigned int tx_pipe;
struct usb_anchor rx_urbs;
struct usb_anchor tx_urbs_busy;
struct usb_anchor tx_urbs_idle;
atomic_t tx_urbs_idle_cnt;
struct es58x_sw_version firmware_version;
struct es58x_sw_version bootloader_version;
struct es58x_hw_revision hardware_revision;
u64 ktime_req_ns;
s64 realtime_diff_ns;
u64 timestamps[ES58X_ECHO_BULK_MAX];
u8 num_can_ch;
u8 opened_channel_cnt;
u16 rx_cmd_buf_len;
union es58x_urb_cmd rx_cmd_buf;
};
/**
* es58x_sizeof_es58x_device() - Calculate the maximum length of
* struct es58x_device.
* @es58x_dev_param: The constant parameters of the device.
*
* The length of struct es58x_device depends on the length of its last
* field: rx_cmd_buf. This macro allows to optimize the memory
* allocation.
*
* Return: length of struct es58x_device.
*/
static inline size_t es58x_sizeof_es58x_device(const struct es58x_parameters
*es58x_dev_param)
{
return offsetof(struct es58x_device, rx_cmd_buf) +
es58x_dev_param->rx_urb_cmd_max_len;
}
static inline int __es58x_check_msg_len(const struct device *dev,
const char *stringified_msg,
size_t actual_len, size_t expected_len)
{
if (expected_len != actual_len) {
dev_err(dev,
"Length of %s is %zu but received command is %zu.\n",
stringified_msg, expected_len, actual_len);
return -EMSGSIZE;
}
return 0;
}
/**
* es58x_check_msg_len() - Check the size of a received message.
* @dev: Device, used to print error messages.
* @msg: Received message, must not be a pointer.
* @actual_len: Length of the message as advertised in the command header.
*
* Must be a macro in order to accept the different types of messages
* as an input. Can be use with any of the messages which have a fixed
* length. Check for an exact match of the size.
*
* Return: zero on success, -EMSGSIZE if @actual_len differs from the
* expected length.
*/
#define es58x_check_msg_len(dev, msg, actual_len) \
__es58x_check_msg_len(dev, __stringify(msg), \
actual_len, sizeof(msg))
static inline int __es58x_check_msg_max_len(const struct device *dev,
const char *stringified_msg,
size_t actual_len,
size_t expected_len)
{
if (actual_len > expected_len) {
dev_err(dev,
"Maximum length for %s is %zu but received command is %zu.\n",
stringified_msg, expected_len, actual_len);
return -EOVERFLOW;
}
return 0;
}
/**
* es58x_check_msg_max_len() - Check the maximum size of a received message.
* @dev: Device, used to print error messages.
* @msg: Received message, must not be a pointer.
* @actual_len: Length of the message as advertised in the command header.
*
* Must be a macro in order to accept the different types of messages
* as an input. To be used with the messages of variable sizes. Only
* check that the message is not bigger than the maximum expected
* size.
*
* Return: zero on success, -EOVERFLOW if @actual_len is greater than
* the expected length.
*/
#define es58x_check_msg_max_len(dev, msg, actual_len) \
__es58x_check_msg_max_len(dev, __stringify(msg), \
actual_len, sizeof(msg))
static inline int __es58x_msg_num_element(const struct device *dev,
const char *stringified_msg,
size_t actual_len, size_t msg_len,
size_t elem_len)
{
size_t actual_num_elem = actual_len / elem_len;
size_t expected_num_elem = msg_len / elem_len;
if (actual_num_elem == 0) {
dev_err(dev,
"Minimum length for %s is %zu but received command is %zu.\n",
stringified_msg, elem_len, actual_len);
return -EMSGSIZE;
} else if ((actual_len % elem_len) != 0) {
dev_err(dev,
"Received command length: %zu is not a multiple of %s[0]: %zu\n",
actual_len, stringified_msg, elem_len);
return -EMSGSIZE;
} else if (actual_num_elem > expected_num_elem) {
dev_err(dev,
"Array %s is supposed to have %zu elements each of size %zu...\n",
stringified_msg, expected_num_elem, elem_len);
dev_err(dev,
"... But received command has %zu elements (total length %zu).\n",
actual_num_elem, actual_len);
return -EOVERFLOW;
}
return actual_num_elem;
}
/**
* es58x_msg_num_element() - Check size and give the number of
* elements in a message of array type.
* @dev: Device, used to print error messages.
* @msg: Received message, must be an array.
* @actual_len: Length of the message as advertised in the command
* header.
*
* Must be a macro in order to accept the different types of messages
* as an input. To be used on message of array type. Array's element
* has to be of fixed size (else use es58x_check_msg_max_len()). Check
* that the total length is an exact multiple of the length of a
* single element.
*
* Return: number of elements in the array on success, -EOVERFLOW if
* @actual_len is greater than the expected length, -EMSGSIZE if
* @actual_len is not a multiple of a single element.
*/
#define es58x_msg_num_element(dev, msg, actual_len) \
({ \
size_t __elem_len = sizeof((msg)[0]) + __must_be_array(msg); \
__es58x_msg_num_element(dev, __stringify(msg), actual_len, \
sizeof(msg), __elem_len); \
})
/**
* es58x_priv() - Get the priv member and cast it to struct es58x_priv.
* @netdev: CAN network device.
*
* Return: ES58X device.
*/
static inline struct es58x_priv *es58x_priv(struct net_device *netdev)
{
return (struct es58x_priv *)netdev_priv(netdev);
}
/**
* ES58X_SIZEOF_URB_CMD() - Calculate the maximum length of an urb
* command for a given message field name.
* @es58x_urb_cmd_type: type (either "struct es581_4_urb_cmd" or
* "struct es58x_fd_urb_cmd").
* @msg_field: name of the message field.
*
* Must be a macro in order to accept the different command types as
* an input.
*
* Return: length of the urb command.
*/
#define ES58X_SIZEOF_URB_CMD(es58x_urb_cmd_type, msg_field) \
(offsetof(es58x_urb_cmd_type, raw_msg) \
+ sizeof_field(es58x_urb_cmd_type, msg_field) \
+ sizeof_field(es58x_urb_cmd_type, \
reserved_for_crc16_do_not_use))
/**
* es58x_get_urb_cmd_len() - Calculate the actual length of an urb
* command for a given message length.
* @es58x_dev: ES58X device.
* @msg_len: Length of the message.
*
* Add the header and CRC lengths to the message length.
*
* Return: length of the urb command.
*/
static inline size_t es58x_get_urb_cmd_len(struct es58x_device *es58x_dev,
u16 msg_len)
{
return es58x_dev->param->urb_cmd_header_len + msg_len + sizeof(u16);
}
/**
* es58x_get_netdev() - Get the network device.
* @es58x_dev: ES58X device.
* @channel_no: The channel number as advertised in the urb command.
* @channel_idx_offset: Some of the ES58x starts channel numbering
* from 0 (ES58X FD), others from 1 (ES581.4).
* @netdev: CAN network device.
*
* Do a sanity check on the index provided by the device.
*
* Return: zero on success, -ECHRNG if the received channel number is
* out of range and -ENODEV if the network device is not yet
* configured.
*/
static inline int es58x_get_netdev(struct es58x_device *es58x_dev,
int channel_no, int channel_idx_offset,
struct net_device **netdev)
{
int channel_idx = channel_no - channel_idx_offset;
*netdev = NULL;
if (channel_idx < 0 || channel_idx >= es58x_dev->num_can_ch)
return -ECHRNG;
*netdev = es58x_dev->netdev[channel_idx];
if (!*netdev || !netif_device_present(*netdev))
return -ENODEV;
return 0;
}
/**
* es58x_get_raw_can_id() - Get the CAN ID.
* @cf: CAN frame.
*
* Mask the CAN ID in order to only keep the significant bits.
*
* Return: the raw value of the CAN ID.
*/
static inline int es58x_get_raw_can_id(const struct can_frame *cf)
{
if (cf->can_id & CAN_EFF_FLAG)
return cf->can_id & CAN_EFF_MASK;
else
return cf->can_id & CAN_SFF_MASK;
}
/**
* es58x_get_flags() - Get the CAN flags.
* @skb: socket buffer of a CAN message.
*
* Return: the CAN flag as an enum es58x_flag.
*/
static inline enum es58x_flag es58x_get_flags(const struct sk_buff *skb)
{
struct canfd_frame *cf = (struct canfd_frame *)skb->data;
enum es58x_flag es58x_flags = 0;
if (cf->can_id & CAN_EFF_FLAG)
es58x_flags |= ES58X_FLAG_EFF;
if (can_is_canfd_skb(skb)) {
es58x_flags |= ES58X_FLAG_FD_DATA;
if (cf->flags & CANFD_BRS)
es58x_flags |= ES58X_FLAG_FD_BRS;
if (cf->flags & CANFD_ESI)
es58x_flags |= ES58X_FLAG_FD_ESI;
} else if (cf->can_id & CAN_RTR_FLAG)
/* Remote frames are only defined in Classical CAN frames */
es58x_flags |= ES58X_FLAG_RTR;
return es58x_flags;
}
/* es58x_core.c. */
int es58x_can_get_echo_skb(struct net_device *netdev, u32 packet_idx,
u64 *tstamps, unsigned int pkts);
int es58x_tx_ack_msg(struct net_device *netdev, u16 tx_free_entries,
enum es58x_ret_u32 rx_cmd_ret_u32);
int es58x_rx_can_msg(struct net_device *netdev, u64 timestamp, const u8 *data,
canid_t can_id, enum es58x_flag es58x_flags, u8 dlc);
int es58x_rx_err_msg(struct net_device *netdev, enum es58x_err error,
enum es58x_event event, u64 timestamp);
void es58x_rx_timestamp(struct es58x_device *es58x_dev, u64 timestamp);
int es58x_rx_cmd_ret_u8(struct device *dev, enum es58x_ret_type cmd_ret_type,
enum es58x_ret_u8 rx_cmd_ret_u8);
int es58x_rx_cmd_ret_u32(struct net_device *netdev,
enum es58x_ret_type cmd_ret_type,
enum es58x_ret_u32 rx_cmd_ret_u32);
int es58x_send_msg(struct es58x_device *es58x_dev, u8 cmd_type, u8 cmd_id,
const void *msg, u16 cmd_len, int channel_idx);
/* es58x_devlink.c. */
void es58x_parse_product_info(struct es58x_device *es58x_dev);
extern const struct devlink_ops es58x_dl_ops;
/* es581_4.c. */
extern const struct es58x_parameters es581_4_param;
extern const struct es58x_operators es581_4_ops;
/* es58x_fd.c. */
extern const struct es58x_parameters es58x_fd_param;
extern const struct es58x_operators es58x_fd_ops;
#endif /* __ES58X_COMMON_H__ */
@@ -0,0 +1,260 @@
// SPDX-License-Identifier: GPL-2.0
/* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.
*
* File es58x_devlink.c: report the product information using devlink.
*
* Copyright (c) 2022 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
*/
#include <linux/ctype.h>
#include <linux/device.h>
#include <linux/usb.h>
#include <net/devlink.h>
#include "es58x_core.h"
/* USB descriptor index containing the product information string. */
#define ES58X_PROD_INFO_IDX 6
/**
* es58x_parse_sw_version() - Extract boot loader or firmware version.
* @es58x_dev: ES58X device.
* @prod_info: USB custom string returned by the device.
* @prefix: Select which information should be parsed. Set it to "FW"
* to parse the firmware version or to "BL" to parse the
* bootloader version.
*
* The @prod_info string contains the firmware and the bootloader
* version number all prefixed by a magic string and concatenated with
* other numbers. Depending on the device, the firmware (bootloader)
* format is either "FW_Vxx.xx.xx" ("BL_Vxx.xx.xx") or "FW:xx.xx.xx"
* ("BL:xx.xx.xx") where 'x' represents a digit. @prod_info must
* contains the common part of those prefixes: "FW" or "BL".
*
* Parse @prod_info and store the version number in
* &es58x_dev.firmware_version or &es58x_dev.bootloader_version
* according to @prefix value.
*
* Return: zero on success, -EINVAL if @prefix contains an invalid
* value and -EBADMSG if @prod_info could not be parsed.
*/
static int es58x_parse_sw_version(struct es58x_device *es58x_dev,
const char *prod_info, const char *prefix)
{
struct es58x_sw_version *version;
int major, minor, revision;
if (!strcmp(prefix, "FW"))
version = &es58x_dev->firmware_version;
else if (!strcmp(prefix, "BL"))
version = &es58x_dev->bootloader_version;
else
return -EINVAL;
/* Go to prefix */
prod_info = strstr(prod_info, prefix);
if (!prod_info)
return -EBADMSG;
/* Go to beginning of the version number */
while (!isdigit(*prod_info)) {
prod_info++;
if (!*prod_info)
return -EBADMSG;
}
if (sscanf(prod_info, "%2u.%2u.%2u", &major, &minor, &revision) != 3)
return -EBADMSG;
version->major = major;
version->minor = minor;
version->revision = revision;
return 0;
}
/**
* es58x_parse_hw_rev() - Extract hardware revision number.
* @es58x_dev: ES58X device.
* @prod_info: USB custom string returned by the device.
*
* @prod_info contains the hardware revision prefixed by a magic
* string and conquenated together with other numbers. Depending on
* the device, the hardware revision format is either
* "HW_VER:axxx/xxx" or "HR:axxx/xxx" where 'a' represents a letter
* and 'x' a digit.
*
* Parse @prod_info and store the hardware revision number in
* &es58x_dev.hardware_revision.
*
* Return: zero on success, -EBADMSG if @prod_info could not be
* parsed.
*/
static int es58x_parse_hw_rev(struct es58x_device *es58x_dev,
const char *prod_info)
{
char letter;
int major, minor;
/* The only occurrence of 'H' is in the hardware revision prefix. */
prod_info = strchr(prod_info, 'H');
if (!prod_info)
return -EBADMSG;
/* Go to beginning of the hardware revision */
prod_info = strchr(prod_info, ':');
if (!prod_info)
return -EBADMSG;
prod_info++;
if (sscanf(prod_info, "%c%3u/%3u", &letter, &major, &minor) != 3)
return -EBADMSG;
es58x_dev->hardware_revision.letter = letter;
es58x_dev->hardware_revision.major = major;
es58x_dev->hardware_revision.minor = minor;
return 0;
}
/**
* es58x_parse_product_info() - Parse the ES58x product information
* string.
* @es58x_dev: ES58X device.
*
* Retrieve the product information string and parse it to extract the
* firmware version, the bootloader version and the hardware
* revision.
*
* If the function fails, set the version or revision to an invalid
* value and emit an informal message. Continue probing because the
* product information is not critical for the driver to operate.
*/
void es58x_parse_product_info(struct es58x_device *es58x_dev)
{
static const struct es58x_sw_version sw_version_not_set = {
.major = -1,
.minor = -1,
.revision = -1,
};
static const struct es58x_hw_revision hw_revision_not_set = {
.letter = '\0',
.major = -1,
.minor = -1,
};
char *prod_info;
es58x_dev->firmware_version = sw_version_not_set;
es58x_dev->bootloader_version = sw_version_not_set;
es58x_dev->hardware_revision = hw_revision_not_set;
prod_info = usb_cache_string(es58x_dev->udev, ES58X_PROD_INFO_IDX);
if (!prod_info) {
dev_warn(es58x_dev->dev,
"could not retrieve the product info string\n");
return;
}
if (es58x_parse_sw_version(es58x_dev, prod_info, "FW") ||
es58x_parse_sw_version(es58x_dev, prod_info, "BL") ||
es58x_parse_hw_rev(es58x_dev, prod_info))
dev_info(es58x_dev->dev,
"could not parse product info: '%s'\n", prod_info);
kfree(prod_info);
}
/**
* es58x_sw_version_is_valid() - Check if the version is a valid number.
* @sw_ver: Version number of either the firmware or the bootloader.
*
* If any of the software version sub-numbers do not fit on two
* digits, the version is invalid, most probably because the product
* string could not be parsed.
*
* Return: @true if the software version is valid, @false otherwise.
*/
static inline bool es58x_sw_version_is_valid(struct es58x_sw_version *sw_ver)
{
return sw_ver->major < 100 && sw_ver->minor < 100 &&
sw_ver->revision < 100;
}
/**
* es58x_hw_revision_is_valid() - Check if the revision is a valid number.
* @hw_rev: Revision number of the hardware.
*
* If &es58x_hw_revision.letter is not a alphanumeric character or if
* any of the hardware revision sub-numbers do not fit on three
* digits, the revision is invalid, most probably because the product
* string could not be parsed.
*
* Return: @true if the hardware revision is valid, @false otherwise.
*/
static inline bool es58x_hw_revision_is_valid(struct es58x_hw_revision *hw_rev)
{
return isalnum(hw_rev->letter) && hw_rev->major < 1000 &&
hw_rev->minor < 1000;
}
/**
* es58x_devlink_info_get() - Report the product information.
* @devlink: Devlink.
* @req: skb wrapper where to put requested information.
* @extack: Unused.
*
* Report the firmware version, the bootloader version, the hardware
* revision and the serial number through netlink.
*
* Return: zero on success, errno when any error occurs.
*/
static int es58x_devlink_info_get(struct devlink *devlink,
struct devlink_info_req *req,
struct netlink_ext_ack *extack)
{
struct es58x_device *es58x_dev = devlink_priv(devlink);
struct es58x_sw_version *fw_ver = &es58x_dev->firmware_version;
struct es58x_sw_version *bl_ver = &es58x_dev->bootloader_version;
struct es58x_hw_revision *hw_rev = &es58x_dev->hardware_revision;
char buf[MAX(sizeof("xx.xx.xx"), sizeof("axxx/xxx"))];
int ret = 0;
if (es58x_sw_version_is_valid(fw_ver)) {
snprintf(buf, sizeof(buf), "%02u.%02u.%02u",
fw_ver->major, fw_ver->minor, fw_ver->revision);
ret = devlink_info_version_running_put(req,
DEVLINK_INFO_VERSION_GENERIC_FW,
buf);
if (ret)
return ret;
}
if (es58x_sw_version_is_valid(bl_ver)) {
snprintf(buf, sizeof(buf), "%02u.%02u.%02u",
bl_ver->major, bl_ver->minor, bl_ver->revision);
ret = devlink_info_version_running_put(req,
DEVLINK_INFO_VERSION_GENERIC_FW_BOOTLOADER,
buf);
if (ret)
return ret;
}
if (es58x_hw_revision_is_valid(hw_rev)) {
snprintf(buf, sizeof(buf), "%c%03u/%03u",
hw_rev->letter, hw_rev->major, hw_rev->minor);
ret = devlink_info_version_fixed_put(req,
DEVLINK_INFO_VERSION_GENERIC_BOARD_REV,
buf);
if (ret)
return ret;
}
if (es58x_dev->udev->serial)
ret = devlink_info_serial_number_put(req,
es58x_dev->udev->serial);
return ret;
}
const struct devlink_ops es58x_dl_ops = {
.info_get = es58x_devlink_info_get,
};
@@ -0,0 +1,565 @@
// SPDX-License-Identifier: GPL-2.0
/* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.
*
* File es58x_fd.c: Adds support to ETAS ES582.1 and ES584.1 (naming
* convention: we use the term "ES58X FD" when referring to those two
* variants together).
*
* Copyright (c) 2019 Robert Bosch Engineering and Business Solutions. All rights reserved.
* Copyright (c) 2020 ETAS K.K.. All rights reserved.
* Copyright (c) 2020-2022 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
*/
#include <linux/unaligned.h>
#include <linux/kernel.h>
#include <linux/units.h>
#include "es58x_core.h"
#include "es58x_fd.h"
/**
* es58x_fd_sizeof_rx_tx_msg() - Calculate the actual length of the
* structure of a rx or tx message.
* @msg: message of variable length, must have a dlc and a len fields.
*
* Even if RTR frames have actually no payload, the ES58X devices
* still expect it. Must be a macro in order to accept several types
* (struct es58x_fd_tx_can_msg and struct es58x_fd_rx_can_msg) as an
* input.
*
* Return: length of the message.
*/
#define es58x_fd_sizeof_rx_tx_msg(msg) \
({ \
typeof(msg) __msg = (msg); \
size_t __msg_len; \
\
if (__msg.flags & ES58X_FLAG_FD_DATA) \
__msg_len = canfd_sanitize_len(__msg.len); \
else \
__msg_len = can_cc_dlc2len(__msg.dlc); \
\
offsetof(typeof(__msg), data[__msg_len]); \
})
static enum es58x_fd_cmd_type es58x_fd_cmd_type(struct net_device *netdev)
{
u32 ctrlmode = es58x_priv(netdev)->can.ctrlmode;
if (ctrlmode & (CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO))
return ES58X_FD_CMD_TYPE_CANFD;
else
return ES58X_FD_CMD_TYPE_CAN;
}
static u16 es58x_fd_get_msg_len(const union es58x_urb_cmd *urb_cmd)
{
return get_unaligned_le16(&urb_cmd->es58x_fd_urb_cmd.msg_len);
}
static int es58x_fd_echo_msg(struct net_device *netdev,
const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd)
{
struct es58x_priv *priv = es58x_priv(netdev);
const struct es58x_fd_echo_msg *echo_msg;
struct es58x_device *es58x_dev = priv->es58x_dev;
u64 *tstamps = es58x_dev->timestamps;
u16 msg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);
int i, num_element;
u32 rcv_packet_idx;
const u32 mask = GENMASK(BITS_PER_TYPE(mask) - 1,
BITS_PER_TYPE(echo_msg->packet_idx));
num_element = es58x_msg_num_element(es58x_dev->dev,
es58x_fd_urb_cmd->echo_msg,
msg_len);
if (num_element < 0)
return num_element;
echo_msg = es58x_fd_urb_cmd->echo_msg;
rcv_packet_idx = (priv->tx_tail & mask) | echo_msg[0].packet_idx;
for (i = 0; i < num_element; i++) {
if ((u8)rcv_packet_idx != echo_msg[i].packet_idx) {
netdev_err(netdev, "Packet idx jumped from %u to %u\n",
(u8)rcv_packet_idx - 1,
echo_msg[i].packet_idx);
return -EBADMSG;
}
tstamps[i] = get_unaligned_le64(&echo_msg[i].timestamp);
rcv_packet_idx++;
}
return es58x_can_get_echo_skb(netdev, priv->tx_tail, tstamps, num_element);
}
static int es58x_fd_rx_can_msg(struct net_device *netdev,
const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd)
{
struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
const u8 *rx_can_msg_buf = es58x_fd_urb_cmd->rx_can_msg_buf;
u16 rx_can_msg_buf_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);
int pkts, ret;
ret = es58x_check_msg_max_len(es58x_dev->dev,
es58x_fd_urb_cmd->rx_can_msg_buf,
rx_can_msg_buf_len);
if (ret)
return ret;
for (pkts = 0; rx_can_msg_buf_len > 0; pkts++) {
const struct es58x_fd_rx_can_msg *rx_can_msg =
(const struct es58x_fd_rx_can_msg *)rx_can_msg_buf;
bool is_can_fd = !!(rx_can_msg->flags & ES58X_FLAG_FD_DATA);
/* rx_can_msg_len is the length of the rx_can_msg
* buffer. Not to be confused with rx_can_msg->len
* which is the length of the CAN payload
* rx_can_msg->data.
*/
u16 rx_can_msg_len = es58x_fd_sizeof_rx_tx_msg(*rx_can_msg);
if (rx_can_msg_len > rx_can_msg_buf_len) {
netdev_err(netdev,
"%s: Expected a rx_can_msg of size %d but only %d bytes are left in rx_can_msg_buf\n",
__func__,
rx_can_msg_len, rx_can_msg_buf_len);
return -EMSGSIZE;
}
if (rx_can_msg->len > CANFD_MAX_DLEN) {
netdev_err(netdev,
"%s: Data length is %d but maximum should be %d\n",
__func__, rx_can_msg->len, CANFD_MAX_DLEN);
return -EMSGSIZE;
}
if (netif_running(netdev)) {
u64 tstamp = get_unaligned_le64(&rx_can_msg->timestamp);
canid_t can_id = get_unaligned_le32(&rx_can_msg->can_id);
u8 dlc;
if (is_can_fd)
dlc = can_fd_len2dlc(rx_can_msg->len);
else
dlc = rx_can_msg->dlc;
ret = es58x_rx_can_msg(netdev, tstamp, rx_can_msg->data,
can_id, rx_can_msg->flags, dlc);
if (ret)
break;
}
rx_can_msg_buf_len -= rx_can_msg_len;
rx_can_msg_buf += rx_can_msg_len;
}
if (!netif_running(netdev)) {
if (net_ratelimit())
netdev_info(netdev,
"%s: %s is down, dropping %d rx packets\n",
__func__, netdev->name, pkts);
netdev->stats.rx_dropped += pkts;
}
return ret;
}
static int es58x_fd_rx_event_msg(struct net_device *netdev,
const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd)
{
struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
u16 msg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);
const struct es58x_fd_rx_event_msg *rx_event_msg;
int ret;
rx_event_msg = &es58x_fd_urb_cmd->rx_event_msg;
ret = es58x_check_msg_len(es58x_dev->dev, *rx_event_msg, msg_len);
if (ret)
return ret;
return es58x_rx_err_msg(netdev, rx_event_msg->error_code,
rx_event_msg->event_code,
get_unaligned_le64(&rx_event_msg->timestamp));
}
static int es58x_fd_rx_cmd_ret_u32(struct net_device *netdev,
const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd,
enum es58x_ret_type cmd_ret_type)
{
struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
u16 msg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);
int ret;
ret = es58x_check_msg_len(es58x_dev->dev,
es58x_fd_urb_cmd->rx_cmd_ret_le32, msg_len);
if (ret)
return ret;
return es58x_rx_cmd_ret_u32(netdev, cmd_ret_type,
get_unaligned_le32(&es58x_fd_urb_cmd->rx_cmd_ret_le32));
}
static int es58x_fd_tx_ack_msg(struct net_device *netdev,
const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd)
{
struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
const struct es58x_fd_tx_ack_msg *tx_ack_msg;
u16 msg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);
int ret;
tx_ack_msg = &es58x_fd_urb_cmd->tx_ack_msg;
ret = es58x_check_msg_len(es58x_dev->dev, *tx_ack_msg, msg_len);
if (ret)
return ret;
return es58x_tx_ack_msg(netdev,
get_unaligned_le16(&tx_ack_msg->tx_free_entries),
get_unaligned_le32(&tx_ack_msg->rx_cmd_ret_le32));
}
static int es58x_fd_can_cmd_id(struct es58x_device *es58x_dev,
const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd)
{
struct net_device *netdev;
int ret;
ret = es58x_get_netdev(es58x_dev, es58x_fd_urb_cmd->channel_idx,
ES58X_FD_CHANNEL_IDX_OFFSET, &netdev);
if (ret)
return ret;
switch ((enum es58x_fd_can_cmd_id)es58x_fd_urb_cmd->cmd_id) {
case ES58X_FD_CAN_CMD_ID_ENABLE_CHANNEL:
return es58x_fd_rx_cmd_ret_u32(netdev, es58x_fd_urb_cmd,
ES58X_RET_TYPE_ENABLE_CHANNEL);
case ES58X_FD_CAN_CMD_ID_DISABLE_CHANNEL:
return es58x_fd_rx_cmd_ret_u32(netdev, es58x_fd_urb_cmd,
ES58X_RET_TYPE_DISABLE_CHANNEL);
case ES58X_FD_CAN_CMD_ID_TX_MSG:
return es58x_fd_tx_ack_msg(netdev, es58x_fd_urb_cmd);
case ES58X_FD_CAN_CMD_ID_ECHO_MSG:
return es58x_fd_echo_msg(netdev, es58x_fd_urb_cmd);
case ES58X_FD_CAN_CMD_ID_RX_MSG:
return es58x_fd_rx_can_msg(netdev, es58x_fd_urb_cmd);
case ES58X_FD_CAN_CMD_ID_RESET_RX:
return es58x_fd_rx_cmd_ret_u32(netdev, es58x_fd_urb_cmd,
ES58X_RET_TYPE_RESET_RX);
case ES58X_FD_CAN_CMD_ID_RESET_TX:
return es58x_fd_rx_cmd_ret_u32(netdev, es58x_fd_urb_cmd,
ES58X_RET_TYPE_RESET_TX);
case ES58X_FD_CAN_CMD_ID_ERROR_OR_EVENT_MSG:
return es58x_fd_rx_event_msg(netdev, es58x_fd_urb_cmd);
default:
return -EBADRQC;
}
}
static int es58x_fd_device_cmd_id(struct es58x_device *es58x_dev,
const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd)
{
u16 msg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);
int ret;
switch ((enum es58x_fd_dev_cmd_id)es58x_fd_urb_cmd->cmd_id) {
case ES58X_FD_DEV_CMD_ID_TIMESTAMP:
ret = es58x_check_msg_len(es58x_dev->dev,
es58x_fd_urb_cmd->timestamp, msg_len);
if (ret)
return ret;
es58x_rx_timestamp(es58x_dev,
get_unaligned_le64(&es58x_fd_urb_cmd->timestamp));
return 0;
default:
return -EBADRQC;
}
}
static int es58x_fd_handle_urb_cmd(struct es58x_device *es58x_dev,
const union es58x_urb_cmd *urb_cmd)
{
const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd;
int ret;
es58x_fd_urb_cmd = &urb_cmd->es58x_fd_urb_cmd;
switch ((enum es58x_fd_cmd_type)es58x_fd_urb_cmd->cmd_type) {
case ES58X_FD_CMD_TYPE_CAN:
case ES58X_FD_CMD_TYPE_CANFD:
ret = es58x_fd_can_cmd_id(es58x_dev, es58x_fd_urb_cmd);
break;
case ES58X_FD_CMD_TYPE_DEVICE:
ret = es58x_fd_device_cmd_id(es58x_dev, es58x_fd_urb_cmd);
break;
default:
ret = -EBADRQC;
break;
}
if (ret == -EBADRQC)
dev_err(es58x_dev->dev,
"%s: Unknown command type (0x%02X) and command ID (0x%02X) combination\n",
__func__, es58x_fd_urb_cmd->cmd_type,
es58x_fd_urb_cmd->cmd_id);
return ret;
}
static void es58x_fd_fill_urb_header(union es58x_urb_cmd *urb_cmd, u8 cmd_type,
u8 cmd_id, u8 channel_idx, u16 msg_len)
{
struct es58x_fd_urb_cmd *es58x_fd_urb_cmd = &urb_cmd->es58x_fd_urb_cmd;
es58x_fd_urb_cmd->SOF = cpu_to_le16(es58x_fd_param.tx_start_of_frame);
es58x_fd_urb_cmd->cmd_type = cmd_type;
es58x_fd_urb_cmd->cmd_id = cmd_id;
es58x_fd_urb_cmd->channel_idx = channel_idx;
es58x_fd_urb_cmd->msg_len = cpu_to_le16(msg_len);
}
static int es58x_fd_tx_can_msg(struct es58x_priv *priv,
const struct sk_buff *skb)
{
struct es58x_device *es58x_dev = priv->es58x_dev;
union es58x_urb_cmd *urb_cmd = priv->tx_urb->transfer_buffer;
struct es58x_fd_urb_cmd *es58x_fd_urb_cmd = &urb_cmd->es58x_fd_urb_cmd;
struct can_frame *cf = (struct can_frame *)skb->data;
struct es58x_fd_tx_can_msg *tx_can_msg;
bool is_fd = can_is_canfd_skb(skb);
u16 msg_len;
int ret;
if (priv->tx_can_msg_cnt == 0) {
msg_len = 0;
es58x_fd_fill_urb_header(urb_cmd,
is_fd ? ES58X_FD_CMD_TYPE_CANFD
: ES58X_FD_CMD_TYPE_CAN,
ES58X_FD_CAN_CMD_ID_TX_MSG_NO_ACK,
priv->channel_idx, msg_len);
} else {
msg_len = es58x_fd_get_msg_len(urb_cmd);
}
ret = es58x_check_msg_max_len(es58x_dev->dev,
es58x_fd_urb_cmd->tx_can_msg_buf,
msg_len + sizeof(*tx_can_msg));
if (ret)
return ret;
/* Fill message contents. */
tx_can_msg = (typeof(tx_can_msg))&es58x_fd_urb_cmd->raw_msg[msg_len];
tx_can_msg->packet_idx = (u8)priv->tx_head;
put_unaligned_le32(es58x_get_raw_can_id(cf), &tx_can_msg->can_id);
tx_can_msg->flags = (u8)es58x_get_flags(skb);
if (is_fd)
tx_can_msg->len = cf->len;
else
tx_can_msg->dlc = can_get_cc_dlc(cf, priv->can.ctrlmode);
memcpy(tx_can_msg->data, cf->data, cf->len);
/* Calculate new sizes */
msg_len += es58x_fd_sizeof_rx_tx_msg(*tx_can_msg);
priv->tx_urb->transfer_buffer_length = es58x_get_urb_cmd_len(es58x_dev,
msg_len);
put_unaligned_le16(msg_len, &es58x_fd_urb_cmd->msg_len);
return 0;
}
static void es58x_fd_convert_bittiming(struct es58x_fd_bittiming *es58x_fd_bt,
struct can_bittiming *bt)
{
/* The actual value set in the hardware registers is one less
* than the functional value.
*/
const int offset = 1;
es58x_fd_bt->bitrate = cpu_to_le32(bt->bitrate);
es58x_fd_bt->tseg1 =
cpu_to_le16(bt->prop_seg + bt->phase_seg1 - offset);
es58x_fd_bt->tseg2 = cpu_to_le16(bt->phase_seg2 - offset);
es58x_fd_bt->brp = cpu_to_le16(bt->brp - offset);
es58x_fd_bt->sjw = cpu_to_le16(bt->sjw - offset);
}
static int es58x_fd_enable_channel(struct es58x_priv *priv)
{
struct es58x_device *es58x_dev = priv->es58x_dev;
struct net_device *netdev = es58x_dev->netdev[priv->channel_idx];
struct es58x_fd_tx_conf_msg tx_conf_msg = { 0 };
u32 ctrlmode;
size_t conf_len = 0;
es58x_fd_convert_bittiming(&tx_conf_msg.nominal_bittiming,
&priv->can.bittiming);
ctrlmode = priv->can.ctrlmode;
if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
tx_conf_msg.samples_per_bit = ES58X_SAMPLES_PER_BIT_THREE;
else
tx_conf_msg.samples_per_bit = ES58X_SAMPLES_PER_BIT_ONE;
tx_conf_msg.sync_edge = ES58X_SYNC_EDGE_SINGLE;
tx_conf_msg.physical_layer = ES58X_PHYSICAL_LAYER_HIGH_SPEED;
tx_conf_msg.echo_mode = ES58X_ECHO_ON;
if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
tx_conf_msg.ctrlmode |= ES58X_FD_CTRLMODE_PASSIVE;
else
tx_conf_msg.ctrlmode |= ES58X_FD_CTRLMODE_ACTIVE;
if (ctrlmode & CAN_CTRLMODE_FD_NON_ISO) {
tx_conf_msg.ctrlmode |= ES58X_FD_CTRLMODE_FD_NON_ISO;
tx_conf_msg.canfd_enabled = 1;
} else if (ctrlmode & CAN_CTRLMODE_FD) {
tx_conf_msg.ctrlmode |= ES58X_FD_CTRLMODE_FD;
tx_conf_msg.canfd_enabled = 1;
}
if (tx_conf_msg.canfd_enabled) {
es58x_fd_convert_bittiming(&tx_conf_msg.data_bittiming,
&priv->can.fd.data_bittiming);
if (can_fd_tdc_is_enabled(&priv->can)) {
tx_conf_msg.tdc_enabled = 1;
tx_conf_msg.tdco = cpu_to_le16(priv->can.fd.tdc.tdco);
tx_conf_msg.tdcf = cpu_to_le16(priv->can.fd.tdc.tdcf);
}
conf_len = ES58X_FD_CANFD_CONF_LEN;
} else {
conf_len = ES58X_FD_CAN_CONF_LEN;
}
return es58x_send_msg(es58x_dev, es58x_fd_cmd_type(netdev),
ES58X_FD_CAN_CMD_ID_ENABLE_CHANNEL,
&tx_conf_msg, conf_len, priv->channel_idx);
}
static int es58x_fd_disable_channel(struct es58x_priv *priv)
{
/* The type (ES58X_FD_CMD_TYPE_CAN or ES58X_FD_CMD_TYPE_CANFD) does
* not matter here.
*/
return es58x_send_msg(priv->es58x_dev, ES58X_FD_CMD_TYPE_CAN,
ES58X_FD_CAN_CMD_ID_DISABLE_CHANNEL,
ES58X_EMPTY_MSG, 0, priv->channel_idx);
}
static int es58x_fd_get_timestamp(struct es58x_device *es58x_dev)
{
return es58x_send_msg(es58x_dev, ES58X_FD_CMD_TYPE_DEVICE,
ES58X_FD_DEV_CMD_ID_TIMESTAMP, ES58X_EMPTY_MSG,
0, ES58X_CHANNEL_IDX_NA);
}
/* Nominal bittiming constants for ES582.1 and ES584.1 as specified in
* the microcontroller datasheet: "SAM E70/S70/V70/V71 Family" section
* 49.6.8 "MCAN Nominal Bit Timing and Prescaler Register" from
* Microchip.
*
* The values from the specification are the hardware register
* values. To convert them to the functional values, all ranges were
* incremented by 1 (e.g. range [0..n-1] changed to [1..n]).
*/
static const struct can_bittiming_const es58x_fd_nom_bittiming_const = {
.name = "ES582.1/ES584.1",
.tseg1_min = 2,
.tseg1_max = 256,
.tseg2_min = 2,
.tseg2_max = 128,
.sjw_max = 128,
.brp_min = 1,
.brp_max = 512,
.brp_inc = 1
};
/* Data bittiming constants for ES582.1 and ES584.1 as specified in
* the microcontroller datasheet: "SAM E70/S70/V70/V71 Family" section
* 49.6.4 "MCAN Data Bit Timing and Prescaler Register" from
* Microchip.
*/
static const struct can_bittiming_const es58x_fd_data_bittiming_const = {
.name = "ES582.1/ES584.1",
.tseg1_min = 2,
.tseg1_max = 32,
.tseg2_min = 1,
.tseg2_max = 16,
.sjw_max = 8,
.brp_min = 1,
.brp_max = 32,
.brp_inc = 1
};
/* Transmission Delay Compensation constants for ES582.1 and ES584.1
* as specified in the microcontroller datasheet: "SAM E70/S70/V70/V71
* Family" section 49.6.15 "MCAN Transmitter Delay Compensation
* Register" from Microchip.
*/
static const struct can_tdc_const es58x_tdc_const = {
.tdcv_min = 0,
.tdcv_max = 0, /* Manual mode not supported. */
.tdco_min = 0,
.tdco_max = 127,
.tdcf_min = 0,
.tdcf_max = 127
};
const struct es58x_parameters es58x_fd_param = {
.bittiming_const = &es58x_fd_nom_bittiming_const,
.data_bittiming_const = &es58x_fd_data_bittiming_const,
.tdc_const = &es58x_tdc_const,
/* The devices use NXP TJA1044G transievers which guarantee
* the timing for data rates up to 5 Mbps. Bitrates up to 8
* Mbps work in an optimal environment but are not recommended
* for production environment.
*/
.bitrate_max = 8 * MEGA /* BPS */,
.clock = {.freq = 80 * MEGA /* Hz */},
.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | CAN_CTRLMODE_LISTENONLY |
CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO |
CAN_CTRLMODE_CC_LEN8_DLC | CAN_CTRLMODE_TDC_AUTO,
.tx_start_of_frame = 0xCEFA, /* FACE in little endian */
.rx_start_of_frame = 0xFECA, /* CAFE in little endian */
.tx_urb_cmd_max_len = ES58X_FD_TX_URB_CMD_MAX_LEN,
.rx_urb_cmd_max_len = ES58X_FD_RX_URB_CMD_MAX_LEN,
/* Size of internal device TX queue is 500.
*
* However, when reaching value around 278, the device's busy
* LED turns on and thus maximum value of 500 is never reached
* in practice. Also, when this value is too high, some error
* on the echo_msg were witnessed when the device is
* recovering from bus off.
*
* For above reasons, a value that would prevent the device
* from becoming busy was chosen. In practice, BQL would
* prevent the value from even getting closer to below
* maximum, so no impact on performance was measured.
*/
.fifo_mask = 255, /* echo_skb_max = 256 */
.dql_min_limit = CAN_FRAME_LEN_MAX * 15, /* Empirical value. */
.tx_bulk_max = ES58X_FD_TX_BULK_MAX,
.urb_cmd_header_len = ES58X_FD_URB_CMD_HEADER_LEN,
.rx_urb_max = ES58X_RX_URBS_MAX,
.tx_urb_max = ES58X_TX_URBS_MAX
};
const struct es58x_operators es58x_fd_ops = {
.get_msg_len = es58x_fd_get_msg_len,
.handle_urb_cmd = es58x_fd_handle_urb_cmd,
.fill_urb_header = es58x_fd_fill_urb_header,
.tx_can_msg = es58x_fd_tx_can_msg,
.enable_channel = es58x_fd_enable_channel,
.disable_channel = es58x_fd_disable_channel,
.reset_device = NULL, /* Not implemented in the device firmware. */
.get_timestamp = es58x_fd_get_timestamp
};
@@ -0,0 +1,234 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.
*
* File es58x_fd.h: Definitions and declarations specific to ETAS
* ES582.1 and ES584.1 (naming convention: we use the term "ES58X FD"
* when referring to those two variants together).
*
* Copyright (c) 2019 Robert Bosch Engineering and Business Solutions. All rights reserved.
* Copyright (c) 2020 ETAS K.K.. All rights reserved.
* Copyright (c) 2020, 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
*/
#ifndef __ES58X_FD_H__
#define __ES58X_FD_H__
#include <linux/types.h>
#define ES582_1_NUM_CAN_CH 2
#define ES584_1_NUM_CAN_CH 1
#define ES58X_FD_NUM_CAN_CH 2
#define ES58X_FD_CHANNEL_IDX_OFFSET 0
#define ES58X_FD_TX_BULK_MAX 100
#define ES58X_FD_RX_BULK_MAX 100
#define ES58X_FD_ECHO_BULK_MAX 100
enum es58x_fd_cmd_type {
ES58X_FD_CMD_TYPE_CAN = 0x03,
ES58X_FD_CMD_TYPE_CANFD = 0x04,
ES58X_FD_CMD_TYPE_DEVICE = 0xFF
};
/* Command IDs for ES58X_FD_CMD_TYPE_{CAN,CANFD}. */
enum es58x_fd_can_cmd_id {
ES58X_FD_CAN_CMD_ID_ENABLE_CHANNEL = 0x01,
ES58X_FD_CAN_CMD_ID_DISABLE_CHANNEL = 0x02,
ES58X_FD_CAN_CMD_ID_TX_MSG = 0x05,
ES58X_FD_CAN_CMD_ID_ECHO_MSG = 0x07,
ES58X_FD_CAN_CMD_ID_RX_MSG = 0x10,
ES58X_FD_CAN_CMD_ID_ERROR_OR_EVENT_MSG = 0x11,
ES58X_FD_CAN_CMD_ID_RESET_RX = 0x20,
ES58X_FD_CAN_CMD_ID_RESET_TX = 0x21,
ES58X_FD_CAN_CMD_ID_TX_MSG_NO_ACK = 0x55
};
/* Command IDs for ES58X_FD_CMD_TYPE_DEVICE. */
enum es58x_fd_dev_cmd_id {
ES58X_FD_DEV_CMD_ID_GETTIMETICKS = 0x01,
ES58X_FD_DEV_CMD_ID_TIMESTAMP = 0x02
};
/**
* enum es58x_fd_ctrlmode - Controller mode.
* @ES58X_FD_CTRLMODE_ACTIVE: send and receive messages.
* @ES58X_FD_CTRLMODE_PASSIVE: only receive messages (monitor). Do not
* send anything, not even the acknowledgment bit.
* @ES58X_FD_CTRLMODE_FD: CAN FD according to ISO11898-1.
* @ES58X_FD_CTRLMODE_FD_NON_ISO: follow Bosch CAN FD Specification
* V1.0
* @ES58X_FD_CTRLMODE_DISABLE_PROTOCOL_EXCEPTION_HANDLING: How to
* behave when CAN FD reserved bit is monitored as
* dominant. (c.f. ISO 11898-1:2015, section 10.4.2.4 "Control
* field", paragraph "r0 bit"). 0 (not disable = enable): send
* error frame. 1 (disable): goes into bus integration mode
* (c.f. below).
* @ES58X_FD_CTRLMODE_EDGE_FILTER_DURING_BUS_INTEGRATION: 0: Edge
* filtering is disabled. 1: Edge filtering is enabled. Two
* consecutive dominant bits required to detect an edge for hard
* synchronization.
*/
enum es58x_fd_ctrlmode {
ES58X_FD_CTRLMODE_ACTIVE = 0,
ES58X_FD_CTRLMODE_PASSIVE = BIT(0),
ES58X_FD_CTRLMODE_FD = BIT(4),
ES58X_FD_CTRLMODE_FD_NON_ISO = BIT(5),
ES58X_FD_CTRLMODE_DISABLE_PROTOCOL_EXCEPTION_HANDLING = BIT(6),
ES58X_FD_CTRLMODE_EDGE_FILTER_DURING_BUS_INTEGRATION = BIT(7)
};
struct es58x_fd_bittiming {
__le32 bitrate;
__le16 tseg1; /* range: [tseg1_min-1..tseg1_max-1] */
__le16 tseg2; /* range: [tseg2_min-1..tseg2_max-1] */
__le16 brp; /* range: [brp_min-1..brp_max-1] */
__le16 sjw; /* range: [0..sjw_max-1] */
} __packed;
/**
* struct es58x_fd_tx_conf_msg - Channel configuration.
* @nominal_bittiming: Nominal bittiming.
* @samples_per_bit: type enum es58x_samples_per_bit.
* @sync_edge: type enum es58x_sync_edge.
* @physical_layer: type enum es58x_physical_layer.
* @echo_mode: type enum es58x_echo_mode.
* @ctrlmode: type enum es58x_fd_ctrlmode.
* @canfd_enabled: boolean (0: Classical CAN, 1: CAN and/or CANFD).
* @data_bittiming: Bittiming for flexible data-rate transmission.
* @tdc_enabled: Transmitter Delay Compensation switch (0: TDC is
* disabled, 1: TDC is enabled).
* @tdco: Transmitter Delay Compensation Offset.
* @tdcf: Transmitter Delay Compensation Filter window.
*
* Please refer to the microcontroller datasheet: "SAM E70/S70/V70/V71
* Family" section 49 "Controller Area Network (MCAN)" for additional
* information.
*/
struct es58x_fd_tx_conf_msg {
struct es58x_fd_bittiming nominal_bittiming;
u8 samples_per_bit;
u8 sync_edge;
u8 physical_layer;
u8 echo_mode;
u8 ctrlmode;
u8 canfd_enabled;
struct es58x_fd_bittiming data_bittiming;
u8 tdc_enabled;
__le16 tdco;
__le16 tdcf;
} __packed;
#define ES58X_FD_CAN_CONF_LEN \
(offsetof(struct es58x_fd_tx_conf_msg, canfd_enabled))
#define ES58X_FD_CANFD_CONF_LEN (sizeof(struct es58x_fd_tx_conf_msg))
struct es58x_fd_tx_can_msg {
u8 packet_idx;
__le32 can_id;
u8 flags;
union {
u8 dlc; /* Only if cmd_id is ES58X_FD_CMD_TYPE_CAN */
u8 len; /* Only if cmd_id is ES58X_FD_CMD_TYPE_CANFD */
} __packed;
u8 data[CANFD_MAX_DLEN];
} __packed;
#define ES58X_FD_CAN_TX_LEN \
(offsetof(struct es58x_fd_tx_can_msg, data[CAN_MAX_DLEN]))
#define ES58X_FD_CANFD_TX_LEN (sizeof(struct es58x_fd_tx_can_msg))
struct es58x_fd_rx_can_msg {
__le64 timestamp;
__le32 can_id;
u8 flags;
union {
u8 dlc; /* Only if cmd_id is ES58X_FD_CMD_TYPE_CAN */
u8 len; /* Only if cmd_id is ES58X_FD_CMD_TYPE_CANFD */
} __packed;
u8 data[CANFD_MAX_DLEN];
} __packed;
#define ES58X_FD_CAN_RX_LEN \
(offsetof(struct es58x_fd_rx_can_msg, data[CAN_MAX_DLEN]))
#define ES58X_FD_CANFD_RX_LEN (sizeof(struct es58x_fd_rx_can_msg))
struct es58x_fd_echo_msg {
__le64 timestamp;
u8 packet_idx;
} __packed;
struct es58x_fd_rx_event_msg {
__le64 timestamp;
__le32 can_id;
u8 flags; /* type enum es58x_flag */
u8 error_type; /* 0: event, 1: error */
u8 error_code;
u8 event_code;
} __packed;
struct es58x_fd_tx_ack_msg {
__le32 rx_cmd_ret_le32; /* type enum es58x_cmd_ret_code_u32 */
__le16 tx_free_entries; /* Number of remaining free entries in the device TX queue */
} __packed;
/**
* struct es58x_fd_urb_cmd - Commands received from or sent to the
* ES58X FD device.
* @SOF: Start of Frame.
* @cmd_type: Command Type (type: enum es58x_fd_cmd_type). The CRC
* calculation starts at this position.
* @cmd_id: Command ID (type: enum es58x_fd_cmd_id).
* @channel_idx: Channel index starting at 0.
* @msg_len: Length of the message, excluding CRC (i.e. length of the
* union).
* @tx_conf_msg: Channel configuration.
* @tx_can_msg_buf: Concatenation of Tx messages. Type is "u8[]"
* instead of "struct es58x_fd_tx_msg[]" because the structure
* has a flexible size.
* @rx_can_msg_buf: Concatenation Rx messages. Type is "u8[]" instead
* of "struct es58x_fd_rx_msg[]" because the structure has a
* flexible size.
* @echo_msg: Array of echo messages (e.g. Tx messages being looped
* back).
* @rx_event_msg: Error or event message.
* @tx_ack_msg: Tx acknowledgment message.
* @timestamp: Timestamp reply.
* @rx_cmd_ret_le32: Rx 32 bits return code (type: enum
* es58x_cmd_ret_code_u32).
* @raw_msg: Message raw payload.
* @reserved_for_crc16_do_not_use: The structure ends with a
* CRC16. Because the structures in above union are of variable
* lengths, we can not predict the offset of the CRC in
* advance. Use functions es58x_get_crc() and es58x_set_crc() to
* manipulate it.
*/
struct es58x_fd_urb_cmd {
__le16 SOF;
u8 cmd_type;
u8 cmd_id;
u8 channel_idx;
__le16 msg_len;
union {
struct es58x_fd_tx_conf_msg tx_conf_msg;
u8 tx_can_msg_buf[ES58X_FD_TX_BULK_MAX * ES58X_FD_CANFD_TX_LEN];
u8 rx_can_msg_buf[ES58X_FD_RX_BULK_MAX * ES58X_FD_CANFD_RX_LEN];
struct es58x_fd_echo_msg echo_msg[ES58X_FD_ECHO_BULK_MAX];
struct es58x_fd_rx_event_msg rx_event_msg;
struct es58x_fd_tx_ack_msg tx_ack_msg;
__le64 timestamp;
__le32 rx_cmd_ret_le32;
DECLARE_FLEX_ARRAY(u8, raw_msg);
} __packed;
__le16 reserved_for_crc16_do_not_use;
} __packed;
#define ES58X_FD_URB_CMD_HEADER_LEN (offsetof(struct es58x_fd_urb_cmd, raw_msg))
#define ES58X_FD_TX_URB_CMD_MAX_LEN \
ES58X_SIZEOF_URB_CMD(struct es58x_fd_urb_cmd, tx_can_msg_buf)
#define ES58X_FD_RX_URB_CMD_MAX_LEN \
ES58X_SIZEOF_URB_CMD(struct es58x_fd_urb_cmd, rx_can_msg_buf)
#endif /* __ES58X_FD_H__ */
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,3 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb.o
kvaser_usb-y = kvaser_usb_core.o kvaser_usb_devlink.o kvaser_usb_leaf.o kvaser_usb_hydra.o
@@ -0,0 +1,272 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* Parts of this driver are based on the following:
* - Kvaser linux leaf driver (version 4.78)
* - CAN driver for esd CAN-USB/2
* - Kvaser linux usbcanII driver (version 5.3)
* - Kvaser linux mhydra driver (version 5.24)
*
* Copyright (C) 2002-2018 KVASER AB, Sweden. All rights reserved.
* Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
* Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
* Copyright (C) 2015 Valeo S.A.
*/
#ifndef KVASER_USB_H
#define KVASER_USB_H
/* Kvaser USB CAN dongles are divided into three major platforms:
* - Hydra: Running firmware labeled as 'mhydra'
* - Leaf: Based on Renesas M32C or Freescale i.MX28, running firmware labeled
* as 'filo'
* - UsbcanII: Based on Renesas M16C, running firmware labeled as 'helios'
*/
#include <linux/completion.h>
#include <linux/ktime.h>
#include <linux/math64.h>
#include <linux/spinlock.h>
#include <linux/types.h>
#include <linux/usb.h>
#include <net/devlink.h>
#include <linux/can.h>
#include <linux/can/dev.h>
#define KVASER_USB_MAX_RX_URBS 4
#define KVASER_USB_MAX_TX_URBS 128
#define KVASER_USB_TIMEOUT 1000 /* msecs */
#define KVASER_USB_RX_BUFFER_SIZE 3072
#define KVASER_USB_MAX_NET_DEVICES 5
/* Kvaser USB device quirks */
#define KVASER_USB_QUIRK_HAS_SILENT_MODE BIT(0)
#define KVASER_USB_QUIRK_HAS_TXRX_ERRORS BIT(1)
#define KVASER_USB_QUIRK_IGNORE_CLK_FREQ BIT(2)
/* Device capabilities */
#define KVASER_USB_CAP_BERR_CAP 0x01
#define KVASER_USB_CAP_EXT_CAP 0x02
#define KVASER_USB_HYDRA_CAP_EXT_CMD 0x04
#define KVASER_USB_SW_VERSION_MAJOR_MASK GENMASK(31, 24)
#define KVASER_USB_SW_VERSION_MINOR_MASK GENMASK(23, 16)
#define KVASER_USB_SW_VERSION_BUILD_MASK GENMASK(15, 0)
struct kvaser_usb_dev_cfg;
enum kvaser_usb_leaf_family {
KVASER_LEAF,
KVASER_USBCAN,
};
enum kvaser_usb_led_state {
KVASER_USB_LED_ON = 0,
KVASER_USB_LED_OFF = 1,
};
#define KVASER_USB_HYDRA_MAX_CMD_LEN 128
struct kvaser_usb_dev_card_data_hydra {
u8 channel_to_he[KVASER_USB_MAX_NET_DEVICES];
u8 sysdbg_he;
spinlock_t transid_lock; /* lock for transid */
u16 transid;
/* lock for usb_rx_leftover and usb_rx_leftover_len */
spinlock_t usb_rx_leftover_lock;
u8 usb_rx_leftover[KVASER_USB_HYDRA_MAX_CMD_LEN];
u8 usb_rx_leftover_len;
};
struct kvaser_usb_dev_card_data {
u32 ctrlmode_supported;
u32 capabilities;
struct kvaser_usb_dev_card_data_hydra hydra;
u32 usbcan_timestamp_msb;
};
/* Context for an outstanding, not yet ACKed, transmission */
struct kvaser_usb_tx_urb_context {
struct kvaser_usb_net_priv *priv;
u32 echo_index;
};
struct kvaser_usb_fw_version {
u8 major;
u8 minor;
u16 build;
};
struct kvaser_usb_busparams {
__le32 bitrate;
u8 tseg1;
u8 tseg2;
u8 sjw;
u8 nsamples;
} __packed;
struct kvaser_usb {
struct usb_device *udev;
struct usb_interface *intf;
struct kvaser_usb_net_priv *nets[KVASER_USB_MAX_NET_DEVICES];
const struct kvaser_usb_driver_info *driver_info;
const struct kvaser_usb_dev_cfg *cfg;
struct usb_endpoint_descriptor *bulk_in, *bulk_out;
struct usb_anchor rx_submitted;
u32 ean[2];
u32 serial_number;
struct kvaser_usb_fw_version fw_version;
u8 hw_revision;
unsigned int nchannels;
/* @max_tx_urbs: Firmware-reported maximum number of outstanding,
* not yet ACKed, transmissions on this device. This value is
* also used as a sentinel for marking free tx contexts.
*/
unsigned int max_tx_urbs;
struct kvaser_usb_dev_card_data card_data;
bool rxinitdone;
void *rxbuf[KVASER_USB_MAX_RX_URBS];
dma_addr_t rxbuf_dma[KVASER_USB_MAX_RX_URBS];
};
struct kvaser_usb_net_priv {
struct can_priv can;
struct devlink_port devlink_port;
struct can_berr_counter bec;
/* subdriver-specific data */
void *sub_priv;
struct kvaser_usb *dev;
struct net_device *netdev;
int channel;
struct completion start_comp, stop_comp, flush_comp,
get_busparams_comp;
struct usb_anchor tx_submitted;
struct kvaser_usb_busparams busparams_nominal, busparams_data;
spinlock_t tx_contexts_lock; /* lock for active_tx_contexts */
int active_tx_contexts;
struct kvaser_usb_tx_urb_context tx_contexts[];
};
/**
* struct kvaser_usb_dev_ops - Device specific functions
* @dev_set_mode: used for can.do_set_mode
* @dev_set_bittiming: used for can.do_set_bittiming
* @dev_get_busparams: readback arbitration busparams
* @dev_set_data_bittiming: used for can.fd.do_set_data_bittiming
* @dev_get_data_busparams: readback data busparams
* @dev_get_berr_counter: used for can.do_get_berr_counter
*
* @dev_setup_endpoints: setup USB in and out endpoints
* @dev_init_card: initialize card
* @dev_init_channel: initialize channel
* @dev_remove_channel: uninitialize channel
* @dev_get_software_info: get software info
* @dev_get_software_details: get software details
* @dev_get_card_info: get card info
* @dev_get_capabilities: discover device capabilities
* @dev_set_led: turn on/off device LED
*
* @dev_set_opt_mode: set ctrlmod
* @dev_start_chip: start the CAN controller
* @dev_stop_chip: stop the CAN controller
* @dev_reset_chip: reset the CAN controller
* @dev_flush_queue: flush outstanding CAN messages
* @dev_read_bulk_callback: handle incoming commands
* @dev_frame_to_cmd: translate struct can_frame into device command
*/
struct kvaser_usb_dev_ops {
int (*dev_set_mode)(struct net_device *netdev, enum can_mode mode);
int (*dev_set_bittiming)(const struct net_device *netdev,
const struct kvaser_usb_busparams *busparams);
int (*dev_get_busparams)(struct kvaser_usb_net_priv *priv);
int (*dev_set_data_bittiming)(const struct net_device *netdev,
const struct kvaser_usb_busparams *busparams);
int (*dev_get_data_busparams)(struct kvaser_usb_net_priv *priv);
int (*dev_get_berr_counter)(const struct net_device *netdev,
struct can_berr_counter *bec);
int (*dev_setup_endpoints)(struct kvaser_usb *dev);
int (*dev_init_card)(struct kvaser_usb *dev);
int (*dev_init_channel)(struct kvaser_usb_net_priv *priv);
void (*dev_remove_channel)(struct kvaser_usb_net_priv *priv);
int (*dev_get_software_info)(struct kvaser_usb *dev);
int (*dev_get_software_details)(struct kvaser_usb *dev);
int (*dev_get_card_info)(struct kvaser_usb *dev);
int (*dev_get_capabilities)(struct kvaser_usb *dev);
int (*dev_set_led)(struct kvaser_usb_net_priv *priv,
enum kvaser_usb_led_state state,
u16 duration_ms);
int (*dev_set_opt_mode)(const struct kvaser_usb_net_priv *priv);
int (*dev_start_chip)(struct kvaser_usb_net_priv *priv);
int (*dev_stop_chip)(struct kvaser_usb_net_priv *priv);
int (*dev_reset_chip)(struct kvaser_usb *dev, int channel);
int (*dev_flush_queue)(struct kvaser_usb_net_priv *priv);
void (*dev_read_bulk_callback)(struct kvaser_usb *dev, void *buf,
int len);
void *(*dev_frame_to_cmd)(const struct kvaser_usb_net_priv *priv,
const struct sk_buff *skb, int *cmd_len,
u16 transid);
};
struct kvaser_usb_driver_info {
u32 quirks;
enum kvaser_usb_leaf_family family;
const struct kvaser_usb_dev_ops *ops;
};
struct kvaser_usb_dev_cfg {
const struct can_clock clock;
const unsigned int timestamp_freq;
const struct can_bittiming_const * const bittiming_const;
const struct can_bittiming_const * const data_bittiming_const;
};
extern const struct kvaser_usb_dev_ops kvaser_usb_hydra_dev_ops;
extern const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops;
extern const struct devlink_ops kvaser_usb_devlink_ops;
int kvaser_usb_devlink_port_register(struct kvaser_usb_net_priv *priv);
void kvaser_usb_devlink_port_unregister(struct kvaser_usb_net_priv *priv);
void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv);
int kvaser_usb_recv_cmd(const struct kvaser_usb *dev, void *cmd, int len,
int *actual_len);
int kvaser_usb_send_cmd(const struct kvaser_usb *dev, void *cmd, int len);
int kvaser_usb_send_cmd_async(struct kvaser_usb_net_priv *priv, void *cmd,
int len);
int kvaser_usb_can_rx_over_error(struct net_device *netdev);
extern const struct can_bittiming_const kvaser_usb_flexc_bittiming_const;
static inline ktime_t kvaser_usb_ticks_to_ktime(const struct kvaser_usb_dev_cfg *cfg,
u64 ticks)
{
return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq));
}
static inline ktime_t kvaser_usb_timestamp48_to_ktime(const struct kvaser_usb_dev_cfg *cfg,
const __le16 *timestamp)
{
u64 ticks = le16_to_cpu(timestamp[0]) |
(u64)(le16_to_cpu(timestamp[1])) << 16 |
(u64)(le16_to_cpu(timestamp[2])) << 32;
return kvaser_usb_ticks_to_ktime(cfg, ticks);
}
static inline ktime_t kvaser_usb_timestamp64_to_ktime(const struct kvaser_usb_dev_cfg *cfg,
__le64 timestamp)
{
return kvaser_usb_ticks_to_ktime(cfg, le64_to_cpu(timestamp));
}
#endif /* KVASER_USB_H */
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,87 @@
// SPDX-License-Identifier: GPL-2.0
/* kvaser_usb devlink functions
*
* Copyright (C) 2025 KVASER AB, Sweden. All rights reserved.
*/
#include "kvaser_usb.h"
#include <linux/netdevice.h>
#include <net/devlink.h>
#define KVASER_USB_EAN_MSB 0x00073301
static int kvaser_usb_devlink_info_get(struct devlink *devlink,
struct devlink_info_req *req,
struct netlink_ext_ack *extack)
{
struct kvaser_usb *dev = devlink_priv(devlink);
char buf[] = "73301XXXXXXXXXX";
int ret;
if (dev->serial_number) {
snprintf(buf, sizeof(buf), "%u", dev->serial_number);
ret = devlink_info_serial_number_put(req, buf);
if (ret)
return ret;
}
if (dev->fw_version.major) {
snprintf(buf, sizeof(buf), "%u.%u.%u",
dev->fw_version.major,
dev->fw_version.minor,
dev->fw_version.build);
ret = devlink_info_version_running_put(req,
DEVLINK_INFO_VERSION_GENERIC_FW,
buf);
if (ret)
return ret;
}
if (dev->hw_revision) {
snprintf(buf, sizeof(buf), "%u", dev->hw_revision);
ret = devlink_info_version_fixed_put(req,
DEVLINK_INFO_VERSION_GENERIC_BOARD_REV,
buf);
if (ret)
return ret;
}
if (dev->ean[1] == KVASER_USB_EAN_MSB) {
snprintf(buf, sizeof(buf), "%x%08x", dev->ean[1], dev->ean[0]);
ret = devlink_info_version_fixed_put(req,
DEVLINK_INFO_VERSION_GENERIC_BOARD_ID,
buf);
if (ret)
return ret;
}
return 0;
}
const struct devlink_ops kvaser_usb_devlink_ops = {
.info_get = kvaser_usb_devlink_info_get,
};
int kvaser_usb_devlink_port_register(struct kvaser_usb_net_priv *priv)
{
int ret;
struct devlink_port_attrs attrs = {
.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL,
.phys.port_number = priv->channel,
};
devlink_port_attrs_set(&priv->devlink_port, &attrs);
ret = devlink_port_register(priv_to_devlink(priv->dev),
&priv->devlink_port, priv->channel);
if (ret)
return ret;
SET_NETDEV_DEVLINK_PORT(priv->netdev, &priv->devlink_port);
return 0;
}
void kvaser_usb_devlink_port_unregister(struct kvaser_usb_net_priv *priv)
{
devlink_port_unregister(&priv->devlink_port);
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+919
View File
@@ -0,0 +1,919 @@
// SPDX-License-Identifier: GPL-2.0-only
/* SocketCAN driver for Microchip CAN BUS Analyzer Tool
*
* Copyright (C) 2017 Mobica Limited
*
* This driver is inspired by the 4.6.2 version of net/can/usb/usb_8dev.c
*/
#include <linux/unaligned.h>
#include <linux/can.h>
#include <linux/can/dev.h>
#include <linux/can/error.h>
#include <linux/ethtool.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/signal.h>
#include <linux/slab.h>
#include <linux/usb.h>
/* vendor and product id */
#define MCBA_MODULE_NAME "mcba_usb"
#define MCBA_VENDOR_ID 0x04d8
#define MCBA_PRODUCT_ID 0x0a30
/* driver constants */
#define MCBA_MAX_RX_URBS 20
#define MCBA_MAX_TX_URBS 20
#define MCBA_CTX_FREE MCBA_MAX_TX_URBS
/* RX buffer must be bigger than msg size since at the
* beginning USB messages are stacked.
*/
#define MCBA_USB_RX_BUFF_SIZE 64
#define MCBA_USB_TX_BUFF_SIZE (sizeof(struct mcba_usb_msg))
/* Microchip command id */
#define MBCA_CMD_RECEIVE_MESSAGE 0xE3
#define MBCA_CMD_I_AM_ALIVE_FROM_CAN 0xF5
#define MBCA_CMD_I_AM_ALIVE_FROM_USB 0xF7
#define MBCA_CMD_CHANGE_BIT_RATE 0xA1
#define MBCA_CMD_TRANSMIT_MESSAGE_EV 0xA3
#define MBCA_CMD_SETUP_TERMINATION_RESISTANCE 0xA8
#define MBCA_CMD_READ_FW_VERSION 0xA9
#define MBCA_CMD_NOTHING_TO_SEND 0xFF
#define MBCA_CMD_TRANSMIT_MESSAGE_RSP 0xE2
#define MCBA_VER_REQ_USB 1
#define MCBA_VER_REQ_CAN 2
/* Drive the CAN_RES signal LOW "0" to activate R24 and R25 */
#define MCBA_VER_TERMINATION_ON 0
#define MCBA_VER_TERMINATION_OFF 1
#define MCBA_SIDL_EXID_MASK 0x8
#define MCBA_DLC_MASK 0xf
#define MCBA_DLC_RTR_MASK 0x40
#define MCBA_CAN_STATE_WRN_TH 95
#define MCBA_CAN_STATE_ERR_PSV_TH 127
#define MCBA_TERMINATION_DISABLED CAN_TERMINATION_DISABLED
#define MCBA_TERMINATION_ENABLED 120
struct mcba_usb_ctx {
struct mcba_priv *priv;
u32 ndx;
bool can;
};
/* Structure to hold all of our device specific stuff */
struct mcba_priv {
struct can_priv can; /* must be the first member */
struct sk_buff *echo_skb[MCBA_MAX_TX_URBS];
struct mcba_usb_ctx tx_context[MCBA_MAX_TX_URBS];
struct usb_device *udev;
struct net_device *netdev;
struct usb_anchor tx_submitted;
struct usb_anchor rx_submitted;
struct can_berr_counter bec;
bool usb_ka_first_pass;
bool can_ka_first_pass;
bool can_speed_check;
atomic_t free_ctx_cnt;
void *rxbuf[MCBA_MAX_RX_URBS];
dma_addr_t rxbuf_dma[MCBA_MAX_RX_URBS];
int rx_pipe;
int tx_pipe;
};
/* CAN frame */
struct __packed mcba_usb_msg_can {
u8 cmd_id;
__be16 eid;
__be16 sid;
u8 dlc;
u8 data[8];
u8 timestamp[4];
u8 checksum;
};
/* command frame */
struct __packed mcba_usb_msg {
u8 cmd_id;
u8 unused[18];
};
struct __packed mcba_usb_msg_ka_usb {
u8 cmd_id;
u8 termination_state;
u8 soft_ver_major;
u8 soft_ver_minor;
u8 unused[15];
};
struct __packed mcba_usb_msg_ka_can {
u8 cmd_id;
u8 tx_err_cnt;
u8 rx_err_cnt;
u8 rx_buff_ovfl;
u8 tx_bus_off;
__be16 can_bitrate;
__le16 rx_lost;
u8 can_stat;
u8 soft_ver_major;
u8 soft_ver_minor;
u8 debug_mode;
u8 test_complete;
u8 test_result;
u8 unused[4];
};
struct __packed mcba_usb_msg_change_bitrate {
u8 cmd_id;
__be16 bitrate;
u8 unused[16];
};
struct __packed mcba_usb_msg_termination {
u8 cmd_id;
u8 termination;
u8 unused[17];
};
struct __packed mcba_usb_msg_fw_ver {
u8 cmd_id;
u8 pic;
u8 unused[17];
};
static const struct usb_device_id mcba_usb_table[] = {
{ USB_DEVICE(MCBA_VENDOR_ID, MCBA_PRODUCT_ID) },
{} /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, mcba_usb_table);
static const u16 mcba_termination[] = { MCBA_TERMINATION_DISABLED,
MCBA_TERMINATION_ENABLED };
static const u32 mcba_bitrate[] = { 20000, 33333, 50000, 80000, 83333,
100000, 125000, 150000, 175000, 200000,
225000, 250000, 275000, 300000, 500000,
625000, 800000, 1000000 };
static inline void mcba_init_ctx(struct mcba_priv *priv)
{
int i = 0;
for (i = 0; i < MCBA_MAX_TX_URBS; i++) {
priv->tx_context[i].ndx = MCBA_CTX_FREE;
priv->tx_context[i].priv = priv;
}
atomic_set(&priv->free_ctx_cnt, ARRAY_SIZE(priv->tx_context));
}
static inline struct mcba_usb_ctx *mcba_usb_get_free_ctx(struct mcba_priv *priv,
struct can_frame *cf)
{
int i = 0;
struct mcba_usb_ctx *ctx = NULL;
for (i = 0; i < MCBA_MAX_TX_URBS; i++) {
if (priv->tx_context[i].ndx == MCBA_CTX_FREE) {
ctx = &priv->tx_context[i];
ctx->ndx = i;
if (cf)
ctx->can = true;
else
ctx->can = false;
atomic_dec(&priv->free_ctx_cnt);
break;
}
}
if (!atomic_read(&priv->free_ctx_cnt))
/* That was the last free ctx. Slow down tx path */
netif_stop_queue(priv->netdev);
return ctx;
}
/* mcba_usb_free_ctx and mcba_usb_get_free_ctx are executed by different
* threads. The order of execution in below function is important.
*/
static inline void mcba_usb_free_ctx(struct mcba_usb_ctx *ctx)
{
/* Increase number of free ctxs before freeing ctx */
atomic_inc(&ctx->priv->free_ctx_cnt);
ctx->ndx = MCBA_CTX_FREE;
/* Wake up the queue once ctx is marked free */
netif_wake_queue(ctx->priv->netdev);
}
static void mcba_usb_write_bulk_callback(struct urb *urb)
{
struct mcba_usb_ctx *ctx = urb->context;
struct net_device *netdev;
WARN_ON(!ctx);
netdev = ctx->priv->netdev;
/* free up our allocated buffer */
usb_free_coherent(urb->dev, urb->transfer_buffer_length,
urb->transfer_buffer, urb->transfer_dma);
if (ctx->can) {
if (!netif_device_present(netdev))
return;
netdev->stats.tx_packets++;
netdev->stats.tx_bytes += can_get_echo_skb(netdev, ctx->ndx,
NULL);
}
if (urb->status)
netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
/* Release the context */
mcba_usb_free_ctx(ctx);
}
/* Send data to device */
static netdev_tx_t mcba_usb_xmit(struct mcba_priv *priv,
struct mcba_usb_msg *usb_msg,
struct mcba_usb_ctx *ctx)
{
struct urb *urb;
u8 *buf;
int err;
/* create a URB, and a buffer for it, and copy the data to the URB */
urb = usb_alloc_urb(0, GFP_ATOMIC);
if (!urb)
return -ENOMEM;
buf = usb_alloc_coherent(priv->udev, MCBA_USB_TX_BUFF_SIZE, GFP_ATOMIC,
&urb->transfer_dma);
if (!buf) {
err = -ENOMEM;
goto nomembuf;
}
memcpy(buf, usb_msg, MCBA_USB_TX_BUFF_SIZE);
usb_fill_bulk_urb(urb, priv->udev, priv->tx_pipe, buf, MCBA_USB_TX_BUFF_SIZE,
mcba_usb_write_bulk_callback, ctx);
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
usb_anchor_urb(urb, &priv->tx_submitted);
err = usb_submit_urb(urb, GFP_ATOMIC);
if (unlikely(err))
goto failed;
/* Release our reference to this URB, the USB core will eventually free
* it entirely.
*/
usb_free_urb(urb);
return 0;
failed:
usb_unanchor_urb(urb);
usb_free_coherent(priv->udev, MCBA_USB_TX_BUFF_SIZE, buf,
urb->transfer_dma);
if (err == -ENODEV)
netif_device_detach(priv->netdev);
else
netdev_warn(priv->netdev, "failed tx_urb %d\n", err);
nomembuf:
usb_free_urb(urb);
return err;
}
/* Send data to device */
static netdev_tx_t mcba_usb_start_xmit(struct sk_buff *skb,
struct net_device *netdev)
{
struct mcba_priv *priv = netdev_priv(netdev);
struct can_frame *cf = (struct can_frame *)skb->data;
struct mcba_usb_ctx *ctx = NULL;
struct net_device_stats *stats = &priv->netdev->stats;
u16 sid;
int err;
struct mcba_usb_msg_can usb_msg = {
.cmd_id = MBCA_CMD_TRANSMIT_MESSAGE_EV
};
if (can_dev_dropped_skb(netdev, skb))
return NETDEV_TX_OK;
ctx = mcba_usb_get_free_ctx(priv, cf);
if (!ctx)
return NETDEV_TX_BUSY;
if (cf->can_id & CAN_EFF_FLAG) {
/* SIDH | SIDL | EIDH | EIDL
* 28 - 21 | 20 19 18 x x x 17 16 | 15 - 8 | 7 - 0
*/
sid = MCBA_SIDL_EXID_MASK;
/* store 28-18 bits */
sid |= (cf->can_id & 0x1ffc0000) >> 13;
/* store 17-16 bits */
sid |= (cf->can_id & 0x30000) >> 16;
put_unaligned_be16(sid, &usb_msg.sid);
/* store 15-0 bits */
put_unaligned_be16(cf->can_id & 0xffff, &usb_msg.eid);
} else {
/* SIDH | SIDL
* 10 - 3 | 2 1 0 x x x x x
*/
put_unaligned_be16((cf->can_id & CAN_SFF_MASK) << 5,
&usb_msg.sid);
usb_msg.eid = 0;
}
usb_msg.dlc = cf->len;
memcpy(usb_msg.data, cf->data, usb_msg.dlc);
if (cf->can_id & CAN_RTR_FLAG)
usb_msg.dlc |= MCBA_DLC_RTR_MASK;
can_put_echo_skb(skb, priv->netdev, ctx->ndx, 0);
err = mcba_usb_xmit(priv, (struct mcba_usb_msg *)&usb_msg, ctx);
if (err)
goto xmit_failed;
return NETDEV_TX_OK;
xmit_failed:
can_free_echo_skb(priv->netdev, ctx->ndx, NULL);
mcba_usb_free_ctx(ctx);
stats->tx_dropped++;
return NETDEV_TX_OK;
}
/* Send cmd to device */
static void mcba_usb_xmit_cmd(struct mcba_priv *priv,
struct mcba_usb_msg *usb_msg)
{
struct mcba_usb_ctx *ctx = NULL;
int err;
ctx = mcba_usb_get_free_ctx(priv, NULL);
if (!ctx) {
netdev_err(priv->netdev,
"Lack of free ctx. Sending (%d) cmd aborted",
usb_msg->cmd_id);
return;
}
err = mcba_usb_xmit(priv, usb_msg, ctx);
if (err)
netdev_err(priv->netdev, "Failed to send cmd (%d)",
usb_msg->cmd_id);
}
static void mcba_usb_xmit_change_bitrate(struct mcba_priv *priv, u16 bitrate)
{
struct mcba_usb_msg_change_bitrate usb_msg = {
.cmd_id = MBCA_CMD_CHANGE_BIT_RATE
};
put_unaligned_be16(bitrate, &usb_msg.bitrate);
mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
}
static void mcba_usb_xmit_read_fw_ver(struct mcba_priv *priv, u8 pic)
{
struct mcba_usb_msg_fw_ver usb_msg = {
.cmd_id = MBCA_CMD_READ_FW_VERSION,
.pic = pic
};
mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
}
static void mcba_usb_process_can(struct mcba_priv *priv,
struct mcba_usb_msg_can *msg)
{
struct can_frame *cf;
struct sk_buff *skb;
struct net_device_stats *stats = &priv->netdev->stats;
u16 sid;
skb = alloc_can_skb(priv->netdev, &cf);
if (!skb)
return;
sid = get_unaligned_be16(&msg->sid);
if (sid & MCBA_SIDL_EXID_MASK) {
/* SIDH | SIDL | EIDH | EIDL
* 28 - 21 | 20 19 18 x x x 17 16 | 15 - 8 | 7 - 0
*/
cf->can_id = CAN_EFF_FLAG;
/* store 28-18 bits */
cf->can_id |= (sid & 0xffe0) << 13;
/* store 17-16 bits */
cf->can_id |= (sid & 3) << 16;
/* store 15-0 bits */
cf->can_id |= get_unaligned_be16(&msg->eid);
} else {
/* SIDH | SIDL
* 10 - 3 | 2 1 0 x x x x x
*/
cf->can_id = (sid & 0xffe0) >> 5;
}
cf->len = can_cc_dlc2len(msg->dlc & MCBA_DLC_MASK);
if (msg->dlc & MCBA_DLC_RTR_MASK) {
cf->can_id |= CAN_RTR_FLAG;
} else {
memcpy(cf->data, msg->data, cf->len);
stats->rx_bytes += cf->len;
}
stats->rx_packets++;
netif_rx(skb);
}
static void mcba_usb_process_ka_usb(struct mcba_priv *priv,
struct mcba_usb_msg_ka_usb *msg)
{
if (unlikely(priv->usb_ka_first_pass)) {
netdev_info(priv->netdev, "PIC USB version %u.%u\n",
msg->soft_ver_major, msg->soft_ver_minor);
priv->usb_ka_first_pass = false;
}
if (msg->termination_state == MCBA_VER_TERMINATION_ON)
priv->can.termination = MCBA_TERMINATION_ENABLED;
else
priv->can.termination = MCBA_TERMINATION_DISABLED;
}
static u32 convert_can2host_bitrate(struct mcba_usb_msg_ka_can *msg)
{
const u32 bitrate = get_unaligned_be16(&msg->can_bitrate);
if ((bitrate == 33) || (bitrate == 83))
return bitrate * 1000 + 333;
else
return bitrate * 1000;
}
static void mcba_usb_process_ka_can(struct mcba_priv *priv,
struct mcba_usb_msg_ka_can *msg)
{
if (unlikely(priv->can_ka_first_pass)) {
netdev_info(priv->netdev, "PIC CAN version %u.%u\n",
msg->soft_ver_major, msg->soft_ver_minor);
priv->can_ka_first_pass = false;
}
if (unlikely(priv->can_speed_check)) {
const u32 bitrate = convert_can2host_bitrate(msg);
priv->can_speed_check = false;
if (bitrate != priv->can.bittiming.bitrate)
netdev_err(
priv->netdev,
"Wrong bitrate reported by the device (%u). Expected %u",
bitrate, priv->can.bittiming.bitrate);
}
priv->bec.txerr = msg->tx_err_cnt;
priv->bec.rxerr = msg->rx_err_cnt;
if (msg->tx_bus_off)
priv->can.state = CAN_STATE_BUS_OFF;
else if ((priv->bec.txerr > MCBA_CAN_STATE_ERR_PSV_TH) ||
(priv->bec.rxerr > MCBA_CAN_STATE_ERR_PSV_TH))
priv->can.state = CAN_STATE_ERROR_PASSIVE;
else if ((priv->bec.txerr > MCBA_CAN_STATE_WRN_TH) ||
(priv->bec.rxerr > MCBA_CAN_STATE_WRN_TH))
priv->can.state = CAN_STATE_ERROR_WARNING;
}
static void mcba_usb_process_rx(struct mcba_priv *priv,
struct mcba_usb_msg *msg)
{
switch (msg->cmd_id) {
case MBCA_CMD_I_AM_ALIVE_FROM_CAN:
mcba_usb_process_ka_can(priv,
(struct mcba_usb_msg_ka_can *)msg);
break;
case MBCA_CMD_I_AM_ALIVE_FROM_USB:
mcba_usb_process_ka_usb(priv,
(struct mcba_usb_msg_ka_usb *)msg);
break;
case MBCA_CMD_RECEIVE_MESSAGE:
mcba_usb_process_can(priv, (struct mcba_usb_msg_can *)msg);
break;
case MBCA_CMD_NOTHING_TO_SEND:
/* Side effect of communication between PIC_USB and PIC_CAN.
* PIC_CAN is telling us that it has nothing to send
*/
break;
case MBCA_CMD_TRANSMIT_MESSAGE_RSP:
/* Transmission response from the device containing timestamp */
break;
default:
netdev_warn(priv->netdev, "Unsupported msg (0x%X)",
msg->cmd_id);
break;
}
}
/* Callback for reading data from device
*
* Check urb status, call read function and resubmit urb read operation.
*/
static void mcba_usb_read_bulk_callback(struct urb *urb)
{
struct mcba_priv *priv = urb->context;
struct net_device *netdev;
int retval;
int pos = 0;
netdev = priv->netdev;
if (!netif_device_present(netdev))
return;
switch (urb->status) {
case 0: /* success */
break;
case -ENOENT:
case -EPIPE:
case -EPROTO:
case -ESHUTDOWN:
return;
default:
netdev_info(netdev, "Rx URB aborted (%d)\n", urb->status);
goto resubmit_urb;
}
while (pos < urb->actual_length) {
struct mcba_usb_msg *msg;
if (pos + sizeof(struct mcba_usb_msg) > urb->actual_length) {
netdev_err(priv->netdev, "format error\n");
break;
}
msg = (struct mcba_usb_msg *)(urb->transfer_buffer + pos);
mcba_usb_process_rx(priv, msg);
pos += sizeof(struct mcba_usb_msg);
}
resubmit_urb:
usb_fill_bulk_urb(urb, priv->udev,
priv->rx_pipe,
urb->transfer_buffer, MCBA_USB_RX_BUFF_SIZE,
mcba_usb_read_bulk_callback, priv);
usb_anchor_urb(urb, &priv->rx_submitted);
retval = usb_submit_urb(urb, GFP_ATOMIC);
if (!retval)
return;
usb_unanchor_urb(urb);
if (retval == -ENODEV)
netif_device_detach(netdev);
else
netdev_err(netdev, "failed resubmitting read bulk urb: %d\n",
retval);
}
/* Start USB device */
static int mcba_usb_start(struct mcba_priv *priv)
{
struct net_device *netdev = priv->netdev;
int err, i;
mcba_init_ctx(priv);
for (i = 0; i < MCBA_MAX_RX_URBS; i++) {
struct urb *urb = NULL;
u8 *buf;
dma_addr_t buf_dma;
/* create a URB, and a buffer for it */
urb = usb_alloc_urb(0, GFP_KERNEL);
if (!urb) {
err = -ENOMEM;
break;
}
buf = usb_alloc_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
GFP_KERNEL, &buf_dma);
if (!buf) {
netdev_err(netdev, "No memory left for USB buffer\n");
usb_free_urb(urb);
err = -ENOMEM;
break;
}
urb->transfer_dma = buf_dma;
usb_fill_bulk_urb(urb, priv->udev,
priv->rx_pipe,
buf, MCBA_USB_RX_BUFF_SIZE,
mcba_usb_read_bulk_callback, priv);
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
usb_anchor_urb(urb, &priv->rx_submitted);
err = usb_submit_urb(urb, GFP_KERNEL);
if (err) {
usb_unanchor_urb(urb);
usb_free_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
buf, buf_dma);
usb_free_urb(urb);
break;
}
priv->rxbuf[i] = buf;
priv->rxbuf_dma[i] = buf_dma;
/* Drop reference, USB core will take care of freeing it */
usb_free_urb(urb);
}
/* Did we submit any URBs */
if (i == 0) {
netdev_warn(netdev, "couldn't setup read URBs\n");
return err;
}
/* Warn if we've couldn't transmit all the URBs */
if (i < MCBA_MAX_RX_URBS)
netdev_warn(netdev, "rx performance may be slow\n");
mcba_usb_xmit_read_fw_ver(priv, MCBA_VER_REQ_USB);
mcba_usb_xmit_read_fw_ver(priv, MCBA_VER_REQ_CAN);
return err;
}
/* Open USB device */
static int mcba_usb_open(struct net_device *netdev)
{
struct mcba_priv *priv = netdev_priv(netdev);
int err;
/* common open */
err = open_candev(netdev);
if (err)
return err;
priv->can_speed_check = true;
priv->can.state = CAN_STATE_ERROR_ACTIVE;
netif_start_queue(netdev);
return 0;
}
static void mcba_urb_unlink(struct mcba_priv *priv)
{
int i;
usb_kill_anchored_urbs(&priv->rx_submitted);
for (i = 0; i < MCBA_MAX_RX_URBS; ++i)
usb_free_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
priv->rxbuf[i], priv->rxbuf_dma[i]);
usb_kill_anchored_urbs(&priv->tx_submitted);
}
/* Close USB device */
static int mcba_usb_close(struct net_device *netdev)
{
struct mcba_priv *priv = netdev_priv(netdev);
priv->can.state = CAN_STATE_STOPPED;
netif_stop_queue(netdev);
/* Stop polling */
mcba_urb_unlink(priv);
close_candev(netdev);
return 0;
}
/* Set network device mode
*
* Maybe we should leave this function empty, because the device
* set mode variable with open command.
*/
static int mcba_net_set_mode(struct net_device *netdev, enum can_mode mode)
{
return 0;
}
static int mcba_net_get_berr_counter(const struct net_device *netdev,
struct can_berr_counter *bec)
{
struct mcba_priv *priv = netdev_priv(netdev);
bec->txerr = priv->bec.txerr;
bec->rxerr = priv->bec.rxerr;
return 0;
}
static const struct net_device_ops mcba_netdev_ops = {
.ndo_open = mcba_usb_open,
.ndo_stop = mcba_usb_close,
.ndo_start_xmit = mcba_usb_start_xmit,
};
static const struct ethtool_ops mcba_ethtool_ops = {
.get_ts_info = ethtool_op_get_ts_info,
};
/* Microchip CANBUS has hardcoded bittiming values by default.
* This function sends request via USB to change the speed and align bittiming
* values for presentation purposes only
*/
static int mcba_net_set_bittiming(struct net_device *netdev)
{
struct mcba_priv *priv = netdev_priv(netdev);
const u16 bitrate_kbps = priv->can.bittiming.bitrate / 1000;
mcba_usb_xmit_change_bitrate(priv, bitrate_kbps);
return 0;
}
static int mcba_set_termination(struct net_device *netdev, u16 term)
{
struct mcba_priv *priv = netdev_priv(netdev);
struct mcba_usb_msg_termination usb_msg = {
.cmd_id = MBCA_CMD_SETUP_TERMINATION_RESISTANCE
};
if (term == MCBA_TERMINATION_ENABLED)
usb_msg.termination = MCBA_VER_TERMINATION_ON;
else
usb_msg.termination = MCBA_VER_TERMINATION_OFF;
mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
return 0;
}
static int mcba_usb_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct net_device *netdev;
struct mcba_priv *priv;
int err;
struct usb_device *usbdev = interface_to_usbdev(intf);
struct usb_endpoint_descriptor *in, *out;
err = usb_find_common_endpoints(intf->cur_altsetting, &in, &out, NULL, NULL);
if (err) {
dev_err(&intf->dev, "Can't find endpoints\n");
return err;
}
netdev = alloc_candev(sizeof(struct mcba_priv), MCBA_MAX_TX_URBS);
if (!netdev) {
dev_err(&intf->dev, "Couldn't alloc candev\n");
return -ENOMEM;
}
priv = netdev_priv(netdev);
priv->udev = usbdev;
priv->netdev = netdev;
priv->usb_ka_first_pass = true;
priv->can_ka_first_pass = true;
priv->can_speed_check = false;
init_usb_anchor(&priv->rx_submitted);
init_usb_anchor(&priv->tx_submitted);
usb_set_intfdata(intf, priv);
/* Init CAN device */
priv->can.state = CAN_STATE_STOPPED;
priv->can.termination_const = mcba_termination;
priv->can.termination_const_cnt = ARRAY_SIZE(mcba_termination);
priv->can.bitrate_const = mcba_bitrate;
priv->can.bitrate_const_cnt = ARRAY_SIZE(mcba_bitrate);
priv->can.do_set_termination = mcba_set_termination;
priv->can.do_set_mode = mcba_net_set_mode;
priv->can.do_get_berr_counter = mcba_net_get_berr_counter;
priv->can.do_set_bittiming = mcba_net_set_bittiming;
netdev->netdev_ops = &mcba_netdev_ops;
netdev->ethtool_ops = &mcba_ethtool_ops;
netdev->flags |= IFF_ECHO; /* we support local echo */
SET_NETDEV_DEV(netdev, &intf->dev);
err = register_candev(netdev);
if (err) {
netdev_err(netdev, "couldn't register CAN device: %d\n", err);
goto cleanup_free_candev;
}
priv->rx_pipe = usb_rcvbulkpipe(priv->udev, in->bEndpointAddress);
priv->tx_pipe = usb_sndbulkpipe(priv->udev, out->bEndpointAddress);
/* Start USB dev only if we have successfully registered CAN device */
err = mcba_usb_start(priv);
if (err) {
if (err == -ENODEV)
netif_device_detach(priv->netdev);
netdev_warn(netdev, "couldn't start device: %d\n", err);
goto cleanup_unregister_candev;
}
dev_info(&intf->dev, "Microchip CAN BUS Analyzer connected\n");
return 0;
cleanup_unregister_candev:
unregister_candev(priv->netdev);
cleanup_free_candev:
free_candev(netdev);
return err;
}
/* Called by the usb core when driver is unloaded or device is removed */
static void mcba_usb_disconnect(struct usb_interface *intf)
{
struct mcba_priv *priv = usb_get_intfdata(intf);
usb_set_intfdata(intf, NULL);
netdev_info(priv->netdev, "device disconnected\n");
unregister_candev(priv->netdev);
mcba_urb_unlink(priv);
free_candev(priv->netdev);
}
static struct usb_driver mcba_usb_driver = {
.name = MCBA_MODULE_NAME,
.probe = mcba_usb_probe,
.disconnect = mcba_usb_disconnect,
.id_table = mcba_usb_table,
};
module_usb_driver(mcba_usb_driver);
MODULE_AUTHOR("Remigiusz Kołłątaj <remigiusz.kollataj@mobica.com>");
MODULE_DESCRIPTION("SocketCAN driver for Microchip CAN BUS Analyzer Tool");
MODULE_LICENSE("GPL v2");
@@ -0,0 +1,831 @@
// SPDX-License-Identifier: GPL-2.0
/* Nuvoton NCT6694 Socket CANfd driver based on USB interface.
*
* Copyright (C) 2025 Nuvoton Technology Corp.
*/
#include <linux/bitfield.h>
#include <linux/can/dev.h>
#include <linux/can/rx-offload.h>
#include <linux/ethtool.h>
#include <linux/idr.h>
#include <linux/irqdomain.h>
#include <linux/kernel.h>
#include <linux/mfd/nct6694.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/platform_device.h>
#define DEVICE_NAME "nct6694-canfd"
/* USB command module type for NCT6694 CANfd controller.
* This defines the module type used for communication with the NCT6694
* CANfd controller over the USB interface.
*/
#define NCT6694_CANFD_MOD 0x05
/* Command 00h - CAN Setting and Initialization */
#define NCT6694_CANFD_SETTING 0x00
#define NCT6694_CANFD_SETTING_ACTIVE_CTRL1 BIT(0)
#define NCT6694_CANFD_SETTING_ACTIVE_CTRL2 BIT(1)
#define NCT6694_CANFD_SETTING_ACTIVE_NBTP_DBTP BIT(2)
#define NCT6694_CANFD_SETTING_CTRL1_MON BIT(0)
#define NCT6694_CANFD_SETTING_CTRL1_NISO BIT(1)
#define NCT6694_CANFD_SETTING_CTRL1_LBCK BIT(2)
#define NCT6694_CANFD_SETTING_NBTP_NTSEG2 GENMASK(6, 0)
#define NCT6694_CANFD_SETTING_NBTP_NTSEG1 GENMASK(15, 8)
#define NCT6694_CANFD_SETTING_NBTP_NBRP GENMASK(24, 16)
#define NCT6694_CANFD_SETTING_NBTP_NSJW GENMASK(31, 25)
#define NCT6694_CANFD_SETTING_DBTP_DSJW GENMASK(3, 0)
#define NCT6694_CANFD_SETTING_DBTP_DTSEG2 GENMASK(7, 4)
#define NCT6694_CANFD_SETTING_DBTP_DTSEG1 GENMASK(12, 8)
#define NCT6694_CANFD_SETTING_DBTP_DBRP GENMASK(20, 16)
#define NCT6694_CANFD_SETTING_DBTP_TDC BIT(23)
/* Command 01h - CAN Information */
#define NCT6694_CANFD_INFORMATION 0x01
#define NCT6694_CANFD_INFORMATION_SEL 0x00
/* Command 02h - CAN Event */
#define NCT6694_CANFD_EVENT 0x02
#define NCT6694_CANFD_EVENT_SEL(idx, mask) \
((idx ? 0x80 : 0x00) | ((mask) & 0x7F))
#define NCT6694_CANFD_EVENT_MASK GENMASK(5, 0)
#define NCT6694_CANFD_EVT_TX_FIFO_EMPTY BIT(7) /* Read-clear */
#define NCT6694_CANFD_EVT_RX_DATA_LOST BIT(5) /* Read-clear */
#define NCT6694_CANFD_EVT_RX_DATA_IN BIT(7) /* Read-clear */
/* Command 10h - CAN Deliver */
#define NCT6694_CANFD_DELIVER 0x10
#define NCT6694_CANFD_DELIVER_SEL(buf_cnt) \
((buf_cnt) & 0xFF)
/* Command 11h - CAN Receive */
#define NCT6694_CANFD_RECEIVE 0x11
#define NCT6694_CANFD_RECEIVE_SEL(idx, buf_cnt) \
((idx ? 0x80 : 0x00) | ((buf_cnt) & 0x7F))
#define NCT6694_CANFD_FRAME_TAG(idx) (0xC0 | (idx))
#define NCT6694_CANFD_FRAME_FLAG_EFF BIT(0)
#define NCT6694_CANFD_FRAME_FLAG_RTR BIT(1)
#define NCT6694_CANFD_FRAME_FLAG_FD BIT(2)
#define NCT6694_CANFD_FRAME_FLAG_BRS BIT(3)
#define NCT6694_CANFD_FRAME_FLAG_ERR BIT(4)
#define NCT6694_NAPI_WEIGHT 32
enum nct6694_event_err {
NCT6694_CANFD_EVT_ERR_NO_ERROR = 0,
NCT6694_CANFD_EVT_ERR_CRC_ERROR,
NCT6694_CANFD_EVT_ERR_STUFF_ERROR,
NCT6694_CANFD_EVT_ERR_ACK_ERROR,
NCT6694_CANFD_EVT_ERR_FORM_ERROR,
NCT6694_CANFD_EVT_ERR_BIT_ERROR,
NCT6694_CANFD_EVT_ERR_TIMEOUT_ERROR,
NCT6694_CANFD_EVT_ERR_UNKNOWN_ERROR,
};
enum nct6694_event_status {
NCT6694_CANFD_EVT_STS_ERROR_ACTIVE = 0,
NCT6694_CANFD_EVT_STS_ERROR_PASSIVE,
NCT6694_CANFD_EVT_STS_BUS_OFF,
NCT6694_CANFD_EVT_STS_WARNING,
};
struct __packed nct6694_canfd_setting {
__le32 nbr;
__le32 dbr;
u8 active;
u8 reserved[3];
__le16 ctrl1;
__le16 ctrl2;
__le32 nbtp;
__le32 dbtp;
};
struct __packed nct6694_canfd_information {
u8 tx_fifo_cnt;
u8 rx_fifo_cnt;
u8 reserved[2];
__le32 can_clk;
};
struct __packed nct6694_canfd_event {
u8 err;
u8 status;
u8 tx_evt;
u8 rx_evt;
u8 rec;
u8 tec;
u8 reserved[2];
};
struct __packed nct6694_canfd_frame {
u8 tag;
u8 flag;
u8 reserved;
u8 length;
__le32 id;
u8 data[CANFD_MAX_DLEN];
};
struct nct6694_canfd_priv {
struct can_priv can; /* must be the first member */
struct can_rx_offload offload;
struct net_device *ndev;
struct nct6694 *nct6694;
struct workqueue_struct *wq;
struct work_struct tx_work;
struct nct6694_canfd_frame tx;
struct nct6694_canfd_frame rx;
struct nct6694_canfd_event event[2];
struct can_berr_counter bec;
};
static inline struct nct6694_canfd_priv *rx_offload_to_priv(struct can_rx_offload *offload)
{
return container_of(offload, struct nct6694_canfd_priv, offload);
}
static const struct can_bittiming_const nct6694_canfd_bittiming_nominal_const = {
.name = DEVICE_NAME,
.tseg1_min = 1,
.tseg1_max = 256,
.tseg2_min = 1,
.tseg2_max = 128,
.sjw_max = 128,
.brp_min = 1,
.brp_max = 512,
.brp_inc = 1,
};
static const struct can_bittiming_const nct6694_canfd_bittiming_data_const = {
.name = DEVICE_NAME,
.tseg1_min = 1,
.tseg1_max = 32,
.tseg2_min = 1,
.tseg2_max = 16,
.sjw_max = 16,
.brp_min = 1,
.brp_max = 32,
.brp_inc = 1,
};
static void nct6694_canfd_rx_offload(struct can_rx_offload *offload,
struct sk_buff *skb)
{
struct nct6694_canfd_priv *priv = rx_offload_to_priv(offload);
int ret;
ret = can_rx_offload_queue_tail(offload, skb);
if (ret)
priv->ndev->stats.rx_fifo_errors++;
}
static void nct6694_canfd_handle_lost_msg(struct net_device *ndev)
{
struct nct6694_canfd_priv *priv = netdev_priv(ndev);
struct net_device_stats *stats = &ndev->stats;
struct can_frame *cf;
struct sk_buff *skb;
netdev_dbg(ndev, "RX FIFO overflow, message(s) lost.\n");
stats->rx_errors++;
stats->rx_over_errors++;
skb = alloc_can_err_skb(ndev, &cf);
if (!skb)
return;
cf->can_id |= CAN_ERR_CRTL;
cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
nct6694_canfd_rx_offload(&priv->offload, skb);
}
static void nct6694_canfd_handle_rx(struct net_device *ndev, u8 rx_evt)
{
struct net_device_stats *stats = &ndev->stats;
struct nct6694_canfd_priv *priv = netdev_priv(ndev);
struct nct6694_canfd_frame *frame = &priv->rx;
const struct nct6694_cmd_header cmd_hd = {
.mod = NCT6694_CANFD_MOD,
.cmd = NCT6694_CANFD_RECEIVE,
.sel = NCT6694_CANFD_RECEIVE_SEL(ndev->dev_port, 1),
.len = cpu_to_le16(sizeof(*frame))
};
struct sk_buff *skb;
int ret;
ret = nct6694_read_msg(priv->nct6694, &cmd_hd, frame);
if (ret)
return;
if (frame->flag & NCT6694_CANFD_FRAME_FLAG_FD) {
struct canfd_frame *cfd;
skb = alloc_canfd_skb(priv->ndev, &cfd);
if (!skb) {
stats->rx_dropped++;
return;
}
cfd->can_id = le32_to_cpu(frame->id);
cfd->len = canfd_sanitize_len(frame->length);
if (frame->flag & NCT6694_CANFD_FRAME_FLAG_EFF)
cfd->can_id |= CAN_EFF_FLAG;
if (frame->flag & NCT6694_CANFD_FRAME_FLAG_BRS)
cfd->flags |= CANFD_BRS;
if (frame->flag & NCT6694_CANFD_FRAME_FLAG_ERR)
cfd->flags |= CANFD_ESI;
memcpy(cfd->data, frame->data, cfd->len);
} else {
struct can_frame *cf;
skb = alloc_can_skb(priv->ndev, &cf);
if (!skb) {
stats->rx_dropped++;
return;
}
cf->can_id = le32_to_cpu(frame->id);
cf->len = can_cc_dlc2len(frame->length);
if (frame->flag & NCT6694_CANFD_FRAME_FLAG_EFF)
cf->can_id |= CAN_EFF_FLAG;
if (frame->flag & NCT6694_CANFD_FRAME_FLAG_RTR)
cf->can_id |= CAN_RTR_FLAG;
else
memcpy(cf->data, frame->data, cf->len);
}
nct6694_canfd_rx_offload(&priv->offload, skb);
}
static int nct6694_canfd_get_berr_counter(const struct net_device *ndev,
struct can_berr_counter *bec)
{
struct nct6694_canfd_priv *priv = netdev_priv(ndev);
*bec = priv->bec;
return 0;
}
static void nct6694_canfd_handle_state_change(struct net_device *ndev, u8 status)
{
struct nct6694_canfd_priv *priv = netdev_priv(ndev);
enum can_state new_state, rx_state, tx_state;
struct can_berr_counter bec;
struct can_frame *cf;
struct sk_buff *skb;
nct6694_canfd_get_berr_counter(ndev, &bec);
can_state_get_by_berr_counter(ndev, &bec, &tx_state, &rx_state);
new_state = max(tx_state, rx_state);
/* state hasn't changed */
if (new_state == priv->can.state)
return;
skb = alloc_can_err_skb(ndev, &cf);
can_change_state(ndev, cf, tx_state, rx_state);
if (new_state == CAN_STATE_BUS_OFF) {
can_bus_off(ndev);
} else if (cf) {
cf->can_id |= CAN_ERR_CNT;
cf->data[6] = bec.txerr;
cf->data[7] = bec.rxerr;
}
if (skb)
nct6694_canfd_rx_offload(&priv->offload, skb);
}
static void nct6694_canfd_handle_bus_err(struct net_device *ndev, u8 bus_err)
{
struct nct6694_canfd_priv *priv = netdev_priv(ndev);
struct can_frame *cf;
struct sk_buff *skb;
priv->can.can_stats.bus_error++;
skb = alloc_can_err_skb(ndev, &cf);
if (cf)
cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
switch (bus_err) {
case NCT6694_CANFD_EVT_ERR_CRC_ERROR:
netdev_dbg(ndev, "CRC error\n");
ndev->stats.rx_errors++;
if (cf)
cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
break;
case NCT6694_CANFD_EVT_ERR_STUFF_ERROR:
netdev_dbg(ndev, "Stuff error\n");
ndev->stats.rx_errors++;
if (cf)
cf->data[2] |= CAN_ERR_PROT_STUFF;
break;
case NCT6694_CANFD_EVT_ERR_ACK_ERROR:
netdev_dbg(ndev, "Ack error\n");
ndev->stats.tx_errors++;
if (cf) {
cf->can_id |= CAN_ERR_ACK;
cf->data[2] |= CAN_ERR_PROT_TX;
}
break;
case NCT6694_CANFD_EVT_ERR_FORM_ERROR:
netdev_dbg(ndev, "Form error\n");
ndev->stats.rx_errors++;
if (cf)
cf->data[2] |= CAN_ERR_PROT_FORM;
break;
case NCT6694_CANFD_EVT_ERR_BIT_ERROR:
netdev_dbg(ndev, "Bit error\n");
ndev->stats.tx_errors++;
if (cf)
cf->data[2] |= CAN_ERR_PROT_TX | CAN_ERR_PROT_BIT;
break;
default:
break;
}
if (skb)
nct6694_canfd_rx_offload(&priv->offload, skb);
}
static void nct6694_canfd_handle_tx(struct net_device *ndev)
{
struct nct6694_canfd_priv *priv = netdev_priv(ndev);
struct net_device_stats *stats = &ndev->stats;
stats->tx_bytes += can_rx_offload_get_echo_skb_queue_tail(&priv->offload,
0, NULL);
stats->tx_packets++;
netif_wake_queue(ndev);
}
static irqreturn_t nct6694_canfd_irq(int irq, void *data)
{
struct net_device *ndev = data;
struct nct6694_canfd_priv *priv = netdev_priv(ndev);
struct nct6694_canfd_event *event = &priv->event[ndev->dev_port];
const struct nct6694_cmd_header cmd_hd = {
.mod = NCT6694_CANFD_MOD,
.cmd = NCT6694_CANFD_EVENT,
.sel = NCT6694_CANFD_EVENT_SEL(ndev->dev_port, NCT6694_CANFD_EVENT_MASK),
.len = cpu_to_le16(sizeof(priv->event))
};
irqreturn_t handled = IRQ_NONE;
int ret;
ret = nct6694_read_msg(priv->nct6694, &cmd_hd, priv->event);
if (ret < 0)
return handled;
if (event->rx_evt & NCT6694_CANFD_EVT_RX_DATA_IN) {
nct6694_canfd_handle_rx(ndev, event->rx_evt);
handled = IRQ_HANDLED;
}
if (event->rx_evt & NCT6694_CANFD_EVT_RX_DATA_LOST) {
nct6694_canfd_handle_lost_msg(ndev);
handled = IRQ_HANDLED;
}
if (event->status) {
nct6694_canfd_handle_state_change(ndev, event->status);
handled = IRQ_HANDLED;
}
if (event->err != NCT6694_CANFD_EVT_ERR_NO_ERROR) {
if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
nct6694_canfd_handle_bus_err(ndev, event->err);
handled = IRQ_HANDLED;
}
if (event->tx_evt & NCT6694_CANFD_EVT_TX_FIFO_EMPTY) {
nct6694_canfd_handle_tx(ndev);
handled = IRQ_HANDLED;
}
if (handled)
can_rx_offload_threaded_irq_finish(&priv->offload);
priv->bec.rxerr = event->rec;
priv->bec.txerr = event->tec;
return handled;
}
static void nct6694_canfd_tx_work(struct work_struct *work)
{
struct nct6694_canfd_priv *priv = container_of(work,
struct nct6694_canfd_priv,
tx_work);
struct nct6694_canfd_frame *frame = &priv->tx;
struct net_device *ndev = priv->ndev;
struct net_device_stats *stats = &ndev->stats;
struct sk_buff *skb = priv->can.echo_skb[0];
static const struct nct6694_cmd_header cmd_hd = {
.mod = NCT6694_CANFD_MOD,
.cmd = NCT6694_CANFD_DELIVER,
.sel = NCT6694_CANFD_DELIVER_SEL(1),
.len = cpu_to_le16(sizeof(*frame))
};
u32 txid;
int err;
memset(frame, 0, sizeof(*frame));
frame->tag = NCT6694_CANFD_FRAME_TAG(ndev->dev_port);
if (can_is_canfd_skb(skb)) {
struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
if (cfd->flags & CANFD_BRS)
frame->flag |= NCT6694_CANFD_FRAME_FLAG_BRS;
if (cfd->can_id & CAN_EFF_FLAG) {
txid = cfd->can_id & CAN_EFF_MASK;
frame->flag |= NCT6694_CANFD_FRAME_FLAG_EFF;
} else {
txid = cfd->can_id & CAN_SFF_MASK;
}
frame->flag |= NCT6694_CANFD_FRAME_FLAG_FD;
frame->id = cpu_to_le32(txid);
frame->length = canfd_sanitize_len(cfd->len);
memcpy(frame->data, cfd->data, frame->length);
} else {
struct can_frame *cf = (struct can_frame *)skb->data;
if (cf->can_id & CAN_EFF_FLAG) {
txid = cf->can_id & CAN_EFF_MASK;
frame->flag |= NCT6694_CANFD_FRAME_FLAG_EFF;
} else {
txid = cf->can_id & CAN_SFF_MASK;
}
if (cf->can_id & CAN_RTR_FLAG)
frame->flag |= NCT6694_CANFD_FRAME_FLAG_RTR;
else
memcpy(frame->data, cf->data, cf->len);
frame->id = cpu_to_le32(txid);
frame->length = cf->len;
}
err = nct6694_write_msg(priv->nct6694, &cmd_hd, frame);
if (err) {
can_free_echo_skb(ndev, 0, NULL);
stats->tx_dropped++;
stats->tx_errors++;
netif_wake_queue(ndev);
}
}
static netdev_tx_t nct6694_canfd_start_xmit(struct sk_buff *skb,
struct net_device *ndev)
{
struct nct6694_canfd_priv *priv = netdev_priv(ndev);
if (can_dev_dropped_skb(ndev, skb))
return NETDEV_TX_OK;
netif_stop_queue(ndev);
can_put_echo_skb(skb, ndev, 0, 0);
queue_work(priv->wq, &priv->tx_work);
return NETDEV_TX_OK;
}
static int nct6694_canfd_start(struct net_device *ndev)
{
struct nct6694_canfd_priv *priv = netdev_priv(ndev);
const struct can_bittiming *n_bt = &priv->can.bittiming;
const struct can_bittiming *d_bt = &priv->can.fd.data_bittiming;
struct nct6694_canfd_setting *setting __free(kfree) = NULL;
const struct nct6694_cmd_header cmd_hd = {
.mod = NCT6694_CANFD_MOD,
.cmd = NCT6694_CANFD_SETTING,
.sel = ndev->dev_port,
.len = cpu_to_le16(sizeof(*setting))
};
u32 en_tdc;
int ret;
setting = kzalloc_obj(*setting);
if (!setting)
return -ENOMEM;
if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
setting->ctrl1 |= cpu_to_le16(NCT6694_CANFD_SETTING_CTRL1_MON);
if (priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
setting->ctrl1 |= cpu_to_le16(NCT6694_CANFD_SETTING_CTRL1_NISO);
if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
setting->ctrl1 |= cpu_to_le16(NCT6694_CANFD_SETTING_CTRL1_LBCK);
/* Disable clock divider */
setting->ctrl2 = 0;
setting->nbtp = cpu_to_le32(FIELD_PREP(NCT6694_CANFD_SETTING_NBTP_NSJW,
n_bt->sjw - 1) |
FIELD_PREP(NCT6694_CANFD_SETTING_NBTP_NBRP,
n_bt->brp - 1) |
FIELD_PREP(NCT6694_CANFD_SETTING_NBTP_NTSEG2,
n_bt->phase_seg2 - 1) |
FIELD_PREP(NCT6694_CANFD_SETTING_NBTP_NTSEG1,
n_bt->prop_seg + n_bt->phase_seg1 - 1));
if (d_bt->brp <= 2)
en_tdc = NCT6694_CANFD_SETTING_DBTP_TDC;
else
en_tdc = 0;
setting->dbtp = cpu_to_le32(FIELD_PREP(NCT6694_CANFD_SETTING_DBTP_DSJW,
d_bt->sjw - 1) |
FIELD_PREP(NCT6694_CANFD_SETTING_DBTP_DBRP,
d_bt->brp - 1) |
FIELD_PREP(NCT6694_CANFD_SETTING_DBTP_DTSEG2,
d_bt->phase_seg2 - 1) |
FIELD_PREP(NCT6694_CANFD_SETTING_DBTP_DTSEG1,
d_bt->prop_seg + d_bt->phase_seg1 - 1) |
en_tdc);
setting->active = NCT6694_CANFD_SETTING_ACTIVE_CTRL1 |
NCT6694_CANFD_SETTING_ACTIVE_CTRL2 |
NCT6694_CANFD_SETTING_ACTIVE_NBTP_DBTP;
ret = nct6694_write_msg(priv->nct6694, &cmd_hd, setting);
if (ret)
return ret;
priv->can.state = CAN_STATE_ERROR_ACTIVE;
return 0;
}
static void nct6694_canfd_stop(struct net_device *ndev)
{
struct nct6694_canfd_priv *priv = netdev_priv(ndev);
struct nct6694_canfd_setting *setting __free(kfree) = NULL;
const struct nct6694_cmd_header cmd_hd = {
.mod = NCT6694_CANFD_MOD,
.cmd = NCT6694_CANFD_SETTING,
.sel = ndev->dev_port,
.len = cpu_to_le16(sizeof(*setting))
};
/* The NCT6694 cannot be stopped. To ensure safe operation and avoid
* interference, the control mode is set to Listen-Only mode. This
* mode allows the device to monitor bus activity without actively
* participating in communication.
*/
setting = kzalloc_obj(*setting);
if (!setting)
return;
nct6694_read_msg(priv->nct6694, &cmd_hd, setting);
setting->ctrl1 = cpu_to_le16(NCT6694_CANFD_SETTING_CTRL1_MON);
setting->active = NCT6694_CANFD_SETTING_ACTIVE_CTRL1;
nct6694_write_msg(priv->nct6694, &cmd_hd, setting);
priv->can.state = CAN_STATE_STOPPED;
}
static int nct6694_canfd_close(struct net_device *ndev)
{
struct nct6694_canfd_priv *priv = netdev_priv(ndev);
netif_stop_queue(ndev);
nct6694_canfd_stop(ndev);
destroy_workqueue(priv->wq);
free_irq(ndev->irq, ndev);
can_rx_offload_disable(&priv->offload);
close_candev(ndev);
return 0;
}
static int nct6694_canfd_set_mode(struct net_device *ndev, enum can_mode mode)
{
int ret;
switch (mode) {
case CAN_MODE_START:
ret = nct6694_canfd_start(ndev);
if (ret)
return ret;
netif_wake_queue(ndev);
break;
default:
return -EOPNOTSUPP;
}
return ret;
}
static int nct6694_canfd_open(struct net_device *ndev)
{
struct nct6694_canfd_priv *priv = netdev_priv(ndev);
int ret;
ret = open_candev(ndev);
if (ret)
return ret;
can_rx_offload_enable(&priv->offload);
ret = request_threaded_irq(ndev->irq, NULL,
nct6694_canfd_irq, IRQF_ONESHOT,
"nct6694_canfd", ndev);
if (ret) {
netdev_err(ndev, "Failed to request IRQ\n");
goto can_rx_offload_disable;
}
priv->wq = alloc_ordered_workqueue("%s-nct6694_wq",
WQ_FREEZABLE | WQ_MEM_RECLAIM,
ndev->name);
if (!priv->wq) {
ret = -ENOMEM;
goto free_irq;
}
ret = nct6694_canfd_start(ndev);
if (ret)
goto destroy_wq;
netif_start_queue(ndev);
return 0;
destroy_wq:
destroy_workqueue(priv->wq);
free_irq:
free_irq(ndev->irq, ndev);
can_rx_offload_disable:
can_rx_offload_disable(&priv->offload);
close_candev(ndev);
return ret;
}
static const struct net_device_ops nct6694_canfd_netdev_ops = {
.ndo_open = nct6694_canfd_open,
.ndo_stop = nct6694_canfd_close,
.ndo_start_xmit = nct6694_canfd_start_xmit,
};
static const struct ethtool_ops nct6694_canfd_ethtool_ops = {
.get_ts_info = ethtool_op_get_ts_info,
};
static int nct6694_canfd_get_clock(struct nct6694_canfd_priv *priv)
{
struct nct6694_canfd_information *info __free(kfree) = NULL;
static const struct nct6694_cmd_header cmd_hd = {
.mod = NCT6694_CANFD_MOD,
.cmd = NCT6694_CANFD_INFORMATION,
.sel = NCT6694_CANFD_INFORMATION_SEL,
.len = cpu_to_le16(sizeof(*info))
};
int ret;
info = kzalloc_obj(*info);
if (!info)
return -ENOMEM;
ret = nct6694_read_msg(priv->nct6694, &cmd_hd, info);
if (ret)
return ret;
return le32_to_cpu(info->can_clk);
}
static int nct6694_canfd_probe(struct platform_device *pdev)
{
struct nct6694 *nct6694 = dev_get_drvdata(pdev->dev.parent);
struct nct6694_canfd_priv *priv;
struct net_device *ndev;
int port, irq, ret, can_clk;
port = ida_alloc(&nct6694->canfd_ida, GFP_KERNEL);
if (port < 0)
return port;
irq = irq_create_mapping(nct6694->domain,
NCT6694_IRQ_CAN0 + port);
if (!irq) {
ret = -EINVAL;
goto free_ida;
}
ndev = alloc_candev(sizeof(struct nct6694_canfd_priv), 1);
if (!ndev) {
ret = -ENOMEM;
goto dispose_irq;
}
ndev->irq = irq;
ndev->flags |= IFF_ECHO;
ndev->dev_port = port;
ndev->netdev_ops = &nct6694_canfd_netdev_ops;
ndev->ethtool_ops = &nct6694_canfd_ethtool_ops;
priv = netdev_priv(ndev);
priv->nct6694 = nct6694;
priv->ndev = ndev;
can_clk = nct6694_canfd_get_clock(priv);
if (can_clk < 0) {
ret = dev_err_probe(&pdev->dev, can_clk,
"Failed to get clock\n");
goto free_candev;
}
INIT_WORK(&priv->tx_work, nct6694_canfd_tx_work);
priv->can.clock.freq = can_clk;
priv->can.bittiming_const = &nct6694_canfd_bittiming_nominal_const;
priv->can.fd.data_bittiming_const = &nct6694_canfd_bittiming_data_const;
priv->can.do_set_mode = nct6694_canfd_set_mode;
priv->can.do_get_berr_counter = nct6694_canfd_get_berr_counter;
priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING |
CAN_CTRLMODE_FD_NON_ISO;
ret = can_set_static_ctrlmode(ndev, CAN_CTRLMODE_FD);
if (ret)
goto free_candev;
ret = can_rx_offload_add_manual(ndev, &priv->offload,
NCT6694_NAPI_WEIGHT);
if (ret) {
dev_err_probe(&pdev->dev, ret, "Failed to add rx_offload\n");
goto free_candev;
}
platform_set_drvdata(pdev, priv);
SET_NETDEV_DEV(priv->ndev, &pdev->dev);
ret = register_candev(priv->ndev);
if (ret)
goto rx_offload_del;
return 0;
rx_offload_del:
can_rx_offload_del(&priv->offload);
free_candev:
free_candev(ndev);
dispose_irq:
irq_dispose_mapping(irq);
free_ida:
ida_free(&nct6694->canfd_ida, port);
return ret;
}
static void nct6694_canfd_remove(struct platform_device *pdev)
{
struct nct6694_canfd_priv *priv = platform_get_drvdata(pdev);
struct nct6694 *nct6694 = priv->nct6694;
struct net_device *ndev = priv->ndev;
int port = ndev->dev_port;
int irq = ndev->irq;
unregister_candev(ndev);
can_rx_offload_del(&priv->offload);
free_candev(ndev);
irq_dispose_mapping(irq);
ida_free(&nct6694->canfd_ida, port);
}
static struct platform_driver nct6694_canfd_driver = {
.driver = {
.name = DEVICE_NAME,
},
.probe = nct6694_canfd_probe,
.remove = nct6694_canfd_remove,
};
module_platform_driver(nct6694_canfd_driver);
MODULE_DESCRIPTION("USB-CAN FD driver for NCT6694");
MODULE_AUTHOR("Ming Yu <tmyu0@nuvoton.com>");
MODULE_LICENSE("GPL");
@@ -0,0 +1,3 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-$(CONFIG_CAN_PEAK_USB) += peak_usb.o
peak_usb-y = pcan_usb_core.o pcan_usb.o pcan_usb_pro.o pcan_usb_fd.o
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,156 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* CAN driver for PEAK System USB adapters
* Derived from the PCAN project file driver/src/pcan_usb_core.c
*
* Copyright (C) 2003-2025 PEAK System-Technik GmbH
* Author: Stéphane Grosjean <s.grosjean@peak-system.fr>
*
* Many thanks to Klaus Hitschler <klaus.hitschler@gmx.de>
*/
#ifndef PCAN_USB_CORE_H
#define PCAN_USB_CORE_H
/* PEAK-System vendor id. */
#define PCAN_USB_VENDOR_ID 0x0c72
/* supported device ids. */
#define PCAN_USB_PRODUCT_ID 0x000c
#define PCAN_USBPRO_PRODUCT_ID 0x000d
#define PCAN_USBPROFD_PRODUCT_ID 0x0011
#define PCAN_USBFD_PRODUCT_ID 0x0012
#define PCAN_USBCHIP_PRODUCT_ID 0x0013
#define PCAN_USBX6_PRODUCT_ID 0x0014
#define PCAN_USB_DRIVER_NAME "peak_usb"
/* number of urbs that are submitted for rx/tx per channel */
#define PCAN_USB_MAX_RX_URBS 4
#define PCAN_USB_MAX_TX_URBS 10
/* usb adapters maximum channels per usb interface */
#define PCAN_USB_MAX_CHANNEL 2
/* maximum length of the usb commands sent to/received from the devices */
#define PCAN_USB_MAX_CMD_LEN 32
struct peak_usb_device;
/* PEAK-System USB adapter descriptor */
struct peak_usb_adapter {
char *name;
u32 device_id;
u32 ctrlmode_supported;
struct can_clock clock;
const struct can_bittiming_const * const bittiming_const;
const struct can_bittiming_const * const data_bittiming_const;
unsigned int ctrl_count;
const struct ethtool_ops *ethtool_ops;
int (*intf_probe)(struct usb_interface *intf);
int (*dev_init)(struct peak_usb_device *dev);
void (*dev_exit)(struct peak_usb_device *dev);
void (*dev_free)(struct peak_usb_device *dev);
int (*dev_open)(struct peak_usb_device *dev);
int (*dev_close)(struct peak_usb_device *dev);
int (*dev_set_bittiming)(struct peak_usb_device *dev,
struct can_bittiming *bt);
int (*dev_set_data_bittiming)(struct peak_usb_device *dev,
struct can_bittiming *bt);
int (*dev_set_bus)(struct peak_usb_device *dev, u8 onoff);
int (*dev_get_can_channel_id)(struct peak_usb_device *dev, u32 *can_ch_id);
int (*dev_set_can_channel_id)(struct peak_usb_device *dev, u32 can_ch_id);
int (*dev_decode_buf)(struct peak_usb_device *dev, struct urb *urb);
int (*dev_encode_msg)(struct peak_usb_device *dev, struct sk_buff *skb,
u8 *obuf, size_t *size);
int (*dev_start)(struct peak_usb_device *dev);
int (*dev_stop)(struct peak_usb_device *dev);
int (*dev_restart_async)(struct peak_usb_device *dev, struct urb *urb,
u8 *buf);
int (*do_get_berr_counter)(const struct net_device *netdev,
struct can_berr_counter *bec);
u8 ep_msg_in;
u8 ep_msg_out[PCAN_USB_MAX_CHANNEL];
u8 ts_used_bits;
u8 us_per_ts_shift;
u32 us_per_ts_scale;
int rx_buffer_size;
int tx_buffer_size;
int sizeof_dev_private;
};
extern const struct peak_usb_adapter pcan_usb;
extern const struct peak_usb_adapter pcan_usb_pro;
extern const struct peak_usb_adapter pcan_usb_fd;
extern const struct peak_usb_adapter pcan_usb_chip;
extern const struct peak_usb_adapter pcan_usb_pro_fd;
extern const struct peak_usb_adapter pcan_usb_x6;
struct peak_time_ref {
ktime_t tv_host_0, tv_host;
u32 ts_dev_1, ts_dev_2;
u64 ts_total;
u32 tick_count;
const struct peak_usb_adapter *adapter;
};
struct peak_tx_urb_context {
struct peak_usb_device *dev;
u32 echo_index;
struct urb *urb;
};
#define PCAN_USB_STATE_CONNECTED 0x00000001
#define PCAN_USB_STATE_STARTED 0x00000002
/* PEAK-System USB device */
struct peak_usb_device {
struct can_priv can;
const struct peak_usb_adapter *adapter;
unsigned int ctrl_idx;
u32 state;
struct usb_device *udev;
struct net_device *netdev;
atomic_t active_tx_urbs;
struct usb_anchor tx_submitted;
struct peak_tx_urb_context tx_contexts[PCAN_USB_MAX_TX_URBS];
u8 *cmd_buf;
struct usb_anchor rx_submitted;
/* equivalent to the device ID in the Windows API */
u32 can_channel_id;
u8 device_rev;
u8 ep_msg_in;
u8 ep_msg_out;
struct peak_usb_device *prev_siblings;
struct peak_usb_device *next_siblings;
};
void pcan_dump_mem(const char *prompt, const void *p, int l);
/* common timestamp management */
void peak_usb_init_time_ref(struct peak_time_ref *time_ref,
const struct peak_usb_adapter *adapter);
void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now);
void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now);
void peak_usb_get_ts_time(struct peak_time_ref *time_ref, u32 ts, ktime_t *tv);
int peak_usb_netif_rx_64(struct sk_buff *skb, u32 ts_low, u32 ts_high);
void peak_usb_async_complete(struct urb *urb);
void peak_usb_restart_complete(struct peak_usb_device *dev);
int pcan_get_ts_info(struct net_device *dev, struct kernel_ethtool_ts_info *info);
/* common 32-bit CAN channel ID ethtool management */
int peak_usb_get_eeprom_len(struct net_device *netdev);
int peak_usb_get_eeprom(struct net_device *netdev,
struct ethtool_eeprom *eeprom, u8 *data);
int peak_usb_set_eeprom(struct net_device *netdev,
struct ethtool_eeprom *eeprom, u8 *data);
#endif
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,191 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* CAN driver for PEAK System PCAN-USB Pro adapter
* Derived from the PCAN project file driver/src/pcan_usbpro_fw.h
*
* Copyright (C) 2003-2025 PEAK System-Technik GmbH
* Author: Stéphane Grosjean <s.grosjean@peak-system.fr>
*/
#ifndef PCAN_USB_PRO_H
#define PCAN_USB_PRO_H
/*
* USB Vendor request data types
*/
#define PCAN_USBPRO_REQ_INFO 0
#define PCAN_USBPRO_REQ_FCT 2
/* Vendor Request value for XXX_INFO */
#define PCAN_USBPRO_INFO_BL 0
#define PCAN_USBPRO_INFO_FW 1
/* PCAN-USB Pro (FD) Endpoints */
#define PCAN_USBPRO_EP_CMDOUT 1
#define PCAN_USBPRO_EP_CMDIN (PCAN_USBPRO_EP_CMDOUT | USB_DIR_IN)
#define PCAN_USBPRO_EP_MSGOUT_0 2
#define PCAN_USBPRO_EP_MSGIN (PCAN_USBPRO_EP_MSGOUT_0 | USB_DIR_IN)
#define PCAN_USBPRO_EP_MSGOUT_1 3
#define PCAN_USBPRO_EP_UNUSED (PCAN_USBPRO_EP_MSGOUT_1 | USB_DIR_IN)
/* Vendor Request value for XXX_FCT */
#define PCAN_USBPRO_FCT_DRVLD 5 /* tell device driver is loaded */
#define PCAN_USBPRO_FCT_DRVLD_REQ_LEN 16
/* PCAN_USBPRO_INFO_BL vendor request record type */
struct __packed pcan_usb_pro_blinfo {
__le32 ctrl_type;
u8 version[4];
u8 day;
u8 month;
u8 year;
u8 dummy;
__le32 serial_num_hi;
__le32 serial_num_lo;
__le32 hw_type;
__le32 hw_rev;
};
/* PCAN_USBPRO_INFO_FW vendor request record type */
struct __packed pcan_usb_pro_fwinfo {
__le32 ctrl_type;
u8 version[4];
u8 day;
u8 month;
u8 year;
u8 dummy;
__le32 fw_type;
};
/*
* USB Command record types
*/
#define PCAN_USBPRO_SETBTR 0x02
#define PCAN_USBPRO_SETBUSACT 0x04
#define PCAN_USBPRO_SETSILENT 0x05
#define PCAN_USBPRO_SETDEVID 0x06
#define PCAN_USBPRO_SETFILTR 0x0a
#define PCAN_USBPRO_SETTS 0x10
#define PCAN_USBPRO_GETDEVID 0x12
#define PCAN_USBPRO_SETLED 0x1C
#define PCAN_USBPRO_RXMSG8 0x80
#define PCAN_USBPRO_RXMSG4 0x81
#define PCAN_USBPRO_RXMSG0 0x82
#define PCAN_USBPRO_RXRTR 0x83
#define PCAN_USBPRO_RXSTATUS 0x84
#define PCAN_USBPRO_RXTS 0x85
#define PCAN_USBPRO_TXMSG8 0x41
#define PCAN_USBPRO_TXMSG4 0x42
#define PCAN_USBPRO_TXMSG0 0x43
/* record structures */
struct __packed pcan_usb_pro_btr {
u8 data_type;
u8 channel;
__le16 dummy;
__le32 CCBT;
};
struct __packed pcan_usb_pro_busact {
u8 data_type;
u8 channel;
__le16 onoff;
};
struct __packed pcan_usb_pro_silent {
u8 data_type;
u8 channel;
__le16 onoff;
};
struct __packed pcan_usb_pro_filter {
u8 data_type;
u8 dummy;
__le16 filter_mode;
};
struct __packed pcan_usb_pro_setts {
u8 data_type;
u8 dummy;
__le16 mode;
};
struct __packed pcan_usb_pro_devid {
u8 data_type;
u8 channel;
__le16 dummy;
__le32 dev_num;
};
#define PCAN_USBPRO_LED_DEVICE 0x00
#define PCAN_USBPRO_LED_BLINK_FAST 0x01
#define PCAN_USBPRO_LED_BLINK_SLOW 0x02
#define PCAN_USBPRO_LED_ON 0x03
#define PCAN_USBPRO_LED_OFF 0x04
struct __packed pcan_usb_pro_setled {
u8 data_type;
u8 channel;
__le16 mode;
__le32 timeout;
};
struct __packed pcan_usb_pro_rxmsg {
u8 data_type;
u8 client;
u8 flags;
u8 len;
__le32 ts32;
__le32 id;
u8 data[8];
};
#define PCAN_USBPRO_STATUS_ERROR 0x0001
#define PCAN_USBPRO_STATUS_BUS 0x0002
#define PCAN_USBPRO_STATUS_OVERRUN 0x0004
#define PCAN_USBPRO_STATUS_QOVERRUN 0x0008
struct __packed pcan_usb_pro_rxstatus {
u8 data_type;
u8 channel;
__le16 status;
__le32 ts32;
__le32 err_frm;
};
struct __packed pcan_usb_pro_rxts {
u8 data_type;
u8 dummy[3];
__le32 ts64[2];
};
struct __packed pcan_usb_pro_txmsg {
u8 data_type;
u8 client;
u8 flags;
u8 len;
__le32 id;
u8 data[8];
};
union pcan_usb_pro_rec {
u8 data_type;
struct pcan_usb_pro_btr btr;
struct pcan_usb_pro_busact bus_act;
struct pcan_usb_pro_silent silent_mode;
struct pcan_usb_pro_filter filter_mode;
struct pcan_usb_pro_setts ts;
struct pcan_usb_pro_devid dev_id;
struct pcan_usb_pro_setled set_led;
struct pcan_usb_pro_rxmsg rx_msg;
struct pcan_usb_pro_rxstatus rx_status;
struct pcan_usb_pro_rxts rx_ts;
struct pcan_usb_pro_txmsg tx_msg;
};
int pcan_usb_pro_probe(struct usb_interface *intf);
int pcan_usb_pro_send_req(struct peak_usb_device *dev, int req_id,
int req_value, void *req_addr, int req_size);
void pcan_usb_pro_restart_complete(struct urb *urb);
#endif
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff