get:
Show a patch.

patch:
Update a patch.

put:
Update a patch.

GET /api/patches/1396158/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 1396158,
    "url": "http://patchwork.ozlabs.org/api/patches/1396158/?format=api",
    "web_url": "http://patchwork.ozlabs.org/project/netdev/patch/20201107141837.277708-1-mailhol.vincent@wanadoo.fr/",
    "project": {
        "id": 7,
        "url": "http://patchwork.ozlabs.org/api/projects/7/?format=api",
        "name": "Linux network development",
        "link_name": "netdev",
        "list_id": "netdev.vger.kernel.org",
        "list_email": "netdev@vger.kernel.org",
        "web_url": null,
        "scm_url": null,
        "webscm_url": null,
        "list_archive_url": "",
        "list_archive_url_format": "",
        "commit_url_format": ""
    },
    "msgid": "<20201107141837.277708-1-mailhol.vincent@wanadoo.fr>",
    "list_archive_url": null,
    "date": "2020-11-07T14:18:09",
    "name": "[v5] can: usb: etas_es58X: add support for ETAS ES58X CAN USB interfaces",
    "commit_ref": null,
    "pull_url": null,
    "state": "awaiting-upstream",
    "archived": false,
    "hash": "f9343f3289a4b98ba532bb2dbc195375b4a3f7fe",
    "submitter": {
        "id": 80206,
        "url": "http://patchwork.ozlabs.org/api/people/80206/?format=api",
        "name": "Vincent Mailhol",
        "email": "mailhol.vincent@wanadoo.fr"
    },
    "delegate": {
        "id": 34,
        "url": "http://patchwork.ozlabs.org/api/users/34/?format=api",
        "username": "davem",
        "first_name": "David",
        "last_name": "Miller",
        "email": "davem@davemloft.net"
    },
    "mbox": "http://patchwork.ozlabs.org/project/netdev/patch/20201107141837.277708-1-mailhol.vincent@wanadoo.fr/mbox/",
    "series": [
        {
            "id": 212860,
            "url": "http://patchwork.ozlabs.org/api/series/212860/?format=api",
            "web_url": "http://patchwork.ozlabs.org/project/netdev/list/?series=212860",
            "date": "2020-11-07T14:18:09",
            "name": "[v5] can: usb: etas_es58X: add support for ETAS ES58X CAN USB interfaces",
            "version": 5,
            "mbox": "http://patchwork.ozlabs.org/series/212860/mbox/"
        }
    ],
    "comments": "http://patchwork.ozlabs.org/api/patches/1396158/comments/",
    "check": "success",
    "checks": "http://patchwork.ozlabs.org/api/patches/1396158/checks/",
    "tags": {},
    "related": [],
    "headers": {
        "Return-Path": "<netdev-owner@vger.kernel.org>",
        "X-Original-To": "patchwork-incoming-netdev@ozlabs.org",
        "Delivered-To": "patchwork-incoming-netdev@ozlabs.org",
        "Authentication-Results": [
            "ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=23.128.96.18; helo=vger.kernel.org;\n envelope-from=netdev-owner@vger.kernel.org; receiver=<UNKNOWN>)",
            "ozlabs.org;\n dmarc=none (p=none dis=none) header.from=wanadoo.fr"
        ],
        "Received": [
            "from vger.kernel.org (vger.kernel.org [23.128.96.18])\n\tby ozlabs.org (Postfix) with ESMTP id 4CSzv73MXxz9sSf\n\tfor <patchwork-incoming-netdev@ozlabs.org>;\n Sun,  8 Nov 2020 01:20:51 +1100 (AEDT)",
            "(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n        id S1728130AbgKGOUd (ORCPT\n        <rfc822;patchwork-incoming-netdev@ozlabs.org>);\n        Sat, 7 Nov 2020 09:20:33 -0500",
            "from smtp07.smtpout.orange.fr ([80.12.242.129]:54740 \"EHLO\n        smtp.smtpout.orange.fr\" rhost-flags-OK-OK-OK-OK) by vger.kernel.org\n        with ESMTP id S1727298AbgKGOUc (ORCPT\n        <rfc822;netdev@vger.kernel.org>); Sat, 7 Nov 2020 09:20:32 -0500",
            "from tomoyo.flets-east.jp ([153.230.197.127])\n        by mwinf5d13 with ME\n        id pSKV2300Z2lQRaH03SKf79; Sat, 07 Nov 2020 15:19:48 +0100"
        ],
        "X-ME-Helo": "tomoyo.flets-east.jp",
        "X-ME-Auth": "bWFpbGhvbC52aW5jZW50QHdhbmFkb28uZnI=",
        "X-ME-Date": "Sat, 07 Nov 2020 15:19:48 +0100",
        "X-ME-IP": "153.230.197.127",
        "From": "Vincent Mailhol <mailhol.vincent@wanadoo.fr>",
        "To": "linux-can@vger.kernel.org, Marc Kleine-Budde <mkl@pengutronix.de>",
        "Cc": "Oliver Hartkopp <socketcan@hartkopp.net>,\n        Vincent Mailhol <mailhol.vincent@wanadoo.fr>,\n        Arunachalam Santhanam <arunachalam.santhanam@in.bosch.com>,\n        Wolfgang Grandegger <wg@grandegger.com>,\n        \"David S. Miller\" <davem@davemloft.net>,\n        Jakub Kicinski <kuba@kernel.org>,\n        Masahiro Yamada <masahiroy@kernel.org>,\n        linux-kernel@vger.kernel.org (open list),\n        netdev@vger.kernel.org (open list:NETWORKING DRIVERS)",
        "Subject": "[PATCH v5] can: usb: etas_es58X: add support for ETAS ES58X CAN USB\n interfaces",
        "Date": "Sat,  7 Nov 2020 23:18:09 +0900",
        "Message-Id": "<20201107141837.277708-1-mailhol.vincent@wanadoo.fr>",
        "X-Mailer": "git-send-email 2.26.2",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Precedence": "bulk",
        "List-ID": "<netdev.vger.kernel.org>",
        "X-Mailing-List": "netdev@vger.kernel.org"
    },
    "content": "This driver supports the ES581.4, ES582.1 and ES584.1 interfaces from\nETAS GmbH (https://www.etas.com/en/products/es58x.php).\n\nCo-developed-by: Arunachalam Santhanam <arunachalam.santhanam@in.bosch.com>\nSigned-off-by: Arunachalam Santhanam <arunachalam.santhanam@in.bosch.com>\nSigned-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>\n--\n\n*Warning*: below patch series are needed to compile:\n * https://lore.kernel.org/linux-can/20201029083218.41505-1-socketcan@hartkopp.net/\n * https://lore.kernel.org/linux-can/20201029133123.2196-1-socketcan@hartkopp.net/\n\nChanges in v5 (2020-11-07):\n  - Add support for DLC greater than 8.\n  - All other patches from the previous series were either accepted or\n    dismissed. As such, this is not a serie any more but a single\n    patch.\n\nChanges in v4 (2020-10-17):\n  - Remove struct es58x_abstracted_can_frame.\n    branch is already done inside net/can/af_can.c#can_send().\n  - Fix formating (spaces, comment style).\n  - Transform macros into static inline functions when possible.\n  - Fix the ctrlmode_supported flags in es581_4.c and removed\n    misleading comments in enum es58x_samples_per_bit.\n  - Rename enums according to the type.\n  - Remove function es58x_can_put_echo_skb().\nReference: https://lkml.org/lkml/2020/10/10/53\n\nChanges in v3 (2020-10-03):\n  - Remove all the calls to likely() and unlikely().\nReference: https://lkml.org/lkml/2020/9/30/995\n\nChanges in v2 (2020-09-30):\n  - Fixed -W1 warnings (v1 was tested with GCC -WExtra but not with -W1).\n---\n drivers/net/can/usb/Kconfig                 |    9 +\n drivers/net/can/usb/Makefile                |    1 +\n drivers/net/can/usb/etas_es58x/Makefile     |    3 +\n drivers/net/can/usb/etas_es58x/es581_4.c    |  552 ++++\n drivers/net/can/usb/etas_es58x/es581_4.h    |  206 ++\n drivers/net/can/usb/etas_es58x/es58x_core.c | 2627 +++++++++++++++++++\n drivers/net/can/usb/etas_es58x/es58x_core.h |  712 +++++\n drivers/net/can/usb/etas_es58x/es58x_fd.c   |  657 +++++\n drivers/net/can/usb/etas_es58x/es58x_fd.h   |  242 ++\n 9 files changed, 5009 insertions(+)\n create mode 100644 drivers/net/can/usb/etas_es58x/Makefile\n create mode 100644 drivers/net/can/usb/etas_es58x/es581_4.c\n create mode 100644 drivers/net/can/usb/etas_es58x/es581_4.h\n create mode 100644 drivers/net/can/usb/etas_es58x/es58x_core.c\n create mode 100644 drivers/net/can/usb/etas_es58x/es58x_core.h\n create mode 100644 drivers/net/can/usb/etas_es58x/es58x_fd.c\n create mode 100644 drivers/net/can/usb/etas_es58x/es58x_fd.h",
    "diff": "diff --git a/drivers/net/can/usb/Kconfig b/drivers/net/can/usb/Kconfig\nindex bcb331b0c958..ffb0cd084571 100644\n--- a/drivers/net/can/usb/Kconfig\n+++ b/drivers/net/can/usb/Kconfig\n@@ -20,6 +20,15 @@ config CAN_ESD_USB2\n \t  This driver supports the CAN-USB/2 interface\n \t  from esd electronic system design gmbh (http://www.esd.eu).\n \n+config CAN_ETAS_ES58X\n+\ttristate \"ETAS ES58X CAN/USB interfaces\"\n+\thelp\n+\t  This driver supports the ES581.4, ES582.1 and ES584.1 interfaces\n+\t  from ETAS GmbH (https://www.etas.com/en/products/es58x.php).\n+\n+\t  To compile this driver as a module, choose M here: the module\n+\t  will be called etas_es58x.\n+\n config CAN_GS_USB\n \ttristate \"Geschwister Schneider UG interfaces\"\n \thelp\ndiff --git a/drivers/net/can/usb/Makefile b/drivers/net/can/usb/Makefile\nindex aa0f17c0b2ed..748cf31a0d53 100644\n--- a/drivers/net/can/usb/Makefile\n+++ b/drivers/net/can/usb/Makefile\n@@ -6,6 +6,7 @@\n obj-$(CONFIG_CAN_8DEV_USB) += usb_8dev.o\n obj-$(CONFIG_CAN_EMS_USB) += ems_usb.o\n obj-$(CONFIG_CAN_ESD_USB2) += esd_usb2.o\n+obj-$(CONFIG_CAN_ETAS_ES58X) += etas_es58x/\n obj-$(CONFIG_CAN_GS_USB) += gs_usb.o\n obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb/\n obj-$(CONFIG_CAN_MCBA_USB) += mcba_usb.o\ndiff --git a/drivers/net/can/usb/etas_es58x/Makefile b/drivers/net/can/usb/etas_es58x/Makefile\nnew file mode 100644\nindex 000000000000..a129b4aa0215\n--- /dev/null\n+++ b/drivers/net/can/usb/etas_es58x/Makefile\n@@ -0,0 +1,3 @@\n+# SPDX-License-Identifier: GPL-2.0\n+obj-$(CONFIG_CAN_ETAS_ES58X) += etas_es58x.o\n+etas_es58x-y = es58x_core.o es581_4.o es58x_fd.o\ndiff --git a/drivers/net/can/usb/etas_es58x/es581_4.c b/drivers/net/can/usb/etas_es58x/es581_4.c\nnew file mode 100644\nindex 000000000000..ded85b06c31a\n--- /dev/null\n+++ b/drivers/net/can/usb/etas_es58x/es581_4.c\n@@ -0,0 +1,552 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+/* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.\n+ *\n+ * File es581_4.c: Adds support to ETAS ES581.4.\n+ *\n+ * Copyright (C) 2019 Robert Bosch Engineering and Business\n+ * Solutions. All rights reserved.\n+ * Copyright (C) 2020 ETAS K.K.. All rights reserved.\n+ */\n+\n+#include <linux/kernel.h>\n+#include <asm/unaligned.h>\n+\n+#include \"es58x_core.h\"\n+#include \"es581_4.h\"\n+\n+/**\n+ * es58x_sizeof_rx_tx_msg() - Calculate the actual length of the\n+ *\tstructure of a rx or tx message.\n+ * @msg: message of variable length, must have a dlc field.\n+ *\n+ * Even if RTR frames have actually no payload, the ES58X devices\n+ * still expect it. Must be a macro in order to retrieve the actual\n+ * size using offsetof() and typeof().\n+ *\n+ * Return: length of the message.\n+ */\n+#define es581_4_sizeof_rx_tx_msg(msg)\t\t\t\t\\\n+\toffsetof(typeof(msg), data[can_get_cc_len((msg).dlc)])\n+\n+static u16 es581_4_get_msg_len(const union es58x_urb_cmd *urb_cmd)\n+{\n+\treturn get_unaligned_le16(&urb_cmd->es581_4_urb_cmd.msg_len);\n+}\n+\n+static int es581_4_rx_loopback_msg(struct es58x_device *es58x_dev,\n+\t\t\t\t   const struct es581_4_urb_cmd\n+\t\t\t\t   *es581_4_urb_cmd)\n+{\n+\tstruct net_device *netdev;\n+\tconst struct es581_4_bulk_rx_loopback_msg *bulk_rx_loopback_msg;\n+\tconst struct es581_4_rx_loopback_msg *rx_loopback_msg;\n+\tu64 *tstamps = es58x_dev->timestamps;\n+\tu16 msg_len;\n+\tu32 first_packet_idx, packet_idx;\n+\tunsigned int dropped = 0;\n+\tint i, num_element, ret;\n+\n+\tbulk_rx_loopback_msg = &es581_4_urb_cmd->bulk_rx_loopback_msg;\n+\tmsg_len = get_unaligned_le16(&es581_4_urb_cmd->msg_len) -\n+\t    sizeof(bulk_rx_loopback_msg->channel_no);\n+\tnum_element =\n+\t    es58x_msg_num_element(es58x_dev->dev,\n+\t\t\t\t  bulk_rx_loopback_msg->rx_loopback_msg,\n+\t\t\t\t  msg_len);\n+\tif (num_element <= 0)\n+\t\treturn num_element;\n+\n+\tret = es58x_get_netdev(es58x_dev, bulk_rx_loopback_msg->channel_no,\n+\t\t\t       &netdev);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\trx_loopback_msg = &bulk_rx_loopback_msg->rx_loopback_msg[0];\n+\tfirst_packet_idx = get_unaligned_le32(&rx_loopback_msg->packet_idx);\n+\tpacket_idx = first_packet_idx;\n+\tfor (i = 0; i < num_element; i++) {\n+\t\trx_loopback_msg = &bulk_rx_loopback_msg->rx_loopback_msg[i];\n+\t\tif (get_unaligned_le32(&rx_loopback_msg->packet_idx)\n+\t\t    == packet_idx - 1) {\n+\t\t\tif (net_ratelimit())\n+\t\t\t\tnetdev_warn(netdev,\n+\t\t\t\t\t    \"Received loopback packet idx %u twice\\n\",\n+\t\t\t\t\t    packet_idx - 1);\n+\t\t\tdropped++;\n+\t\t\tcontinue;\n+\t\t}\n+\t\tif (get_unaligned_le32(&rx_loopback_msg->packet_idx)\n+\t\t    != packet_idx) {\n+\t\t\tnetdev_err(netdev, \"Packet idx jumped from %u to %u\\n\",\n+\t\t\t\t   packet_idx - 1, rx_loopback_msg->packet_idx);\n+\t\t\treturn -EBADMSG;\n+\t\t}\n+\n+\t\ttstamps[i] = get_unaligned_le64(&rx_loopback_msg->timestamp);\n+\t\tpacket_idx++;\n+\t}\n+\n+\tnetdev->stats.tx_dropped += dropped;\n+\treturn es58x_can_get_echo_skb(netdev, first_packet_idx,\n+\t\t\t\t      tstamps, num_element - dropped);\n+}\n+\n+static int es581_4_rx_can_msg(struct es58x_device *es58x_dev,\n+\t\t\t      const struct es581_4_urb_cmd *es581_4_urb_cmd,\n+\t\t\t      u16 msg_len)\n+{\n+\tconst struct device *dev = es58x_dev->dev;\n+\tstruct net_device *netdev;\n+\tint pkts, num_element, channel_no, ret;\n+\n+\tnum_element = es58x_msg_num_element(dev, es581_4_urb_cmd->rx_can_msg,\n+\t\t\t\t\t    msg_len);\n+\tif (num_element <= 0)\n+\t\treturn num_element;\n+\n+\tchannel_no = es581_4_urb_cmd->rx_can_msg[0].channel_no;\n+\tret = es58x_get_netdev(es58x_dev, channel_no, &netdev);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tif (!netif_running(netdev)) {\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_info(netdev,\n+\t\t\t\t    \"%s: %s is down, dropping %d rx packets\\n\",\n+\t\t\t\t    __func__, netdev->name, num_element);\n+\t\tnetdev->stats.rx_dropped += num_element;\n+\t\treturn 0;\n+\t}\n+\n+\tfor (pkts = 0; pkts < num_element; pkts++) {\n+\t\tconst struct es581_4_rx_can_msg *rx_can_msg =\n+\t\t    &es581_4_urb_cmd->rx_can_msg[pkts];\n+\t\tu64 tstamp = get_unaligned_le64(&rx_can_msg->timestamp);\n+\t\tcanid_t can_id = get_unaligned_le32(&rx_can_msg->can_id);\n+\n+\t\tif (channel_no != rx_can_msg->channel_no) {\n+\t\t\tnetdev_err(netdev,\n+\t\t\t\t   \"%s: channel_no changed from %d to %d\\n\",\n+\t\t\t\t   __func__,\n+\t\t\t\t   channel_no, rx_can_msg->channel_no);\n+\t\t\treturn -EBADMSG;\n+\t\t}\n+\n+\t\tret = es58x_rx_can_msg(netdev, tstamp, rx_can_msg->data,\n+\t\t\t\t       can_id, rx_can_msg->flags,\n+\t\t\t\t       rx_can_msg->dlc);\n+\t\tif (ret)\n+\t\t\tbreak;\n+\t}\n+\n+\treturn ret;\n+}\n+\n+static int es581_4_rx_err_msg(struct es58x_device *es58x_dev,\n+\t\t\t      const struct es581_4_rx_err_msg *rx_err_msg)\n+{\n+\tstruct net_device *netdev;\n+\tenum es58x_err error = get_unaligned_le32(&rx_err_msg->error);\n+\tint ret;\n+\n+\tret = es58x_get_netdev(es58x_dev, rx_err_msg->channel_no, &netdev);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\treturn es58x_rx_err_msg(netdev, error, 0,\n+\t\t\t\tget_unaligned_le64(&rx_err_msg->timestamp));\n+}\n+\n+static int es581_4_rx_event_msg(struct es58x_device *es58x_dev,\n+\t\t\t\tconst struct es581_4_rx_event_msg *rx_event_msg)\n+{\n+\tstruct net_device *netdev;\n+\tenum es58x_event event = get_unaligned_le32(&rx_event_msg->event);\n+\tint ret;\n+\n+\tret = es58x_get_netdev(es58x_dev, rx_event_msg->channel_no, &netdev);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\treturn es58x_rx_err_msg(netdev, 0, event,\n+\t\t\t\tget_unaligned_le64(&rx_event_msg->timestamp));\n+}\n+\n+static int es581_4_rx_cmd_ret_u32(struct es58x_device *es58x_dev,\n+\t\t\t\t  const struct es581_4_urb_cmd *es581_4_urb_cmd,\n+\t\t\t\t  enum es58x_ret_type ret_type)\n+{\n+\tstruct net_device *netdev;\n+\tconst struct es581_4_rx_dev_ret *rx_dev_ret;\n+\tu16 msg_len = get_unaligned_le16(&es581_4_urb_cmd->msg_len);\n+\tenum es58x_ret_u32 rx_cmd_ret_u32;\n+\tint ret;\n+\n+\tret = es58x_check_msg_len(es58x_dev->dev,\n+\t\t\t\t  es581_4_urb_cmd->rx_dev_ret, msg_len);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\trx_dev_ret = &es581_4_urb_cmd->rx_dev_ret;\n+\n+\tret = es58x_get_netdev(es58x_dev, rx_dev_ret->channel_no, &netdev);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\trx_cmd_ret_u32 = get_unaligned_le32(&rx_dev_ret->rx_cmd_ret_le32);\n+\treturn es58x_rx_cmd_ret_u32(netdev, ret_type, rx_cmd_ret_u32);\n+}\n+\n+static int es581_4_tx_ack_msg(struct es58x_device *es58x_dev,\n+\t\t\t      const struct es581_4_urb_cmd *es581_4_urb_cmd)\n+{\n+\tstruct net_device *netdev;\n+\tconst struct es581_4_tx_ack_msg *tx_ack_msg;\n+\tu16 msg_len = get_unaligned_le16(&es581_4_urb_cmd->msg_len);\n+\tint ret;\n+\n+\ttx_ack_msg = &es581_4_urb_cmd->tx_ack_msg;\n+\tret = es58x_check_msg_len(es58x_dev->dev, *tx_ack_msg, msg_len);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tif (tx_ack_msg->rx_dev_ret_u8 != ES58X_RET_U8_OK)\n+\t\treturn es58x_rx_dev_ret_u8(es58x_dev->dev,\n+\t\t\t\t\t   ES58X_RET_TYPE_TX_MSG,\n+\t\t\t\t\t   tx_ack_msg->rx_dev_ret_u8);\n+\n+\tret = es58x_get_netdev(es58x_dev, tx_ack_msg->channel_no, &netdev);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\treturn\n+\t    es58x_tx_ack_msg(netdev,\n+\t\t\t     get_unaligned_le16(&tx_ack_msg->tx_free_entries),\n+\t\t\t     ES58X_RET_U32_OK);\n+}\n+\n+static int es581_4_dispatch_rx_cmd(struct es58x_device *es58x_dev,\n+\t\t\t\t   const struct es581_4_urb_cmd\n+\t\t\t\t   *es581_4_urb_cmd)\n+{\n+\tconst struct device *dev = es58x_dev->dev;\n+\tu16 msg_len = get_unaligned_le16(&es581_4_urb_cmd->msg_len);\n+\tenum es581_4_rx_type rx_type = es581_4_urb_cmd->rx_can_msg[0].rx_type;\n+\tint ret = 0;\n+\n+\tswitch (rx_type) {\n+\tcase ES581_4_RX_TYPE_MESSAGE:\n+\t\treturn es581_4_rx_can_msg(es58x_dev, es581_4_urb_cmd, msg_len);\n+\n+\tcase ES581_4_RX_TYPE_ERROR:\n+\t\tret = es58x_check_msg_len(dev, es581_4_urb_cmd->rx_err_msg,\n+\t\t\t\t\t  msg_len);\n+\t\tif (ret < 0)\n+\t\t\treturn ret;\n+\t\treturn es581_4_rx_err_msg(es58x_dev,\n+\t\t\t\t\t  &es581_4_urb_cmd->rx_err_msg);\n+\n+\tcase ES581_4_RX_TYPE_EVENT:\n+\t\tret = es58x_check_msg_len(dev, es581_4_urb_cmd->rx_event_msg,\n+\t\t\t\t\t  msg_len);\n+\t\tif (ret < 0)\n+\t\t\treturn ret;\n+\t\treturn es581_4_rx_event_msg(es58x_dev,\n+\t\t\t\t\t    &es581_4_urb_cmd->rx_event_msg);\n+\n+\tdefault:\n+\t\tdev_err(dev, \"%s: Unknown rx_type 0x%02X\\n\", __func__, rx_type);\n+\t\treturn -EBADRQC;\n+\t}\n+}\n+\n+static int es581_4_handle_urb_cmd(struct es58x_device *es58x_dev,\n+\t\t\t\t  const union es58x_urb_cmd *urb_cmd)\n+{\n+\tconst struct es581_4_urb_cmd *es581_4_urb_cmd;\n+\tconst struct device *dev = es58x_dev->dev;\n+\tu16 msg_len = es581_4_get_msg_len(urb_cmd);\n+\tint ret;\n+\n+\tes581_4_urb_cmd = &urb_cmd->es581_4_urb_cmd;\n+\n+\tif (es581_4_urb_cmd->cmd_type != ES581_4_CAN_COMMAND_TYPE) {\n+\t\tdev_err(dev, \"%s: Unknown command type (0x%02X)\\n\",\n+\t\t\t__func__, es581_4_urb_cmd->cmd_type);\n+\t\treturn -EBADRQC;\n+\t}\n+\n+\tswitch ((enum es581_4_cmd_id)es581_4_urb_cmd->cmd_id) {\n+\tcase ES581_4_CMD_ID_SET_BITTIMING:\n+\t\treturn es581_4_rx_cmd_ret_u32(es58x_dev, es581_4_urb_cmd,\n+\t\t\t\t\t      ES58X_RET_TYPE_SET_BITTIMING);\n+\n+\tcase ES581_4_CMD_ID_ENABLE_CHANNEL:\n+\t\treturn es581_4_rx_cmd_ret_u32(es58x_dev, es581_4_urb_cmd,\n+\t\t\t\t\t      ES58X_RET_TYPE_ENABLE_CHANNEL);\n+\n+\tcase ES581_4_CMD_ID_TX_MSG:\n+\t\treturn es581_4_tx_ack_msg(es58x_dev, es581_4_urb_cmd);\n+\n+\tcase ES581_4_CMD_ID_RX_MSG:\n+\t\treturn es581_4_dispatch_rx_cmd(es58x_dev, es581_4_urb_cmd);\n+\n+\tcase ES581_4_CMD_ID_RESET_RX:\n+\t\tret = es581_4_rx_cmd_ret_u32(es58x_dev, es581_4_urb_cmd,\n+\t\t\t\t\t     ES58X_RET_TYPE_RESET_RX);\n+\t\treturn ret;\n+\n+\tcase ES581_4_CMD_ID_RESET_TX:\n+\t\tret = es581_4_rx_cmd_ret_u32(es58x_dev, es581_4_urb_cmd,\n+\t\t\t\t\t     ES58X_RET_TYPE_RESET_TX);\n+\t\treturn ret;\n+\n+\tcase ES581_4_CMD_ID_DISABLE_CHANNEL:\n+\t\treturn es581_4_rx_cmd_ret_u32(es58x_dev, es581_4_urb_cmd,\n+\t\t\t\t\t      ES58X_RET_TYPE_DISABLE_CHANNEL);\n+\n+\tcase ES581_4_CMD_ID_TIMESTAMP:\n+\t\tret = es58x_check_msg_len(dev, es581_4_urb_cmd->timestamp,\n+\t\t\t\t\t  msg_len);\n+\t\tif (ret < 0)\n+\t\t\treturn ret;\n+\t\treturn es58x_rx_timestamp(es58x_dev,\n+\t\t\t\t\t  get_unaligned_le64\n+\t\t\t\t\t  (&es581_4_urb_cmd->timestamp));\n+\n+\tcase ES581_4_CMD_ID_SELF_RECEPTION:\n+\t\treturn es581_4_rx_loopback_msg(es58x_dev, es581_4_urb_cmd);\n+\n+\tcase ES581_4_CMD_ID_DEVICE_ERR:\n+\t\tret = es58x_check_msg_len(dev, es581_4_urb_cmd->rx_dev_ret_u8,\n+\t\t\t\t\t  msg_len);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\t\treturn es58x_rx_dev_ret_u8(es58x_dev->dev,\n+\t\t\t\t\t   ES58X_RET_TYPE_DEVICE_ERR,\n+\t\t\t\t\t   es581_4_urb_cmd->rx_dev_ret_u8);\n+\n+\tdefault:\n+\t\tdev_warn(dev, \"%s: Unexpected command ID: 0x%02X\\n\",\n+\t\t\t __func__, es581_4_urb_cmd->cmd_id);\n+\t\treturn -EBADRQC;\n+\t}\n+}\n+\n+static void es581_4_fill_urb_header(union es58x_urb_cmd *urb_cmd, u8 cmd_type,\n+\t\t\t\t    u8 cmd_id, u8 channel_idx, u16 msg_len)\n+{\n+\tstruct es581_4_urb_cmd *es581_4_urb_cmd = &urb_cmd->es581_4_urb_cmd;\n+\n+\tes581_4_urb_cmd->SOF = cpu_to_le16(es581_4_param.tx_start_of_frame);\n+\tes581_4_urb_cmd->cmd_type = cmd_type;\n+\tes581_4_urb_cmd->cmd_id = cmd_id;\n+\tes581_4_urb_cmd->msg_len = cpu_to_le16(msg_len);\n+}\n+\n+static int es581_4_tx_can_msg(struct es58x_priv *priv, int channel_idx,\n+\t\t\t      u32 packet_idx, const struct sk_buff *skb,\n+\t\t\t      union es58x_urb_cmd *urb_cmd, u32 *urb_cmd_len)\n+{\n+\tstruct es58x_device *es58x_dev = priv->es58x_dev;\n+\tstruct es581_4_urb_cmd *es581_4_urb_cmd = &urb_cmd->es581_4_urb_cmd;\n+\tstruct can_frame *cf = (struct can_frame *)skb->data;\n+\tstruct es581_4_tx_can_msg *tx_can_msg;\n+\tu16 msg_len;\n+\tint ret;\n+\n+\tret = es58x_check_msg_max_len(es58x_dev->dev,\n+\t\t\t\t      es581_4_urb_cmd->bulk_tx_can_msg,\n+\t\t\t\t      es581_4_get_msg_len(urb_cmd) +\n+\t\t\t\t      sizeof(*tx_can_msg));\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tif (can_is_canfd_skb(skb))\n+\t\treturn -EMSGSIZE;\n+\n+\t/* Fill message contents. */\n+\tif (*urb_cmd_len == es58x_dev->param->urb_cmd_header_len) {\n+\t\tes581_4_fill_urb_header(urb_cmd, ES581_4_CAN_COMMAND_TYPE,\n+\t\t\t\t\tES581_4_CMD_ID_TX_MSG,\n+\t\t\t\t\tchannel_idx, *urb_cmd_len);\n+\t\tmsg_len = 1;\n+\t} else {\n+\t\tmsg_len = le16_to_cpu(es581_4_urb_cmd->msg_len);\n+\t}\n+\ttx_can_msg = (struct es581_4_tx_can_msg *)\n+\t    &es581_4_urb_cmd->bulk_tx_can_msg.tx_can_msg_buf[msg_len - 1];\n+\n+\tput_unaligned_le32(es58x_get_raw_can_id(cf), &tx_can_msg->can_id);\n+\tput_unaligned_le32(packet_idx, &tx_can_msg->packet_idx);\n+\tput_unaligned_le16((u16)es58x_get_flags(skb), &tx_can_msg->flags);\n+\ttx_can_msg->channel_no = channel_idx + 1;\n+\ttx_can_msg->dlc = can_get_cc_dlc(priv->can.ctrlmode,\n+\t\t\t\t\t cf->len, cf->len8_dlc);\n+\n+\tmemcpy(tx_can_msg->data, cf->data, cf->len);\n+\n+\t/* Calculate new sizes. */\n+\tes581_4_urb_cmd->bulk_tx_can_msg.num_can_msg++;\n+\tmsg_len += es581_4_sizeof_rx_tx_msg(*tx_can_msg);\n+\t*urb_cmd_len = es58x_get_urb_cmd_len(es58x_dev, msg_len);\n+\tes581_4_urb_cmd->msg_len = cpu_to_le16(msg_len);\n+\n+\treturn 0;\n+}\n+\n+static void es581_4_print_conf(struct net_device *netdev,\n+\t\t\t       struct es581_4_tx_conf_msg *tx_conf_msg)\n+{\n+\tnetdev_vdbg(netdev, \"bitrate         = %d\\n\",\n+\t\t    le32_to_cpu(tx_conf_msg->bitrate));\n+\tnetdev_vdbg(netdev, \"sample_point    = %d\\n\",\n+\t\t    le32_to_cpu(tx_conf_msg->sample_point));\n+\tnetdev_vdbg(netdev, \"samples_per_bit = %d\\n\",\n+\t\t    le32_to_cpu(tx_conf_msg->samples_per_bit));\n+\tnetdev_vdbg(netdev, \"bit_time        = %d\\n\",\n+\t\t    le32_to_cpu(tx_conf_msg->bit_time));\n+\tnetdev_vdbg(netdev, \"sjw             = %d\\n\",\n+\t\t    le32_to_cpu(tx_conf_msg->sjw));\n+\tnetdev_vdbg(netdev, \"sync_edge       = %d\\n\",\n+\t\t    le32_to_cpu(tx_conf_msg->sync_edge));\n+\tnetdev_vdbg(netdev, \"physical_media  = %d\\n\",\n+\t\t    le32_to_cpu(tx_conf_msg->physical_media));\n+\tnetdev_vdbg(netdev, \"self_reception  = %d\\n\",\n+\t\t    le32_to_cpu(tx_conf_msg->self_reception_mode));\n+\tnetdev_vdbg(netdev, \"channel_no      = %d\\n\", tx_conf_msg->channel_no);\n+}\n+\n+static int es581_4_set_bittiming(struct es58x_device *es58x_dev,\n+\t\t\t\t int channel_idx)\n+{\n+\tstruct es581_4_tx_conf_msg tx_conf_msg = { 0 };\n+\tstruct net_device *netdev = es58x_dev->netdev[channel_idx];\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tstruct can_bittiming *bt = &priv->can.bittiming;\n+\n+\ttx_conf_msg.bitrate = cpu_to_le32(bt->bitrate);\n+\t/* bt->sample_point is in tenth of percent. Convert it to percent. */\n+\ttx_conf_msg.sample_point = cpu_to_le32(bt->sample_point / 10U);\n+\ttx_conf_msg.samples_per_bit = cpu_to_le32(ES58X_SAMPLES_PER_BIT_ONE);\n+\ttx_conf_msg.bit_time = cpu_to_le32(can_bit_time(bt));\n+\ttx_conf_msg.sjw = cpu_to_le32(bt->sjw);\n+\ttx_conf_msg.sync_edge = cpu_to_le32(ES58X_SYNC_EDGE_SINGLE);\n+\ttx_conf_msg.physical_media =\n+\t    cpu_to_le32(ES58X_PHYSICAL_MEDIA_HIGH_SPEED);\n+\ttx_conf_msg.self_reception_mode = cpu_to_le32(ES58X_SELF_RECEPTION_ON);\n+\ttx_conf_msg.channel_no = channel_idx + es581_4_param.channel_idx_offset;\n+\n+\tes581_4_print_conf(netdev, &tx_conf_msg);\n+\n+\treturn es58x_send_msg(es58x_dev, ES581_4_CAN_COMMAND_TYPE,\n+\t\t\t      ES581_4_CMD_ID_SET_BITTIMING, &tx_conf_msg,\n+\t\t\t      sizeof(tx_conf_msg), channel_idx);\n+}\n+\n+static int es581_4_enable_channel(struct es58x_device *es58x_dev,\n+\t\t\t\t  int channel_idx)\n+{\n+\tu8 msg = channel_idx + 1;\n+\n+\treturn es58x_send_msg(es58x_dev, ES581_4_CAN_COMMAND_TYPE,\n+\t\t\t      ES581_4_CMD_ID_ENABLE_CHANNEL, &msg, sizeof(msg),\n+\t\t\t      channel_idx);\n+}\n+\n+static int es581_4_disable_channel(struct es58x_device *es58x_dev,\n+\t\t\t\t   int channel_idx)\n+{\n+\tu8 msg = channel_idx + 1;\n+\n+\treturn es58x_send_msg(es58x_dev, ES581_4_CAN_COMMAND_TYPE,\n+\t\t\t      ES581_4_CMD_ID_DISABLE_CHANNEL, &msg, sizeof(msg),\n+\t\t\t      channel_idx);\n+}\n+\n+static int es581_4_reset_rx(struct es58x_device *es58x_dev, int channel_idx)\n+{\n+\tu8 msg = channel_idx + 1;\n+\n+\treturn es58x_send_msg(es58x_dev, ES581_4_CAN_COMMAND_TYPE,\n+\t\t\t      ES581_4_CMD_ID_RESET_RX, &msg, sizeof(msg),\n+\t\t\t      channel_idx);\n+}\n+\n+static int es581_4_reset_tx(struct es58x_device *es58x_dev, int channel_idx)\n+{\n+\tu8 msg = channel_idx + 1;\n+\n+\treturn es58x_send_msg(es58x_dev, ES581_4_CAN_COMMAND_TYPE,\n+\t\t\t      ES581_4_CMD_ID_RESET_TX, &msg, sizeof(msg),\n+\t\t\t      channel_idx);\n+}\n+\n+static int es581_4_get_timestamp(struct es58x_device *es58x_dev)\n+{\n+\treturn es58x_send_msg(es58x_dev, ES581_4_CAN_COMMAND_TYPE,\n+\t\t\t      ES581_4_CMD_ID_TIMESTAMP, ES58X_EMPTY_MSG,\n+\t\t\t      0, ES58X_CHANNEL_IDX_NA);\n+}\n+\n+/* struct es581_4_bittiming_const - Nominal bittiming constants.\n+ *\n+ * Nominal bittiming constants for ES581.4 as specified in the\n+ * microcontroller datasheet: \"Stellaris(R) LM3S5B91 Microcontroller\"\n+ * table 17-4 \"CAN Protocol Ranges\" from Texas Instruments.\n+ */\n+static const struct can_bittiming_const es581_4_bittiming_const = {\n+\t.name = \"ES581.4\",\n+\t.tseg1_min = 1,\n+\t.tseg1_max = 8,\n+\t.tseg2_min = 1,\n+\t.tseg2_max = 8,\n+\t.sjw_max = 4,\n+\t.brp_min = 1,\n+\t.brp_max = 128,\n+\t.brp_inc = 1\n+};\n+\n+const struct es58x_parameters es581_4_param = {\n+\t.bittiming_const = &es581_4_bittiming_const,\n+\t.data_bittiming_const = NULL,\n+\t.bitrate_max = ES58X_MBPS(1),\n+\t.clock = {.freq = ES58X_MHZ(50)},\n+\t.ctrlmode_supported =\n+\t    CAN_CTRLMODE_LOOPBACK | CAN_CTRLMODE_CC_LEN8_DLC,\n+\t.tx_start_of_frame = 0xAFAF,\n+\t.rx_start_of_frame = 0xFAFA,\n+\t.tx_urb_cmd_max_len = ES581_4_TX_URB_CMD_MAX_LEN,\n+\t.rx_urb_cmd_max_len = ES581_4_RX_URB_CMD_MAX_LEN,\n+\t/* Size of internal device TX queue is 330.\n+\t *\n+\t * However, we witnessed some ES58X_ERR_PROT_CRC errors from\n+\t * the device and thus, echo_skb_max was lowered to the\n+\t * empirical value of 75 which seems stable.\n+\t *\n+\t * Root cause of those ES58X_ERR_PROT_CRC errors is still\n+\t * unclear.\n+\t */\n+\t.echo_skb_max = 75,\n+\t/* Empirical value. */\n+\t.dql_limit_min = ES58X_CAN_FRAME_BYTES_MAX * 50,\n+\t.tx_bulk_max = ES581_4_TX_BULK_MAX,\n+\t.urb_cmd_header_len = ES581_4_URB_CMD_HEADER_LEN,\n+\t.rx_urb_max = ES58X_RX_URBS_MAX,\n+\t.tx_urb_max = ES58X_TX_URBS_MAX,\n+\t.channel_idx_offset = 1\n+};\n+\n+const struct es58x_operators es581_4_ops = {\n+\t.get_msg_len = es581_4_get_msg_len,\n+\t.handle_urb_cmd = es581_4_handle_urb_cmd,\n+\t.fill_urb_header = es581_4_fill_urb_header,\n+\t.tx_can_msg = es581_4_tx_can_msg,\n+\t.set_bittiming = es581_4_set_bittiming,\n+\t.enable_channel = es581_4_enable_channel,\n+\t.disable_channel = es581_4_disable_channel,\n+\t.reset_rx = es581_4_reset_rx,\n+\t.reset_tx = es581_4_reset_tx,\n+\t.get_timestamp = es581_4_get_timestamp\n+};\ndiff --git a/drivers/net/can/usb/etas_es58x/es581_4.h b/drivers/net/can/usb/etas_es58x/es581_4.h\nnew file mode 100644\nindex 000000000000..1fbb41cadeea\n--- /dev/null\n+++ b/drivers/net/can/usb/etas_es58x/es581_4.h\n@@ -0,0 +1,206 @@\n+/* SPDX-License-Identifier: GPL-2.0 */\n+\n+/* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.\n+ *\n+ * File es581_4.h: Definitions and declarations specific to ETAS\n+ * ES581.4.\n+ *\n+ * Copyright (C) 2019 Robert Bosch Engineering and Business\n+ * Solutions. All rights reserved.\n+ * Copyright (C) 2020 ETAS K.K.. All rights reserved.\n+ */\n+\n+#ifndef __ES581_4_H__\n+#define __ES581_4_H__\n+\n+#include <linux/types.h>\n+\n+#define ES581_4_NUM_CAN_CH 2\n+\n+#define ES581_4_TX_BULK_MAX 25\n+#define ES581_4_RX_BULK_MAX 30\n+#define ES581_4_RX_LOOPBACK_BULK_MAX 30\n+\n+enum es581_4_cmd_type {\n+\tES581_4_CAN_COMMAND_TYPE = 0x45\n+};\n+\n+enum es581_4_cmd_id {\n+\tES581_4_CMD_ID_OPEN_CHANNEL = 0x01,\n+\tES581_4_CMD_ID_CLOSE_CHANNEL = 0x02,\n+\tES581_4_CMD_ID_SET_BITTIMING = 0x03,\n+\tES581_4_CMD_ID_ENABLE_CHANNEL = 0x04,\n+\tES581_4_CMD_ID_TX_MSG = 0x05,\n+\tES581_4_CMD_ID_RX_MSG = 0x06,\n+\tES581_4_CMD_ID_RESET_RX = 0x0A,\n+\tES581_4_CMD_ID_RESET_TX = 0x0B,\n+\tES581_4_CMD_ID_DISABLE_CHANNEL = 0x0C,\n+\tES581_4_CMD_ID_TIMESTAMP = 0x0E,\n+\tES581_4_CMD_ID_RESET_DEVICE = 0x28,\n+\tES581_4_CMD_ID_SELF_RECEPTION = 0x71,\n+\tES581_4_CMD_ID_DEVICE_ERR = 0x72\n+};\n+\n+enum es581_4_rx_type {\n+\tES581_4_RX_TYPE_MESSAGE = 1,\n+\tES581_4_RX_TYPE_ERROR = 3,\n+\tES581_4_RX_TYPE_EVENT = 4\n+};\n+\n+/**\n+ * struct es581_4_tx_conf_msg - Channel configuration.\n+ * @bitrate: Bitrate.\n+ * @sample_point: Sample point is in percent [0..100].\n+ * @samples_per_bit: type enum es58x_samples_per_bit.\n+ * @bit_time: Number of time quanta in one bit.\n+ * @sjw: Synchronization Jump Width.\n+ * @sync_edge: type enum es58x_sync_edge.\n+ * @physical_media: type enum es58x_physical_media.\n+ * @self_reception_mode: type enum es58x_self_reception_mode.\n+ * @channel_no: Channel number, starting from 1. Not to be confused\n+ *\twith channed_idx of the ES58X FD which starts from 0.\n+ */\n+struct es581_4_tx_conf_msg {\n+\t__le32 bitrate;\n+\t__le32 sample_point;\n+\t__le32 samples_per_bit;\n+\t__le32 bit_time;\n+\t__le32 sjw;\n+\t__le32 sync_edge;\n+\t__le32 physical_media;\n+\t__le32 self_reception_mode;\n+\tu8 channel_no;\n+} __packed;\n+\n+struct es581_4_tx_can_msg {\n+\t__le32 can_id;\n+\t__le32 packet_idx;\n+\t__le16 flags;\n+\tu8 channel_no;\n+\tu8 dlc;\n+\tu8 data[CAN_MAX_DLEN];\n+} __packed;\n+\n+/* The ES581.4 allows bulk transfer.  */\n+struct es581_4_bulk_tx_can_msg {\n+\tu8 num_can_msg;\n+\t/* Using type \"u8[]\" instead of \"struct es581_4_tx_can_msg[]\"\n+\t * for tx_msg_buf because each member has a flexible size.\n+\t */\n+\tu8 tx_can_msg_buf[ES581_4_TX_BULK_MAX *\n+\t\t\t  sizeof(struct es581_4_tx_can_msg)];\n+} __packed;\n+\n+struct es581_4_rx_loopback_msg {\n+\t__le64 timestamp;\n+\t__le32 packet_idx;\n+} __packed;\n+\n+struct es581_4_bulk_rx_loopback_msg {\n+\tu8 channel_no;\n+\tstruct es581_4_rx_loopback_msg\n+\t rx_loopback_msg[ES581_4_RX_LOOPBACK_BULK_MAX];\n+} __packed;\n+\n+/* Normal Rx CAN Message */\n+struct es581_4_rx_can_msg {\n+\t__le64 timestamp;\n+\tu8 rx_type;\t\t/* type enum es581_4_rx_type */\n+\tu8 flags;\t\t/* type enum es58x_flag */\n+\tu8 channel_no;\n+\tu8 dlc;\n+\t__le32 can_id;\n+\tu8 data[CAN_MAX_DLEN];\n+} __packed;\n+\n+struct es581_4_rx_err_msg {\n+\t__le64 timestamp;\n+\t__le16 rx_type;\t\t/* type enum es581_4_rx_type */\n+\t__le16 flags;\t\t/* type enum es58x_flag */\n+\tu8 channel_no;\n+\tu8 __padding[2];\n+\tu8 dlc;\n+\t__le32 tag;\t\t/* Related to the CAN filtering. Unused in this module */\n+\t__le32 can_id;\n+\t__le32 error;\t\t/* type enum es58x_error */\n+\t__le32 destination;\t/* Unused in this module */\n+} __packed;\n+\n+struct es581_4_rx_event_msg {\n+\t__le64 timestamp;\n+\t__le16 rx_type;\t\t/* type enum es581_4_rx_type */\n+\tu8 channel_no;\n+\tu8 __padding;\n+\t__le32 tag;\t\t/* Related to the CAN filtering. Unused in this module */\n+\t__le32 event;\t\t/* type enum es58x_event */\n+\t__le32 destination;\t/* Unused in this module */\n+} __packed;\n+\n+struct es581_4_tx_ack_msg {\n+\t__le16 tx_free_entries;\t/* Number of remaining free entries in the device TX queue */\n+\tu8 channel_no;\n+\tu8 rx_dev_ret_u8;\t/* type enum es58x_dev_ret_code_u8 */\n+} __packed;\n+\n+struct es581_4_rx_dev_ret {\n+\t__le32 rx_cmd_ret_le32;\n+\tu8 channel_no;\n+\tu8 __padding[3];\n+} __packed;\n+\n+/**\n+ * struct es581_4_urb_cmd - Commands received from or sent to the\n+ *\tES581.4 device.\n+ * @SOF: Start of Frame.\n+ * @cmd_type: Command Type (type: enum es581_4_cmd_type). The CRC\n+ *\tcalculation starts at this position.\n+ * @cmd_id: Command ID (type: enum es581_4_cmd_id).\n+ * @msg_len: Length of the message, excluding CRC (i.e. length of the\n+ *\tunion).\n+ * @raw_msg: Message raw payload.\n+ * @tx_conf_msg: Channel configuration.\n+ * @bulk_tx_can_msg: Tx messages.\n+ * @rx_can_msg: Array of Rx messages.\n+ * @bulk_rx_loopback_msg: Loopback messages.\n+ * @rx_err_msg: Error message.\n+ * @rx_event_msg: Event message.\n+ * @tx_ack_msg: Tx acknowledgment message.\n+ * @timestamp: Timestamp reply.\n+ * @rx_cmd_ret_u8: Rx 8 bits return code (type: enum\n+ *\tes58x_dev_ret_code_u8).\n+ * @reserved_for_crc16_do_not_use: The structure ends with a\n+ *\tCRC16. Because the structures in above union are of variable\n+ *\tlengths, we can not predict the offset of the CRC in\n+ *\tadvance. Use functions es58x_get_crc() and es58x_set_crc() to\n+ *\tmanipulate it.\n+ */\n+struct es581_4_urb_cmd {\n+\t__le16 SOF;\n+\tu8 cmd_type;\n+\tu8 cmd_id;\n+\t__le16 msg_len;\n+\n+\tunion {\n+\t\tstruct es581_4_tx_conf_msg tx_conf_msg;\n+\t\tstruct es581_4_bulk_tx_can_msg bulk_tx_can_msg;\n+\t\tstruct es581_4_rx_can_msg rx_can_msg[ES581_4_RX_BULK_MAX];\n+\t\tstruct es581_4_bulk_rx_loopback_msg bulk_rx_loopback_msg;\n+\t\tstruct es581_4_rx_err_msg rx_err_msg;\n+\t\tstruct es581_4_rx_event_msg rx_event_msg;\n+\t\tstruct es581_4_tx_ack_msg tx_ack_msg;\n+\t\tstruct es581_4_rx_dev_ret rx_dev_ret;\n+\t\t__le64 timestamp;\n+\t\tu8 rx_dev_ret_u8;\n+\t\tu8 raw_msg[0];\n+\t} __packed;\n+\n+\t__le16 reserved_for_crc16_do_not_use;\n+} __packed;\n+\n+#define ES581_4_URB_CMD_HEADER_LEN (offsetof(struct es581_4_urb_cmd, raw_msg))\n+#define ES581_4_TX_URB_CMD_MAX_LEN\t\t\t\t\t\\\n+\tES58X_SIZEOF_URB_CMD(struct es581_4_urb_cmd, bulk_tx_can_msg)\n+#define ES581_4_RX_URB_CMD_MAX_LEN\t\t\t\t\t\\\n+\tES58X_SIZEOF_URB_CMD(struct es581_4_urb_cmd, rx_can_msg)\n+\n+#endif /* __ES581_4_H__ */\ndiff --git a/drivers/net/can/usb/etas_es58x/es58x_core.c b/drivers/net/can/usb/etas_es58x/es58x_core.c\nnew file mode 100644\nindex 000000000000..bfe9e94b7fbc\n--- /dev/null\n+++ b/drivers/net/can/usb/etas_es58x/es58x_core.c\n@@ -0,0 +1,2627 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+/* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.\n+ *\n+ * File es58x_core.c: Core logic to manage the network devices and the\n+ * USB interface.\n+ *\n+ * Copyright (C) 2019 Robert Bosch Engineering and Business\n+ * Solutions. All rights reserved.\n+ * Copyright (C) 2020 ETAS K.K.. All rights reserved.\n+ */\n+\n+#include <linux/kernel.h>\n+#include <linux/module.h>\n+#include <linux/moduleparam.h>\n+#include <linux/usb.h>\n+#include <linux/crc16.h>\n+#include <linux/spinlock.h>\n+#include <asm/unaligned.h>\n+\n+#include \"es58x_core.h\"\n+\n+#define DRV_VERSION \"1.03\"\n+MODULE_AUTHOR(\"Mailhol Vincent <mailhol.vincent@wanadoo.fr>\");\n+MODULE_AUTHOR(\"Arunachalam Santhanam <arunachalam.santhanam@in.bosch.com>\");\n+MODULE_DESCRIPTION(\"Socket CAN driver for ETAS ES58X USB adapters\");\n+MODULE_VERSION(DRV_VERSION);\n+MODULE_LICENSE(\"GPL v2\");\n+\n+/* Vendor and product id */\n+#define ES58X_MODULE_NAME         \"etas_es58x\"\n+#define ES58X_VENDOR_ID             0x108C\n+#define ES581_4_PRODUCT_ID          0x0159\n+#define ES582_1_PRODUCT_ID          0x0168\n+#define ES584_1_PRODUCT_ID          0x0169\n+\n+/* Table of devices which work with this driver */\n+static const struct usb_device_id es58x_id_table[] = {\n+\t{USB_DEVICE(ES58X_VENDOR_ID, ES581_4_PRODUCT_ID)},\n+\t{USB_DEVICE(ES58X_VENDOR_ID, ES582_1_PRODUCT_ID)},\n+\t{USB_DEVICE(ES58X_VENDOR_ID, ES584_1_PRODUCT_ID)},\n+\t{}\t\t\t/* Terminating entry */\n+};\n+\n+MODULE_DEVICE_TABLE(usb, es58x_id_table);\n+\n+#define es58x_print_hex_dump(buf, len)\t\t\t\t\t\\\n+\tprint_hex_dump(KERN_DEBUG,\t\t\t\t\t\\\n+\t\t       ES58X_MODULE_NAME \" \" __stringify(buf) \": \",\t\\\n+\t\t       DUMP_PREFIX_NONE, 16, 1, buf, len, false)\n+\n+#define es58x_print_hex_dump_debug(buf, len)\t\t\t\t \\\n+\tprint_hex_dump_debug(ES58X_MODULE_NAME \" \" __stringify(buf) \": \",\\\n+\t\t\t     DUMP_PREFIX_NONE, 16, 1, buf, len, false)\n+\n+/* The last two bytes of an ES58X command is a CRC16. The first two\n+ * bytes (the start of frame) are skipped and the CRC calculation\n+ * starts on the third byte.\n+ */\n+#define ES58X_CRC_CALC_OFFSET\t2\n+\n+/**\n+ * es58x_calculate_crc() - Compute the crc16 of a given URB.\n+ * @urb_cmd: The URB command for which we want to calculate the CRC.\n+ * @urb_len: Length of @urb_cmd. Must be at least bigger than 4\n+ *\t(ES58X_CRC_CALC_OFFSET + sizeof(crc))\n+ *\n+ * Return: crc16 value.\n+ */\n+static u16 es58x_calculate_crc(const union es58x_urb_cmd *urb_cmd, u16 urb_len)\n+{\n+\tu16 crc;\n+\tssize_t len = urb_len - ES58X_CRC_CALC_OFFSET - sizeof(crc);\n+\n+\tWARN_ON(len < 0);\n+\tcrc = crc16(0, &urb_cmd->raw_cmd[ES58X_CRC_CALC_OFFSET], len);\n+\treturn crc;\n+}\n+\n+/**\n+ * es58x_get_crc() - Get the CRC value of a given URB.\n+ * @urb_cmd: The URB command for which we want to get the CRC.\n+ * @urb_len: Length of @urb_cmd. Must be at least bigger than 4\n+ *\t(ES58X_CRC_CALC_OFFSET + sizeof(crc))\n+ *\n+ * Return: crc16 value.\n+ */\n+static u16 es58x_get_crc(const union es58x_urb_cmd *urb_cmd, u16 urb_len)\n+{\n+\tu16 crc;\n+\tconst __le16 *crc_addr;\n+\n+\tWARN_ON(urb_len - (ssize_t)sizeof(crc) < 0);\n+\tcrc_addr = (__le16 *)&urb_cmd->raw_cmd[urb_len - sizeof(crc)];\n+\tcrc = get_unaligned_le16(crc_addr);\n+\treturn crc;\n+}\n+\n+/**\n+ * es58x_set_crc() - Set the CRC value of a given URB.\n+ * @urb_cmd: The URB command for which we want to get the CRC.\n+ * @urb_len: Length of @urb_cmd. Must be at least bigger than 4\n+ *\t(ES58X_CRC_CALC_OFFSET + sizeof(crc))\n+ */\n+static void es58x_set_crc(union es58x_urb_cmd *urb_cmd, u16 urb_len)\n+{\n+\tu16 crc;\n+\t__le16 *crc_addr;\n+\n+\tcrc = es58x_calculate_crc(urb_cmd, urb_len);\n+\tcrc_addr = (__le16 *)&urb_cmd->raw_cmd[urb_len - sizeof(crc)];\n+\tput_unaligned_le16(crc, crc_addr);\n+}\n+\n+/**\n+ * es58x_check_crc() - Validate the CRC value of a given URB.\n+ * @es58x_dev: ES58X device.\n+ * @urb_cmd: The URB command for which we want to check the CRC.\n+ * @urb_len: Length of @urb_cmd. Must be at least bigger than 4\n+ *\t(ES58X_CRC_CALC_OFFSET + sizeof(crc))\n+ *\n+ * Return: zero on success, -EBADMSG if the CRC check fails.\n+ */\n+static int es58x_check_crc(struct es58x_device *es58x_dev,\n+\t\t\t   const union es58x_urb_cmd *urb_cmd, u16 urb_len)\n+{\n+\tu16 calculated_crc = es58x_calculate_crc(urb_cmd, urb_len);\n+\tu16 expected_crc = es58x_get_crc(urb_cmd, urb_len);\n+\n+\tif (expected_crc != calculated_crc) {\n+\t\tdev_err_ratelimited(es58x_dev->dev,\n+\t\t\t\t    \"%s: Bad CRC, urb_len: %d\\n\",\n+\t\t\t\t    __func__, urb_len);\n+\t\treturn -EBADMSG;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_timestamp_to_ns() - Convert a timestamp value received from a\n+ *\tES58X device to nanoseconds.\n+ * @timestamp: Timestamp received from a ES58X device.\n+ *\n+ * The timestamp received from ES58X is expressed in multiple of 0.5\n+ * micro seconds. This function converts it in to nanoseconds.\n+ *\n+ * Return: Timestamp value in nanoseconds.\n+ */\n+static u64 es58x_timestamp_to_ns(u64 timestamp)\n+{\n+\tconst u64 es58x_timestamp_ns_mult_coef = 500ULL;\n+\n+\treturn es58x_timestamp_ns_mult_coef * timestamp;\n+}\n+\n+/**\n+ * es58x_set_skb_timestamp() - Set the hardware timestamp of an skb.\n+ * @netdev: CAN network device.\n+ * @skb: socket buffer of a CAN message.\n+ * @timestamp: Timestamp received from an ES58X device.\n+ *\n+ * Used for both received and loopback messages.\n+ *\n+ * Return: zero on success, -EFAULT if @skb is NULL.\n+ */\n+static int es58x_set_skb_timestamp(struct net_device *netdev,\n+\t\t\t\t   struct sk_buff *skb, u64 timestamp)\n+{\n+\tstruct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;\n+\tstruct skb_shared_hwtstamps *hwts;\n+\n+\thwts = skb_hwtstamps(skb);\n+\t/* Ignoring overflow (overflow on 64 bits timestamp with nano\n+\t * second precision would occur after more than 500 years).\n+\t */\n+\thwts->hwtstamp = ns_to_ktime(es58x_timestamp_to_ns(timestamp) +\n+\t\t\t\t     es58x_dev->realtime_diff_ns);\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_rx_timestamp() - Handle a received timestamp.\n+ * @es58x_dev: ES58X device.\n+ * @timestamp: Timestamp received from a ES58X device.\n+ *\n+ * Calculate the difference between the ES58X device and the kernel\n+ * internal clocks. This difference will be later used as an offset to\n+ * convert the timestamps of RX and loopback messages to match the\n+ * kernel system time (e.g. convert to UNIX time).\n+ *\n+ * Return: zero on success.\n+ */\n+int es58x_rx_timestamp(struct es58x_device *es58x_dev, u64 timestamp)\n+{\n+\tu64 ktime_real_ns = ktime_get_real_ns();\n+\tu64 device_timestamp = es58x_timestamp_to_ns(timestamp);\n+\n+\tdev_dbg(es58x_dev->dev, \"%s: request round-trip time: %llu\\n\",\n+\t\t__func__, ktime_real_ns - es58x_dev->ktime_req_ns);\n+\n+\tes58x_dev->realtime_diff_ns =\n+\t    (es58x_dev->ktime_req_ns + ktime_real_ns) / 2 - device_timestamp;\n+\tes58x_dev->ktime_req_ns = 0;\n+\n+\tdev_dbg(es58x_dev->dev,\n+\t\t\"%s: Device timestamp: %llu, diff with kernel: %llu\\n\",\n+\t\t__func__, device_timestamp, es58x_dev->realtime_diff_ns);\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_set_realtime_diff_ns() - Calculate difference between the\n+ *\tclocks of the ES58X device and the kernel\n+ * @es58x_dev: ES58X device.\n+ *\n+ * Request a timestamp from the ES58X device. Once the answer is\n+ * received, the timestamp difference will be set by the callback\n+ * function es58x_rx_timestamp().\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+static int es58x_set_realtime_diff_ns(struct es58x_device *es58x_dev)\n+{\n+\tif (es58x_dev->ktime_req_ns) {\n+\t\tdev_warn(es58x_dev->dev,\n+\t\t\t \"%s: Previous request to set timestamp has not completed yet\\n\",\n+\t\t\t __func__);\n+\t\treturn -EBUSY;\n+\t}\n+\n+\tes58x_dev->ktime_req_ns = ktime_get_real_ns();\n+\treturn es58x_dev->ops->get_timestamp(es58x_dev);\n+}\n+\n+/**\n+ * es58x_is_can_state_active() - Is the network device in an active\n+ *\tCAN state?\n+ * @netdev: CAN network device.\n+ *\n+ * The device is considered active if it is able to send or receive\n+ * CAN frames, that is to say if it is in any of\n+ * CAN_STATE_ERROR_ACTIVE, CAN_STATE_ERROR_WARNING or\n+ * CAN_STATE_ERROR_PASSIVE states.\n+ *\n+ * Caution: when recovering from a bus-off,\n+ * net/core/dev.c#can_restart() will call\n+ * net/core/dev.c#can_flush_echo_skb() without using any kind of\n+ * locks. For this reason, it is critical to guaranty that no TX or\n+ * loopback operations (i.e. any access to priv->echo_skb[]) can be\n+ * done while this function is returning false.\n+ *\n+ * Return: true if the device is active, else returns false.\n+ */\n+static bool es58x_is_can_state_active(struct net_device *netdev)\n+{\n+\treturn es58x_priv(netdev)->can.state < CAN_STATE_BUS_OFF;\n+}\n+\n+/**\n+ * es58x_echo_skb_stop_threshold() - Determine the limit of how many\n+ *\tskb slots can be taken before we should stop the network\n+ *\tqueue.\n+ * @priv: ES58X private parameters related to the network device.\n+ *\n+ * We need to save enough free skb slots in order to be able to do\n+ * bulk send. This function can be used to determine when to wake or\n+ * stop the network queue in regard to the number of skb slots already\n+ * taken if the loopback FIFO.\n+ *\n+ * Return: stop threshold limit.\n+ */\n+static int es58x_echo_skb_stop_threshold(struct es58x_priv *priv)\n+{\n+\treturn priv->can.echo_skb_max - priv->es58x_dev->param->tx_bulk_max + 1;\n+}\n+\n+/**\n+ * es58x_netif_wake_queue() - Wake-up the network queue if conditions\n+ *\tare met.\n+ * @netdev: CAN network device.\n+ *\n+ * If the queue is already awoken, do nothing.\n+ *\n+ * The conditions to wake up the queue are: a.) The device should be\n+ * active (e.g. not in bus off state), b.) The network device must be\n+ * up, c.) The echo_skb must have enough free slots for a bulk sent\n+ * (c.f. es58x_echo_skb_stop_threshold()), d.) Enough idle URBs should\n+ * be available.\n+ */\n+static void es58x_netif_wake_queue(struct net_device *netdev)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tstruct es58x_device *es58x_dev = priv->es58x_dev;\n+\tunsigned long echo_skb_flags, tx_urbs_flags;\n+\n+\tif (!netif_queue_stopped(netdev))\n+\t\treturn;\n+\n+\tspin_lock_irqsave(&priv->echo_skb_spinlock, echo_skb_flags);\n+\tif (!es58x_is_can_state_active(netdev))\n+\t\tgoto echo_skb_unlock;\n+\tif (netif_running(netdev) &&\n+\t    priv->num_echo_skb < es58x_echo_skb_stop_threshold(priv)) {\n+\t\t/* Confirm that we have at least one idle URB per CAN\n+\t\t * network device. Otherwise, we might run into race\n+\t\t * conditions in the function es58x_start_xmit() which\n+\t\t * would force us to return NETDEV_TX_BUSY.\n+\t\t */\n+\t\tspin_lock_irqsave(&es58x_dev->tx_urbs_idle.lock, tx_urbs_flags);\n+\t\tif (atomic_read(&es58x_dev->tx_urbs_idle_cnt) >=\n+\t\t    es58x_dev->num_can_ch)\n+\t\t\tnetif_wake_queue(netdev);\n+\t\tspin_unlock_irqrestore(&es58x_dev->tx_urbs_idle.lock,\n+\t\t\t\t       tx_urbs_flags);\n+\t}\n+ echo_skb_unlock:\n+\tspin_unlock_irqrestore(&priv->echo_skb_spinlock, echo_skb_flags);\n+}\n+\n+/**\n+ * es58x_netif_wake_all_queues() - Wake-up all the network queues if\n+ *\tconditions are met.\n+ * @es58x_dev: ES58X device.\n+ *\n+ * Try to wake-up all the queues associated with a ES58X device. This\n+ * function should be used when idle URB resources become available.\n+ */\n+static void es58x_netif_wake_all_queues(struct es58x_device *es58x_dev)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i < es58x_dev->num_can_ch; i++)\n+\t\tes58x_netif_wake_queue(es58x_dev->netdev[i]);\n+}\n+\n+/**\n+ * es58x_netif_stop_all_queues() - Unconditionally stop all the\n+ *\tnetwork queues.\n+ * @es58x_dev: ES58X device.\n+ *\n+ * Stop all the queues associated with a ES58X device. This\n+ * function should be used when idle URB resources become unavailable.\n+ */\n+static void es58x_netif_stop_all_queues(struct es58x_device *es58x_dev)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i < es58x_dev->num_can_ch; i++)\n+\t\tif (es58x_dev->netdev[i])\n+\t\t\tnetif_stop_queue(es58x_dev->netdev[i]);\n+}\n+\n+/**\n+ * es58x_can_frame_bytes() - Calculate the CAN frame length in bytes\n+ *\tof a given skb.\n+ * @skb: socket buffer of a CAN message.\n+ *\n+ * Do a rough calculation: bitstuffing is ignored and length in bits\n+ * is rounded up to a length in bytes.\n+ *\n+ * Rationale: this function is to be used for the BQL functions\n+ * (__netdev_sent_queue() and netdev_completed_queue()) which expect a\n+ * value in bytes. Just using skb->len is insufficient because it will\n+ * return the constant value of CAN(FD)_MTU. Doing the bitstuffing\n+ * calculation would be too expensive in term of computing resources\n+ * for no noticeable gain.\n+ *\n+ * Remarks: a.)Tthe payload of CAN FD frames with BRS flag are sent at\n+ * a different bitrate. Currently, the can-utils canbusload tool does\n+ * not support CAN-FD yet and so we could not run any benchmark to\n+ * measure the impact. There might be possible improvement here. b.)\n+ * The macro ES58X_EFF_BYTES() and ES58X_SFF_BYTES() are for classical\n+ * CAN. Need to do the addition for CAN-FD (the value are expected to\n+ * be close enough so the impact should be minimal or none).\n+ *\n+ * Return: length in bytes.\n+ */\n+static unsigned int es58x_can_frame_bytes(const struct sk_buff *skb)\n+{\n+\tconst struct canfd_frame *cf = (const struct canfd_frame *)skb->data;\n+\tu8 len = cf->can_id & CAN_RTR_FLAG ? 0 : cf->len;\n+\n+\tif (cf->can_id & CAN_EFF_FLAG)\n+\t\treturn ES58X_EFF_BYTES(len);\n+\telse\n+\t\treturn ES58X_SFF_BYTES(len);\n+}\n+\n+/**\n+ * es58x_add_skb_idx() - Increment an index of the loopback FIFO.\n+ * @priv: ES58X private parameters related to the network device.\n+ * @idx: address of the index to be incremented.\n+ * @a: the increment. Must be positive and less or equal to\n+ *\t@priv->can.echo_skb_max.\n+ *\n+ * Do a modulus addition: set *@idx to (*@idx + @a) %\n+ * @priv->can.echo_skb_max.\n+ *\n+ * Rationale: the modulus operator % takes a decent amount of CPU\n+ * cycles (c.f. other division functions such as\n+ * include/linux/math64.h:iter_div_u64_rem()).\n+ */\n+static __always_inline void es58x_add_skb_idx(struct es58x_priv *priv,\n+\t\t\t\t\t      u16 *idx, u16 a)\n+{\n+\t*idx += a;\n+\tif (*idx >= priv->can.echo_skb_max)\n+\t\t*idx -= priv->can.echo_skb_max;\n+}\n+\n+/**\n+ * es58x_can_free_echo_skb_tail() - Remove the oldest echo skb of the\n+ *\tloopback FIFO.\n+ * @netdev: CAN network device.\n+ *\n+ * Naming convention: the tail is the beginning of the FIFO, i.e. the\n+ * first skb to have entered the FIFO.\n+ */\n+static void es58x_can_free_echo_skb_tail(struct net_device *netdev)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tu16 tail_idx = priv->echo_skb_tail_idx;\n+\tstruct sk_buff *skb = priv->can.echo_skb[tail_idx];\n+\tunsigned long flags;\n+\n+\tnetdev_completed_queue(netdev, 1, es58x_can_frame_bytes(skb));\n+\tcan_free_echo_skb(netdev, tail_idx);\n+\n+\tspin_lock_irqsave(&priv->echo_skb_spinlock, flags);\n+\tes58x_add_skb_idx(priv, &priv->echo_skb_tail_idx, 1);\n+\tpriv->num_echo_skb--;\n+\tspin_unlock_irqrestore(&priv->echo_skb_spinlock, flags);\n+\n+\tnetdev->stats.tx_dropped++;\n+}\n+\n+/**\n+ * es58x_can_get_echo_skb_recovery() - Try to re-sync the loopback FIFO.\n+ * @netdev: CAN network device.\n+ * @packet_idx: Index\n+ *\n+ * This function should not be called under normal circumstances. In\n+ * the unlikely case that one or several URB packages get dropped by\n+ * the device, the index will get out of sync. Try to recover by\n+ * dropping the echo skb packets with older indexes.\n+ *\n+ * Return: zero if recovery was successful, -EINVAL otherwise.\n+ */\n+static int es58x_can_get_echo_skb_recovery(struct net_device *netdev,\n+\t\t\t\t\t   u32 packet_idx)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tu32 current_packet_idx, first_packet_idx, num_echo_skb;\n+\tunsigned long flags;\n+\tint ret = 0;\n+\n+\tnetdev->stats.tx_errors++;\n+\n+\tspin_lock_irqsave(&priv->echo_skb_spinlock, flags);\n+\tcurrent_packet_idx = priv->current_packet_idx;\n+\tnum_echo_skb = priv->num_echo_skb;\n+\tspin_unlock_irqrestore(&priv->echo_skb_spinlock, flags);\n+\tfirst_packet_idx = current_packet_idx - num_echo_skb;\n+\n+\tif (net_ratelimit())\n+\t\tnetdev_warn(netdev,\n+\t\t\t    \"Bad loopback packet index: %u. First index: %u, end index %u, num_echo_skb: %02u/%02u\\n\",\n+\t\t\t    packet_idx, first_packet_idx,\n+\t\t\t    current_packet_idx - 1, num_echo_skb,\n+\t\t\t    priv->can.echo_skb_max);\n+\n+\tif (packet_idx < first_packet_idx) {\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_warn(netdev,\n+\t\t\t\t    \"Received loopback is from the past. Ignoring it\\n\");\n+\t\tret = -EINVAL;\n+\t} else if ((s32)(packet_idx - current_packet_idx) >= 0LL) {\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_err(netdev,\n+\t\t\t\t   \"Received loopback is from the future. Ignoring it\\n\");\n+\t\tret = -EINVAL;\n+\t} else {\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_warn(netdev,\n+\t\t\t\t    \"Loopback recovery: dropping %u echo skb from index %u to %u\\n\",\n+\t\t\t\t    packet_idx - first_packet_idx,\n+\t\t\t\t    first_packet_idx, packet_idx - 1);\n+\t\twhile (first_packet_idx != packet_idx) {\n+\t\t\tif (num_echo_skb == 0)\n+\t\t\t\treturn -EINVAL;\n+\t\t\tes58x_can_free_echo_skb_tail(netdev);\n+\t\t\tfirst_packet_idx++;\n+\t\t\tnum_echo_skb--;\n+\t\t}\n+\t}\n+\treturn ret;\n+}\n+\n+/**\n+ * es58x_can_get_echo_skb() - Get the skb from the loopback FIFO and\n+ *\tloop it back locally.\n+ * @netdev: CAN network device.\n+ * @packet_idx: Index of the first packet.\n+ * @tstamps: Array of hardware timestamps received from a ES58X device.\n+ * @pkts: Number of packets (and so, length of @tstamps).\n+ *\n+ * Callback function for when we receive a self reception acknowledgment.\n+ * Retrieves the skb from the loopback FIFO, sets its hardware timestamp\n+ * (the actual time it was sent) and loops it back locally.\n+ *\n+ * The device has to be active (i.e. network interface UP and not in\n+ * bus off state or restarting).\n+ *\n+ * Packet indexes must be consecutive (i.e. index of first packet is\n+ * @packet_idx, index of second packet is @packet_idx + 1 and index of\n+ * last packet is @packet_idx + @pkts - 1).\n+ *\n+ * Return: zero on success.\n+ */\n+int es58x_can_get_echo_skb(struct net_device *netdev, u32 packet_idx,\n+\t\t\t   u64 *tstamps, unsigned int pkts)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tu16 tail_idx;\n+\tu32 first_packet_idx;\n+\tunsigned long flags;\n+\tunsigned int frames_bytes = 0;\n+\tint i;\n+\tint ret = 0;\n+\n+\tif (!netif_running(netdev)) {\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_info(netdev,\n+\t\t\t\t    \"%s: %s is down, dropping %d loopback packets\\n\",\n+\t\t\t\t    __func__, netdev->name, pkts);\n+\t\tnetdev->stats.tx_dropped++;\n+\t\treturn 0;\n+\t} else if (!es58x_is_can_state_active(netdev)) {\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_dbg(netdev,\n+\t\t\t\t   \"Bus is off or device is restarting. Ignoring %u loopback packets from index %u\\n\",\n+\t\t\t\t   pkts, packet_idx);\n+\t\t/* stats.tx_dropped will be (or was already)\n+\t\t * incremented by\n+\t\t * drivers/net/can/net/dev.c:can_flush_echo_skb().\n+\t\t */\n+\t\treturn 0;\n+\t} else if (priv->num_echo_skb == 0) {\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_warn(netdev,\n+\t\t\t\t    \"Received %u loopback packets from index: %u but echo skb queue is empty.\\n\",\n+\t\t\t\t    pkts, packet_idx);\n+\t\tnetdev->stats.tx_dropped += pkts;\n+\t\treturn 0;\n+\t}\n+\n+\tspin_lock_irqsave(&priv->echo_skb_spinlock, flags);\n+\tfirst_packet_idx = priv->current_packet_idx - priv->num_echo_skb;\n+\tif (first_packet_idx != packet_idx) {\n+\t\tspin_unlock_irqrestore(&priv->echo_skb_spinlock, flags);\n+\t\tret = es58x_can_get_echo_skb_recovery(netdev, packet_idx);\n+\t\tif (ret < 0) {\n+\t\t\tif (net_ratelimit())\n+\t\t\t\tnetdev_warn(netdev,\n+\t\t\t\t\t    \"Could not find echo skb for loopback packet index: %u\\n\",\n+\t\t\t\t\t    packet_idx);\n+\t\t\treturn 0;\n+\t\t}\n+\t\tspin_lock_irqsave(&priv->echo_skb_spinlock, flags);\n+\t\tfirst_packet_idx =\n+\t\t    priv->current_packet_idx - priv->num_echo_skb;\n+\t\tWARN_ON(first_packet_idx != packet_idx);\n+\t}\n+\ttail_idx = priv->echo_skb_tail_idx;\n+\tif (priv->num_echo_skb < pkts) {\n+\t\tint pkts_drop = pkts - priv->num_echo_skb;\n+\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_err(netdev,\n+\t\t\t\t   \"Received %u loopback packets but have only %d echo skb. Dropping %d echo skb\\n\",\n+\t\t\t\t   pkts, priv->num_echo_skb, pkts_drop);\n+\t\tnetdev->stats.tx_dropped += pkts_drop;\n+\t\tpkts -= pkts_drop;\n+\t}\n+\tspin_unlock_irqrestore(&priv->echo_skb_spinlock, flags);\n+\n+\tfor (i = 0; i < pkts; i++) {\n+\t\tstruct sk_buff *skb = priv->can.echo_skb[tail_idx];\n+\n+\t\tif (skb) {\n+\t\t\tframes_bytes += es58x_can_frame_bytes(skb);\n+\t\t\tes58x_set_skb_timestamp(netdev, skb, tstamps[i]);\n+\t\t}\n+\t\tnetdev->stats.tx_bytes += can_get_echo_skb(netdev, tail_idx);\n+\n+\t\tes58x_add_skb_idx(priv, &tail_idx, 1);\n+\t}\n+\n+\tspin_lock_irqsave(&priv->echo_skb_spinlock, flags);\n+\tes58x_add_skb_idx(priv, &priv->echo_skb_tail_idx, pkts);\n+\tpriv->num_echo_skb -= pkts;\n+\tspin_unlock_irqrestore(&priv->echo_skb_spinlock, flags);\n+\n+\tnetdev_completed_queue(netdev, pkts, frames_bytes);\n+\tnetdev->stats.tx_packets += pkts;\n+\n+\tpriv->err_passive_before_rtx_success = 0;\n+\tes58x_netif_wake_queue(netdev);\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * es58x_can_flush_echo_skb() - Reset the loopback FIFO.\n+ * @netdev: CAN network device.\n+ *\n+ * The echo_skb array of struct can_priv will be flushed by\n+ * drivers/net/can/dev.c:can_flush_echo_skb(). This function resets\n+ * the parameters of the struct es58x_priv of our device and reset the\n+ * queue (c.f. BQL).\n+ */\n+static void es58x_can_flush_echo_skb(struct net_device *netdev)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tunsigned long flags;\n+\n+\tspin_lock_irqsave(&priv->echo_skb_spinlock, flags);\n+\tpriv->current_packet_idx = 0;\n+\tpriv->echo_skb_tail_idx = 0;\n+\tpriv->echo_skb_head_idx = 0;\n+\tpriv->num_echo_skb = 0;\n+\tspin_unlock_irqrestore(&priv->echo_skb_spinlock, flags);\n+\n+\tnetdev_reset_queue(netdev);\n+}\n+\n+/**\n+ * es58x_flush_pending_tx_msg() - Reset the buffer for transmission messages.\n+ * @netdev: CAN network device.\n+ *\n+ * es58x_start_xmit() will queue up to tx_bulk_max messages in\n+ * &tx_urb buffer and do a bulk send of all messages in one single URB\n+ * (c.f. xmit_more flag). When the device recovers from a bus off\n+ * state or when the device stops, the tx_urb buffer might still have\n+ * pending messages in it and thus need to be flushed.\n+ */\n+static void es58x_flush_pending_tx_msg(struct net_device *netdev)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tstruct es58x_device *es58x_dev = priv->es58x_dev;\n+\tunsigned long flags;\n+\tint i;\n+\n+\tspin_lock_irqsave(&priv->echo_skb_spinlock, flags);\n+\tif (priv->tx_urb) {\n+\t\tunsigned int frames_bytes = 0;\n+\t\tu16 head_idx = priv->echo_skb_head_idx;\n+\n+\t\tnetdev_warn(netdev,\n+\t\t\t    \"%s: dropping %d tx_msg that were pending for transmission\\n\",\n+\t\t\t    __func__, priv->tx_can_msg_cnt);\n+\t\tfor (i = 0; i < priv->tx_can_msg_cnt; i++) {\n+\t\t\tframes_bytes +=\n+\t\t\t    es58x_can_frame_bytes(priv->can.echo_skb[head_idx]);\n+\t\t\tcan_free_echo_skb(netdev, head_idx);\n+\t\t\tes58x_add_skb_idx(priv, &head_idx, 1);\n+\t\t}\n+\t\t__netdev_sent_queue(netdev, 0, false);\n+\t\tnetdev_completed_queue(netdev,\n+\t\t\t\t       priv->tx_can_msg_cnt, frames_bytes);\n+\t\tusb_anchor_urb(priv->tx_urb, &priv->es58x_dev->tx_urbs_idle);\n+\t\tnetdev->stats.tx_dropped += priv->tx_can_msg_cnt;\n+\n+\t\tatomic_inc(&es58x_dev->tx_urbs_idle_cnt);\n+\t\tusb_free_urb(priv->tx_urb);\n+\t}\n+\tpriv->tx_urb = NULL;\n+\tpriv->tx_can_msg_cnt = 0;\n+\tspin_unlock_irqrestore(&priv->echo_skb_spinlock, flags);\n+}\n+\n+/**\n+ * es58x_tx_ack_msg() - Handle acknowledgment messages.\n+ * @netdev: CAN network device.\n+ * @tx_free_entries: Number of free entries in the device transmit FIFO.\n+ * @rx_cmd_ret_u32: error code as returned by the ES58X device.\n+ *\n+ * ES58X sends an acknowledgment message after a transmission request\n+ * is done. This is mandatory for the ES581.4 but is optional (and\n+ * deactivated in this driver) for the ES58X_FD family.\n+ *\n+ * Under normal circumstances, this function should never throw an\n+ * error message.\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+int es58x_tx_ack_msg(struct net_device *netdev, u16 tx_free_entries,\n+\t\t     enum es58x_ret_u32 rx_cmd_ret_u32)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\n+\tif (tx_free_entries <= priv->es58x_dev->param->tx_bulk_max) {\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_err(netdev,\n+\t\t\t\t   \"Only %d entries remaining in device queue, echo_skb_max: %d, num_echo_skb: %d, tx_free_entries + num_echo_skb: %d\\n\",\n+\t\t\t\t   tx_free_entries, priv->can.echo_skb_max,\n+\t\t\t\t   priv->num_echo_skb,\n+\t\t\t\t   tx_free_entries + priv->num_echo_skb);\n+\t\tnetif_stop_queue(netdev);\n+\t}\n+\n+\treturn es58x_rx_cmd_ret_u32(netdev, ES58X_RET_TYPE_TX_MSG,\n+\t\t\t\t    rx_cmd_ret_u32);\n+}\n+\n+/**\n+ * es58x_rx_can_msg() - Handle a received a CAN message.\n+ * @netdev: CAN network device.\n+ * @timestamp: Hardware time stamp (only relevant in rx branches).\n+ * @data: CAN payload.\n+ * @can_id: CAN ID.\n+ * @es58x_flags: Please refer to enum es58x_flag.\n+ * @dlc: Data Length Code (raw value).\n+ *\n+ * Fill up a CAN skb and post it.\n+ *\n+ * This function handles the case where the DLC of a classical CAN\n+ * frame is greater than CAN_MAX_DLEN (c.f. the len8_dlc field of\n+ * struct can_frame).\n+ *\n+ * Return: zero on success.\n+ */\n+int es58x_rx_can_msg(struct net_device *netdev, u64 timestamp, const u8 *data,\n+\t\t     canid_t can_id, enum es58x_flag es58x_flags, u8 dlc)\n+{\n+\tstruct canfd_frame *cfd;\n+\tstruct can_frame *ccf;\n+\tstruct sk_buff *skb;\n+\tu8 len;\n+\tbool is_can_fd = !!(es58x_flags & ES58X_FLAG_FD_DATA);\n+\n+\tif (dlc > CAN_MAX_RAW_DLC) {\n+\t\tnetdev_err(netdev,\n+\t\t\t   \"%s: DLC is %d but maximum should be %d\\n\",\n+\t\t\t   __func__, dlc, CAN_MAX_RAW_DLC);\n+\t\treturn -EMSGSIZE;\n+\t}\n+\n+\tif (is_can_fd) {\n+\t\tlen = can_dlc2len(dlc);\n+\t\tskb = alloc_canfd_skb(netdev, &cfd);\n+\t} else {\n+\t\tlen = can_get_cc_len(dlc);\n+\t\tskb = alloc_can_skb(netdev, &ccf);\n+\t\tcfd = (struct canfd_frame *)ccf;\n+\t}\n+\n+\tif (!skb) {\n+\t\tnetdev->stats.rx_dropped++;\n+\t\treturn -ENOMEM;\n+\t}\n+\tcfd->can_id = can_id;\n+\tcfd->len = len;\n+\tif (es58x_flags & ES58X_FLAG_EFF)\n+\t\tcfd->can_id |= CAN_EFF_FLAG;\n+\tif (is_can_fd) {\n+\t\tif (es58x_flags & ES58X_FLAG_FD_BRS)\n+\t\t\tcfd->flags |= CANFD_BRS;\n+\t\tif (es58x_flags & ES58X_FLAG_FD_ESI)\n+\t\t\tcfd->flags |= CANFD_ESI;\n+\t} else {\n+\t\tccf->len8_dlc = can_get_len8_dlc(es58x_priv(netdev)->can.ctrlmode,\n+\t\t\t\t\t\t len, dlc);\n+\t\tif (es58x_flags & ES58X_FLAG_RTR) {\n+\t\t\tccf->can_id |= CAN_RTR_FLAG;\n+\t\t\tlen = 0;\n+\t\t}\n+\t}\n+\tmemcpy(cfd->data, data, len);\n+\tnetdev->stats.rx_packets++;\n+\tnetdev->stats.rx_bytes += len;\n+\n+\tes58x_set_skb_timestamp(netdev, skb, timestamp);\n+\tnetif_rx(skb);\n+\n+\tes58x_priv(netdev)->err_passive_before_rtx_success = 0;\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_rx_err_msg() - Handle a received CAN event or error message.\n+ * @netdev: CAN network device.\n+ * @error: Error code.\n+ * @event: Event code.\n+ * @timestamp: Timestamp received from a ES58X device.\n+ *\n+ * Handle the errors and events received by the ES58X device, create\n+ * a CAN error skb and post it.\n+ *\n+ * In some rare cases the devices might get stucked alternating between\n+ * CAN_STATE_ERROR_PASSIVE and CAN_STATE_ERROR_WARNING. To prevent\n+ * this behavior, we force a bus off state if the device goes in\n+ * CAN_STATE_ERROR_WARNING for ES58X_MAX_CONSECUTIVE_WARN consecutive\n+ * times with no successful transmission or reception in between.\n+ *\n+ * Once the device is in bus off state, the only way to restart it is\n+ * through the drivers/net/can/dev.c:can_restart() function. The\n+ * device is technically capable to recover by itself under certain\n+ * circumstances, however, allowing self recovery would create\n+ * complex race conditions with drivers/net/can/dev.c:can_restart()\n+ * and thus was not implemented. To activate automatic restart, please\n+ * set the restart-ms parameter (e.g. ip link set can0 type can\n+ * restart-ms 100).\n+ *\n+ * If the bus is really instable, this function would try to send a\n+ * lot of log messages. Those are rate limited (i.e. you will see\n+ * messages such as \"net_ratelimit: XXX callbacks suppressed\" in\n+ * dmesg).\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+int es58x_rx_err_msg(struct net_device *netdev, enum es58x_err error,\n+\t\t     enum es58x_event event, u64 timestamp)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tstruct can_priv *can = netdev_priv(netdev);\n+\tstruct can_device_stats *can_stats = &can->can_stats;\n+\tstruct can_frame *cf;\n+\tstruct sk_buff *skb;\n+\tunsigned long flags;\n+\n+\tif (!netif_running(netdev)) {\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_info(netdev, \"%s: %s is down, dropping packet\\n\",\n+\t\t\t\t    __func__, netdev->name);\n+\t\tnetdev->stats.rx_dropped++;\n+\t\treturn 0;\n+\t}\n+\n+\tif (error == ES58X_ERR_OK && event == ES58X_EVENT_OK) {\n+\t\tnetdev_err(netdev, \"%s: Both error and event are zero\\n\",\n+\t\t\t   __func__);\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tskb = alloc_can_err_skb(netdev, &cf);\n+\tif (!skb)\n+\t\treturn -ENOMEM;\n+\n+\tswitch (error) {\n+\tcase ES58X_ERR_OK:\t/* 0: No error */\n+\t\tbreak;\n+\n+\tcase ES58X_ERR_PROT_STUFF:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_dbg(netdev, \"Error BITSUFF\\n\");\n+\t\tcf->data[2] |= CAN_ERR_PROT_STUFF;\n+\t\tbreak;\n+\n+\tcase ES58X_ERR_PROT_FORM:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_dbg(netdev, \"Error FORMAT\\n\");\n+\t\tcf->data[2] |= CAN_ERR_PROT_FORM;\n+\t\tbreak;\n+\n+\tcase ES58X_ERR_ACK:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_dbg(netdev, \"Error ACK\\n\");\n+\t\tcf->can_id |= CAN_ERR_ACK;\n+\t\tbreak;\n+\n+\tcase ES58X_ERR_PROT_BIT:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_dbg(netdev, \"Error BIT\\n\");\n+\t\tcf->data[2] |= CAN_ERR_PROT_BIT;\n+\t\tbreak;\n+\n+\tcase ES58X_ERR_PROT_CRC:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_dbg(netdev, \"Error CRC\\n\");\n+\t\tcf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;\n+\t\tbreak;\n+\n+\tcase ES58X_ERR_PROT_BIT1:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_dbg(netdev,\n+\t\t\t\t   \"Error: expected a recessive bit but monitored a dominant one\\n\");\n+\t\tcf->data[2] |= CAN_ERR_PROT_BIT1;\n+\t\tbreak;\n+\n+\tcase ES58X_ERR_PROT_BIT0:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_dbg(netdev,\n+\t\t\t\t   \"Error expected a dominant bit but monitored a recessive one\\n\");\n+\t\tcf->data[2] |= CAN_ERR_PROT_BIT0;\n+\t\tbreak;\n+\n+\tcase ES58X_ERR_PROT_OVERLOAD:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_dbg(netdev, \"Error OVERLOAD\\n\");\n+\t\tcf->data[2] |= CAN_ERR_PROT_OVERLOAD;\n+\t\tbreak;\n+\n+\tcase ES58X_ERR_PROT_UNSPEC:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_dbg(netdev, \"Unspecified error\\n\");\n+\t\tcf->can_id |= CAN_ERR_PROT;\n+\t\tbreak;\n+\n+\tdefault:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_err(netdev,\n+\t\t\t\t   \"%s: Unspecified error code 0x%04X\\n\",\n+\t\t\t\t   __func__, (int)error);\n+\t\tcf->can_id |= CAN_ERR_PROT;\n+\t\tbreak;\n+\t}\n+\n+\tswitch (event) {\n+\tcase ES58X_EVENT_OK:\t/* 0: No event */\n+\t\tbreak;\n+\n+\tcase ES58X_EVENT_CRTL_ACTIVE:\n+\t\tif (can->state == CAN_STATE_BUS_OFF) {\n+\t\t\tnetdev_err(netdev,\n+\t\t\t\t   \"%s: state transition: BUS OFF -> ACTIVE\\n\",\n+\t\t\t\t   __func__);\n+\t\t}\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_dbg(netdev, \"Event CAN BUS ACTIVE\\n\");\n+\t\tcf->data[1] |= CAN_ERR_CRTL_ACTIVE;\n+\t\tcan->state = CAN_STATE_ERROR_ACTIVE;\n+\t\tbreak;\n+\n+\tcase ES58X_EVENT_CRTL_PASSIVE:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_dbg(netdev, \"Event CAN BUS PASSIVE\\n\");\n+\t\t/* Either TX or RX error count reached passive state\n+\t\t * but we do not know which. Setting both flags by\n+\t\t * default.\n+\t\t */\n+\t\tcf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;\n+\t\tcf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;\n+\t\tif (can->state < CAN_STATE_BUS_OFF)\n+\t\t\tcan->state = CAN_STATE_ERROR_PASSIVE;\n+\t\tcan_stats->error_passive++;\n+\t\tif (priv->err_passive_before_rtx_success < U8_MAX)\n+\t\t\tpriv->err_passive_before_rtx_success++;\n+\t\tbreak;\n+\n+\tcase ES58X_EVENT_CRTL_WARNING:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_dbg(netdev, \"Event CAN BUS WARNING\\n\");\n+\t\t/* Either TX or RX error count reached warning state\n+\t\t * but we do not know which. Setting both flags by\n+\t\t * default.\n+\t\t */\n+\t\tcf->data[1] |= CAN_ERR_CRTL_RX_WARNING;\n+\t\tcf->data[1] |= CAN_ERR_CRTL_TX_WARNING;\n+\t\tif (can->state < CAN_STATE_BUS_OFF)\n+\t\t\tcan->state = CAN_STATE_ERROR_WARNING;\n+\t\tcan_stats->error_warning++;\n+\t\tbreak;\n+\n+\tcase ES58X_EVENT_BUSOFF:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_dbg(netdev, \"Event CAN BUS OFF\\n\");\n+\t\t/* Prevent the network device queue for being woke-up\n+\t\t * by another interrupt while we set the bus to off\n+\t\t * status.\n+\t\t */\n+\t\tspin_lock_irqsave(&priv->echo_skb_spinlock, flags);\n+\t\tnetif_stop_queue(netdev);\n+\t\tif (can->state != CAN_STATE_BUS_OFF) {\n+\t\t\tcan->state = CAN_STATE_BUS_OFF;\n+\t\t\tcan_bus_off(netdev);\n+\t\t}\n+\t\tspin_unlock_irqrestore(&priv->echo_skb_spinlock, flags);\n+\t\tcf->can_id |= CAN_ERR_BUSOFF;\n+\t\tcan_stats->bus_off++;\n+\t\tbreak;\n+\n+\tcase ES58X_EVENT_SINGLE_WIRE:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_warn(netdev,\n+\t\t\t\t    \"Lost connection on either CAN high or CAN low\\n\");\n+\t\t/* Lost connection on either CAN high or CAN\n+\t\t * low. Setting both flags by default.\n+\t\t */\n+\t\tcf->data[4] |= CAN_ERR_TRX_CANH_NO_WIRE;\n+\t\tcf->data[4] |= CAN_ERR_TRX_CANL_NO_WIRE;\n+\t\tbreak;\n+\n+\tdefault:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_err(netdev,\n+\t\t\t\t   \"%s: Unspecified event code 0x%04X\\n\",\n+\t\t\t\t   __func__, (int)event);\n+\t\tcf->can_id |= CAN_ERR_CRTL;\n+\t\tbreak;\n+\t}\n+\n+\tif (cf->data[1])\n+\t\tcf->can_id |= CAN_ERR_CRTL;\n+\tif (cf->data[2] || cf->data[3]) {\n+\t\tcf->can_id |= CAN_ERR_PROT;\n+\t\tcan_stats->bus_error++;\n+\t}\n+\tif (cf->data[4])\n+\t\tcf->can_id |= CAN_ERR_TRX;\n+\n+\t/* driver/net/can/dev.c:can_restart() takes in account error\n+\t * messages in the RX stats. Doing same here for\n+\t * consistency.\n+\t */\n+\tnetdev->stats.rx_packets++;\n+\tnetdev->stats.rx_bytes += cf->can_dlc;\n+\n+\tes58x_set_skb_timestamp(netdev, skb, timestamp);\n+\tnetif_rx(skb);\n+\n+\tif ((event & ES58X_EVENT_CRTL_PASSIVE) &&\n+\t    priv->err_passive_before_rtx_success ==\n+\t    ES58X_CONSECUTIVE_ERR_PASSIVE_MAX) {\n+\t\tnetdev_info(netdev,\n+\t\t\t    \"Got %d consecutive warning events with no successful rx or tx. Forcing bus-off\\n\",\n+\t\t\t    priv->err_passive_before_rtx_success);\n+\t\treturn es58x_rx_err_msg(netdev, ES58X_ERR_OK,\n+\t\t\t\t\tES58X_EVENT_BUSOFF, timestamp);\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/* Human readable description of the &enum es58x_cmd_ret_type. */\n+static const char *const es58x_cmd_ret_desc[] = {\n+\t\"Set bittiming\",\n+\t\"Enable channel\",\n+\t\"Disable channel\",\n+\t\"Transmit message\",\n+\t\"Reset rx\",\n+\t\"Reset tx\",\n+\t\"Device error\"\n+};\n+\n+/**\n+ * es58x_rx_dev_ret_u8() - Handle return codes received from the\n+ *\tES58X device.\n+ * @dev: Device, only used for the dev_XXX() print functions.\n+ * @cmd_ret_type: Type of the command which triggered the return code.\n+ * @rx_dev_ret_u8: error code as returned by the ES58X device.\n+ *\n+ * Handles the 8 bits return code. Those are specific to the ES581.4\n+ * device. The return value will eventually be used by\n+ * es58x_handle_urb_cmd() function which will take proper actions in\n+ * case of critical issues such and memory errors or bad CRC values.\n+ *\n+ * In contrast with es58x_rx_cmd_ret_u32(), the network device is\n+ * unknown.\n+ *\n+ * Return: zero on success, return errno when any error occurs.\n+ */\n+int es58x_rx_dev_ret_u8(struct device *dev,\n+\t\t\tenum es58x_ret_type cmd_ret_type,\n+\t\t\tenum es58x_ret_u8 rx_dev_ret_u8)\n+{\n+\tconst char *ret_desc = es58x_cmd_ret_desc[cmd_ret_type];\n+\n+\tcompiletime_assert(ARRAY_SIZE(es58x_cmd_ret_desc) ==\n+\t\t\t   ES58X_RET_TYPE_NUM_ENTRIES,\n+\t\t\t   \"Size mismatch between es58x_cmd_ret_desc and enum es58x_cmd_ret_type\");\n+\n+\tswitch (rx_dev_ret_u8) {\n+\tcase ES58X_RET_U8_OK:\n+\t\tdev_dbg_ratelimited(dev, \"%s: OK\\n\", ret_desc);\n+\t\tbreak;\n+\n+\tcase ES58X_RET_U8_ERR_UNSPECIFIED_FAILURE:\n+\t\tdev_err(dev, \"%s: unspecified failure\\n\", ret_desc);\n+\t\tbreak;\n+\n+\tcase ES58X_RET_U8_ERR_NO_MEM:\n+\t\tdev_err(dev, \"%s: device ran out of memory\\n\", ret_desc);\n+\t\treturn -ENOMEM;\n+\n+\tcase ES58X_RET_U8_ERR_BAD_CRC:\n+\t\tdev_err(dev, \"%s: CRC of previous command is incorrect\\n\",\n+\t\t\tret_desc);\n+\t\treturn -EIO;\n+\n+\tdefault:\n+\t\tdev_err(dev, \"%s: returned unknown value: 0x%02X\\n\",\n+\t\t\tret_desc, rx_dev_ret_u8);\n+\t\tbreak;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_rx_dev_ret_u32() - Handle return codes received from the\n+ *\tES58X device.\n+ * @netdev: CAN network device.\n+ * @cmd_ret_type: Type of the command which triggered the return code.\n+ * @rx_cmd_ret_u32: error code as returned by the ES58X device.\n+ *\n+ * Handles the 32 bits return code. The return value will eventually\n+ * be used by es58x_handle_urb_cmd() function which will take proper\n+ * actions in case of critical issues such and memory errors or bad\n+ * CRC values.\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+int es58x_rx_cmd_ret_u32(struct net_device *netdev,\n+\t\t\t enum es58x_ret_type cmd_ret_type,\n+\t\t\t enum es58x_ret_u32 rx_cmd_ret_u32)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tconst char *ret_desc = es58x_cmd_ret_desc[cmd_ret_type];\n+\n+\tswitch (rx_cmd_ret_u32) {\n+\tcase ES58X_RET_U32_OK:\n+\t\tswitch (cmd_ret_type) {\n+\t\tcase ES58X_RET_TYPE_ENABLE_CHANNEL:\n+\t\t\tes58x_flush_pending_tx_msg(netdev);\n+\t\t\tes58x_can_flush_echo_skb(netdev);\n+\t\t\tpriv->err_passive_before_rtx_success = 0;\n+\t\t\tpriv->can.state = CAN_STATE_ERROR_ACTIVE;\n+\t\t\tes58x_netif_wake_queue(netdev);\n+\t\t\tnetdev_info(netdev,\n+\t\t\t\t    \"%s (Serial Number %s): CAN%d channel becomes ready\\n\",\n+\t\t\t\t    priv->es58x_dev->udev->product,\n+\t\t\t\t    priv->es58x_dev->udev->serial,\n+\t\t\t\t    priv->channel_idx + 1);\n+\t\t\tbreak;\n+\n+\t\tcase ES58X_RET_TYPE_TX_MSG:\n+#ifdef VERBOSE_DEBUG\n+\t\t\tif (net_ratelimit())\n+\t\t\t\tnetdev_vdbg(netdev, \"%s: OK\\n\", ret_desc);\n+#endif\n+\t\t\tbreak;\n+\n+\t\tdefault:\n+\t\t\tnetdev_dbg(netdev, \"%s: OK\\n\", ret_desc);\n+\t\t\tbreak;\n+\t\t}\n+\t\tbreak;\n+\n+\tcase ES58X_RET_U32_ERR_UNSPECIFIED_FAILURE:\n+\t\tif (cmd_ret_type == ES58X_RET_TYPE_DISABLE_CHANNEL)\n+\t\t\tnetdev_dbg(netdev,\n+\t\t\t\t   \"%s: channel is already closed\\n\", ret_desc);\n+\t\telse\n+\t\t\tnetdev_err(netdev,\n+\t\t\t\t   \"%s: unspecified failure\\n\", ret_desc);\n+\t\tbreak;\n+\n+\tcase ES58X_RET_U32_ERR_NO_MEM:\n+\t\tnetdev_err(netdev, \"%s: device ran out of memory\\n\", ret_desc);\n+\t\treturn -ENOMEM;\n+\n+\tcase ES58X_RET_U32_WARN_PARAM_ADJUSTED:\n+\t\tnetdev_warn(netdev,\n+\t\t\t    \"%s: some incompatible parameters have been adjusted\\n\",\n+\t\t\t    ret_desc);\n+\t\tbreak;\n+\n+\tcase ES58X_RET_U32_WARN_TX_MAYBE_REORDER:\n+\t\tnetdev_warn(netdev,\n+\t\t\t    \"%s: TX messages might have been reordered\\n\",\n+\t\t\t    ret_desc);\n+\t\tbreak;\n+\n+\tcase ES58X_RET_U32_ERR_TIMEOUT:\n+\t\tnetdev_err(netdev, \"%s: command timed out\\n\", ret_desc);\n+\t\tbreak;\n+\n+\tcase ES58X_RET_U32_ERR_FIFO_FULL:\n+\t\tnetdev_warn(netdev, \"%s: fifo is full\\n\", ret_desc);\n+\t\tbreak;\n+\n+\tcase ES58X_RET_U32_ERR_BAD_CONFIG:\n+\t\tnetdev_err(netdev, \"%s: bad configuration\\n\", ret_desc);\n+\t\treturn -EINVAL;\n+\n+\tcase ES58X_RET_U32_ERR_NO_RESOURCE:\n+\t\tnetdev_err(netdev, \"%s: no resource available\\n\", ret_desc);\n+\t\tbreak;\n+\n+\tdefault:\n+\t\tnetdev_err(netdev, \"%s returned unknown value: 0x%08X\\n\",\n+\t\t\t   ret_desc, rx_cmd_ret_u32);\n+\t\tbreak;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_increment_rx_errors() - Increment the network devices' error\n+ *\tcount.\n+ * @es58x_dev: ES58X device.\n+ *\n+ * If an error occurs on the early stages on receiving an URB command,\n+ * we might not be able to figure out on which network device the\n+ * error occurred. In such case, we arbitrarily increment the error\n+ * count of all the network devices attached to our ES58X device.\n+ */\n+static void es58x_increment_rx_errors(struct es58x_device *es58x_dev)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i < es58x_dev->num_can_ch; i++)\n+\t\tif (es58x_dev->netdev[i])\n+\t\t\tes58x_dev->netdev[i]->stats.rx_errors++;\n+}\n+\n+/**\n+ * es58x_handle_urb_cmd() - Handle the URB command\n+ * @es58x_dev: ES58X device.\n+ * @urb_cmd: The URB command received from the ES58X device, might not\n+ *\tbe aligned.\n+ *\n+ * Sends the URB command to the device specific function. Manages the\n+ * errors throwed back by those functions.\n+ */\n+static void es58x_handle_urb_cmd(struct es58x_device *es58x_dev,\n+\t\t\t\t const union es58x_urb_cmd *urb_cmd)\n+{\n+\tconst struct es58x_operators *ops = es58x_dev->ops;\n+\tsize_t cmd_len;\n+\tint i, ret;\n+\n+\tret = ops->handle_urb_cmd(es58x_dev, urb_cmd);\n+\tswitch (ret) {\n+\tcase 0:\t\t/* OK */\n+\t\treturn;\n+\n+\tcase -ENODEV:\n+\t\tdev_err_ratelimited(es58x_dev->dev, \"Device is not ready\\n\");\n+\t\tbreak;\n+\n+\tcase -EINVAL:\n+\tcase -EMSGSIZE:\n+\tcase -EBADRQC:\n+\tcase -EBADMSG:\n+\tcase -ECHRNG:\n+\t\tcmd_len = es58x_get_urb_cmd_len(es58x_dev,\n+\t\t\t\t\t\tops->get_msg_len(urb_cmd));\n+\t\tdev_err(es58x_dev->dev,\n+\t\t\t\"ops->handle_urb_cmd() returned error %pe\",\n+\t\t\tERR_PTR(ret));\n+\t\tes58x_print_hex_dump(urb_cmd, cmd_len);\n+\t\tbreak;\n+\n+\tcase -EFAULT:\n+\tcase -ENOMEM:\n+\tcase -EIO:\n+\tdefault:\n+\t\tdev_crit(es58x_dev->dev,\n+\t\t\t \"ops->handle_urb_cmd() returned error %pe, detaching all network devices\\n\",\n+\t\t\t ERR_PTR(ret));\n+\t\tfor (i = 0; i < es58x_dev->num_can_ch; i++)\n+\t\t\tif (es58x_dev->netdev[i])\n+\t\t\t\tnetif_device_detach(es58x_dev->netdev[i]);\n+\t\tbreak;\n+\t}\n+\n+\t/* Because the urb command could not fully be parsed,\n+\t * channel_id is not confirmed. Incrementing rx_errors count\n+\t * of all channels.\n+\t */\n+\tes58x_increment_rx_errors(es58x_dev);\n+}\n+\n+/**\n+ * es58x_check_rx_urb() - Check the length and format of the URB command.\n+ * @es58x_dev: ES58X device.\n+ * @urb_cmd: The URB command received from the ES58X device, might not\n+ *\tbe aligned.\n+ * @urb_actual_len: The actual length of the URB command.\n+ *\n+ * Check if the first message of the received urb is valid, that is to\n+ * say that both the header and the length are coherent.\n+ *\n+ * Return:\n+ * the length of the first message of the URB on success.\n+ *\n+ * -ENODATA if the URB command is incomplete (in which case, the URB\n+ * command should be buffered and combined with the next URB to try to\n+ * reconstitute the URB command).\n+ *\n+ * -EOVERFLOW if the length is bigger than the maximum expected one.\n+ *\n+ * -EBADRQC if the start of frame does not match the expected value.\n+ */\n+static signed int es58x_check_rx_urb(struct es58x_device *es58x_dev,\n+\t\t\t\t     const union es58x_urb_cmd *urb_cmd,\n+\t\t\t\t     u32 urb_actual_len)\n+{\n+\tconst struct device *dev = es58x_dev->dev;\n+\tconst struct es58x_parameters *param = es58x_dev->param;\n+\tu16 sof, msg_len;\n+\tsigned int urb_cmd_len, ret;\n+\n+\tif (urb_actual_len < param->urb_cmd_header_len) {\n+\t\tdev_vdbg(dev,\n+\t\t\t \"%s: Received %d bytes [%*ph]: header incomplete\\n\",\n+\t\t\t __func__, urb_actual_len, urb_actual_len,\n+\t\t\t urb_cmd->raw_cmd);\n+\t\treturn -ENODATA;\n+\t}\n+\n+\tsof = get_unaligned_le16(&urb_cmd->sof);\n+\tif (sof != param->rx_start_of_frame) {\n+\t\tdev_err_ratelimited(es58x_dev->dev,\n+\t\t\t\t    \"%s: Expected sequence 0x%04X for start of frame but got 0x%04X.\\n\",\n+\t\t\t\t    __func__, param->rx_start_of_frame, sof);\n+\t\treturn -EBADRQC;\n+\t}\n+\n+\tmsg_len = es58x_dev->ops->get_msg_len(urb_cmd);\n+\turb_cmd_len = es58x_get_urb_cmd_len(es58x_dev, msg_len);\n+\tif (urb_cmd_len > param->rx_urb_cmd_max_len) {\n+\t\tdev_err_ratelimited(es58x_dev->dev,\n+\t\t\t\t    \"%s: Biggest expected size for rx urb_cmd is %u but receive a command of size %d\\n\",\n+\t\t\t\t    __func__,\n+\t\t\t\t    param->rx_urb_cmd_max_len, urb_cmd_len);\n+\t\treturn -EOVERFLOW;\n+\t} else if (urb_actual_len < urb_cmd_len) {\n+\t\tdev_vdbg(dev, \"%s: Received %02d/%02d bytes\\n\",\n+\t\t\t __func__, urb_actual_len, urb_cmd_len);\n+\t\treturn -ENODATA;\n+\t}\n+\n+\tret = es58x_check_crc(es58x_dev, urb_cmd, urb_cmd_len);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\treturn urb_cmd_len;\n+}\n+\n+/**\n+ * es58x_flush_cmd_buf() - Reset the buffer for URB commands.\n+ * @es58x_dev: ES58X device.\n+ */\n+static void es58x_flush_cmd_buf(struct es58x_device *es58x_dev)\n+{\n+\tmemset(&es58x_dev->rx_cmd_buf, 0, es58x_dev->param->rx_urb_cmd_max_len);\n+\tes58x_dev->rx_cmd_buf_len = 0;\n+}\n+\n+/**\n+ * es58x_copy_to_cmd_buf() - Copy an array to the URB command buffer.\n+ * @es58x_dev: ES58X device.\n+ * @raw_cmd: the buffer we want to copy.\n+ * @raw_cmd_len: length of @raw_cmd.\n+ *\n+ * Concatenates @raw_cmd_len bytes of @raw_cmd to the end of the URB\n+ * command buffer.\n+ *\n+ * Return: zero on success, -EMSGSIZE if not enough space is available\n+ * to do the copy.\n+ */\n+static int es58x_copy_to_cmd_buf(struct es58x_device *es58x_dev,\n+\t\t\t\t u8 *raw_cmd, int raw_cmd_len)\n+{\n+\tif (es58x_dev->rx_cmd_buf_len + raw_cmd_len >\n+\t    es58x_dev->param->rx_urb_cmd_max_len)\n+\t\treturn -EMSGSIZE;\n+\n+\tmemcpy(&es58x_dev->rx_cmd_buf.raw_cmd[es58x_dev->rx_cmd_buf_len],\n+\t       raw_cmd, raw_cmd_len);\n+\tes58x_dev->rx_cmd_buf_len += raw_cmd_len;\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_split_urb_try_recovery() - Try to recover bad URB sequences.\n+ * @es58x_dev: ES58X device.\n+ * @raw_cmd: pointer to the buffer we want to copy.\n+ * @raw_cmd_len: length of @raw_cmd.\n+ *\n+ * Under some rare conditions, we might get incorrect URBs from the\n+ * device. From our observations, one of the valid URB gets replaced\n+ * by one from the past. The full root cause is not identified.\n+ *\n+ * This function looks for the next start of frame in the urb buffer\n+ * in order to try to recover.\n+ *\n+ * Such behavior was not observed on the devices of the ES58X FD\n+ * family and only seems to impact the ES581.4.\n+ *\n+ * Return: the number of bytes dropped on success, -EBADMSG if recovery failed.\n+ */\n+static int es58x_split_urb_try_recovery(struct es58x_device *es58x_dev,\n+\t\t\t\t\tu8 *raw_cmd, size_t raw_cmd_len)\n+{\n+\tunion es58x_urb_cmd *urb_cmd;\n+\tsigned int urb_cmd_len;\n+\tu16 sof;\n+\tint dropped_bytes = 0;\n+\n+\tes58x_increment_rx_errors(es58x_dev);\n+\n+\twhile (raw_cmd_len > sizeof(sof)) {\n+\t\turb_cmd = (union es58x_urb_cmd *)raw_cmd;\n+\t\tsof = get_unaligned_le16(&urb_cmd->sof);\n+\n+\t\tif (sof == es58x_dev->param->rx_start_of_frame) {\n+\t\t\turb_cmd_len = es58x_check_rx_urb(es58x_dev,\n+\t\t\t\t\t\t\t urb_cmd, raw_cmd_len);\n+\t\t\tif ((urb_cmd_len == -ENODATA) || urb_cmd_len > 0) {\n+\t\t\t\tdev_info_ratelimited(es58x_dev->dev,\n+\t\t\t\t\t\t     \"Recovery successful! Dropped %d bytes (urb_cmd_len: %d)\\n\",\n+\t\t\t\t\t\t     dropped_bytes,\n+\t\t\t\t\t\t     urb_cmd_len);\n+\t\t\t\treturn dropped_bytes;\n+\t\t\t}\n+\t\t}\n+\t\traw_cmd++;\n+\t\traw_cmd_len--;\n+\t\tdropped_bytes++;\n+\t}\n+\n+\tdev_warn_ratelimited(es58x_dev->dev, \"%s: Recovery failed\\n\", __func__);\n+\treturn -EBADMSG;\n+}\n+\n+/**\n+ * es58x_handle_incomplete_cmd() - Reconstitute an URB command from\n+ *\tdifferent URB pieces.\n+ * @es58x_dev: ES58X device.\n+ * @urb: last urb buffer received.\n+ *\n+ * The device might split the URB commands in an arbitrary amount of\n+ * pieces. This function concatenates those in an URB buffer until a\n+ * full URB command is reconstituted and consume it.\n+ *\n+ * Return:\n+ * number of bytes consumed from @urb if successful.\n+ *\n+ * -ENODATA if the URB command is still incomplete.\n+ *\n+ * -EBADMSG if the URB command is incorrect.\n+ */\n+static signed int es58x_handle_incomplete_cmd(struct es58x_device *es58x_dev,\n+\t\t\t\t\t      struct urb *urb)\n+{\n+\tsize_t cpy_len;\n+\tsigned int urb_cmd_len, tmp_cmd_buf_len, ret;\n+\n+\ttmp_cmd_buf_len = es58x_dev->rx_cmd_buf_len;\n+\tcpy_len = min_t(int, es58x_dev->param->rx_urb_cmd_max_len -\n+\t\t\tes58x_dev->rx_cmd_buf_len, urb->actual_length);\n+\tret = es58x_copy_to_cmd_buf(es58x_dev, urb->transfer_buffer, cpy_len);\n+\tif (ret < 0)\n+\t\treturn ret;\n+\n+\turb_cmd_len = es58x_check_rx_urb(es58x_dev, &es58x_dev->rx_cmd_buf,\n+\t\t\t\t\t es58x_dev->rx_cmd_buf_len);\n+\tif (urb_cmd_len == -ENODATA) {\n+\t\treturn -ENODATA;\n+\t} else if (urb_cmd_len < 0) {\n+\t\tdev_err_ratelimited(es58x_dev->dev,\n+\t\t\t\t    \"Could not reconstitute incomplete command from previous URB, dropping %d bytes\\n\",\n+\t\t\t\t    tmp_cmd_buf_len + urb->actual_length);\n+\t\tdev_err_ratelimited(es58x_dev->dev,\n+\t\t\t\t    \"Error code: %pe, es58x_dev->rx_cmd_buf_len: %d, urb->actual_length: %u\\n\",\n+\t\t\t\t    ERR_PTR(urb_cmd_len),\n+\t\t\t\t    tmp_cmd_buf_len, urb->actual_length);\n+\t\tes58x_print_hex_dump(&es58x_dev->rx_cmd_buf, tmp_cmd_buf_len);\n+\t\tes58x_print_hex_dump(urb->transfer_buffer, urb->actual_length);\n+\t\treturn urb->actual_length;\n+\t}\n+\n+\tes58x_handle_urb_cmd(es58x_dev, &es58x_dev->rx_cmd_buf);\n+\treturn urb_cmd_len - tmp_cmd_buf_len;\t/* consumed length */\n+}\n+\n+/**\n+ * es58x_split_urb() - Cut the received URB in individual URB commands.\n+ * @es58x_dev: ES58X device.\n+ * @urb: last urb buffer received.\n+ *\n+ * The device might send urb in bulk format (i.e. several URB commands\n+ * concatenated together). This function will split all the commands\n+ * contained in the urb.\n+ *\n+ * Return:\n+ * number of bytes consumed from @urb if successful.\n+ *\n+ * -ENODATA if the URB command is still incomplete.\n+ *\n+ * -EBADMSG if the URB command is incorrect.\n+ */\n+static signed int es58x_split_urb(struct es58x_device *es58x_dev,\n+\t\t\t\t  struct urb *urb)\n+{\n+\tconst u8 es58x_is_alive = 0x11;\n+\tunion es58x_urb_cmd *urb_cmd;\n+\tu8 *raw_cmd = urb->transfer_buffer;\n+\tssize_t raw_cmd_len = urb->actual_length;\n+\tint ret;\n+\n+\tif (es58x_dev->rx_cmd_buf_len != 0) {\n+\t\tret = es58x_handle_incomplete_cmd(es58x_dev, urb);\n+\t\tif (ret != -ENODATA)\n+\t\t\tes58x_flush_cmd_buf(es58x_dev);\n+\t\tif (ret < 0)\n+\t\t\treturn ret;\n+\n+\t\traw_cmd += ret;\n+\t\traw_cmd_len -= ret;\n+\t}\n+\n+\twhile (raw_cmd_len > 0) {\n+\t\tif (raw_cmd[0] == es58x_is_alive) {\n+\t\t\traw_cmd++;\n+\t\t\traw_cmd_len--;\n+\t\t\tcontinue;\n+\t\t}\n+\t\turb_cmd = (union es58x_urb_cmd *)raw_cmd;\n+\t\tret = es58x_check_rx_urb(es58x_dev, urb_cmd, raw_cmd_len);\n+\t\tif (ret > 0) {\n+\t\t\tes58x_handle_urb_cmd(es58x_dev, urb_cmd);\n+\t\t} else if (ret == -ENODATA) {\n+\t\t\tes58x_copy_to_cmd_buf(es58x_dev, raw_cmd, raw_cmd_len);\n+\t\t\treturn -ENODATA;\n+\t\t} else if (ret < 0) {\n+\t\t\tret = es58x_split_urb_try_recovery(es58x_dev, raw_cmd,\n+\t\t\t\t\t\t\t   raw_cmd_len);\n+\t\t\tif (ret < 0)\n+\t\t\t\treturn ret;\n+\t\t}\n+\t\traw_cmd += ret;\n+\t\traw_cmd_len -= ret;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_read_bulk_callback() - Callback for reading data from device.\n+ * @urb: last urb buffer received.\n+ *\n+ * This function gets eventually called each time an URB is received\n+ * from the ES58X device.\n+ *\n+ * Checks urb status, calls read function and resubmits urb read\n+ * operation.\n+ */\n+static void es58x_read_bulk_callback(struct urb *urb)\n+{\n+\tstruct es58x_device *es58x_dev = urb->context;\n+\tconst struct device *dev = es58x_dev->dev;\n+\tint i, ret;\n+\n+\tswitch (urb->status) {\n+\tcase 0:\t\t/* success */\n+\t\tbreak;\n+\n+\tcase -EOVERFLOW:\n+\t\tdev_err_ratelimited(dev, \"%s: error %pe\\n\",\n+\t\t\t\t    __func__, ERR_PTR(urb->status));\n+\t\tes58x_print_hex_dump_debug(urb->transfer_buffer,\n+\t\t\t\t\t   urb->transfer_buffer_length);\n+\t\tgoto resubmit_urb;\n+\n+\tcase -EPROTO:\n+\t\tdev_warn_ratelimited(dev, \"%s: error %pe. Device unplugged?\\n\",\n+\t\t\t\t     __func__, ERR_PTR(urb->status));\n+\t\tgoto free_urb;\n+\n+\tcase -ENOENT:\n+\tcase -EPIPE:\n+\t\tdev_err_ratelimited(dev, \"%s: error %pe\\n\",\n+\t\t\t\t    __func__, ERR_PTR(urb->status));\n+\t\tgoto free_urb;\n+\n+\tcase -ESHUTDOWN:\n+\t\tdev_dbg_ratelimited(dev, \"%s: error %pe\\n\",\n+\t\t\t\t    __func__, ERR_PTR(urb->status));\n+\t\tgoto free_urb;\n+\n+\tdefault:\n+\t\tdev_err_ratelimited(dev, \"%s: error %pe\\n\",\n+\t\t\t\t    __func__, ERR_PTR(urb->status));\n+\t\tgoto resubmit_urb;\n+\t}\n+\n+\tret = es58x_split_urb(es58x_dev, urb);\n+\tif ((ret != -ENODATA) && ret < 0) {\n+\t\tdev_err(es58x_dev->dev, \"es58x_split_urb() returned error %pe\",\n+\t\t\tERR_PTR(ret));\n+\t\tes58x_print_hex_dump_debug(urb->transfer_buffer,\n+\t\t\t\t\t   urb->actual_length);\n+\n+\t\t/* Because the urb command could not be parsed,\n+\t\t * channel_id is not confirmed. Incrementing rx_errors\n+\t\t * count of all channels.\n+\t\t */\n+\t\tes58x_increment_rx_errors(es58x_dev);\n+\t}\n+\n+ resubmit_urb:\n+\tusb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->rx_pipe,\n+\t\t\t  urb->transfer_buffer, urb->transfer_buffer_length,\n+\t\t\t  es58x_read_bulk_callback, es58x_dev);\n+\n+\tret = usb_submit_urb(urb, GFP_ATOMIC);\n+\tif (ret == -ENODEV) {\n+\t\tfor (i = 0; i < es58x_dev->num_can_ch; i++)\n+\t\t\tif (es58x_dev->netdev[i])\n+\t\t\t\tnetif_device_detach(es58x_dev->netdev[i]);\n+\t} else if (ret)\n+\t\tdev_err_ratelimited(dev,\n+\t\t\t\t    \"Failed resubmitting read bulk urb: %pe\\n\",\n+\t\t\t\t    ERR_PTR(ret));\n+\treturn;\n+\n+ free_urb:\n+\tusb_free_coherent(urb->dev, urb->transfer_buffer_length,\n+\t\t\t  urb->transfer_buffer, urb->transfer_dma);\n+}\n+\n+/**\n+ * es58x_write_bulk_callback() - Callback after writing data to the device.\n+ * @urb: urb buffer which was previously submitted.\n+ *\n+ * This function gets eventually called each time an URB was sent to\n+ * the ES58X device.\n+ *\n+ * Puts the @urb back to the urbs idle anchor and tries to restart the\n+ * network queue.\n+ */\n+static void es58x_write_bulk_callback(struct urb *urb)\n+{\n+\tstruct net_device *netdev = urb->context;\n+\tstruct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;\n+\n+\tswitch (urb->status) {\n+\tcase 0:\t\t/* success */\n+\t\tbreak;\n+\n+\tcase -EOVERFLOW:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_err(netdev, \"%s: error %pe\\n\",\n+\t\t\t\t   __func__, ERR_PTR(urb->status));\n+\t\tes58x_print_hex_dump(urb->transfer_buffer,\n+\t\t\t\t     urb->transfer_buffer_length);\n+\t\tbreak;\n+\n+\tcase -ENOENT:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_dbg(netdev, \"%s: error %pe\\n\",\n+\t\t\t\t   __func__, ERR_PTR(urb->status));\n+\t\tusb_free_coherent(urb->dev,\n+\t\t\t\t  es58x_dev->param->tx_urb_cmd_max_len,\n+\t\t\t\t  urb->transfer_buffer, urb->transfer_dma);\n+\t\treturn;\n+\n+\tdefault:\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_info(netdev, \"%s: error %pe\\n\",\n+\t\t\t\t    __func__, ERR_PTR(urb->status));\n+\t\tbreak;\n+\t}\n+\n+\tusb_anchor_urb(urb, &es58x_dev->tx_urbs_idle);\n+\t/* Wake the queue if it was stopped because of exhaustion of\n+\t * urb resources.\n+\t */\n+\tif (atomic_inc_return(&es58x_dev->tx_urbs_idle_cnt) ==\n+\t    es58x_dev->num_can_ch)\n+\t\tes58x_netif_wake_all_queues(es58x_dev);\n+}\n+\n+/**\n+ * es58x_alloc_urb() - Allocate memory for an URB and its transfer\n+ *\tbuffer.\n+ * @es58x_dev: ES58X device.\n+ * @urb: URB to be allocated.\n+ * @buf: used to return DMA address of buffer.\n+ * @buf_len: requested buffer size.\n+ * @mem_flags: affect whether allocation may block.\n+ *\n+ * Allocates an URB and its @transfer_buffer and set its @transfer_dma\n+ * address.\n+ *\n+ * This function is used at start-up to allocate all of our TX and RX\n+ * URBs at once (in order to avoid numerous memory allocations during\n+ * run time) and might be used during run time under extremely rare\n+ * conditions by @es58x_get_tx_urb() (c.f. comments of this function).\n+ *\n+ * Return: zero on success, -ENOMEM if no memory is available.\n+ */\n+static int es58x_alloc_urb(struct es58x_device *es58x_dev, struct urb **urb,\n+\t\t\t   u8 **buf, size_t buf_len, gfp_t mem_flags)\n+{\n+\t*urb = usb_alloc_urb(0, mem_flags);\n+\tif (!*urb) {\n+\t\tdev_err(es58x_dev->dev, \"No memory left for URBs\\n\");\n+\t\treturn -ENOMEM;\n+\t}\n+\n+\t*buf = usb_alloc_coherent(es58x_dev->udev, buf_len,\n+\t\t\t\t  mem_flags, &(*urb)->transfer_dma);\n+\tif (!*buf) {\n+\t\tdev_err(es58x_dev->dev, \"No memory left for USB buffer\\n\");\n+\t\tusb_free_urb(*urb);\n+\t\treturn -ENOMEM;\n+\t}\n+\n+\t(*urb)->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_get_tx_urb() - Get an URB for transmission.\n+ * @netdev: CAN network device.\n+ *\n+ * Gets an URB from the idle urbs anchor and stop all network devices'\n+ * queue if the idle urbs anchor is nearly empty.\n+ *\n+ * If the URB anchor is empty, allocates an new URB.\n+ *\n+ * Under normal circumstances, the network device stop/wake\n+ * management should prevent any further request for memory\n+ * allocations to occur when trying to send a message.\n+ *\n+ * Yet, new URB memory allocation might occur after a restart from bus\n+ * off status when @es58x_set_mode() tries to disable and re-enable\n+ * the device. This would only occur if no idle URBs are available at\n+ * the moment (which is highly improbable because\n+ * @es58x_write_bulk_callback() should have returned the URBs to the\n+ * idle urbs anchor by that point).\n+ *\n+ * Return: a pointer to an URB on success, NULL if no memory is\n+ * available.\n+ */\n+static struct urb *es58x_get_tx_urb(struct net_device *netdev)\n+{\n+\tstruct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;\n+\tstruct urb *urb;\n+\tunsigned long flags;\n+\tint ret;\n+\n+\turb = usb_get_from_anchor(&es58x_dev->tx_urbs_idle);\n+\n+\tif (!urb) {\n+\t\tsize_t tx_buf_len;\n+\t\tu8 *buf;\n+\n+\t\tnetdev_warn(netdev,\n+\t\t\t    \"%s: Idle TX URB anchor is empty. Allocating an extra URB\\n\",\n+\t\t\t    __func__);\n+\t\ttx_buf_len = es58x_dev->param->tx_urb_cmd_max_len;\n+\t\tret = es58x_alloc_urb(es58x_dev, &urb, &buf, tx_buf_len,\n+\t\t\t\t      GFP_ATOMIC);\n+\t\tif (ret)\n+\t\t\turb = NULL;\n+\t\telse\n+\t\t\tusb_fill_bulk_urb(urb, es58x_dev->udev,\n+\t\t\t\t\t  es58x_dev->tx_pipe, buf, tx_buf_len,\n+\t\t\t\t\t  NULL, NULL);\n+\t} else {\n+\t\tspin_lock_irqsave(&es58x_dev->tx_urbs_idle.lock, flags);\n+\t\tif (atomic_dec_return(&es58x_dev->tx_urbs_idle_cnt) <\n+\t\t    es58x_dev->num_can_ch)\n+\t\t\tes58x_netif_stop_all_queues(es58x_dev);\n+\t\tspin_unlock_irqrestore(&es58x_dev->tx_urbs_idle.lock, flags);\n+\t}\n+\n+\treturn urb;\n+}\n+\n+/**\n+ * es58x_submit_urb() - Send data to the device.\n+ * @es58x_dev: ES58X device.\n+ * @urb: URB to be sent.\n+ * @netdev: CAN network device.\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+static int es58x_submit_urb(struct es58x_device *es58x_dev, struct urb *urb,\n+\t\t\t    struct net_device *netdev)\n+{\n+\tint ret;\n+\n+\tes58x_set_crc(urb->transfer_buffer, urb->transfer_buffer_length);\n+\tusb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->tx_pipe,\n+\t\t\t  urb->transfer_buffer, urb->transfer_buffer_length,\n+\t\t\t  es58x_write_bulk_callback, netdev);\n+\tusb_anchor_urb(urb, &es58x_dev->tx_urbs_busy);\n+\tret = usb_submit_urb(urb, GFP_ATOMIC);\n+\tif (ret) {\n+\t\tnetdev_err(netdev, \"%s: USB send urb failure: %pe\\n\",\n+\t\t\t   __func__, ERR_PTR(ret));\n+\t\tusb_unanchor_urb(urb);\n+\t\tusb_free_coherent(urb->dev,\n+\t\t\t\t  es58x_dev->param->tx_urb_cmd_max_len,\n+\t\t\t\t  urb->transfer_buffer, urb->transfer_dma);\n+\t}\n+\tusb_free_urb(urb);\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * es58x_send_msg() - Prepare an URB and submit it.\n+ * @es58x_dev: ES58X device.\n+ * @cmd_type: Command type.\n+ * @cmd_id: Command ID.\n+ * @msg: ES58X message to be sent.\n+ * @msg_len: Length of @msg.\n+ * @channel_idx: Index of the network device.\n+ *\n+ * Creates an URB command from a given message, sets the header and the\n+ * CRC and then submits it.\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+int es58x_send_msg(struct es58x_device *es58x_dev, u8 cmd_type, u8 cmd_id,\n+\t\t   const void *msg, u16 msg_len, int channel_idx)\n+{\n+\tstruct net_device *netdev;\n+\tunion es58x_urb_cmd *urb_cmd;\n+\tstruct urb *urb;\n+\tint urb_cmd_len;\n+\n+\tif (channel_idx == ES58X_CHANNEL_IDX_NA)\n+\t\tnetdev = es58x_dev->netdev[0];\t/* Default to first channel */\n+\telse\n+\t\tnetdev = es58x_dev->netdev[channel_idx];\n+\n+\turb_cmd_len = es58x_get_urb_cmd_len(es58x_dev, msg_len);\n+\tif (urb_cmd_len > es58x_dev->param->tx_urb_cmd_max_len)\n+\t\treturn -EOVERFLOW;\n+\n+\turb = es58x_get_tx_urb(netdev);\n+\tif (!urb)\n+\t\treturn -EBUSY;\n+\n+\turb_cmd = urb->transfer_buffer;\n+\tes58x_dev->ops->fill_urb_header(urb_cmd, cmd_type, cmd_id,\n+\t\t\t\t\tchannel_idx, msg_len);\n+\tmemcpy(&urb_cmd->raw_cmd[es58x_dev->param->urb_cmd_header_len],\n+\t       msg, msg_len);\n+\turb->transfer_buffer_length = urb_cmd_len;\n+\n+\treturn es58x_submit_urb(es58x_dev, urb, netdev);\n+}\n+\n+/**\n+ * es58x_start() - Start USB device.\n+ * @es58x_dev: ES58X device.\n+ *\n+ * Allocate URBs for transmission and reception and put them in their\n+ * respective anchors.\n+ *\n+ * This function took a lot of inspiration from the other drivers in\n+ * drivers/net/can/usb.\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+static int es58x_start(struct es58x_device *es58x_dev)\n+{\n+\tconst struct device *dev = es58x_dev->dev;\n+\tconst struct es58x_parameters *param = es58x_dev->param;\n+\tsize_t rx_buf_len, tx_buf_len;\n+\tstruct urb *urb;\n+\tu8 *buf;\n+\tint i;\n+\tint ret = -EINVAL;\n+\n+\trx_buf_len = es58x_dev->rx_max_packet_size;\n+\tdev_dbg(dev, \"%s: Allocation %d rx URBs each of size %zu\\n\", __func__,\n+\t\tparam->rx_urb_max, rx_buf_len);\n+\n+\tfor (i = 0; i < param->rx_urb_max; i++) {\n+\t\tret = es58x_alloc_urb(es58x_dev, &urb, &buf, rx_buf_len,\n+\t\t\t\t      GFP_KERNEL);\n+\t\tif (ret)\n+\t\t\tbreak;\n+\n+\t\tusb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->rx_pipe,\n+\t\t\t\t  buf, rx_buf_len, es58x_read_bulk_callback,\n+\t\t\t\t  es58x_dev);\n+\t\tusb_anchor_urb(urb, &es58x_dev->rx_urbs);\n+\n+\t\tret = usb_submit_urb(urb, GFP_KERNEL);\n+\t\tif (ret) {\n+\t\t\tusb_unanchor_urb(urb);\n+\t\t\tusb_free_coherent(es58x_dev->udev, rx_buf_len,\n+\t\t\t\t\t  buf, urb->transfer_dma);\n+\t\t\tusb_free_urb(urb);\n+\t\t\tbreak;\n+\t\t}\n+\t\tusb_free_urb(urb);\n+\t}\n+\n+\tif (i == 0) {\n+\t\tdev_err(dev, \"%s: Could not setup any rx URBs\\n\", __func__);\n+\t\treturn ret;\n+\t} else if (i < param->rx_urb_max) {\n+\t\tdev_warn(dev,\n+\t\t\t \"%s: Could only allocate %d on %d URBs. Rx performance may be slow\\n\",\n+\t\t\t __func__, i, param->rx_urb_max);\n+\t}\n+\n+\ttx_buf_len = es58x_dev->param->tx_urb_cmd_max_len;\n+\tdev_dbg(dev, \"%s: Allocation %d tx URBs each of size %zu\\n\", __func__,\n+\t\tparam->tx_urb_max, tx_buf_len);\n+\n+\tfor (i = 0; i < param->tx_urb_max; i++) {\n+\t\tret = es58x_alloc_urb(es58x_dev, &urb, &buf, tx_buf_len,\n+\t\t\t\t      GFP_KERNEL);\n+\t\tif (ret)\n+\t\t\tbreak;\n+\n+\t\tusb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->tx_pipe,\n+\t\t\t\t  buf, tx_buf_len, NULL, NULL);\n+\t\tusb_anchor_urb(urb, &es58x_dev->tx_urbs_idle);\n+\t\tusb_free_urb(urb);\n+\t}\n+\n+\tif (i == 0) {\n+\t\tdev_err(dev, \"%s: Could not setup any tx URBs\\n\", __func__);\n+\t\treturn ret;\n+\t} else if (i < param->tx_urb_max) {\n+\t\tdev_warn(dev,\n+\t\t\t \"%s: Could only allocate %d on %d URBs. Tx performance may be slow\\n\",\n+\t\t\t __func__, i, param->tx_urb_max);\n+\t}\n+\tatomic_set(&es58x_dev->tx_urbs_idle_cnt, i);\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * es58x_enable_channel() - Enable the network device.\n+ * @netdev: CAN network device.\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+static int es58x_enable_channel(struct net_device *netdev)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tstruct es58x_device *es58x_dev = priv->es58x_dev;\n+\tint ret;\n+\n+\tret = es58x_dev->ops->enable_channel(es58x_dev, priv->channel_idx);\n+\tif (ret)\n+\t\tnetdev_err(netdev, \"%s: Could not enable the channel: %pe\\n\",\n+\t\t\t   __func__, ERR_PTR(ret));\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * es58x_open() - Open and start network device.\n+ * @netdev: CAN network device.\n+ *\n+ * Called when the network transitions to the up state.\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+static int es58x_open(struct net_device *netdev)\n+{\n+\tint ret;\n+\n+\tret = open_candev(netdev);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tret = es58x_enable_channel(netdev);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tnetif_start_queue(netdev);\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * es58x_stop() - Disable the network device.\n+ * @netdev: CAN network device.\n+ *\n+ * Called when the network transitions to the down state.\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+static int es58x_stop(struct net_device *netdev)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tunsigned long flags;\n+\n+\tspin_lock_irqsave(&priv->echo_skb_spinlock, flags);\n+\tnetif_stop_queue(netdev);\n+\tpriv->can.state = CAN_STATE_STOPPED;\n+\tspin_unlock_irqrestore(&priv->echo_skb_spinlock, flags);\n+\tes58x_can_flush_echo_skb(netdev);\n+\tclose_candev(netdev);\n+\n+\tes58x_flush_pending_tx_msg(netdev);\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_xmit_commit() - Send the bulk urb.\n+ * @netdev: CAN network device.\n+ *\n+ * Update the loopback FIFO and do a bulk send.\n+ *\n+ * Each loopback skb should be put individually with\n+ * can_put_echo_skb(). This function should be called only once by\n+ * bulk transmission.\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+static int es58x_xmit_commit(struct net_device *netdev)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tunsigned long flags;\n+\tunsigned int pkts = priv->tx_can_msg_cnt;\n+\tint ret;\n+\n+\tspin_lock_irqsave(&priv->echo_skb_spinlock, flags);\n+\tif (!es58x_is_can_state_active(netdev)) {\n+\t\tnetdev_warn(netdev,\n+\t\t\t    \"%s: Device turned to CAN state: %d while preparing to send. Aborting transmission\\n\",\n+\t\t\t    __func__, priv->can.state);\n+\t\tspin_unlock_irqrestore(&priv->echo_skb_spinlock, flags);\n+\t\treturn -ENETDOWN;\n+\t}\n+\tpriv->num_echo_skb += pkts;\n+\tpriv->current_packet_idx += pkts;\n+\tes58x_add_skb_idx(priv, &priv->echo_skb_head_idx, pkts);\n+\tif (priv->num_echo_skb >= es58x_echo_skb_stop_threshold(priv))\n+\t\tnetif_stop_queue(netdev);\n+\tspin_unlock_irqrestore(&priv->echo_skb_spinlock, flags);\n+\n+\tret = es58x_submit_urb(priv->es58x_dev, priv->tx_urb, netdev);\n+\tif (ret) {\n+\t\tnetdev_warn(netdev,\n+\t\t\t    \"%s: es58x_submit_urb returned failed: %pe priv->num_echo_skb = %u\\n\",\n+\t\t\t    __func__, ERR_PTR(ret), priv->num_echo_skb);\n+\t\tspin_lock_irqsave(&priv->echo_skb_spinlock, flags);\n+\t\tpriv->num_echo_skb -= pkts;\n+\t\tpriv->current_packet_idx -= pkts;\n+\t\tes58x_add_skb_idx(priv, &priv->echo_skb_head_idx,\n+\t\t\t\t  priv->can.echo_skb_max - pkts);\n+\t\tspin_unlock_irqrestore(&priv->echo_skb_spinlock, flags);\n+\t}\n+\tpriv->tx_urb = NULL;\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * es58x_start_xmit() - Transmit an skb.\n+ * @skb: socket buffer of a CAN message.\n+ * @netdev: CAN network device.\n+ *\n+ * Called when a packet needs to be transmitted.\n+ *\n+ * This function relies on Byte Queue Limits (BQL). The main benefit\n+ * it to increase the throughput by allowing bulk transfers\n+ * (c.f. xmit_more flag).\n+ *\n+ * Queues up to tx_bulk_max messages in &tx_urb buffer and does\n+ * a bulk send of all messages in one single URB.\n+ *\n+ * Return:\n+ * NETDEV_TX_OK if we could manage the @skb (either transmit it or\n+ * drop it)\n+ *\n+ * NETDEV_TX_BUSY if the device is busy (this is a bug, the network\n+ * device stop/wake management should prevent this return code to\n+ * occur).\n+ */\n+static netdev_tx_t es58x_start_xmit(struct sk_buff *skb,\n+\t\t\t\t    struct net_device *netdev)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tstruct es58x_device *es58x_dev = priv->es58x_dev;\n+\tunsigned int packet_idx, tx_msg_threshold;\n+\tu16 skb_idx;\n+\tu8 channel_idx = priv->channel_idx;\n+\tbool send_doorbell;\n+\tint ret;\n+\n+\tif (((struct can_frame *)skb->data)->can_id & CAN_ERR_FLAG) {\n+\t\tnetdev_info(netdev,\n+\t\t\t    \"%s: CAN_ERR_FLAG is set but device does not support sending error frames. Dropping message\\n\",\n+\t\t\t    __func__);\n+\t\tif (priv->tx_urb && !netdev_xmit_more()) {\n+\t\t\t__netdev_sent_queue(netdev, 0, false);\n+\t\t\tret = es58x_xmit_commit(netdev);\n+\t\t\tif (ret)\n+\t\t\t\tgoto xmit_failure;\n+\t\t}\n+\t\treturn NETDEV_TX_OK;\n+\t}\n+\n+\tif (!priv->tx_urb) {\n+\t\tpriv->tx_urb = es58x_get_tx_urb(netdev);\n+\t\tif (!priv->tx_urb)\n+\t\t\treturn NETDEV_TX_BUSY;\n+\t\tmemset(priv->tx_urb->transfer_buffer, 0,\n+\t\t       es58x_dev->param->tx_urb_cmd_max_len);\n+\t\tpriv->tx_urb->transfer_buffer_length =\n+\t\t    es58x_dev->param->urb_cmd_header_len;\n+\t\tpriv->tx_can_msg_cnt = 0;\n+\t\tpriv->tx_can_msg_is_fd = can_is_canfd_skb(skb);\n+\t} else if (priv->tx_can_msg_is_fd != can_is_canfd_skb(skb)) {\n+\t\t/* Can not send bulk message with mixed CAN and CAN-FD\n+\t\t * frames.\n+\t\t */\n+\t\t__netdev_sent_queue(netdev, 0, false);\n+\t\tret = es58x_xmit_commit(netdev);\n+\t\tif (ret)\n+\t\t\tgoto xmit_failure;\n+\t\treturn es58x_start_xmit(skb, netdev);\n+\t}\n+\n+\tpacket_idx = priv->current_packet_idx + priv->tx_can_msg_cnt;\n+\tret = es58x_dev->ops->tx_can_msg(priv, channel_idx,\n+\t\t\t\t\t packet_idx, skb,\n+\t\t\t\t\t priv->tx_urb->transfer_buffer,\n+\t\t\t\t\t &priv->tx_urb->transfer_buffer_length);\n+\tif (ret)\n+\t\tgoto xmit_failure;\n+\n+\tskb_idx = priv->echo_skb_head_idx;\n+\tes58x_add_skb_idx(priv, &skb_idx, priv->tx_can_msg_cnt);\n+\tskb_tx_timestamp(skb);\n+\tret = can_put_echo_skb(skb, netdev, skb_idx);\n+\tif (ret)\n+\t\tgoto xmit_failure;\n+\tpriv->tx_can_msg_cnt++;\n+\n+\tsend_doorbell = __netdev_sent_queue(netdev, es58x_can_frame_bytes(skb),\n+\t\t\t\t\t    netdev_xmit_more());\n+\ttx_msg_threshold = min_t(unsigned int,\n+\t\t\t\t es58x_dev->param->tx_bulk_max,\n+\t\t\t\t priv->can.echo_skb_max - priv->num_echo_skb);\n+\tif (send_doorbell || priv->tx_can_msg_cnt >= tx_msg_threshold) {\n+\t\tret = es58x_xmit_commit(netdev);\n+\t\tif (ret)\n+\t\t\tgoto xmit_failure;\n+\t}\n+\n+\treturn NETDEV_TX_OK;\n+\n+ xmit_failure:\n+\tnetdev->stats.tx_errors++;\n+\tes58x_flush_pending_tx_msg(netdev);\n+\tnetdev_err(netdev, \"%s: send message failure: %pe\\n\",\n+\t\t   __func__, ERR_PTR(ret));\n+\treturn NETDEV_TX_OK;\n+}\n+\n+static const struct net_device_ops es58x_netdev_ops = {\n+\t.ndo_open = es58x_open,\n+\t.ndo_stop = es58x_stop,\n+\t.ndo_start_xmit = es58x_start_xmit\n+};\n+\n+/**\n+ * es58x_set_bittiming() - Configure all the nominal bit timing\n+ *\tparameters.\n+ * @netdev: CAN network device.\n+ *\n+ * Only relevant for ES581.4 (the ES58X FD family sets the bittiming\n+ * parameters at the same time as the channel gets enabled).\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+static int es58x_set_bittiming(struct net_device *netdev)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tstruct es58x_device *es58x_dev = priv->es58x_dev;\n+\tint ret;\n+\n+\t/* Make sure that channel is closed before sending configuration. */\n+\tret = es58x_dev->ops->disable_channel(es58x_dev, priv->channel_idx);\n+\tif (ret)\n+\t\tnetdev_err(netdev, \"%s: Could not disable the channel: %pe\\n\",\n+\t\t\t   __func__, ERR_PTR(ret));\n+\tret = es58x_dev->ops->set_bittiming(es58x_dev, priv->channel_idx);\n+\tif (ret)\n+\t\tnetdev_err(netdev, \"%s: Could not set bittiming: %pe\\n\",\n+\t\t\t   __func__, ERR_PTR(ret));\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * es58x_set_data_bittiming() - Configure all the data bit timing parameters.\n+ * @netdev: CAN network device.\n+ *\n+ * Dummy function: ES58X FD family sets the bittiming\n+ * parameters at the same time as the channel gets enabled.\n+ *\n+ * Return: zero.\n+ */\n+static int es58x_set_data_bittiming(struct net_device *netdev)\n+{\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_reset() - Reset one channel of the device.\n+ * @netdev: CAN network device.\n+ *\n+ * Race conditions might appear when trying to use the reset_rx()\n+ * and reset_tx() methods (e.g. loopback packets might remain in the\n+ * queue causing some use after free issues in\n+ * es58x_can_get_echo_skb()). Disabling and reenabling the channel is\n+ * safer.\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+static int es58x_reset(struct net_device *netdev)\n+{\n+\tint ret = 0;\n+\n+\tif (es58x_is_can_state_active(netdev))\n+\t\tes58x_priv(netdev)->can.do_set_mode(netdev, CAN_MODE_STOP);\n+\tWARN_ON(!netif_queue_stopped(netdev));\n+\n+\tret = es58x_set_bittiming(netdev);\n+\tif (ret)\n+\t\tgoto failure;\n+\tret = es58x_enable_channel(netdev);\n+\tif (ret)\n+\t\tgoto failure;\n+\n+\treturn ret;\n+\n+ failure:\n+\tnetdev_err(netdev, \"%s: reset failure: %pe\\n\", __func__, ERR_PTR(ret));\n+\treturn ret;\n+}\n+\n+/**\n+ * es58x_set_mode() - Change network device mode.\n+ * @netdev: CAN network device.\n+ * @mode: either %CAN_MODE_START, %CAN_MODE_STOP or %CAN_MODE_SLEEP\n+ *\n+ * This function is used by drivers/net/can/dev.c:can_restart() to\n+ * recover from a bus off state. Thus only the only value expected for\n+ * @mode is %CAN_MODE_START. None the less, other modes were\n+ * implemented as well.\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+static int es58x_set_mode(struct net_device *netdev, enum can_mode mode)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tstruct can_priv *can = netdev_priv(netdev);\n+\tunsigned long flags;\n+\n+\tnetdev_dbg(netdev, \"%s: Switching to can_mode %d\\n\", __func__, mode);\n+\n+\tswitch (mode) {\n+\tcase CAN_MODE_START:\n+\t\tswitch (can->state) {\n+\t\tcase CAN_STATE_BUS_OFF:\n+\t\t\treturn es58x_reset(netdev);\n+\n+\t\tcase CAN_STATE_STOPPED:\n+\t\tcase CAN_STATE_SLEEPING:\n+\t\t\tes58x_netif_wake_queue(netdev);\n+\t\t\tcan->state = CAN_STATE_ERROR_ACTIVE;\n+\t\t\treturn 0;\n+\n+\t\tcase CAN_STATE_ERROR_ACTIVE:\n+\t\tcase CAN_STATE_ERROR_WARNING:\n+\t\tcase CAN_STATE_ERROR_PASSIVE:\n+\t\tdefault:\n+\t\t\treturn 0;\n+\t\t}\n+\n+\tcase CAN_MODE_STOP:\n+\tcase CAN_MODE_SLEEP:\n+\t\tspin_lock_irqsave(&priv->echo_skb_spinlock, flags);\n+\t\tnetif_stop_queue(netdev);\n+\t\tcan->state = (mode == CAN_MODE_STOP) ?\n+\t\t    CAN_STATE_STOPPED : CAN_STATE_SLEEPING;\n+\t\tspin_unlock_irqrestore(&priv->echo_skb_spinlock, flags);\n+\t\treturn 0;\n+\n+\tdefault:\n+\t\tnetdev_err(netdev, \"%s: Invalid can_mode value: %d\\n\",\n+\t\t\t   __func__, mode);\n+\t\treturn -EOPNOTSUPP;\n+\t}\n+}\n+\n+/**\n+ * es58x_init_priv() - Initialize private parameters.\n+ * @es58x_dev: ES58X device.\n+ * @priv: ES58X private parameters related to the network device.\n+ * @channel_idx: Index of the network device.\n+ */\n+static void es58x_init_priv(struct es58x_device *es58x_dev,\n+\t\t\t    struct es58x_priv *priv, int channel_idx)\n+{\n+\tconst struct es58x_parameters *param = es58x_dev->param;\n+\tstruct can_priv *can = &priv->can;\n+\n+\tpriv->es58x_dev = es58x_dev;\n+\tpriv->channel_idx = channel_idx;\n+\tpriv->tx_urb = NULL;\n+\tpriv->tx_can_msg_cnt = 0;\n+\tspin_lock_init(&priv->echo_skb_spinlock);\n+\n+\tcan->bittiming_const = param->bittiming_const;\n+\tcan->do_set_bittiming = es58x_set_bittiming;\n+\tif (param->ctrlmode_supported & CAN_CTRLMODE_FD) {\n+\t\tcan->data_bittiming_const = param->data_bittiming_const;\n+\t\tcan->do_set_data_bittiming = es58x_set_data_bittiming;\n+\t}\n+\tcan->bitrate_max = param->bitrate_max;\n+\tcan->clock = param->clock;\n+\n+\tcan->state = CAN_STATE_STOPPED;\n+\n+\t/* Hardware loopback is always enabled, regardless of the\n+\t * value of can.ctrlmode & CAN_CTRLMODE_LOOPBACK.\n+\t */\n+\tcan->ctrlmode_supported = param->ctrlmode_supported;\n+\tcan->do_get_berr_counter = NULL;\t/* Not supported */\n+\n+\tcan->do_set_mode = es58x_set_mode;\n+}\n+\n+/**\n+ * es58x_init_netdev() - Initialize the network device.\n+ * @es58x_dev: ES58X device.\n+ * @channel_idx: Index of the network device.\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+static int es58x_init_netdev(struct es58x_device *es58x_dev, int channel_idx)\n+{\n+\tstruct net_device *netdev;\n+\tstruct device *dev = es58x_dev->dev;\n+\tint ret;\n+\n+\tnetdev = alloc_candev(sizeof(struct es58x_priv),\n+\t\t\t      es58x_dev->param->echo_skb_max);\n+\tif (!netdev) {\n+\t\tdev_err(dev, \"Could not allocate candev\\n\");\n+\t\treturn -ENOMEM;\n+\t}\n+\tSET_NETDEV_DEV(netdev, dev);\n+\tes58x_dev->netdev[channel_idx] = netdev;\n+\tes58x_init_priv(es58x_dev, es58x_priv(netdev), channel_idx);\n+\n+\tnetdev->netdev_ops = &es58x_netdev_ops;\n+\tnetdev->flags |= IFF_ECHO;\t/* we support local echo */\n+\n+\tret = register_candev(netdev);\n+\tif (ret)\n+\t\treturn ret;\n+\tnetdev_dbg(netdev, \"%s: Registered channel %s\\n\",\n+\t\t   es58x_dev->udev->product, netdev->name);\n+\n+\tnetdev_get_tx_queue(netdev, 0)->dql.min_limit =\n+\t    es58x_dev->param->dql_limit_min;\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * es58x_get_product_info() - Get the product information and print them.\n+ * @es58x_dev: ES58X device.\n+ *\n+ * Do a synchronous call to get the product information.\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+static int es58x_get_product_info(struct es58x_device *es58x_dev)\n+{\n+\tstruct usb_device *udev = es58x_dev->udev;\n+\tconst int es58x_prod_info_idx = 6;\n+\t/* Empirical tests show a prod_info length of maximum 83,\n+\t * below should be more than enough.\n+\t */\n+\tconst size_t prod_info_len = 127;\n+\tchar *prod_info;\n+\tint ret;\n+\n+\tprod_info = kmalloc(prod_info_len, GFP_KERNEL);\n+\tif (!prod_info)\n+\t\treturn -ENOMEM;\n+\n+\tret = usb_string(udev, es58x_prod_info_idx, prod_info, prod_info_len);\n+\tif (ret < 0) {\n+\t\tdev_err(es58x_dev->dev,\n+\t\t\t\"%s: Could not read the product info: %pe\\n\",\n+\t\t\t__func__, ERR_PTR(ret));\n+\t\treturn ret;\n+\t} else if (ret >= prod_info_len - 1) {\n+\t\tdev_warn(es58x_dev->dev,\n+\t\t\t \"%s: Buffer is too small, result might be truncated\\n\",\n+\t\t\t __func__);\n+\t}\n+\tdev_info(es58x_dev->dev, \"Product info: %s\\n\", prod_info);\n+\tkfree(prod_info);\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_init_es58x_dev() - Initialize the ES58X device.\n+ * @intf: USB interface\n+ * @p_es58x_dev: pointer to the address of the ES58X device.\n+ *\n+ * Return: zero on success, errno when any error occurs.\n+ */\n+static int es58x_init_es58x_dev(struct usb_interface *intf,\n+\t\t\t\tstruct es58x_device **p_es58x_dev)\n+{\n+\tstruct device *dev = &intf->dev;\n+\tstruct es58x_device *es58x_dev;\n+\tconst struct es58x_parameters *param;\n+\tconst struct es58x_operators *ops;\n+\tstruct usb_device *udev = interface_to_usbdev(intf);\n+\tstruct usb_endpoint_descriptor *ep_in, *ep_out;\n+\tconst u16 usb_id_product = le16_to_cpu(udev->descriptor.idProduct);\n+\tu8 num_can_ch;\n+\tint ret;\n+\n+\tdev_info(dev,\n+\t\t \"Starting %s %s (Serial Number %s) driver version %s\\n\",\n+\t\t udev->manufacturer, udev->product, udev->serial, DRV_VERSION);\n+\n+\tret = usb_find_common_endpoints(intf->cur_altsetting, &ep_in, &ep_out,\n+\t\t\t\t\tNULL, NULL);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tswitch (usb_id_product) {\n+\tcase ES581_4_PRODUCT_ID:\t/* 2 Classical CAN channels */\n+\t\tparam = &es581_4_param;\n+\t\tops = &es581_4_ops;\n+\t\tnum_can_ch = ES581_4_NUM_CAN_CH;\n+\t\tbreak;\n+\n+\tcase ES582_1_PRODUCT_ID:\t/* 2 CAN(-FD) channels */\n+\t\tparam = &es58x_fd_param;\n+\t\tops = &es58x_fd_ops;\n+\t\tnum_can_ch = ES582_1_NUM_CAN_CH;\n+\t\tbreak;\n+\n+\tcase ES584_1_PRODUCT_ID:\t/* 1 CAN(-FD) and 1 LIN channel */\n+\t\tparam = &es58x_fd_param;\n+\t\tops = &es58x_fd_ops;\n+\t\tnum_can_ch = ES584_1_NUM_CAN_CH;\n+\t\tbreak;\n+\n+\tdefault:\n+\t\treturn -ENODEV;\n+\t}\n+\tif (num_can_ch > ARRAY_SIZE(es58x_dev->netdev) ||\n+\t    num_can_ch >= param->echo_skb_max)\n+\t\treturn -ECHRNG;\n+\n+\tes58x_dev = kzalloc(es58x_sizeof_es58x_device(param), GFP_KERNEL);\n+\tif (!es58x_dev)\n+\t\treturn -ENOMEM;\n+\n+\tes58x_dev->param = param;\n+\tes58x_dev->ops = ops;\n+\tes58x_dev->num_can_ch = num_can_ch;\n+\tes58x_dev->dev = dev;\n+\tes58x_dev->udev = udev;\n+\n+\tinit_usb_anchor(&es58x_dev->rx_urbs);\n+\tinit_usb_anchor(&es58x_dev->tx_urbs_idle);\n+\tinit_usb_anchor(&es58x_dev->tx_urbs_busy);\n+\tusb_set_intfdata(intf, es58x_dev);\n+\n+\tes58x_dev->rx_pipe = usb_rcvbulkpipe(es58x_dev->udev,\n+\t\t\t\t\t     ep_in->bEndpointAddress);\n+\tes58x_dev->tx_pipe = usb_sndbulkpipe(es58x_dev->udev,\n+\t\t\t\t\t     ep_out->bEndpointAddress);\n+\tes58x_dev->rx_max_packet_size = le16_to_cpu(ep_in->wMaxPacketSize);\n+\n+\t*p_es58x_dev = es58x_dev;\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_probe() - Initialize the USB device.\n+ * @intf: USB interface\n+ * @id: USB device ID\n+ *\n+ * Return: zero on success, -ENODEV if the interface is not supported\n+ * or errno when any other error occurs.\n+ */\n+static int es58x_probe(struct usb_interface *intf,\n+\t\t       const struct usb_device_id *id)\n+{\n+\tstruct es58x_device *es58x_dev;\n+\tint ch_idx, ret;\n+\n+\t/* ES58X FD has some interface protocols unsupported by this driver. */\n+\tif (intf->cur_altsetting->desc.bInterfaceProtocol != 0) {\n+\t\tdev_dbg(&intf->dev, \"Interface protocol %d is not supported\\n\",\n+\t\t\tintf->cur_altsetting->desc.bInterfaceProtocol);\n+\t\treturn -ENODEV;\n+\t}\n+\n+\tret = es58x_init_es58x_dev(intf, &es58x_dev);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tret = es58x_get_product_info(es58x_dev);\n+\tif (ret)\n+\t\tgoto cleanup_es58x_dev;\n+\n+\tfor (ch_idx = 0; ch_idx < es58x_dev->num_can_ch; ch_idx++) {\n+\t\tret = es58x_init_netdev(es58x_dev, ch_idx);\n+\t\tif (ret)\n+\t\t\tgoto cleanup_candev;\n+\t}\n+\n+\tret = es58x_start(es58x_dev);\n+\tif (ret)\n+\t\tgoto cleanup_candev;\n+\n+\tes58x_set_realtime_diff_ns(es58x_dev);\n+\n+\treturn ret;\n+\n+ cleanup_candev:\n+\tfor (ch_idx = 0; ch_idx < es58x_dev->num_can_ch; ch_idx++)\n+\t\tif (es58x_dev->netdev[ch_idx]) {\n+\t\t\tunregister_candev(es58x_dev->netdev[ch_idx]);\n+\t\t\tfree_candev(es58x_dev->netdev[ch_idx]);\n+\t\t}\n+ cleanup_es58x_dev:\n+\tkfree(es58x_dev);\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * es58x_disconnect() - Disconnect the USB device.\n+ * @intf: USB interface\n+ *\n+ * Called by the usb core when driver is unloaded or device is\n+ * removed.\n+ */\n+static void es58x_disconnect(struct usb_interface *intf)\n+{\n+\tstruct es58x_device *es58x_dev = usb_get_intfdata(intf);\n+\tstruct net_device *netdev;\n+\tstruct urb *urb;\n+\tint i;\n+\n+\tdev_info(&intf->dev, \"Disconnecting %s %s\\n\",\n+\t\t es58x_dev->udev->manufacturer, es58x_dev->udev->product);\n+\n+\tfor (i = 0; i < es58x_dev->num_can_ch; i++) {\n+\t\tnetdev = es58x_dev->netdev[i];\n+\t\tif (!netdev)\n+\t\t\tcontinue;\n+\t\tnetdev_dbg(netdev, \"Unregistering %s\\n\", netdev->name);\n+\t\tunregister_candev(netdev);\n+\t\tnetdev_dbg(netdev, \"Freeing %s\\n\", netdev->name);\n+\t\tes58x_dev->netdev[i] = NULL;\n+\t\tfree_candev(netdev);\n+\t}\n+\n+\tdev_dbg(&intf->dev, \"Killing TX busy anchored urbs\\n\");\n+\tif (!usb_wait_anchor_empty_timeout(&es58x_dev->tx_urbs_busy, 1000)) {\n+\t\tdev_err(&intf->dev, \"%s: Timeout, some TX urbs still remain\\n\",\n+\t\t\t__func__);\n+\t\tusb_kill_anchored_urbs(&es58x_dev->tx_urbs_busy);\n+\t}\n+\n+\tdev_dbg(&intf->dev, \"Freeing TX idle anchored urbs\\n\");\n+\twhile ((urb = usb_get_from_anchor(&es58x_dev->tx_urbs_idle)) != NULL) {\n+\t\tusb_free_coherent(urb->dev,\n+\t\t\t\t  es58x_dev->param->tx_urb_cmd_max_len,\n+\t\t\t\t  urb->transfer_buffer, urb->transfer_dma);\n+\t\tusb_free_urb(urb);\n+\t\tatomic_dec(&es58x_dev->tx_urbs_idle_cnt);\n+\t}\n+\tif (atomic_read(&es58x_dev->tx_urbs_idle_cnt) > 0)\n+\t\tdev_err(&intf->dev,\n+\t\t\t\"All idle urbs were freed but tx_urb_idle_cnt is %u\\n\",\n+\t\t\tatomic_read(&es58x_dev->tx_urbs_idle_cnt));\n+\n+\tdev_dbg(&intf->dev, \"Unlinking RX anchored urbs\\n\");\n+\tusb_kill_anchored_urbs(&es58x_dev->rx_urbs);\n+\n+\tdev_dbg(&intf->dev, \"Freeing %s\\n\", es58x_dev->udev->product);\n+\tkfree(es58x_dev);\n+\tdev_dbg(&intf->dev, \"Setting USB interface to NULL\\n\");\n+\tusb_set_intfdata(intf, NULL);\n+}\n+\n+static struct usb_driver es58x_driver = {\n+\t.name = ES58X_MODULE_NAME,\n+\t.probe = es58x_probe,\n+\t.disconnect = es58x_disconnect,\n+\t.id_table = es58x_id_table,\n+};\n+\n+module_usb_driver(es58x_driver);\ndiff --git a/drivers/net/can/usb/etas_es58x/es58x_core.h b/drivers/net/can/usb/etas_es58x/es58x_core.h\nnew file mode 100644\nindex 000000000000..e74fb8bc9dde\n--- /dev/null\n+++ b/drivers/net/can/usb/etas_es58x/es58x_core.h\n@@ -0,0 +1,712 @@\n+/* SPDX-License-Identifier: GPL-2.0 */\n+\n+/* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.\n+ *\n+ * File es58x_core.h: All common definitions and declarations.\n+ *\n+ * Copyright (C) 2019 Robert Bosch Engineering and Business\n+ * Solutions. All rights reserved.\n+ * Copyright (C) 2020 ETAS K.K.. All rights reserved.\n+ */\n+\n+#ifndef __ES58X_COMMON_H__\n+#define __ES58X_COMMON_H__\n+\n+#include <linux/types.h>\n+#include <linux/usb.h>\n+#include <linux/netdevice.h>\n+#include <linux/can.h>\n+#include <linux/can/dev.h>\n+\n+#include \"es581_4.h\"\n+#include \"es58x_fd.h\"\n+\n+/* Size of a Classical CAN Standard Frame (rounded up and ignoring\n+ * bitsuffing).\n+ */\n+#define ES58X_SFF_BYTES(data_len) (DIV_ROUND_UP(47, 8) + (data_len))\n+\n+/* Size of a Classical CAN Extended Frame (rounded up and ignoring\n+ * bitsuffing).\n+ */\n+#define ES58X_EFF_BYTES(data_len) (DIV_ROUND_UP(67, 8) + (data_len))\n+\n+/* Maximum size of a Classical CAN frame (rounded up and ignoring\n+ * bitsuffing).\n+ */\n+#define ES58X_CAN_FRAME_BYTES_MAX ES58X_EFF_BYTES(CAN_MAX_DLEN)\n+\n+/* Maximum size of a CAN-FD frame (rough estimation because\n+ * ES58X_SFF_BYTES() and ES58X_EFF_BYTES() macros are using the\n+ * constant values for Classical CAN, not CAN-FD).\n+ */\n+#define ES58X_CANFD_FRAME_BYTES_MAX ES58X_EFF_BYTES(CANFD_MAX_DLEN)\n+\n+/* Driver constants */\n+#define ES58X_RX_URBS_MAX 5\t/* Empirical value */\n+#define ES58X_TX_URBS_MAX 8\t/* Empirical value */\n+\n+#define ES58X_MAX(param)\t\t\t\t\\\n+\t(ES581_4_##param > ES58X_FD_##param ?\t\t\\\n+\t\tES581_4_##param : ES58X_FD_##param)\n+#define ES58X_TX_BULK_MAX ES58X_MAX(TX_BULK_MAX)\n+#define ES58X_RX_BULK_MAX ES58X_MAX(RX_BULK_MAX)\n+#define ES58X_RX_LOOPBACK_BULK_MAX ES58X_MAX(RX_LOOPBACK_BULK_MAX)\n+#define ES58X_NUM_CAN_CH_MAX ES58X_MAX(NUM_CAN_CH)\n+\n+/* Use this when channel index is irrelevant (e.g. device\n+ * timestamp).\n+ */\n+#define ES58X_CHANNEL_IDX_NA 0xFF\n+#define ES58X_EMPTY_MSG NULL\n+\n+/* Threshold on consecutive CAN_STATE_ERROR_PASSIVE. If we receive\n+ * ES58X_CONSECUTIVE_ERR_PASSIVE_MAX times the event\n+ * ES58X_ERR_CRTL_PASSIVE in a row without any successful Rx or Tx,\n+ * we force the device to switch to CAN_STATE_BUS_OFF state.\n+ */\n+#define ES58X_CONSECUTIVE_ERR_PASSIVE_MAX 254\n+\n+enum es58x_self_reception {\n+\tES58X_SELF_RECEPTION_OFF = 0,\n+\tES58X_SELF_RECEPTION_ON = 1\n+};\n+\n+enum es58x_physical_media {\n+\tES58X_PHYSICAL_MEDIA_HIGH_SPEED = 1\n+};\n+\n+enum es58x_samples_per_bit {\n+\tES58X_SAMPLES_PER_BIT_ONE = 1,\n+\tES58X_SAMPLES_PER_BIT_THREE = 2\n+};\n+\n+/**\n+ * enum es58x_sync_edge - Synchronization method.\n+ * @ES58X_SYNC_EDGE_SINGLE: ISO CAN specification defines the use of a\n+ *\tsingle edge synchronization.  The synchronization should be\n+ *\tdone on recessive to dominant level change.\n+ *\n+ * Some product of the ETAS portfolio also support a double\n+ * synchronization, requiring both recessive to dominant then dominant\n+ * to recessive level change. However, the devices of this driver do\n+ * not support the option, thus, the enum has only one member.\n+ */\n+enum es58x_sync_edge {\n+\tES58X_SYNC_EDGE_SINGLE = 1\n+};\n+\n+/**\n+ * enum es58x_flag - CAN flags for RX/TX messages.\n+ * @ES58X_FLAG_EFF: Extended Frame Format (EFF).\n+ * @ES58X_FLAG_RTR: Remote Transmission Request (RTR).\n+ * @ES58X_FLAG_FD_BRS: Bit rate switch (BRS): second bitrate for\n+ *\tpayload data.\n+ * @ES58X_FLAG_FD_ESI: Error State Indicator (ESI): tell if the\n+ *\ttransmitting node is in error passive mode.\n+ * @ES58X_FLAG_FD_DATA: CAN FD frame.\n+ */\n+enum es58x_flag {\n+\tES58X_FLAG_EFF = BIT(0),\n+\tES58X_FLAG_RTR = BIT(1),\n+\tES58X_FLAG_FD_BRS = BIT(3),\n+\tES58X_FLAG_FD_ESI = BIT(5),\n+\tES58X_FLAG_FD_DATA = BIT(6)\n+};\n+\n+/**\n+ * enum es58x_err - CAN error detection.\n+ * @ES58X_ERR_OK: No errors.\n+ * @ES58X_ERR_PROT_STUFF: Bit stuffing error: more than 5 consecutive\n+ *\tequal bits.\n+ * @ES58X_ERR_PROT_FORM: Frame format error.\n+ * @ES58X_ERR_ACK: Received no ACK on transmission.\n+ * @ES58X_ERR_PROT_BIT: Single bit error.\n+ * @ES58X_ERR_PROT_CRC: Incorrect 15, 17 or 21 bits CRC.\n+ * @ES58X_ERR_PROT_BIT1: Unable to send recessive bit: tried to send\n+ *\trecessive bit 1 but monitored dominant bit 0.\n+ * @ES58X_ERR_PROT_BIT0: Unable to send dominant bit: tried to send\n+ *\tdominant bit 0 but monitored recessive bit 1.\n+ * @ES58X_ERR_PROT_OVERLOAD: Bus overload.\n+ * @ES58X_ERR_PROT_UNSPEC: Unspecified.\n+ *\n+ * Please refer to ISO 11898-1:2015, section 10.11 \"Error detection\"\n+ * and section 10.13 \"Overload signaling\" for additional details.\n+ */\n+enum es58x_err {\n+\tES58X_ERR_OK = 0,\n+\tES58X_ERR_PROT_STUFF = BIT(0),\n+\tES58X_ERR_PROT_FORM = BIT(1),\n+\tES58X_ERR_ACK = BIT(2),\n+\tES58X_ERR_PROT_BIT = BIT(3),\n+\tES58X_ERR_PROT_CRC = BIT(4),\n+\tES58X_ERR_PROT_BIT1 = BIT(5),\n+\tES58X_ERR_PROT_BIT0 = BIT(6),\n+\tES58X_ERR_PROT_OVERLOAD = BIT(7),\n+\tES58X_ERR_PROT_UNSPEC = BIT(31)\n+};\n+\n+/**\n+ * enum es58x_event - CAN error codes returned by the device.\n+ * @ES58X_EVENT_OK: No errors.\n+ * @ES58X_EVENT_CRTL_ACTIVE: Active state: both TR and RX error count\n+ *\tis less than 128.\n+ * @ES58X_EVENT_CRTL_PASSIVE: Passive state: either TX or RX error\n+ *\tcount is greater than 127.\n+ * @ES58X_EVENT_CRTL_WARNING: Warning state: either TX or RX error\n+ *\tcount is greater than 96.\n+ * @ES58X_EVENT_BUSOFF: Bus off.\n+ * @ES58X_EVENT_SINGLE_WIRE: Lost connection on either CAN high or CAN\n+ *\tlow.\n+ *\n+ * Please refer to ISO 11898-1:2015, section 12.1.4 \"Rules of fault\n+ * confinement\" for additional details.\n+ */\n+enum es58x_event {\n+\tES58X_EVENT_OK = 0,\n+\tES58X_EVENT_CRTL_ACTIVE = BIT(0),\n+\tES58X_EVENT_CRTL_PASSIVE = BIT(1),\n+\tES58X_EVENT_CRTL_WARNING = BIT(2),\n+\tES58X_EVENT_BUSOFF = BIT(3),\n+\tES58X_EVENT_SINGLE_WIRE = BIT(4)\n+};\n+\n+/**\n+ * enum es58x_ret_u8 - Device return error codes, 8 bit format.\n+ *\n+ * Specific to ES581.4.\n+ */\n+enum es58x_ret_u8 {\n+\tES58X_RET_U8_OK = 0x00,\n+\tES58X_RET_U8_ERR_UNSPECIFIED_FAILURE = 0x80,\n+\tES58X_RET_U8_ERR_NO_MEM = 0x81,\n+\tES58X_RET_U8_ERR_BAD_CRC = 0x99\n+};\n+\n+/**\n+ * enum es58x_ret_u32 - Device return error codes, 32 bit format.\n+ */\n+enum es58x_ret_u32 {\n+\tES58X_RET_U32_OK = 0x00000000UL,\n+\tES58X_RET_U32_ERR_UNSPECIFIED_FAILURE = 0x80000000UL,\n+\tES58X_RET_U32_ERR_NO_MEM = 0x80004001UL,\n+\tES58X_RET_U32_WARN_PARAM_ADJUSTED = 0x40004000UL,\n+\tES58X_RET_U32_WARN_TX_MAYBE_REORDER = 0x40004001UL,\n+\tES58X_RET_U32_ERR_TIMEOUT = 0x80000008UL,\n+\tES58X_RET_U32_ERR_FIFO_FULL = 0x80003002UL,\n+\tES58X_RET_U32_ERR_BAD_CONFIG = 0x80004000UL,\n+\tES58X_RET_U32_ERR_NO_RESOURCE = 0x80004002UL\n+};\n+\n+/**\n+ * enum es58x_ret_type - Type of the command returned by the ES58X\n+ *\tdevice.\n+ */\n+enum es58x_ret_type {\n+\tES58X_RET_TYPE_SET_BITTIMING,\n+\tES58X_RET_TYPE_ENABLE_CHANNEL,\n+\tES58X_RET_TYPE_DISABLE_CHANNEL,\n+\tES58X_RET_TYPE_TX_MSG,\n+\tES58X_RET_TYPE_RESET_RX,\n+\tES58X_RET_TYPE_RESET_TX,\n+\tES58X_RET_TYPE_DEVICE_ERR,\n+\tES58X_RET_TYPE_NUM_ENTRIES\n+};\n+\n+union es58x_urb_cmd {\n+\tstruct es581_4_urb_cmd es581_4_urb_cmd;\n+\tstruct es58x_fd_urb_cmd es58x_fd_urb_cmd;\n+\tstruct {\t\t/* Common header parts of all variants */\n+\t\t__le16 sof;\n+\t\tu8 cmd_type;\n+\t\tu8 cmd_id;\n+\t} __packed;\n+\tu8 raw_cmd[0];\n+};\n+\n+/**\n+ * struct es58x_priv - All information specific to a can channel.\n+ * @can: struct can_priv must be the first member (Socket CAN relies\n+ *\ton the fact that function netdev_priv() returns a pointer to\n+ *\ta struct can_priv).\n+ * @es58x_dev: pointer to the corresponding ES58X device.\n+ * @tx_urb: Used as a buffer to concatenate the TX messages and to do\n+ *\ta bulk send. Please refer to es58x_start_xmit() for more\n+ *\tdetails.\n+ * @echo_skb_spinlock: Spinlock to protect the access to the echo skb\n+ *\tFIFO.\n+ * @current_packet_idx: Keeps track of the packet indexes.\n+ * @echo_skb_tail_idx: beginning of the echo skb FIFO, i.e. index of\n+ *\tthe first element.\n+ * @echo_skb_head_idx: end of the echo skb FIFO plus one, i.e. first\n+ *\tfree index.\n+ * @num_echo_skb: actual number of elements in the FIFO. Thus, the end\n+ *\tof the FIFO is echo_skb_head = (echo_skb_tail_idx +\n+ *\tnum_echo_skb) % can.echo_skb_max.\n+ * @tx_can_msg_is_fd: false: all messages in @tx_urb are Classical\n+ *\tCAN, true: all messages in @tx_urb are CAN-FD. Rationale:\n+ *\tES58X FD devices do not allow to mix standard and FD CAN in\n+ *\tone single bulk transmission.\n+ * @tx_can_msg_cnt: Number of messages in @tx_urb.\n+ * @err_passive_before_rtx_success: The ES58X device might enter in a\n+ *\tstate in which it keeps alternating between error passive\n+ *\tand active state. This counter keeps track of the number of\n+ *\terror passive and if it gets bigger than\n+ *\tES58X_CONSECUTIVE_ERR_PASSIVE_MAX, es58x_rx_err_msg() will\n+ *\tforce the status to bus-off.\n+ * @channel_idx: Channel index, starts at zero.\n+ */\n+struct es58x_priv {\n+\tstruct can_priv can;\n+\tstruct es58x_device *es58x_dev;\n+\tstruct urb *tx_urb;\n+\n+\tspinlock_t echo_skb_spinlock;\t/* Comments: c.f. supra */\n+\tu32 current_packet_idx;\n+\tu16 echo_skb_tail_idx;\n+\tu16 echo_skb_head_idx;\n+\tu16 num_echo_skb;\n+\n+\tbool tx_can_msg_is_fd;\n+\tu8 tx_can_msg_cnt;\n+\n+\tu8 err_passive_before_rtx_success;\n+\n+\tu8 channel_idx;\n+};\n+\n+/**\n+ * struct es58x_parameters - Constant parameters of a given hardware\n+ *\tvariant.\n+ * @can_bittiming_const: Nominal bittimming parameters.\n+ * @data_bittiming_const: Data bittiming parameters.\n+ * @bitrate_max: Maximum bitrate supported by the device.\n+ * @clock: CAN clock parameters.\n+ * @ctrlmode_supported: List of supported modes. Please refer to\n+ *\tcan/netlink.h file for additional details.\n+ * @tx_start_of_frame: Magic number at the beginning of each TX URB\n+ *\tcommand.\n+ * @rx_start_of_frame: Magic number at the beginning of each RX URB\n+ *\tcommand.\n+ * @tx_urb_cmd_max_len: Maximum length of a TX URB command.\n+ * @rx_urb_cmd_max_len: Maximum length of a RX URB command.\n+ * @echo_skb_max: Maximum number of echo SKB. This value must not\n+ *\texceed the maximum size of the device internal TX FIFO\n+ *\tlength. This parameter is used to control the network queue\n+ *\twake/stop logic.\n+ * @dql_limit_min: Dynamic Queue Limits (DQL) absolute minimum limit\n+ *\tof bytes allowed to be queued on this network device transmit\n+ *\tqueue. Used by the Byte Queue Limits (BQL) to determine how\n+ *\tfrequently the xmit_more flag will be set to true in\n+ *\tes58x_start_xmit(). Set this value higher to optimize for\n+ *\tthroughput but be aware that it might have a negative impact\n+ *\ton the latency! This value can also be set dynamically. Please\n+ *\trefer to Documentation/ABI/testing/sysfs-class-net-queues for\n+ *\tmore details.\n+ * @tx_bulk_max: Maximum number of Tx messages that can be sent in one\n+ *\tsingle URB packet.\n+ * @urb_cmd_header_len: Length of the URB command header.\n+ * @rx_urb_max: Number of RX URB to be allocated during device probe.\n+ * @tx_urb_max: Number of TX URB to be allocated during device probe.\n+ * @channel_idx_offset: Some of the ES58x starts channel numbering\n+ *\tfrom 0 (ES58X FD), others from 1 (ES581.4).\n+ */\n+struct es58x_parameters {\n+\tconst struct can_bittiming_const *bittiming_const;\n+\tconst struct can_bittiming_const *data_bittiming_const;\n+\tu32 bitrate_max;\n+\tstruct can_clock clock;\n+\tu32 ctrlmode_supported;\n+\tu16 tx_start_of_frame;\n+\tu16 rx_start_of_frame;\n+\tu16 tx_urb_cmd_max_len;\n+\tu16 rx_urb_cmd_max_len;\n+\tu16 echo_skb_max;\n+\tu16 dql_limit_min;\n+\tu8 tx_bulk_max;\n+\tu8 urb_cmd_header_len;\n+\tu8 rx_urb_max;\n+\tu8 tx_urb_max;\n+\tu8 channel_idx_offset;\n+};\n+\n+/**\n+ * struct es58x_operators - Function pointers used to encode/decode\n+ *\tthe TX/RX messages.\n+ * @get_msg_len: Get field msg_len of the urb_cmd. The offset of\n+ *\tmsg_len inside urb_cmd depends of the device model.\n+ * @handle_urb_cmd: Handle URB command received from the device and\n+ *\tmanage the return code from device.\n+ * @fill_urb_header: Fill the header of urb_cmd.\n+ * @tx_can_msg: Encode a TX CAN message and add it to the bulk buffer\n+ *\tcmd_buf of es58x_dev.\n+ * @set_bittiming: Encode the bittiming information and send it.\n+ * @enable_channel: Start the CAN channel with index channel_idx.\n+ * @disable_channel: Stop the CAN channel with index channel_idx.\n+ * @reset_rx: Reset the RX queue of the ES58X device.\n+ * @reset_tx: Reset the TX queue of the ES58X device.\n+ * @get_timestamp: Request a timestamp from the ES58X device.\n+ */\n+struct es58x_operators {\n+\tu16 (*get_msg_len)(const union es58x_urb_cmd *urb_cmd);\n+\tint (*handle_urb_cmd)(struct es58x_device *es58x_dev,\n+\t\t\t      const union es58x_urb_cmd *urb_cmd);\n+\tvoid (*fill_urb_header)(union es58x_urb_cmd *urb_cmd, u8 cmd_type,\n+\t\t\t\tu8 cmd_id, u8 channel_idx, u16 cmd_len);\n+\tint (*tx_can_msg)(struct es58x_priv *priv, int channel_idx,\n+\t\t\t  u32 packet_idx, const struct sk_buff *skb,\n+\t\t\t   union es58x_urb_cmd *urb_cmd, u32 *urb_cmd_len);\n+\tint (*set_bittiming)(struct es58x_device *es58x_dev, int channel_idx);\n+\tint (*enable_channel)(struct es58x_device *es58x_dev,\n+\t\t\t      int channel_idx);\n+\tint (*disable_channel)(struct es58x_device *es58x_dev,\n+\t\t\t       int channel_idx);\n+\tint (*reset_rx)(struct es58x_device *es58x_dev, int channel_idx);\n+\tint (*reset_tx)(struct es58x_device *es58x_dev, int channel_idx);\n+\tint (*get_timestamp)(struct es58x_device *es58x_dev);\n+};\n+\n+/**\n+ * struct es58x_device - All information specific to an ES58X device.\n+ * @dev: Device information.\n+ * @udev: USB device information.\n+ * @netdev: Array of our CAN channels.\n+ * @param: The constant parameters.\n+ * @ops: Operators.\n+ * @rx_pipe: USB reception pipe.\n+ * @tx_pipe: USB transmission pipe.\n+ * @rx_urbs: Anchor for received URBs.\n+ * @tx_urbs_busy: Anchor for TX URBs which were send to the device.\n+ * @tx_urbs_idle: Anchor for TX USB which are idle. This driver\n+ *\tallocates the memory for the URBs during the probe. When a TX\n+ *\tURB is needed, it can be taken from this anchor. The network\n+ *\tqueue wake/stop logic should prevent this URB from getting\n+ *\tempty. Please refer to es58x_get_tx_urb() for more details.\n+ * @tx_urbs_idle_cnt: number of urbs in @tx_urbs_idle.\n+ * @ktime_req_ns: kernel timestamp when es58x_set_realtime_diff_ns()\n+ *\twas called.\n+ * @realtime_diff_ns: difference in nanoseconds between the clocks of\n+ *\tthe ES58X device and the kernel.\n+ * @timestamps: a temporary buffer to store the time stamps before\n+ *\tfeeding them to es58x_can_get_echo_skb(). Can only be used\n+ *\tin rx branches.\n+ * @rx_max_packet_size: Maximum length of bulk-in URB.\n+ * @num_can_ch: Number of CAN channel (i.e. number of elements of @netdev).\n+ * @rx_cmd_buf_len: Length of @rx_cmd_buf.\n+ * @rx_cmd_buf: The device might split the URB commands in an\n+ *\tarbitrary amount of pieces. This buffer is used to concatenate\n+ *\tall those pieces. Can only be used in rx branches. This field\n+ *\thas to be the last one of the structure because it is has a\n+ *\tflexible size (c.f. es58x_sizeof_es58x_device() function).\n+ */\n+struct es58x_device {\n+\tstruct device *dev;\n+\tstruct usb_device *udev;\n+\tstruct net_device *netdev[ES58X_NUM_CAN_CH_MAX];\n+\n+\tconst struct es58x_parameters *param;\n+\tconst struct es58x_operators *ops;\n+\n+\tint rx_pipe;\n+\tint tx_pipe;\n+\n+\tstruct usb_anchor rx_urbs;\n+\tstruct usb_anchor tx_urbs_busy;\n+\tstruct usb_anchor tx_urbs_idle;\n+\tatomic_t tx_urbs_idle_cnt;\n+\n+\tu64 ktime_req_ns;\n+\ts64 realtime_diff_ns;\n+\n+\tu64 timestamps[ES58X_RX_LOOPBACK_BULK_MAX];\n+\n+\tu16 rx_max_packet_size;\n+\tu8 num_can_ch;\n+\n+\tu16 rx_cmd_buf_len;\n+\tunion es58x_urb_cmd rx_cmd_buf;\n+};\n+\n+/**\n+ * es58x_sizeof_es58x_device() - Calculate the maximum length of\n+ *\tstruct es58x_device.\n+ * @es58x_dev_param: The constant parameters of the device.\n+ *\n+ * The length of struct es58x_device depends on the length of its last\n+ * field: rx_cmd_buf. This macro allows to optimize size for memory\n+ * allocation.\n+ *\n+ * Return: length of struct es58x_device.\n+ */\n+static inline size_t es58x_sizeof_es58x_device(const struct es58x_parameters\n+\t\t\t\t\t       *es58x_dev_param)\n+{\n+\treturn offsetof(struct es58x_device, rx_cmd_buf) +\n+\t\tes58x_dev_param->rx_urb_cmd_max_len;\n+}\n+\n+/* Megabit per second (multiply x per one million) */\n+#define ES58X_MBPS(x) (1000000UL * (x))\n+\n+/* Megahertz (multiply x per one million) */\n+#define ES58X_MHZ(x) (1000000UL * (x))\n+\n+static inline int __es58x_check_msg_len(const struct device *dev,\n+\t\t\t\t\tconst char *stringified_msg,\n+\t\t\t\t\tsize_t actual_len, size_t expected_len)\n+{\n+\tif (expected_len != actual_len) {\n+\t\tdev_err(dev,\n+\t\t\t\"Length of %s is %zu but received command is %zu.\\n\",\n+\t\t\tstringified_msg, expected_len, actual_len);\n+\t\treturn -EMSGSIZE;\n+\t}\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_check_msg_len() - Check the size of a received message.\n+ * @dev: Device, used to print error messages.\n+ * @msg: Received message, must not be a pointer.\n+ * @actual_len: Length of the message as advertised in the command header.\n+ *\n+ * Must be a macro in order to retrieve the actual size using\n+ * sizeof(). Can be use with any of the messages which have a fixed\n+ * length. Check for an exact match of the size.\n+ *\n+ * Return: zero on success, -EMSGSIZE if @actual_len differs from the\n+ * expected length.\n+ */\n+#define es58x_check_msg_len(dev, msg, actual_len)\t\t\t\\\n+\t__es58x_check_msg_len(dev, __stringify(msg),\t\t\t\\\n+\t\t\t      actual_len, sizeof(msg))\n+\n+static inline int __es58x_check_msg_max_len(const struct device *dev,\n+\t\t\t\t\t    const char *stringified_msg,\n+\t\t\t\t\t    size_t actual_len,\n+\t\t\t\t\t    size_t expected_len)\n+{\n+\tif (actual_len > expected_len) {\n+\t\tdev_err(dev,\n+\t\t\t\"Maximum length for %s is %zu but received command is %zu.\\n\",\n+\t\t\tstringified_msg, expected_len, actual_len);\n+\t\treturn -EOVERFLOW;\n+\t}\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_check_msg_max_len() - Check the maximum size of a received message.\n+ * @dev: Device, used to print error messages.\n+ * @msg: Received message, must not be a pointer.\n+ * @actual_len: Length of the message as advertised in the command header.\n+ *\n+ * Must be a macro in order to retrieve the actual size using\n+ * sizeof(). To be used with the messages of variable sizes. Only\n+ * check that the message is not bigger than the maximum expected\n+ * size.\n+ *\n+ * Return: zero on success, -EOVERFLOW if @actual_len is greater than\n+ * the expected length.\n+ */\n+#define es58x_check_msg_max_len(dev, msg, actual_len)\t\t\t\\\n+\t__es58x_check_msg_max_len(dev, __stringify(msg),\t\t\\\n+\t\t\t\t  actual_len, sizeof(msg))\n+\n+static inline int __es58x_msg_num_element(const struct device *dev,\n+\t\t\t\t\t  const char *stringified_msg,\n+\t\t\t\t\t  size_t actual_len, size_t msg_len,\n+\t\t\t\t\t  size_t elem_len)\n+{\n+\tsize_t actual_num_elem = actual_len / elem_len;\n+\tsize_t expected_num_elem = msg_len / elem_len;\n+\n+\tif (actual_num_elem == 0) {\n+\t\tdev_err(dev,\n+\t\t\t\"Minimum length for %s is %zu but received command is %zu.\\n\",\n+\t\t\tstringified_msg, elem_len, actual_len);\n+\t\treturn -EMSGSIZE;\n+\t} else if ((actual_len % elem_len) != 0) {\n+\t\tdev_err(dev,\n+\t\t\t\"Received command length: %zu is not a multiple of %s[0]: %zu\\n\",\n+\t\t\tactual_len, stringified_msg, elem_len);\n+\t\treturn -EMSGSIZE;\n+\t} else if (actual_num_elem > expected_num_elem) {\n+\t\tdev_err(dev,\n+\t\t\t\"Array %s is supposed to have %zu elements each of size %zu...\\n\",\n+\t\t\tstringified_msg, expected_num_elem, elem_len);\n+\t\tdev_err(dev,\n+\t\t\t\"... But received command has %zu elements (total length %zu).\\n\",\n+\t\t\tactual_num_elem, actual_len);\n+\t\treturn -EOVERFLOW;\n+\t}\n+\treturn actual_num_elem;\n+}\n+\n+/**\n+ * es58x_msg_num_element() - Check size and give the number of\n+ *\telements in a message of array type.\n+ * @dev: Device, used to print error messages.\n+ * @msg: Received message, must be an array.\n+ * @actual_len: Length of the message as advertised in the command\n+ *\theader.\n+ *\n+ * Must be a macro in order to retrieve the actual size using\n+ * sizeof(). To be used on message of array type. Array's element has\n+ * to be of fixed size (else use es58x_check_msg_max_len()). Check\n+ * that the total length is an exact multiple of the length of a\n+ * single element.\n+ *\n+ * Return: number of elements in the array on success, -EOVERFLOW if\n+ * @actual_len is greater than the expected length, -EMSGSIZE if\n+ * @actual_len is not a multiple of a single element.\n+ */\n+#define es58x_msg_num_element(dev, msg, actual_len)\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\tsize_t __elem_len = sizeof((msg)[0]) + __must_be_array(msg);\t\\\n+\t__es58x_msg_num_element(dev, __stringify(msg), actual_len,\t\\\n+\t\t\t\tsizeof(msg), __elem_len);\t\t\\\n+})\n+\n+/**\n+ * es58x_priv() - Get the priv member and cast it to struct es58x_priv.\n+ * @netdev: CAN network device.\n+ *\n+ * Return: ES58X device.\n+ */\n+static inline struct es58x_priv *es58x_priv(struct net_device *netdev)\n+{\n+\treturn (struct es58x_priv *)netdev_priv(netdev);\n+}\n+\n+/**\n+ * ES58X_SIZEOF_URB_CMD() - Calculate the maximum length of an urb\n+ *\tcommand for a given message field name.\n+ * @es58x_urb_cmd_type: type (either \"struct es581_4_urb_cmd\" or\n+ *\t\"struct es58x_fd_urb_cmd\").\n+ * @msg_field: name of the message field.\n+ *\n+ * Must be a macro in order to retrieve the actual size using\n+ * offsetof() and sizeof_field().\n+ *\n+ * Return: length of the urb command.\n+ */\n+#define ES58X_SIZEOF_URB_CMD(es58x_urb_cmd_type, msg_field)\t\t\\\n+\t(offsetof(es58x_urb_cmd_type, raw_msg)\t\t\t\t\\\n+\t\t+ sizeof_field(es58x_urb_cmd_type, msg_field)\t\t\\\n+\t\t+ sizeof_field(es58x_urb_cmd_type,\t\t\t\\\n+\t\t\t       reserved_for_crc16_do_not_use))\n+\n+/**\n+ * es58x_get_urb_cmd_len() - Calculate the actual length of an urb\n+ *\tcommand for a given message length.\n+ * @es58x_dev: ES58X device.\n+ * @msg_len: Length of the message.\n+ *\n+ * Add the header and CRC lengths to the message length.\n+ *\n+ * Return: length of the urb command.\n+ */\n+static inline size_t es58x_get_urb_cmd_len(struct es58x_device *es58x_dev,\n+\t\t\t\t\t   u16 msg_len)\n+{\n+\t/* URB Length = URB header len + Message len + sizeof crc16 */\n+\treturn es58x_dev->param->urb_cmd_header_len + msg_len + sizeof(u16);\n+}\n+\n+/**\n+ * es58x_get_netdev() - Get the network device.\n+ * @es58x_dev: ES58X device.\n+ * @channel_no: The channel number as advertised in the urb command.\n+ * @netdev: CAN network device.\n+ *\n+ * ES581.4 starts the numbering on channels from 1, ES58X FD family\n+ * starts it from 0. This method does the sanity check.\n+ *\n+ * Return: zero on success, -ECHRNG if the received channel number is\n+ * out of range and -ENODEV if the network device is not yet\n+ * configured.\n+ */\n+static inline int es58x_get_netdev(struct es58x_device *es58x_dev,\n+\t\t\t\t   int channel_no, struct net_device **netdev)\n+{\n+\tint channel_idx = channel_no - es58x_dev->param->channel_idx_offset;\n+\n+\t*netdev = NULL;\n+\tif (channel_idx < 0 || channel_idx >= es58x_dev->num_can_ch)\n+\t\treturn -ECHRNG;\n+\n+\t*netdev = es58x_dev->netdev[channel_idx];\n+\tif (!netdev || !netif_device_present(*netdev))\n+\t\treturn -ENODEV;\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * es58x_get_raw_can_id() - Get the CAN ID.\n+ * @cf: CAN frame.\n+ *\n+ * Mask the CAN ID in order to only keep the significant bits.\n+ *\n+ * Return: the raw value of the CAN ID.\n+ */\n+static inline int es58x_get_raw_can_id(const struct can_frame *cf)\n+{\n+\tif (cf->can_id & CAN_EFF_FLAG)\n+\t\treturn cf->can_id & CAN_EFF_MASK;\n+\telse\n+\t\treturn cf->can_id & CAN_SFF_MASK;\n+}\n+\n+/**\n+ * es58x_get_flags() - Get the CAN flags.\n+ * @skb: socket buffer of a CAN message.\n+ *\n+ * Return: the CAN flag as an enum es58x_flag.\n+ */\n+static inline enum es58x_flag es58x_get_flags(const struct sk_buff *skb)\n+{\n+\tstruct canfd_frame *cf = (struct canfd_frame *)skb->data;\n+\tenum es58x_flag es58x_flags = 0;\n+\n+\tif (cf->can_id & CAN_EFF_FLAG)\n+\t\tes58x_flags |= ES58X_FLAG_EFF;\n+\n+\tif (can_is_canfd_skb(skb)) {\n+\t\tes58x_flags |= ES58X_FLAG_FD_DATA;\n+\t\tif (cf->flags & CANFD_BRS)\n+\t\t\tes58x_flags |= ES58X_FLAG_FD_BRS;\n+\t\tif (cf->flags & CANFD_ESI)\n+\t\t\tes58x_flags |= ES58X_FLAG_FD_ESI;\n+\t} else if (cf->can_id & CAN_RTR_FLAG)\n+\t\t/* Remote frames are only defined in Classical CAN frames */\n+\t\tes58x_flags |= ES58X_FLAG_RTR;\n+\n+\treturn es58x_flags;\n+}\n+\n+int es58x_can_get_echo_skb(struct net_device *netdev, u32 packet_idx,\n+\t\t\t   u64 *tstamps, unsigned int pkts);\n+int es58x_tx_ack_msg(struct net_device *netdev, u16 tx_free_entries,\n+\t\t     enum es58x_ret_u32 rx_cmd_ret_u32);\n+int es58x_rx_can_msg(struct net_device *netdev, u64 timestamp, const u8 *data,\n+\t\t     canid_t can_id, enum es58x_flag es58x_flags, u8 dlc);\n+int es58x_rx_err_msg(struct net_device *netdev, enum es58x_err error,\n+\t\t     enum es58x_event event, u64 timestamp);\n+int es58x_rx_timestamp(struct es58x_device *es58x_dev, u64 timestamp);\n+int es58x_rx_dev_ret_u8(struct device *dev, enum es58x_ret_type cmd_ret_type,\n+\t\t\tenum es58x_ret_u8 rx_dev_ret_u8);\n+int es58x_rx_cmd_ret_u32(struct net_device *netdev,\n+\t\t\t enum es58x_ret_type cmd_ret_type,\n+\t\t\t enum es58x_ret_u32 rx_cmd_ret_u32);\n+int es58x_send_msg(struct es58x_device *es58x_dev, u8 cmd_type, u8 cmd_id,\n+\t\t   const void *msg, u16 cmd_len, int channel_idx);\n+\n+extern const struct es58x_parameters es581_4_param;\n+extern const struct es58x_operators es581_4_ops;\n+\n+extern const struct es58x_parameters es58x_fd_param;\n+extern const struct es58x_operators es58x_fd_ops;\n+\n+#endif /* __ES58X_COMMON_H__ */\ndiff --git a/drivers/net/can/usb/etas_es58x/es58x_fd.c b/drivers/net/can/usb/etas_es58x/es58x_fd.c\nnew file mode 100644\nindex 000000000000..0c8e850e330b\n--- /dev/null\n+++ b/drivers/net/can/usb/etas_es58x/es58x_fd.c\n@@ -0,0 +1,657 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+/* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.\n+ *\n+ * File es58x_fd.c: Adds support to ETAS ES582.1 and ES584.1 (naming\n+ * convention: we use the term \"ES58X FD\" when referring to those two\n+ * variants together).\n+ *\n+ * Copyright (C) 2019 Robert Bosch Engineering and Business\n+ * Solutions. All rights reserved.\n+ * Copyright (C) 2020 ETAS K.K.. All rights reserved.\n+ */\n+\n+#include <linux/kernel.h>\n+#include <asm/unaligned.h>\n+\n+#include \"es58x_core.h\"\n+#include \"es58x_fd.h\"\n+\n+/**\n+ * es58x_sizeof_rx_tx_msg() - Calculate the actual length of the\n+ *\tstructure of a rx or tx message.\n+ * @msg: message of variable length, must have a dlc and a len fields.\n+ *\n+ * Even if RTR frames have actually no payload, the ES58X devices\n+ * still expect it. Must be a macro in order to retrieve the actual\n+ * size using offsetof() and typeof().\n+ *\n+ * Return: length of the message.\n+ */\n+#define es58x_fd_sizeof_rx_tx_msg(msg)\t\t\t\t\t\\\n+({\t\t\t\t\t\t\t\t\t\\\n+\ttypeof(msg) __msg = (msg);\t\t\t\t\t\\\n+\tsize_t __msg_len = __msg.flags & ES58X_FLAG_FD_DATA ?\t\t\\\n+\t\tmin_t(u8, __msg.len, CANFD_MAX_DLEN) :\t\t\t\\\n+\t\tcan_get_cc_len(__msg.dlc);\t\t\t\t\\\n+\t(offsetof(typeof(__msg), data[__msg_len]));\t\t\t\\\n+})\n+\n+static enum es58x_fd_cmd_type es58x_fd_cmd_type(struct net_device *netdev)\n+{\n+\tu32 ctrlmode = es58x_priv(netdev)->can.ctrlmode;\n+\n+\treturn ctrlmode & (CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO) ?\n+\t    ES58X_FD_CMD_TYPE_CANFD : ES58X_FD_CMD_TYPE_CAN;\n+}\n+\n+static u16 es58x_fd_get_msg_len(const union es58x_urb_cmd *urb_cmd)\n+{\n+\treturn get_unaligned_le16(&urb_cmd->es58x_fd_urb_cmd.msg_len);\n+}\n+\n+static int es58x_fd_rx_loopback_msg(struct net_device *netdev,\n+\t\t\t\t    const struct es58x_fd_urb_cmd\n+\t\t\t\t    *es58x_fd_urb_cmd)\n+{\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tconst struct es58x_fd_rx_loopback_msg *rx_loopback_msg;\n+\tstruct es58x_device *es58x_dev = priv->es58x_dev;\n+\tu64 *tstamps = es58x_dev->timestamps;\n+\tu16 msg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);\n+\tint i, num_element;\n+\tu32 first_packet_idx, packet_idx;\n+\tunsigned long flags;\n+\n+\tconst u32 mask = ~(u32)((typeof(rx_loopback_msg->packet_idx))(~0U));\n+\n+\tnum_element = es58x_msg_num_element(es58x_dev->dev,\n+\t\t\t\t\t    es58x_fd_urb_cmd->rx_loopback_msg,\n+\t\t\t\t\t    msg_len);\n+\tif (num_element < 0)\n+\t\treturn num_element;\n+\trx_loopback_msg = es58x_fd_urb_cmd->rx_loopback_msg;\n+\n+\tspin_lock_irqsave(&priv->echo_skb_spinlock, flags);\n+\tfirst_packet_idx = priv->current_packet_idx - priv->num_echo_skb;\n+\tspin_unlock_irqrestore(&priv->echo_skb_spinlock, flags);\n+\n+\tpacket_idx = (first_packet_idx & mask) | rx_loopback_msg[0].packet_idx;\n+\tfor (i = 0; i < num_element; i++) {\n+\t\tif ((u8)packet_idx != rx_loopback_msg[i].packet_idx) {\n+\t\t\tnetdev_err(netdev, \"Packet idx jumped from %u to %u\\n\",\n+\t\t\t\t   (u8)packet_idx - 1,\n+\t\t\t\t   rx_loopback_msg[i].packet_idx);\n+\t\t\treturn -EBADMSG;\n+\t\t}\n+\n+\t\ttstamps[i] = get_unaligned_le64(&rx_loopback_msg[i].timestamp);\n+\t\tpacket_idx++;\n+\t}\n+\n+\treturn es58x_can_get_echo_skb(netdev, first_packet_idx,\n+\t\t\t\t      tstamps, num_element);\n+}\n+\n+static int es58x_fd_rx_can_msg(struct net_device *netdev,\n+\t\t\t       const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd)\n+{\n+\tstruct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;\n+\tconst u8 *rx_can_msg_buf = es58x_fd_urb_cmd->rx_can_msg_buf;\n+\tu16 rx_can_msg_buf_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);\n+\tint pkts, ret;\n+\n+\tret = es58x_check_msg_max_len(es58x_dev->dev,\n+\t\t\t\t      es58x_fd_urb_cmd->rx_can_msg_buf,\n+\t\t\t\t      rx_can_msg_buf_len);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tfor (pkts = 0; rx_can_msg_buf_len > 0; pkts++) {\n+\t\tconst struct es58x_fd_rx_can_msg *rx_can_msg =\n+\t\t    (const struct es58x_fd_rx_can_msg *)rx_can_msg_buf;\n+\t\tbool is_can_fd = !!(rx_can_msg->flags & ES58X_FLAG_FD_DATA);\n+\t\t/* rx_can_msg_len is the length of the rx_can_msg\n+\t\t * buffer. Not to be confused with rx_can_msg->len\n+\t\t * which is the length of the CAN payload\n+\t\t * rx_can_msg->data.\n+\t\t */\n+\t\tu16 rx_can_msg_len = es58x_fd_sizeof_rx_tx_msg(*rx_can_msg);\n+\n+\t\tif (rx_can_msg_len > rx_can_msg_buf_len) {\n+\t\t\tnetdev_err(netdev,\n+\t\t\t\t   \"%s: Expected a rx_can_msg of size %d but only %d bytes are left in rx_can_msg_buf\\n\",\n+\t\t\t\t   __func__,\n+\t\t\t\t   rx_can_msg_len, rx_can_msg_buf_len);\n+\t\t\treturn -EMSGSIZE;\n+\t\t}\n+\t\tif (rx_can_msg->len > CANFD_MAX_DLEN) {\n+\t\t\tnetdev_err(netdev,\n+\t\t\t\t   \"%s: Data length is %d but maximum should be %d\\n\",\n+\t\t\t\t   __func__, rx_can_msg->len, CANFD_MAX_DLEN);\n+\t\t\treturn -EMSGSIZE;\n+\t\t}\n+\n+\t\tif (netif_running(netdev)) {\n+\t\t\tu64 tstamp = get_unaligned_le64(&rx_can_msg->timestamp);\n+\t\t\tcanid_t can_id = get_unaligned_le32(&rx_can_msg->can_id);\n+\t\t\tu8 dlc;\n+\n+\t\t\tif (is_can_fd)\n+\t\t\t\tdlc = can_len2dlc(rx_can_msg->len);\n+\t\t\telse\n+\t\t\t\tdlc = rx_can_msg->dlc;\n+\n+\t\t\tret = es58x_rx_can_msg(netdev, tstamp, rx_can_msg->data,\n+\t\t\t\t\t       can_id, rx_can_msg->flags, dlc);\n+\t\t\tif (ret)\n+\t\t\t\tbreak;\n+\t\t}\n+\n+\t\trx_can_msg_buf_len -= rx_can_msg_len;\n+\t\trx_can_msg_buf += rx_can_msg_len;\n+\t}\n+\n+\tif (!netif_running(netdev)) {\n+\t\tif (net_ratelimit())\n+\t\t\tnetdev_info(netdev,\n+\t\t\t\t    \"%s: %s is down, dropping %d rx packets\\n\",\n+\t\t\t\t    __func__, netdev->name, pkts);\n+\t\tnetdev->stats.rx_dropped += pkts;\n+\t}\n+\n+\treturn ret;\n+}\n+\n+static int es58x_fd_rx_event_msg(struct net_device *netdev,\n+\t\t\t\t const struct es58x_fd_urb_cmd\n+\t\t\t\t *es58x_fd_urb_cmd)\n+{\n+\tstruct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;\n+\tu16 msg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);\n+\tconst struct es58x_fd_rx_event_msg *rx_event_msg;\n+\tint ret;\n+\n+\tret = es58x_check_msg_len(es58x_dev->dev, *rx_event_msg, msg_len);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\trx_event_msg = &es58x_fd_urb_cmd->rx_event_msg;\n+\n+\treturn es58x_rx_err_msg(netdev, rx_event_msg->error_code,\n+\t\t\t\trx_event_msg->event_code,\n+\t\t\t\tget_unaligned_le64(&rx_event_msg->timestamp));\n+}\n+\n+static int es58x_fd_rx_cmd_ret_u32(struct net_device *netdev,\n+\t\t\t\t   const struct es58x_fd_urb_cmd\n+\t\t\t\t   *es58x_fd_urb_cmd,\n+\t\t\t\t   enum es58x_ret_type cmd_ret_type)\n+{\n+\tstruct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;\n+\tu16 msg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);\n+\tenum es58x_ret_u32 rx_cmd_ret_u32;\n+\tint ret;\n+\n+\tret = es58x_check_msg_len(es58x_dev->dev,\n+\t\t\t\t  es58x_fd_urb_cmd->rx_cmd_ret_le32, msg_len);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\trx_cmd_ret_u32 = get_unaligned_le32(&es58x_fd_urb_cmd->rx_cmd_ret_le32);\n+\n+\treturn es58x_rx_cmd_ret_u32(netdev, cmd_ret_type, rx_cmd_ret_u32);\n+}\n+\n+static int es58x_fd_tx_ack_msg(struct net_device *netdev,\n+\t\t\t       const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd)\n+{\n+\tstruct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;\n+\tconst struct es58x_fd_tx_ack_msg *tx_ack_msg;\n+\tu16 msg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);\n+\tint ret;\n+\n+\ttx_ack_msg = &es58x_fd_urb_cmd->tx_ack_msg;\n+\tret = es58x_check_msg_len(es58x_dev->dev, *tx_ack_msg, msg_len);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\treturn\n+\t    es58x_tx_ack_msg(netdev,\n+\t\t\t     get_unaligned_le16(&tx_ack_msg->tx_free_entries),\n+\t\t\t     get_unaligned_le32(&tx_ack_msg->rx_cmd_ret_le32));\n+}\n+\n+static int es58x_fd_can_cmd_id(struct es58x_device *es58x_dev,\n+\t\t\t       const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd)\n+{\n+\tstruct net_device *netdev;\n+\tint ret;\n+\n+\tret = es58x_get_netdev(es58x_dev, es58x_fd_urb_cmd->channel_idx,\n+\t\t\t       &netdev);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tswitch ((enum es58x_fd_can_cmd_id)es58x_fd_urb_cmd->cmd_id) {\n+\tcase ES58X_FD_CAN_CMD_ID_ENABLE_CHANNEL:\n+\t\treturn es58x_fd_rx_cmd_ret_u32(netdev, es58x_fd_urb_cmd,\n+\t\t\t\t\t       ES58X_RET_TYPE_ENABLE_CHANNEL);\n+\n+\tcase ES58X_FD_CAN_CMD_ID_DISABLE_CHANNEL:\n+\t\treturn es58x_fd_rx_cmd_ret_u32(netdev, es58x_fd_urb_cmd,\n+\t\t\t\t\t       ES58X_RET_TYPE_DISABLE_CHANNEL);\n+\n+\tcase ES58X_FD_CAN_CMD_ID_TX_MSG:\n+\t\treturn es58x_fd_tx_ack_msg(netdev, es58x_fd_urb_cmd);\n+\n+\tcase ES58X_FD_CAN_CMD_ID_MSG_SELFRX_ACK:\n+\t\treturn es58x_fd_rx_loopback_msg(netdev, es58x_fd_urb_cmd);\n+\n+\tcase ES58X_FD_CAN_CMD_ID_RX_MSG:\n+\t\treturn es58x_fd_rx_can_msg(netdev, es58x_fd_urb_cmd);\n+\n+\tcase ES58X_FD_CAN_CMD_ID_RESET_RX:\n+\t\treturn es58x_fd_rx_cmd_ret_u32(netdev, es58x_fd_urb_cmd,\n+\t\t\t\t\t       ES58X_RET_TYPE_RESET_RX);\n+\n+\tcase ES58X_FD_CAN_CMD_ID_RESET_TX:\n+\t\treturn es58x_fd_rx_cmd_ret_u32(netdev, es58x_fd_urb_cmd,\n+\t\t\t\t\t       ES58X_RET_TYPE_RESET_TX);\n+\n+\tcase ES58X_FD_CAN_CMD_ID_ERROR_OR_EVENT_MSG:\n+\t\treturn es58x_fd_rx_event_msg(netdev, es58x_fd_urb_cmd);\n+\n+\tdefault:\n+\t\treturn -EBADRQC;\n+\t}\n+}\n+\n+static int es58x_fd_device_cmd_id(struct es58x_device *es58x_dev,\n+\t\t\t\t  const struct es58x_fd_urb_cmd\n+\t\t\t\t  *es58x_fd_urb_cmd)\n+{\n+\tu16 msg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);\n+\tu64 timestamp;\n+\tint ret;\n+\n+\tswitch ((enum es58x_fd_dev_cmd_id)es58x_fd_urb_cmd->cmd_id) {\n+\tcase ES58X_FD_DEV_CMD_ID_TIMESTAMP:\n+\t\tret = es58x_check_msg_len(es58x_dev->dev,\n+\t\t\t\t\t  es58x_fd_urb_cmd->timestamp, msg_len);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\t\ttimestamp = get_unaligned_le64(&es58x_fd_urb_cmd->timestamp);\n+\t\treturn es58x_rx_timestamp(es58x_dev, timestamp);\n+\n+\tdefault:\n+\t\treturn -EBADRQC;\n+\t}\n+}\n+\n+static int es58x_fd_handle_urb_cmd(struct es58x_device *es58x_dev,\n+\t\t\t\t   const union es58x_urb_cmd *urb_cmd)\n+{\n+\tconst struct es58x_fd_urb_cmd *es58x_fd_urb_cmd;\n+\tconst struct device *dev = es58x_dev->dev;\n+\tint ret;\n+\n+\tes58x_fd_urb_cmd = &urb_cmd->es58x_fd_urb_cmd;\n+\n+\tswitch ((enum es58x_fd_cmd_type)es58x_fd_urb_cmd->cmd_type) {\n+\tcase ES58X_FD_CMD_TYPE_CAN:\n+\tcase ES58X_FD_CMD_TYPE_CANFD:\n+\t\tret = es58x_fd_can_cmd_id(es58x_dev, es58x_fd_urb_cmd);\n+\t\tbreak;\n+\n+\tcase ES58X_FD_CMD_TYPE_DEVICE:\n+\t\tret = es58x_fd_device_cmd_id(es58x_dev, es58x_fd_urb_cmd);\n+\t\tbreak;\n+\n+\tdefault:\n+\t\tret = -EBADRQC;\n+\t\tbreak;\n+\t}\n+\n+\tif (ret == -EBADRQC)\n+\t\tdev_err(dev,\n+\t\t\t\"%s: Unknown command type (0x%02X) and command ID (0x%02X) combination\\n\",\n+\t\t\t__func__, es58x_fd_urb_cmd->cmd_type,\n+\t\t\tes58x_fd_urb_cmd->cmd_id);\n+\n+\treturn ret;\n+}\n+\n+static void es58x_fd_fill_urb_header(union es58x_urb_cmd *urb_cmd, u8 cmd_type,\n+\t\t\t\t     u8 cmd_id, u8 channel_idx, u16 msg_len)\n+{\n+\tstruct es58x_fd_urb_cmd *es58x_fd_urb_cmd = &urb_cmd->es58x_fd_urb_cmd;\n+\n+\tes58x_fd_urb_cmd->SOF = cpu_to_le16(es58x_fd_param.tx_start_of_frame);\n+\tes58x_fd_urb_cmd->cmd_type = cmd_type;\n+\tes58x_fd_urb_cmd->cmd_id = cmd_id;\n+\tes58x_fd_urb_cmd->channel_idx = channel_idx;\n+\tes58x_fd_urb_cmd->msg_len = cpu_to_le16(msg_len);\n+}\n+\n+static int es58x_fd_tx_can_msg(struct es58x_priv *priv, int channel_idx,\n+\t\t\t       u32 packet_idx, const struct sk_buff *skb,\n+\t\t\t       union es58x_urb_cmd *urb_cmd, u32 *urb_cmd_len)\n+{\n+\tstruct es58x_device *es58x_dev = priv->es58x_dev;\n+\tstruct es58x_fd_urb_cmd *es58x_fd_urb_cmd = &urb_cmd->es58x_fd_urb_cmd;\n+\tstruct can_frame *cf = (struct can_frame *)skb->data;\n+\tstruct es58x_fd_tx_can_msg *tx_can_msg;\n+\tbool is_fd = can_is_canfd_skb(skb);\n+\tu16 msg_len;\n+\tint ret;\n+\n+\tret = es58x_check_msg_max_len(es58x_dev->dev,\n+\t\t\t\t      es58x_fd_urb_cmd->tx_can_msg_buf,\n+\t\t\t\t      es58x_fd_get_msg_len(urb_cmd) +\n+\t\t\t\t      sizeof(*tx_can_msg));\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tmsg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);\n+\ttx_can_msg = (struct es58x_fd_tx_can_msg *)\n+\t    &es58x_fd_urb_cmd->tx_can_msg_buf[msg_len];\n+\n+\t/* Fill message contents. */\n+\tif (*urb_cmd_len == es58x_dev->param->urb_cmd_header_len)\n+\t\tes58x_fd_fill_urb_header(urb_cmd,\n+\t\t\t\t\t is_fd ? ES58X_FD_CMD_TYPE_CANFD\n+\t\t\t\t\t       : ES58X_FD_CMD_TYPE_CAN,\n+\t\t\t\t\t ES58X_FD_CAN_CMD_ID_TX_MSG_NO_ACK,\n+\t\t\t\t\t channel_idx, *urb_cmd_len);\n+\ttx_can_msg->packet_idx = packet_idx;\n+\tput_unaligned_le32(es58x_get_raw_can_id(cf), &tx_can_msg->can_id);\n+\ttx_can_msg->flags = (u8)es58x_get_flags(skb);\n+\tif (is_fd)\n+\t\ttx_can_msg->len = cf->len;\n+\telse\n+\t\ttx_can_msg->dlc = can_get_cc_dlc(priv->can.ctrlmode,\n+\t\t\t\t\t\t cf->len, cf->len8_dlc);\n+\tmemcpy(tx_can_msg->data, cf->data, cf->len);\n+\n+\t/* Calculate new sizes */\n+\tmsg_len += es58x_fd_sizeof_rx_tx_msg(*tx_can_msg);\n+\t*urb_cmd_len = es58x_get_urb_cmd_len(es58x_dev, msg_len);\n+\tput_unaligned_le16(msg_len, &es58x_fd_urb_cmd->msg_len);\n+\n+\treturn 0;\n+}\n+\n+/* ES58X uses the same command to set the bittiming parameters and\n+ * enable the channel. The modification is done in\n+ * es58x_fd_enable_channel().\n+ */\n+static int es58x_fd_set_bittiming(struct es58x_device *es58x_dev,\n+\t\t\t\t  int channel_idx)\n+{\n+\treturn 0;\n+}\n+\n+static void es58x_fd_print_bittiming(struct net_device *netdev,\n+\t\t\t\t     struct es58x_fd_bittiming\n+\t\t\t\t     *es58x_fd_bittiming, char *type)\n+{\n+\tnetdev_vdbg(netdev, \"bitrate %s    = %d\\n\", type,\n+\t\t    le32_to_cpu(es58x_fd_bittiming->bitrate));\n+\tnetdev_vdbg(netdev, \"tseg1 %s      = %d\\n\", type,\n+\t\t    le16_to_cpu(es58x_fd_bittiming->tseg1));\n+\tnetdev_vdbg(netdev, \"tseg2 %s      = %d\\n\", type,\n+\t\t    le16_to_cpu(es58x_fd_bittiming->tseg2));\n+\tnetdev_vdbg(netdev, \"brp %s        = %d\\n\", type,\n+\t\t    le16_to_cpu(es58x_fd_bittiming->brp));\n+\tnetdev_vdbg(netdev, \"sjw %s        = %d\\n\", type,\n+\t\t    le16_to_cpu(es58x_fd_bittiming->sjw));\n+}\n+\n+static void es58x_fd_print_conf(struct net_device *netdev,\n+\t\t\t\tstruct es58x_fd_tx_conf_msg *tx_conf_msg)\n+{\n+\tes58x_fd_print_bittiming(netdev, &tx_conf_msg->nominal_bittiming,\n+\t\t\t\t \"nominal\");\n+\tnetdev_vdbg(netdev, \"samples_per_bit    = %d\\n\",\n+\t\t    tx_conf_msg->samples_per_bit);\n+\tnetdev_vdbg(netdev, \"sync_edge          = %d\\n\",\n+\t\t    tx_conf_msg->sync_edge);\n+\tnetdev_vdbg(netdev, \"physical_media     = %d\\n\",\n+\t\t    tx_conf_msg->physical_media);\n+\tnetdev_vdbg(netdev, \"self_reception     = %d\\n\",\n+\t\t    tx_conf_msg->self_reception_mode);\n+\tnetdev_vdbg(netdev, \"ctrlmode           = %d\\n\", tx_conf_msg->ctrlmode);\n+\tnetdev_vdbg(netdev, \"canfd_enabled      = %d\\n\",\n+\t\t    tx_conf_msg->canfd_enabled);\n+\tif (tx_conf_msg->canfd_enabled) {\n+\t\tes58x_fd_print_bittiming(netdev,\n+\t\t\t\t\t &tx_conf_msg->data_bittiming, \"data\");\n+\t\tnetdev_vdbg(netdev,\n+\t\t\t    \"Transmitter Delay Compensation        = %d\\n\",\n+\t\t\t    tx_conf_msg->tdc);\n+\t\tnetdev_vdbg(netdev,\n+\t\t\t    \"Transmitter Delay Compensation Offset = %d\\n\",\n+\t\t\t    le16_to_cpu(tx_conf_msg->tdco));\n+\t\tnetdev_vdbg(netdev,\n+\t\t\t    \"Transmitter Delay Compensation Filter = %d\\n\",\n+\t\t\t    le16_to_cpu(tx_conf_msg->tdcf));\n+\t}\n+}\n+\n+static void es58x_fd_convert_bittiming(struct es58x_fd_bittiming *es58x_fd_bt,\n+\t\t\t\t       struct can_bittiming *bt)\n+{\n+\t/* The actual value set in the hardware registers is one less\n+\t * than the functional value.\n+\t */\n+\tconst int offset = 1;\n+\n+\tes58x_fd_bt->bitrate = cpu_to_le32(bt->bitrate);\n+\tes58x_fd_bt->tseg1 =\n+\t    cpu_to_le16(bt->prop_seg + bt->phase_seg1 - offset);\n+\tes58x_fd_bt->tseg2 = cpu_to_le16(bt->phase_seg2 - offset);\n+\tes58x_fd_bt->brp = cpu_to_le16(bt->brp - offset);\n+\tes58x_fd_bt->sjw = cpu_to_le16(bt->sjw - offset);\n+}\n+\n+static int es58x_fd_enable_channel(struct es58x_device *es58x_dev,\n+\t\t\t\t   int channel_idx)\n+{\n+\tstruct net_device *netdev = es58x_dev->netdev[channel_idx];\n+\tstruct es58x_priv *priv = es58x_priv(netdev);\n+\tstruct es58x_fd_tx_conf_msg tx_conf_msg = { 0 };\n+\tu32 ctrlmode;\n+\tbool is_can_fd = false;\n+\tsize_t conf_len = 0;\n+\n+\tes58x_fd_convert_bittiming(&tx_conf_msg.nominal_bittiming,\n+\t\t\t\t   &priv->can.bittiming);\n+\tctrlmode = priv->can.ctrlmode;\n+\n+\ttx_conf_msg.samples_per_bit =\n+\t    ctrlmode & CAN_CTRLMODE_3_SAMPLES ?\n+\t    ES58X_SAMPLES_PER_BIT_THREE : ES58X_SAMPLES_PER_BIT_ONE;\n+\ttx_conf_msg.sync_edge = ES58X_SYNC_EDGE_SINGLE;\n+\ttx_conf_msg.physical_media = ES58X_PHYSICAL_MEDIA_HIGH_SPEED;\n+\ttx_conf_msg.self_reception_mode = ES58X_SELF_RECEPTION_ON;\n+\n+\ttx_conf_msg.ctrlmode |=\n+\t    ctrlmode & CAN_CTRLMODE_LISTENONLY ?\n+\t    ES58X_FD_CTRLMODE_PASSIVE : ES58X_FD_CTRLMODE_ACTIVE;\n+\n+\tconf_len = ES58X_FD_CAN_CONF_LEN;\n+\tif (ctrlmode & CAN_CTRLMODE_FD) {\n+\t\ttx_conf_msg.ctrlmode |= ES58X_FD_CTRLMODE_FD_ISO;\n+\t\tis_can_fd = true;\n+\t} else if (ctrlmode & CAN_CTRLMODE_FD_NON_ISO) {\n+\t\ttx_conf_msg.ctrlmode |= ES58X_FD_CTRLMODE_FD_NON_ISO;\n+\t\tis_can_fd = true;\n+\t}\n+\tif (is_can_fd) {\n+\t\tstruct can_bittiming *dbt = &priv->can.data_bittiming;\n+\t\tint tdco = 0;\n+\n+\t\tconf_len = ES58X_FD_CANFD_CONF_LEN;\n+\n+\t\ttx_conf_msg.canfd_enabled = !!is_can_fd;\n+\t\tes58x_fd_convert_bittiming(&tx_conf_msg.data_bittiming, dbt);\n+\n+\t\t/* As specified in ISO 11898-1 section 11.3.3\n+\t\t * \"Transmitter delay compensation\" (TDC) is only\n+\t\t * applicable if data BRP is one or two.\n+\t\t */\n+\t\ttx_conf_msg.tdc = (dbt->brp == 1) || (dbt->brp == 2);\n+\t\tif (tx_conf_msg.tdc) {\n+\t\t\t/* Set the Transmitter Delay Compensation\n+\t\t\t * offset as the duration (in time quanta)\n+\t\t\t * from the start of the bit to the sample\n+\t\t\t * point.\n+\t\t\t */\n+\t\t\ttdco = can_bit_time(dbt) * dbt->sample_point / 1000;\n+\t\t}\n+\t\ttx_conf_msg.tdco = cpu_to_le16(min_t(u16, 127, tdco));\n+\t\ttx_conf_msg.tdcf = cpu_to_le16(0);\n+\t}\n+\tes58x_fd_print_conf(netdev, &tx_conf_msg);\n+\n+\treturn es58x_send_msg(es58x_dev, es58x_fd_cmd_type(netdev),\n+\t\t\t      ES58X_FD_CAN_CMD_ID_ENABLE_CHANNEL,\n+\t\t\t      &tx_conf_msg, conf_len, channel_idx);\n+}\n+\n+static int es58x_fd_disable_channel(struct es58x_device *es58x_dev,\n+\t\t\t\t    int channel_idx)\n+{\n+\t/* The type (ES58X_FD_CMD_TYPE_CAN or ES58X_FD_CMD_TYPE_CANFD) does\n+\t * not matter here.\n+\t */\n+\treturn es58x_send_msg(es58x_dev, ES58X_FD_CMD_TYPE_CAN,\n+\t\t\t      ES58X_FD_CAN_CMD_ID_DISABLE_CHANNEL,\n+\t\t\t      ES58X_EMPTY_MSG, 0, channel_idx);\n+}\n+\n+static int es58x_fd_reset_rx(struct es58x_device *es58x_dev, int channel_idx)\n+{\n+\tstruct net_device *netdev;\n+\tint ret;\n+\n+\tret = es58x_get_netdev(es58x_dev, channel_idx, &netdev);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\treturn es58x_send_msg(es58x_dev, es58x_fd_cmd_type(netdev),\n+\t\t\t      ES58X_FD_CAN_CMD_ID_RESET_RX, ES58X_EMPTY_MSG,\n+\t\t\t      0, channel_idx);\n+}\n+\n+static int es58x_fd_reset_tx(struct es58x_device *es58x_dev, int channel_idx)\n+{\n+\tstruct net_device *netdev;\n+\tint ret;\n+\n+\tret = es58x_get_netdev(es58x_dev, channel_idx, &netdev);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\treturn es58x_send_msg(es58x_dev, es58x_fd_cmd_type(netdev),\n+\t\t\t      ES58X_FD_CAN_CMD_ID_RESET_TX, ES58X_EMPTY_MSG,\n+\t\t\t      0, channel_idx);\n+}\n+\n+static int es58x_fd_get_timestamp(struct es58x_device *es58x_dev)\n+{\n+\treturn es58x_send_msg(es58x_dev, ES58X_FD_CMD_TYPE_DEVICE,\n+\t\t\t      ES58X_FD_DEV_CMD_ID_TIMESTAMP, ES58X_EMPTY_MSG,\n+\t\t\t      0, ES58X_CHANNEL_IDX_NA);\n+}\n+\n+/* struct es58x_fd_nom_bittiming_const - Nominal bittiming constants.\n+ *\n+ * Nominal bittiming constants for ES582.1 and ES584.1 as specified in\n+ * the microcontroller datasheet: \"SAM E701/S70/V70/V71 Family\"\n+ * section 49.6.8 \"MCAN Nominal Bit Timing and Prescaler Register\"\n+ * from Microchip.\n+ *\n+ * The values from the specification are the hardware register\n+ * values. To convert them to the functional values, all ranges were\n+ * incremented by 1 (e.g. range [0..n-1] changed to [1..n]).\n+ */\n+static const struct can_bittiming_const es58x_fd_nom_bittiming_const = {\n+\t.name = \"ES582.1/ES584.1\",\n+\t.tseg1_min = 2,\n+\t.tseg1_max = 256,\n+\t.tseg2_min = 2,\n+\t.tseg2_max = 128,\n+\t.sjw_max = 128,\n+\t.brp_min = 1,\n+\t.brp_max = 512,\n+\t.brp_inc = 1\n+};\n+\n+/* struct es58x_fd_data_bittiming_const - Data bittiming constants.\n+ *\n+ * Data bittiming constants for ES582.1 and ES584.1 as specified in\n+ * the microcontroller datasheet: \"SAM E701/S70/V70/V71 Family\"\n+ * section 49.6.4 \"MCAN Data Bit Timing and Prescaler Register\" from\n+ * Microchip.\n+ */\n+static const struct can_bittiming_const es58x_fd_data_bittiming_const = {\n+\t.name = \"ES582.1/ES584.1\",\n+\t.tseg1_min = 2,\n+\t.tseg1_max = 32,\n+\t.tseg2_min = 1,\n+\t.tseg2_max = 16,\n+\t.sjw_max = 8,\n+\t.brp_min = 1,\n+\t.brp_max = 32,\n+\t.brp_inc = 1\n+};\n+\n+const struct es58x_parameters es58x_fd_param = {\n+\t.bittiming_const = &es58x_fd_nom_bittiming_const,\n+\t.data_bittiming_const = &es58x_fd_data_bittiming_const,\n+\t.bitrate_max = ES58X_MBPS(8),\n+\t.clock = {.freq = ES58X_MHZ(80)},\n+\t.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | CAN_CTRLMODE_LISTENONLY |\n+\t    CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO |\n+\t    CAN_CTRLMODE_CC_LEN8_DLC,\n+\t.tx_start_of_frame = 0xCEFA,\t/* FACE in little endian */\n+\t.rx_start_of_frame = 0xFECA,\t/* CAFE in little endian */\n+\t.tx_urb_cmd_max_len = ES58X_FD_TX_URB_CMD_MAX_LEN,\n+\t.rx_urb_cmd_max_len = ES58X_FD_RX_URB_CMD_MAX_LEN,\n+\t/* Size of internal device TX queue is 500.\n+\t *\n+\t * However, when reaching value around 278, the device's busy\n+\t * LED turns on and thus maximum value of 500 is never reached\n+\t * in practice. Also, when this value is too high, some error\n+\t * on the rx_loopback_msg were witnessed when the device is\n+\t * recovering from bus off.\n+\t *\n+\t * For above reasons, a value that would prevent the device\n+\t * from becoming busy was chosen. In practice, BQL would\n+\t * prevent the value from even getting closer to below\n+\t * maximum, so no impact on performance was measured.\n+\t */\n+\t.echo_skb_max = U8_MAX,\n+\t/* Empirical value. */\n+\t.dql_limit_min = ES58X_CAN_FRAME_BYTES_MAX * 15,\n+\t.tx_bulk_max = ES58X_FD_TX_BULK_MAX,\n+\t.urb_cmd_header_len = ES58X_FD_URB_CMD_HEADER_LEN,\n+\t.rx_urb_max = ES58X_RX_URBS_MAX,\n+\t.tx_urb_max = ES58X_TX_URBS_MAX,\n+\t.channel_idx_offset = 0\n+};\n+\n+const struct es58x_operators es58x_fd_ops = {\n+\t.get_msg_len = es58x_fd_get_msg_len,\n+\t.handle_urb_cmd = es58x_fd_handle_urb_cmd,\n+\t.fill_urb_header = es58x_fd_fill_urb_header,\n+\t.tx_can_msg = es58x_fd_tx_can_msg,\n+\t.set_bittiming = es58x_fd_set_bittiming,\n+\t.enable_channel = es58x_fd_enable_channel,\n+\t.disable_channel = es58x_fd_disable_channel,\n+\t.reset_rx = es58x_fd_reset_rx,\n+\t.reset_tx = es58x_fd_reset_tx,\n+\t.get_timestamp = es58x_fd_get_timestamp\n+};\ndiff --git a/drivers/net/can/usb/etas_es58x/es58x_fd.h b/drivers/net/can/usb/etas_es58x/es58x_fd.h\nnew file mode 100644\nindex 000000000000..e9457ea542d4\n--- /dev/null\n+++ b/drivers/net/can/usb/etas_es58x/es58x_fd.h\n@@ -0,0 +1,242 @@\n+/* SPDX-License-Identifier: GPL-2.0 */\n+\n+/* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.\n+ *\n+ * File es58x_fd.h: Definitions and declarations specific to ETAS\n+ * ES582.1 and ES584.1 (naming convention: we use the term \"ES58X FD\"\n+ * when referring to those two variants together).\n+ *\n+ * Copyright (C) 2019 Robert Bosch Engineering and Business\n+ * Solutions. All rights reserved.\n+ * Copyright (C) 2020 ETAS K.K.. All rights reserved.\n+ */\n+\n+#ifndef __ES58X_FD_H__\n+#define __ES58X_FD_H__\n+\n+#include <linux/types.h>\n+\n+#define ES582_1_NUM_CAN_CH 2\n+#define ES584_1_NUM_CAN_CH 1\n+#define ES58X_FD_NUM_CAN_CH 2\n+\n+#define ES58X_FD_TX_BULK_MAX 100\n+#define ES58X_FD_RX_BULK_MAX 100\n+#define ES58X_FD_RX_LOOPBACK_BULK_MAX 100\n+\n+enum es58x_fd_cmd_type {\n+\tES58X_FD_CMD_TYPE_CAN = 0x03,\n+\tES58X_FD_CMD_TYPE_CANFD = 0x04,\n+\tES58X_FD_CMD_TYPE_DEVICE = 0xFF\n+};\n+\n+/* Command IDs for ES58X_FD_CMD_TYPE_{CAN,CANFD}. */\n+enum es58x_fd_can_cmd_id {\n+\tES58X_FD_CAN_CMD_ID_ENABLE_CHANNEL = 0x01,\n+\tES58X_FD_CAN_CMD_ID_DISABLE_CHANNEL = 0x02,\n+\tES58X_FD_CAN_CMD_ID_TX_MSG = 0x05,\n+\tES58X_FD_CAN_CMD_ID_MSG_SELFRX_ACK = 0x07,\n+\tES58X_FD_CAN_CMD_ID_RX_MSG = 0x10,\n+\tES58X_FD_CAN_CMD_ID_ERROR_OR_EVENT_MSG = 0x11,\n+\tES58X_FD_CAN_CMD_ID_RESET_RX = 0x20,\n+\tES58X_FD_CAN_CMD_ID_RESET_TX = 0x21,\n+\tES58X_FD_CAN_CMD_ID_TX_MSG_NO_ACK = 0x55\n+};\n+\n+/* Command IDs for ES58X_FD_CMD_TYPE_DEVICE. */\n+enum es58x_fd_dev_cmd_id {\n+\tES58X_FD_DEV_CMD_ID_GETTIMETICKS = 0x01,\n+\tES58X_FD_DEV_CMD_ID_TIMESTAMP = 0x02\n+};\n+\n+/**\n+ * enum es58x_fd_ctrlmode - Controller mode.\n+ * @ES58X_FD_CTRLMODE_ACTIVE: send and receive messages.\n+ * @ES58X_FD_CTRLMODE_PASSIVE: only receive messages (monitor). Do not\n+ *\tsend anything, not even the acknowledgment bit.\n+ * @ES58X_FD_CTRLMODE_FD_ISO: CAN-FD according to ISO11898-1.\n+ * @ES58X_FD_CTRLMODE_FD_NON_ISO: follow Bosch CAN FD Specification\n+ *\tV1.0\n+ * @ES58X_FD_CTRLMODE_DISABLE_PROTOCOL_EXCEPTION_HANDLING: How to\n+ *\tbehave when CAN FD reserved bit is monitored as\n+ *\tdominant. (c.f. ISO 11898-1:2015, section 10.4.2.4 \"Control\n+ *\tfield\", paragraph \"r0 bit\"). 0 (not disable = enable): send\n+ *\terror frame. 1 (disable): goes into bus integration mode\n+ *\t(c.f. below).\n+ * @ES58X_FD_CTRLMODE_EDGE_FILTER_DURING_BUS_INTEGRATION: 0: Edge\n+ *\tfiltering is disabled. 1: Edge filtering is enabled. Two\n+ *\tconsecutive dominant bits required to detect an edge for hard\n+ *\tsynchronization.\n+ */\n+enum es58x_fd_ctrlmode {\n+\tES58X_FD_CTRLMODE_ACTIVE = 0,\n+\tES58X_FD_CTRLMODE_PASSIVE = BIT(0),\n+\tES58X_FD_CTRLMODE_FD_ISO = BIT(4),\n+\tES58X_FD_CTRLMODE_FD_NON_ISO = BIT(5),\n+\tES58X_FD_CTRLMODE_DISABLE_PROTOCOL_EXCEPTION_HANDLING = BIT(6),\n+\tES58X_FD_CTRLMODE_EDGE_FILTER_DURING_BUS_INTEGRATION = BIT(7)\n+};\n+\n+struct es58x_fd_bittiming {\n+\t__le32 bitrate;\n+\t__le16 tseg1;\t\t/* range: [tseg1_min-1..tseg1_max-1] */\n+\t__le16 tseg2;\t\t/* range: [tseg2_min-1..tseg2_max-1] */\n+\t__le16 brp;\t\t/* range: [brp_min-1..brp_max-1] */\n+\t__le16 sjw;\t\t/* range: [0..sjw_max-1] */\n+} __packed;\n+\n+/**\n+ * struct es58x_fd_tx_conf_msg - Channel configuration.\n+ * @nominal_bittiming: Nominal bittiming.\n+ * @samples_per_bit: type enum es58x_samples_per_bit.\n+ * @sync_edge: type enum es58x_sync_edge.\n+ * @physical_media: type enum es58x_physical_media.\n+ * @self_reception_mode: type enum es58x_self_reception_mode.\n+ * @ctrlmode: type enum es58x_fd_ctrlmode.\n+ * @canfd_enabled: boolean (0: Classical CAN, 1: CAN and/or CANFD).\n+ * @data_bittiming: Bittiming for flexible data-rate transmission.\n+ * @tdc: Transmitter Delay Compensation switch (0: disabled, 1:\n+ *\tenabled). On very high bitrates, the delay between when the\n+ *\tbit is sent and received on the CANTX and CANRX pins of the\n+ *\ttransceiver start to be significant enough for errors to occur\n+ *\tand thus need to be compensated.\n+ * @tdco: Transmitter Delay Compensation Offset. Offset value, in time\n+ *\tquanta, defining the delay between the start of the bit\n+ *\treception on the CANRX pin of the transceiver and the SSP\n+ *\t(Secondary Sample Point). Valid values: 0 to 127.\n+ * @tdcf: Transmitter Delay Compensation Filter window. Defines the\n+ *\tminimum value for the SSP position, in time quanta. The\n+ *\tfeature is enabled when TDCF is configured to a value greater\n+ *\tthan TDCO. Valid values: 0 to 127.\n+ *\n+ * Please refer to the microcontroller datasheet: \"SAM\n+ * E701/S70/V70/V71 Family\" section 49 \"Controller Area Network\n+ * (MCAN)\" for additional information.\n+ */\n+struct es58x_fd_tx_conf_msg {\n+\tstruct es58x_fd_bittiming nominal_bittiming;\n+\tu8 samples_per_bit;\n+\tu8 sync_edge;\n+\tu8 physical_media;\n+\tu8 self_reception_mode;\n+\tu8 ctrlmode;\n+\tu8 canfd_enabled;\n+\tstruct es58x_fd_bittiming data_bittiming;\n+\tu8 tdc;\n+\t__le16 tdco;\n+\t__le16 tdcf;\n+} __packed;\n+\n+#define ES58X_FD_CAN_CONF_LEN\t\t\t\t\t\\\n+\t(offsetof(struct es58x_fd_tx_conf_msg, canfd_enabled))\n+#define ES58X_FD_CANFD_CONF_LEN (sizeof(struct es58x_fd_tx_conf_msg))\n+\n+struct es58x_fd_tx_can_msg {\n+\tu8 packet_idx;\n+\t__le32 can_id;\n+\tu8 flags;\n+\tunion {\n+\t\tu8 dlc;\t\t/* Only if cmd_id is ES58X_FD_CMD_TYPE_CAN */\n+\t\tu8 len;\t\t/* Only if cmd_id is ES58X_FD_CMD_TYPE_CANFD */\n+\t} __packed;\n+\tu8 data[CANFD_MAX_DLEN];\n+} __packed;\n+\n+#define ES58X_FD_CAN_TX_LEN\t\t\t\t\t\t\\\n+\t(offsetof(struct es58x_fd_tx_can_msg, data[CAN_MAX_DLEN]))\n+#define ES58X_FD_CANFD_TX_LEN (sizeof(struct es58x_fd_tx_can_msg))\n+\n+struct es58x_fd_rx_can_msg {\n+\t__le64 timestamp;\n+\t__le32 can_id;\n+\tu8 flags;\n+\tunion {\n+\t\tu8 dlc;\t\t/* Only if cmd_id is ES58X_FD_CMD_TYPE_CAN */\n+\t\tu8 len;\t\t/* Only if cmd_id is ES58X_FD_CMD_TYPE_CANFD */\n+\t} __packed;\n+\tu8 data[CANFD_MAX_DLEN];\n+} __packed;\n+\n+#define ES58X_FD_CAN_RX_LEN\t\t\t\t\t\t\\\n+\t(offsetof(struct es58x_fd_rx_can_msg, data[CAN_MAX_DLEN]))\n+#define ES58X_FD_CANFD_RX_LEN (sizeof(struct es58x_fd_rx_can_msg))\n+\n+struct es58x_fd_rx_loopback_msg {\n+\t__le64 timestamp;\n+\tu8 packet_idx;\n+} __packed;\n+\n+struct es58x_fd_rx_event_msg {\n+\t__le64 timestamp;\n+\t__le32 can_id;\n+\tu8 flags;\t\t/* type enum es58x_flag */\n+\tu8 error_type;\t\t/* 0: event, 1: error */\n+\tu8 error_code;\n+\tu8 event_code;\n+} __packed;\n+\n+struct es58x_fd_tx_ack_msg {\n+\t__le32 rx_cmd_ret_le32;\t/* type enum es58x_cmd_ret_code_u32 */\n+\t__le16 tx_free_entries;\t/* Number of remaining free entries in the device TX queue */\n+} __packed;\n+\n+/**\n+ * struct es58x_fd_urb_cmd - Commands received from or sent to the\n+ *\tES58X FD device.\n+ * @SOF: Start of Frame.\n+ * @cmd_type: Command Type (type: enum es58x_fd_cmd_type). The CRC\n+ *\tcalculation starts at this position.\n+ * @cmd_id: Command ID (type: enum es58x_fd_cmd_id).\n+ * @channel_idx: Channel index starting at 0.\n+ * @msg_len: Length of the message, excluding CRC (i.e. length of the\n+ *\tunion).\n+ * @raw_msg: Message raw payload.\n+ * @tx_conf_msg: Channel configuration.\n+ * @tx_can_msg_buf: Concatenation of Tx messages. Type is \"u8[]\"\n+ *\tinstead of \"struct es58x_fd_tx_msg[]\" because the structure\n+ *\thas a flexible size.\n+ * @rx_can_msg_buf: Concatenation Rx messages. Type is \"u8[]\" instead\n+ *\tof \"struct es58x_fd_rx_msg[]\" because the structure has a\n+ *\tflexible size.\n+ * @rx_loopback_msg: Array of loopback messages.\n+ * @rx_event_msg: Error or event message.\n+ * @tx_ack_msg: Tx acknowledgment message.\n+ * @timestamp: Timestamp reply.\n+ * @rx_cmd_ret_le32: Rx 32 bits return code (type: enum\n+ *\tes58x_cmd_ret_code_u32).\n+ * @reserved_for_crc16_do_not_use: The structure ends with a\n+ *\tCRC16. Because the structures in above union are of variable\n+ *\tlengths, we can not predict the offset of the CRC in\n+ *\tadvance. Use functions es58x_get_crc() and es58x_set_crc() to\n+ *\tmanipulate it.\n+ */\n+struct es58x_fd_urb_cmd {\n+\t__le16 SOF;\n+\tu8 cmd_type;\n+\tu8 cmd_id;\n+\tu8 channel_idx;\n+\t__le16 msg_len;\n+\n+\tunion {\n+\t\tstruct es58x_fd_tx_conf_msg tx_conf_msg;\n+\t\tu8 tx_can_msg_buf[ES58X_FD_TX_BULK_MAX * ES58X_FD_CANFD_TX_LEN];\n+\t\tu8 rx_can_msg_buf[ES58X_FD_RX_BULK_MAX * ES58X_FD_CANFD_RX_LEN];\n+\t\tstruct es58x_fd_rx_loopback_msg\n+\t\t rx_loopback_msg[ES58X_FD_RX_LOOPBACK_BULK_MAX];\n+\t\tstruct es58x_fd_rx_event_msg rx_event_msg;\n+\t\tstruct es58x_fd_tx_ack_msg tx_ack_msg;\n+\t\t__le64 timestamp;\n+\t\t__le32 rx_cmd_ret_le32;\n+\t\tu8 raw_msg[0];\n+\t} __packed;\n+\n+\t__le16 reserved_for_crc16_do_not_use;\n+} __packed;\n+\n+#define ES58X_FD_URB_CMD_HEADER_LEN (offsetof(struct es58x_fd_urb_cmd, raw_msg))\n+#define ES58X_FD_TX_URB_CMD_MAX_LEN\t\t\t\t\t\\\n+\tES58X_SIZEOF_URB_CMD(struct es58x_fd_urb_cmd, tx_can_msg_buf)\n+#define ES58X_FD_RX_URB_CMD_MAX_LEN\t\t\t\t\t\\\n+\tES58X_SIZEOF_URB_CMD(struct es58x_fd_urb_cmd, rx_can_msg_buf)\n+\n+#endif /* __ES58X_FD_H__ */\n",
    "prefixes": [
        "v5"
    ]
}