can: kvaser_usb: kvaser_usb_leaf: Handle CMD_ERROR_EVENT
authorJimmy Assarsson <extja@kvaser.com>
Mon, 10 Oct 2022 18:52:30 +0000 (20:52 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 31 Dec 2022 12:32:05 +0000 (13:32 +0100)
[ Upstream commit b24cb2d169e0c9dce664a959e1f2aa9781285dc9 ]

The device will send an error event command, to indicate certain errors.
This indicates a misbehaving driver, and should never occur.

Fixes: 080f40a6fa28 ("can: kvaser_usb: Add support for Kvaser CAN/USB devices")
Tested-by: Anssi Hannula <anssi.hannula@bitwise.fi>
Co-developed-by: Anssi Hannula <anssi.hannula@bitwise.fi>
Signed-off-by: Anssi Hannula <anssi.hannula@bitwise.fi>
Signed-off-by: Jimmy Assarsson <extja@kvaser.com>
Link: https://lore.kernel.org/all/20221010185237.319219-5-extja@kvaser.com
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c

index a1d4ac8d4a0809489a34ebdcab5baaff464ed88f..5d96ab85d04cbfa2ccf1c75e39b0bceebd50a782 100644 (file)
@@ -70,6 +70,7 @@
 #define CMD_GET_CARD_INFO_REPLY                35
 #define CMD_GET_SOFTWARE_INFO          38
 #define CMD_GET_SOFTWARE_INFO_REPLY    39
+#define CMD_ERROR_EVENT                        45
 #define CMD_FLUSH_QUEUE                        48
 #define CMD_TX_ACKNOWLEDGE             50
 #define CMD_CAN_ERROR_EVENT            51
@@ -258,6 +259,28 @@ struct usbcan_cmd_can_error_event {
        __le16 time;
 } __packed;
 
+/* CMD_ERROR_EVENT error codes */
+#define KVASER_USB_LEAF_ERROR_EVENT_TX_QUEUE_FULL 0x8
+#define KVASER_USB_LEAF_ERROR_EVENT_PARAM 0x9
+
+struct leaf_cmd_error_event {
+       u8 tid;
+       u8 error_code;
+       __le16 timestamp[3];
+       __le16 padding;
+       __le16 info1;
+       __le16 info2;
+} __packed;
+
+struct usbcan_cmd_error_event {
+       u8 tid;
+       u8 error_code;
+       __le16 info1;
+       __le16 info2;
+       __le16 timestamp;
+       __le16 padding;
+} __packed;
+
 struct kvaser_cmd_ctrl_mode {
        u8 tid;
        u8 channel;
@@ -321,6 +344,7 @@ struct kvaser_cmd {
                        struct leaf_cmd_chip_state_event chip_state_event;
                        struct leaf_cmd_can_error_event can_error_event;
                        struct leaf_cmd_log_message log_message;
+                       struct leaf_cmd_error_event error_event;
                        struct kvaser_cmd_cap_req cap_req;
                        struct kvaser_cmd_cap_res cap_res;
                } __packed leaf;
@@ -330,6 +354,7 @@ struct kvaser_cmd {
                        struct usbcan_cmd_rx_can rx_can;
                        struct usbcan_cmd_chip_state_event chip_state_event;
                        struct usbcan_cmd_can_error_event can_error_event;
+                       struct usbcan_cmd_error_event error_event;
                } __packed usbcan;
 
                struct kvaser_cmd_tx_can tx_can;
@@ -353,6 +378,7 @@ static const u8 kvaser_usb_leaf_cmd_sizes_leaf[] = {
        [CMD_CHIP_STATE_EVENT]          = kvaser_fsize(u.leaf.chip_state_event),
        [CMD_CAN_ERROR_EVENT]           = kvaser_fsize(u.leaf.can_error_event),
        [CMD_GET_CAPABILITIES_RESP]     = kvaser_fsize(u.leaf.cap_res),
+       [CMD_ERROR_EVENT]               = kvaser_fsize(u.leaf.error_event),
        /* ignored events: */
        [CMD_FLUSH_QUEUE_REPLY]         = CMD_SIZE_ANY,
 };
@@ -367,6 +393,7 @@ static const u8 kvaser_usb_leaf_cmd_sizes_usbcan[] = {
        [CMD_RX_EXT_MESSAGE]            = kvaser_fsize(u.usbcan.rx_can),
        [CMD_CHIP_STATE_EVENT]          = kvaser_fsize(u.usbcan.chip_state_event),
        [CMD_CAN_ERROR_EVENT]           = kvaser_fsize(u.usbcan.can_error_event),
+       [CMD_ERROR_EVENT]               = kvaser_fsize(u.usbcan.error_event),
        /* ignored events: */
        [CMD_USBCAN_CLOCK_OVERFLOW_EVENT] = CMD_SIZE_ANY,
 };
@@ -1304,6 +1331,74 @@ static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev,
        netif_rx(skb);
 }
 
+static void kvaser_usb_leaf_error_event_parameter(const struct kvaser_usb *dev,
+                                                 const struct kvaser_cmd *cmd)
+{
+       u16 info1 = 0;
+
+       switch (dev->driver_info->family) {
+       case KVASER_LEAF:
+               info1 = le16_to_cpu(cmd->u.leaf.error_event.info1);
+               break;
+       case KVASER_USBCAN:
+               info1 = le16_to_cpu(cmd->u.usbcan.error_event.info1);
+               break;
+       }
+
+       /* info1 will contain the offending cmd_no */
+       switch (info1) {
+       case CMD_SET_CTRL_MODE:
+               dev_warn(&dev->intf->dev,
+                        "CMD_SET_CTRL_MODE error in parameter\n");
+               break;
+
+       case CMD_SET_BUS_PARAMS:
+               dev_warn(&dev->intf->dev,
+                        "CMD_SET_BUS_PARAMS error in parameter\n");
+               break;
+
+       default:
+               dev_warn(&dev->intf->dev,
+                        "Unhandled parameter error event cmd_no (%u)\n",
+                        info1);
+               break;
+       }
+}
+
+static void kvaser_usb_leaf_error_event(const struct kvaser_usb *dev,
+                                       const struct kvaser_cmd *cmd)
+{
+       u8 error_code = 0;
+
+       switch (dev->driver_info->family) {
+       case KVASER_LEAF:
+               error_code = cmd->u.leaf.error_event.error_code;
+               break;
+       case KVASER_USBCAN:
+               error_code = cmd->u.usbcan.error_event.error_code;
+               break;
+       }
+
+       switch (error_code) {
+       case KVASER_USB_LEAF_ERROR_EVENT_TX_QUEUE_FULL:
+               /* Received additional CAN message, when firmware TX queue is
+                * already full. Something is wrong with the driver.
+                * This should never happen!
+                */
+               dev_err(&dev->intf->dev,
+                       "Received error event TX_QUEUE_FULL\n");
+               break;
+       case KVASER_USB_LEAF_ERROR_EVENT_PARAM:
+               kvaser_usb_leaf_error_event_parameter(dev, cmd);
+               break;
+
+       default:
+               dev_warn(&dev->intf->dev,
+                        "Unhandled error event (%d)\n", error_code);
+               break;
+       }
+}
+
 static void kvaser_usb_leaf_start_chip_reply(const struct kvaser_usb *dev,
                                             const struct kvaser_cmd *cmd)
 {
@@ -1382,6 +1477,10 @@ static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev,
                kvaser_usb_leaf_tx_acknowledge(dev, cmd);
                break;
 
+       case CMD_ERROR_EVENT:
+               kvaser_usb_leaf_error_event(dev, cmd);
+               break;
+
        /* Ignored commands */
        case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
                if (dev->driver_info->family != KVASER_USBCAN)