linux/drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/* Parts of this driver are based on the following:
   3 *  - Kvaser linux mhydra driver (version 5.24)
   4 *  - CAN driver for esd CAN-USB/2
   5 *
   6 * Copyright (C) 2018 KVASER AB, Sweden. All rights reserved.
   7 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
   8 *
   9 * Known issues:
  10 *  - Transition from CAN_STATE_ERROR_WARNING to CAN_STATE_ERROR_ACTIVE is only
  11 *    reported after a call to do_get_berr_counter(), since firmware does not
  12 *    distinguish between ERROR_WARNING and ERROR_ACTIVE.
  13 *  - Hardware timestamps are not set for CAN Tx frames.
  14 */
  15
  16#include <linux/completion.h>
  17#include <linux/device.h>
  18#include <linux/gfp.h>
  19#include <linux/jiffies.h>
  20#include <linux/kernel.h>
  21#include <linux/netdevice.h>
  22#include <linux/spinlock.h>
  23#include <linux/string.h>
  24#include <linux/types.h>
  25#include <linux/units.h>
  26#include <linux/usb.h>
  27
  28#include <linux/can.h>
  29#include <linux/can/dev.h>
  30#include <linux/can/error.h>
  31#include <linux/can/netlink.h>
  32
  33#include "kvaser_usb.h"
  34
  35/* Forward declarations */
  36static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_kcan;
  37static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_flexc;
  38static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_rt;
  39
  40#define KVASER_USB_HYDRA_BULK_EP_IN_ADDR        0x82
  41#define KVASER_USB_HYDRA_BULK_EP_OUT_ADDR       0x02
  42
  43#define KVASER_USB_HYDRA_MAX_TRANSID            0xff
  44#define KVASER_USB_HYDRA_MIN_TRANSID            0x01
  45
  46/* Minihydra command IDs */
  47#define CMD_SET_BUSPARAMS_REQ                   16
  48#define CMD_GET_CHIP_STATE_REQ                  19
  49#define CMD_CHIP_STATE_EVENT                    20
  50#define CMD_SET_DRIVERMODE_REQ                  21
  51#define CMD_START_CHIP_REQ                      26
  52#define CMD_START_CHIP_RESP                     27
  53#define CMD_STOP_CHIP_REQ                       28
  54#define CMD_STOP_CHIP_RESP                      29
  55#define CMD_TX_CAN_MESSAGE                      33
  56#define CMD_GET_CARD_INFO_REQ                   34
  57#define CMD_GET_CARD_INFO_RESP                  35
  58#define CMD_GET_SOFTWARE_INFO_REQ               38
  59#define CMD_GET_SOFTWARE_INFO_RESP              39
  60#define CMD_ERROR_EVENT                         45
  61#define CMD_FLUSH_QUEUE                         48
  62#define CMD_TX_ACKNOWLEDGE                      50
  63#define CMD_FLUSH_QUEUE_RESP                    66
  64#define CMD_SET_BUSPARAMS_FD_REQ                69
  65#define CMD_SET_BUSPARAMS_FD_RESP               70
  66#define CMD_SET_BUSPARAMS_RESP                  85
  67#define CMD_GET_CAPABILITIES_REQ                95
  68#define CMD_GET_CAPABILITIES_RESP               96
  69#define CMD_RX_MESSAGE                          106
  70#define CMD_MAP_CHANNEL_REQ                     200
  71#define CMD_MAP_CHANNEL_RESP                    201
  72#define CMD_GET_SOFTWARE_DETAILS_REQ            202
  73#define CMD_GET_SOFTWARE_DETAILS_RESP           203
  74#define CMD_EXTENDED                            255
  75
  76/* Minihydra extended command IDs */
  77#define CMD_TX_CAN_MESSAGE_FD                   224
  78#define CMD_TX_ACKNOWLEDGE_FD                   225
  79#define CMD_RX_MESSAGE_FD                       226
  80
  81/* Hydra commands are handled by different threads in firmware.
  82 * The threads are denoted hydra entity (HE). Each HE got a unique 6-bit
  83 * address. The address is used in hydra commands to get/set source and
  84 * destination HE. There are two predefined HE addresses, the remaining
  85 * addresses are different between devices and firmware versions. Hence, we need
  86 * to enumerate the addresses (see kvaser_usb_hydra_map_channel()).
  87 */
  88
  89/* Well-known HE addresses */
  90#define KVASER_USB_HYDRA_HE_ADDRESS_ROUTER      0x00
  91#define KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL     0x3e
  92
  93#define KVASER_USB_HYDRA_TRANSID_CANHE          0x40
  94#define KVASER_USB_HYDRA_TRANSID_SYSDBG         0x61
  95
  96struct kvaser_cmd_map_ch_req {
  97        char name[16];
  98        u8 channel;
  99        u8 reserved[11];
 100} __packed;
 101
 102struct kvaser_cmd_map_ch_res {
 103        u8 he_addr;
 104        u8 channel;
 105        u8 reserved[26];
 106} __packed;
 107
 108struct kvaser_cmd_card_info {
 109        __le32 serial_number;
 110        __le32 clock_res;
 111        __le32 mfg_date;
 112        __le32 ean[2];
 113        u8 hw_version;
 114        u8 usb_mode;
 115        u8 hw_type;
 116        u8 reserved0;
 117        u8 nchannels;
 118        u8 reserved1[3];
 119} __packed;
 120
 121struct kvaser_cmd_sw_info {
 122        u8 reserved0[8];
 123        __le16 max_outstanding_tx;
 124        u8 reserved1[18];
 125} __packed;
 126
 127struct kvaser_cmd_sw_detail_req {
 128        u8 use_ext_cmd;
 129        u8 reserved[27];
 130} __packed;
 131
 132/* Software detail flags */
 133#define KVASER_USB_HYDRA_SW_FLAG_FW_BETA        BIT(2)
 134#define KVASER_USB_HYDRA_SW_FLAG_FW_BAD         BIT(4)
 135#define KVASER_USB_HYDRA_SW_FLAG_FREQ_80M       BIT(5)
 136#define KVASER_USB_HYDRA_SW_FLAG_EXT_CMD        BIT(9)
 137#define KVASER_USB_HYDRA_SW_FLAG_CANFD          BIT(10)
 138#define KVASER_USB_HYDRA_SW_FLAG_NONISO         BIT(11)
 139#define KVASER_USB_HYDRA_SW_FLAG_EXT_CAP        BIT(12)
 140#define KVASER_USB_HYDRA_SW_FLAG_CAN_FREQ_80M   BIT(13)
 141struct kvaser_cmd_sw_detail_res {
 142        __le32 sw_flags;
 143        __le32 sw_version;
 144        __le32 sw_name;
 145        __le32 ean[2];
 146        __le32 max_bitrate;
 147        u8 reserved[4];
 148} __packed;
 149
 150/* Sub commands for cap_req and cap_res */
 151#define KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE    0x02
 152#define KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT     0x05
 153#define KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT       0x06
 154struct kvaser_cmd_cap_req {
 155        __le16 cap_cmd;
 156        u8 reserved[26];
 157} __packed;
 158
 159/* Status codes for cap_res */
 160#define KVASER_USB_HYDRA_CAP_STAT_OK            0x00
 161#define KVASER_USB_HYDRA_CAP_STAT_NOT_IMPL      0x01
 162#define KVASER_USB_HYDRA_CAP_STAT_UNAVAIL       0x02
 163struct kvaser_cmd_cap_res {
 164        __le16 cap_cmd;
 165        __le16 status;
 166        __le32 mask;
 167        __le32 value;
 168        u8 reserved[16];
 169} __packed;
 170
 171/* CMD_ERROR_EVENT error codes */
 172#define KVASER_USB_HYDRA_ERROR_EVENT_CAN        0x01
 173#define KVASER_USB_HYDRA_ERROR_EVENT_PARAM      0x09
 174struct kvaser_cmd_error_event {
 175        __le16 timestamp[3];
 176        u8 reserved;
 177        u8 error_code;
 178        __le16 info1;
 179        __le16 info2;
 180} __packed;
 181
 182/* Chip state status flags. Used for chip_state_event and err_frame_data. */
 183#define KVASER_USB_HYDRA_BUS_ERR_ACT            0x00
 184#define KVASER_USB_HYDRA_BUS_ERR_PASS           BIT(5)
 185#define KVASER_USB_HYDRA_BUS_BUS_OFF            BIT(6)
 186struct kvaser_cmd_chip_state_event {
 187        __le16 timestamp[3];
 188        u8 tx_err_counter;
 189        u8 rx_err_counter;
 190        u8 bus_status;
 191        u8 reserved[19];
 192} __packed;
 193
 194/* Busparam modes */
 195#define KVASER_USB_HYDRA_BUS_MODE_CAN           0x00
 196#define KVASER_USB_HYDRA_BUS_MODE_CANFD_ISO     0x01
 197#define KVASER_USB_HYDRA_BUS_MODE_NONISO        0x02
 198struct kvaser_cmd_set_busparams {
 199        __le32 bitrate;
 200        u8 tseg1;
 201        u8 tseg2;
 202        u8 sjw;
 203        u8 nsamples;
 204        u8 reserved0[4];
 205        __le32 bitrate_d;
 206        u8 tseg1_d;
 207        u8 tseg2_d;
 208        u8 sjw_d;
 209        u8 nsamples_d;
 210        u8 canfd_mode;
 211        u8 reserved1[7];
 212} __packed;
 213
 214/* Ctrl modes */
 215#define KVASER_USB_HYDRA_CTRLMODE_NORMAL        0x01
 216#define KVASER_USB_HYDRA_CTRLMODE_LISTEN        0x02
 217struct kvaser_cmd_set_ctrlmode {
 218        u8 mode;
 219        u8 reserved[27];
 220} __packed;
 221
 222struct kvaser_err_frame_data {
 223        u8 bus_status;
 224        u8 reserved0;
 225        u8 tx_err_counter;
 226        u8 rx_err_counter;
 227        u8 reserved1[4];
 228} __packed;
 229
 230struct kvaser_cmd_rx_can {
 231        u8 cmd_len;
 232        u8 cmd_no;
 233        u8 channel;
 234        u8 flags;
 235        __le16 timestamp[3];
 236        u8 dlc;
 237        u8 padding;
 238        __le32 id;
 239        union {
 240                u8 data[8];
 241                struct kvaser_err_frame_data err_frame_data;
 242        };
 243} __packed;
 244
 245/* Extended CAN ID flag. Used in rx_can and tx_can */
 246#define KVASER_USB_HYDRA_EXTENDED_FRAME_ID      BIT(31)
 247struct kvaser_cmd_tx_can {
 248        __le32 id;
 249        u8 data[8];
 250        u8 dlc;
 251        u8 flags;
 252        __le16 transid;
 253        u8 channel;
 254        u8 reserved[11];
 255} __packed;
 256
 257struct kvaser_cmd_header {
 258        u8 cmd_no;
 259        /* The destination HE address is stored in 0..5 of he_addr.
 260         * The upper part of source HE address is stored in 6..7 of he_addr, and
 261         * the lower part is stored in 12..15 of transid.
 262         */
 263        u8 he_addr;
 264        __le16 transid;
 265} __packed;
 266
 267struct kvaser_cmd {
 268        struct kvaser_cmd_header header;
 269        union {
 270                struct kvaser_cmd_map_ch_req map_ch_req;
 271                struct kvaser_cmd_map_ch_res map_ch_res;
 272
 273                struct kvaser_cmd_card_info card_info;
 274                struct kvaser_cmd_sw_info sw_info;
 275                struct kvaser_cmd_sw_detail_req sw_detail_req;
 276                struct kvaser_cmd_sw_detail_res sw_detail_res;
 277
 278                struct kvaser_cmd_cap_req cap_req;
 279                struct kvaser_cmd_cap_res cap_res;
 280
 281                struct kvaser_cmd_error_event error_event;
 282
 283                struct kvaser_cmd_set_busparams set_busparams_req;
 284
 285                struct kvaser_cmd_chip_state_event chip_state_event;
 286
 287                struct kvaser_cmd_set_ctrlmode set_ctrlmode;
 288
 289                struct kvaser_cmd_rx_can rx_can;
 290                struct kvaser_cmd_tx_can tx_can;
 291        } __packed;
 292} __packed;
 293
 294/* CAN frame flags. Used in rx_can, ext_rx_can, tx_can and ext_tx_can */
 295#define KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME    BIT(0)
 296#define KVASER_USB_HYDRA_CF_FLAG_OVERRUN        BIT(1)
 297#define KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME   BIT(4)
 298#define KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID    BIT(5)
 299#define KVASER_USB_HYDRA_CF_FLAG_TX_ACK         BIT(6)
 300/* CAN frame flags. Used in ext_rx_can and ext_tx_can */
 301#define KVASER_USB_HYDRA_CF_FLAG_OSM_NACK       BIT(12)
 302#define KVASER_USB_HYDRA_CF_FLAG_ABL            BIT(13)
 303#define KVASER_USB_HYDRA_CF_FLAG_FDF            BIT(16)
 304#define KVASER_USB_HYDRA_CF_FLAG_BRS            BIT(17)
 305#define KVASER_USB_HYDRA_CF_FLAG_ESI            BIT(18)
 306
 307/* KCAN packet header macros. Used in ext_rx_can and ext_tx_can */
 308#define KVASER_USB_KCAN_DATA_DLC_BITS           4
 309#define KVASER_USB_KCAN_DATA_DLC_SHIFT          8
 310#define KVASER_USB_KCAN_DATA_DLC_MASK \
 311                                GENMASK(KVASER_USB_KCAN_DATA_DLC_BITS - 1 + \
 312                                KVASER_USB_KCAN_DATA_DLC_SHIFT, \
 313                                KVASER_USB_KCAN_DATA_DLC_SHIFT)
 314
 315#define KVASER_USB_KCAN_DATA_BRS                BIT(14)
 316#define KVASER_USB_KCAN_DATA_FDF                BIT(15)
 317#define KVASER_USB_KCAN_DATA_OSM                BIT(16)
 318#define KVASER_USB_KCAN_DATA_AREQ               BIT(31)
 319#define KVASER_USB_KCAN_DATA_SRR                BIT(31)
 320#define KVASER_USB_KCAN_DATA_RTR                BIT(29)
 321#define KVASER_USB_KCAN_DATA_IDE                BIT(30)
 322struct kvaser_cmd_ext_rx_can {
 323        __le32 flags;
 324        __le32 id;
 325        __le32 kcan_id;
 326        __le32 kcan_header;
 327        __le64 timestamp;
 328        union {
 329                u8 kcan_payload[64];
 330                struct kvaser_err_frame_data err_frame_data;
 331        };
 332} __packed;
 333
 334struct kvaser_cmd_ext_tx_can {
 335        __le32 flags;
 336        __le32 id;
 337        __le32 kcan_id;
 338        __le32 kcan_header;
 339        u8 databytes;
 340        u8 dlc;
 341        u8 reserved[6];
 342        u8 kcan_payload[64];
 343} __packed;
 344
 345struct kvaser_cmd_ext_tx_ack {
 346        __le32 flags;
 347        u8 reserved0[4];
 348        __le64 timestamp;
 349        u8 reserved1[8];
 350} __packed;
 351
 352/* struct for extended commands (CMD_EXTENDED) */
 353struct kvaser_cmd_ext {
 354        struct kvaser_cmd_header header;
 355        __le16 len;
 356        u8 cmd_no_ext;
 357        u8 reserved;
 358
 359        union {
 360                struct kvaser_cmd_ext_rx_can rx_can;
 361                struct kvaser_cmd_ext_tx_can tx_can;
 362                struct kvaser_cmd_ext_tx_ack tx_ack;
 363        } __packed;
 364} __packed;
 365
 366static const struct can_bittiming_const kvaser_usb_hydra_kcan_bittiming_c = {
 367        .name = "kvaser_usb_kcan",
 368        .tseg1_min = 1,
 369        .tseg1_max = 255,
 370        .tseg2_min = 1,
 371        .tseg2_max = 32,
 372        .sjw_max = 16,
 373        .brp_min = 1,
 374        .brp_max = 8192,
 375        .brp_inc = 1,
 376};
 377
 378const struct can_bittiming_const kvaser_usb_flexc_bittiming_const = {
 379        .name = "kvaser_usb_flex",
 380        .tseg1_min = 4,
 381        .tseg1_max = 16,
 382        .tseg2_min = 2,
 383        .tseg2_max = 8,
 384        .sjw_max = 4,
 385        .brp_min = 1,
 386        .brp_max = 256,
 387        .brp_inc = 1,
 388};
 389
 390static const struct can_bittiming_const kvaser_usb_hydra_rt_bittiming_c = {
 391        .name = "kvaser_usb_rt",
 392        .tseg1_min = 2,
 393        .tseg1_max = 96,
 394        .tseg2_min = 2,
 395        .tseg2_max = 32,
 396        .sjw_max = 32,
 397        .brp_min = 1,
 398        .brp_max = 1024,
 399        .brp_inc = 1,
 400};
 401
 402static const struct can_bittiming_const kvaser_usb_hydra_rtd_bittiming_c = {
 403        .name = "kvaser_usb_rt",
 404        .tseg1_min = 2,
 405        .tseg1_max = 39,
 406        .tseg2_min = 2,
 407        .tseg2_max = 8,
 408        .sjw_max = 8,
 409        .brp_min = 1,
 410        .brp_max = 1024,
 411        .brp_inc = 1,
 412};
 413
 414#define KVASER_USB_HYDRA_TRANSID_BITS           12
 415#define KVASER_USB_HYDRA_TRANSID_MASK \
 416                                GENMASK(KVASER_USB_HYDRA_TRANSID_BITS - 1, 0)
 417#define KVASER_USB_HYDRA_HE_ADDR_SRC_MASK       GENMASK(7, 6)
 418#define KVASER_USB_HYDRA_HE_ADDR_DEST_MASK      GENMASK(5, 0)
 419#define KVASER_USB_HYDRA_HE_ADDR_SRC_BITS       2
 420static inline u16 kvaser_usb_hydra_get_cmd_transid(const struct kvaser_cmd *cmd)
 421{
 422        return le16_to_cpu(cmd->header.transid) & KVASER_USB_HYDRA_TRANSID_MASK;
 423}
 424
 425static inline void kvaser_usb_hydra_set_cmd_transid(struct kvaser_cmd *cmd,
 426                                                    u16 transid)
 427{
 428        cmd->header.transid =
 429                        cpu_to_le16(transid & KVASER_USB_HYDRA_TRANSID_MASK);
 430}
 431
 432static inline u8 kvaser_usb_hydra_get_cmd_src_he(const struct kvaser_cmd *cmd)
 433{
 434        return (cmd->header.he_addr & KVASER_USB_HYDRA_HE_ADDR_SRC_MASK) >>
 435                KVASER_USB_HYDRA_HE_ADDR_SRC_BITS |
 436                le16_to_cpu(cmd->header.transid) >>
 437                KVASER_USB_HYDRA_TRANSID_BITS;
 438}
 439
 440static inline void kvaser_usb_hydra_set_cmd_dest_he(struct kvaser_cmd *cmd,
 441                                                    u8 dest_he)
 442{
 443        cmd->header.he_addr =
 444                (cmd->header.he_addr & KVASER_USB_HYDRA_HE_ADDR_SRC_MASK) |
 445                (dest_he & KVASER_USB_HYDRA_HE_ADDR_DEST_MASK);
 446}
 447
 448static u8 kvaser_usb_hydra_channel_from_cmd(const struct kvaser_usb *dev,
 449                                            const struct kvaser_cmd *cmd)
 450{
 451        int i;
 452        u8 channel = 0xff;
 453        u8 src_he = kvaser_usb_hydra_get_cmd_src_he(cmd);
 454
 455        for (i = 0; i < KVASER_USB_MAX_NET_DEVICES; i++) {
 456                if (dev->card_data.hydra.channel_to_he[i] == src_he) {
 457                        channel = i;
 458                        break;
 459                }
 460        }
 461
 462        return channel;
 463}
 464
 465static u16 kvaser_usb_hydra_get_next_transid(struct kvaser_usb *dev)
 466{
 467        unsigned long flags;
 468        u16 transid;
 469        struct kvaser_usb_dev_card_data_hydra *card_data =
 470                                                        &dev->card_data.hydra;
 471
 472        spin_lock_irqsave(&card_data->transid_lock, flags);
 473        transid = card_data->transid;
 474        if (transid >= KVASER_USB_HYDRA_MAX_TRANSID)
 475                transid = KVASER_USB_HYDRA_MIN_TRANSID;
 476        else
 477                transid++;
 478        card_data->transid = transid;
 479        spin_unlock_irqrestore(&card_data->transid_lock, flags);
 480
 481        return transid;
 482}
 483
 484static size_t kvaser_usb_hydra_cmd_size(struct kvaser_cmd *cmd)
 485{
 486        size_t ret;
 487
 488        if (cmd->header.cmd_no == CMD_EXTENDED)
 489                ret = le16_to_cpu(((struct kvaser_cmd_ext *)cmd)->len);
 490        else
 491                ret = sizeof(struct kvaser_cmd);
 492
 493        return ret;
 494}
 495
 496static struct kvaser_usb_net_priv *
 497kvaser_usb_hydra_net_priv_from_cmd(const struct kvaser_usb *dev,
 498                                   const struct kvaser_cmd *cmd)
 499{
 500        struct kvaser_usb_net_priv *priv = NULL;
 501        u8 channel = kvaser_usb_hydra_channel_from_cmd(dev, cmd);
 502
 503        if (channel >= dev->nchannels)
 504                dev_err(&dev->intf->dev,
 505                        "Invalid channel number (%d)\n", channel);
 506        else
 507                priv = dev->nets[channel];
 508
 509        return priv;
 510}
 511
 512static ktime_t
 513kvaser_usb_hydra_ktime_from_rx_cmd(const struct kvaser_usb_dev_cfg *cfg,
 514                                   const struct kvaser_cmd *cmd)
 515{
 516        u64 ticks;
 517
 518        if (cmd->header.cmd_no == CMD_EXTENDED) {
 519                struct kvaser_cmd_ext *cmd_ext = (struct kvaser_cmd_ext *)cmd;
 520
 521                ticks = le64_to_cpu(cmd_ext->rx_can.timestamp);
 522        } else {
 523                ticks = le16_to_cpu(cmd->rx_can.timestamp[0]);
 524                ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[1])) << 16;
 525                ticks += (u64)(le16_to_cpu(cmd->rx_can.timestamp[2])) << 32;
 526        }
 527
 528        return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq));
 529}
 530
 531static int kvaser_usb_hydra_send_simple_cmd(struct kvaser_usb *dev,
 532                                            u8 cmd_no, int channel)
 533{
 534        struct kvaser_cmd *cmd;
 535        int err;
 536
 537        cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
 538        if (!cmd)
 539                return -ENOMEM;
 540
 541        cmd->header.cmd_no = cmd_no;
 542        if (channel < 0) {
 543                kvaser_usb_hydra_set_cmd_dest_he
 544                                (cmd, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL);
 545        } else {
 546                if (channel >= KVASER_USB_MAX_NET_DEVICES) {
 547                        dev_err(&dev->intf->dev, "channel (%d) out of range.\n",
 548                                channel);
 549                        err = -EINVAL;
 550                        goto end;
 551                }
 552                kvaser_usb_hydra_set_cmd_dest_he
 553                        (cmd, dev->card_data.hydra.channel_to_he[channel]);
 554        }
 555        kvaser_usb_hydra_set_cmd_transid
 556                                (cmd, kvaser_usb_hydra_get_next_transid(dev));
 557
 558        err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
 559        if (err)
 560                goto end;
 561
 562end:
 563        kfree(cmd);
 564
 565        return err;
 566}
 567
 568static int
 569kvaser_usb_hydra_send_simple_cmd_async(struct kvaser_usb_net_priv *priv,
 570                                       u8 cmd_no)
 571{
 572        struct kvaser_cmd *cmd;
 573        struct kvaser_usb *dev = priv->dev;
 574        int err;
 575
 576        cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_ATOMIC);
 577        if (!cmd)
 578                return -ENOMEM;
 579
 580        cmd->header.cmd_no = cmd_no;
 581
 582        kvaser_usb_hydra_set_cmd_dest_he
 583                (cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
 584        kvaser_usb_hydra_set_cmd_transid
 585                                (cmd, kvaser_usb_hydra_get_next_transid(dev));
 586
 587        err = kvaser_usb_send_cmd_async(priv, cmd,
 588                                        kvaser_usb_hydra_cmd_size(cmd));
 589        if (err)
 590                kfree(cmd);
 591
 592        return err;
 593}
 594
 595/* This function is used for synchronously waiting on hydra control commands.
 596 * Note: Compared to kvaser_usb_hydra_read_bulk_callback(), we never need to
 597 *       handle partial hydra commands. Since hydra control commands are always
 598 *       non-extended commands.
 599 */
 600static int kvaser_usb_hydra_wait_cmd(const struct kvaser_usb *dev, u8 cmd_no,
 601                                     struct kvaser_cmd *cmd)
 602{
 603        void *buf;
 604        int err;
 605        unsigned long timeout = jiffies + msecs_to_jiffies(KVASER_USB_TIMEOUT);
 606
 607        if (cmd->header.cmd_no == CMD_EXTENDED) {
 608                dev_err(&dev->intf->dev, "Wait for CMD_EXTENDED not allowed\n");
 609                return -EINVAL;
 610        }
 611
 612        buf = kzalloc(KVASER_USB_RX_BUFFER_SIZE, GFP_KERNEL);
 613        if (!buf)
 614                return -ENOMEM;
 615
 616        do {
 617                int actual_len = 0;
 618                int pos = 0;
 619
 620                err = kvaser_usb_recv_cmd(dev, buf, KVASER_USB_RX_BUFFER_SIZE,
 621                                          &actual_len);
 622                if (err < 0)
 623                        goto end;
 624
 625                while (pos < actual_len) {
 626                        struct kvaser_cmd *tmp_cmd;
 627                        size_t cmd_len;
 628
 629                        tmp_cmd = buf + pos;
 630                        cmd_len = kvaser_usb_hydra_cmd_size(tmp_cmd);
 631                        if (pos + cmd_len > actual_len) {
 632                                dev_err_ratelimited(&dev->intf->dev,
 633                                                    "Format error\n");
 634                                break;
 635                        }
 636
 637                        if (tmp_cmd->header.cmd_no == cmd_no) {
 638                                memcpy(cmd, tmp_cmd, cmd_len);
 639                                goto end;
 640                        }
 641                        pos += cmd_len;
 642                }
 643        } while (time_before(jiffies, timeout));
 644
 645        err = -EINVAL;
 646
 647end:
 648        kfree(buf);
 649
 650        return err;
 651}
 652
 653static int kvaser_usb_hydra_map_channel_resp(struct kvaser_usb *dev,
 654                                             const struct kvaser_cmd *cmd)
 655{
 656        u8 he, channel;
 657        u16 transid = kvaser_usb_hydra_get_cmd_transid(cmd);
 658        struct kvaser_usb_dev_card_data_hydra *card_data =
 659                                                        &dev->card_data.hydra;
 660
 661        if (transid > 0x007f || transid < 0x0040) {
 662                dev_err(&dev->intf->dev,
 663                        "CMD_MAP_CHANNEL_RESP, invalid transid: 0x%x\n",
 664                        transid);
 665                return -EINVAL;
 666        }
 667
 668        switch (transid) {
 669        case KVASER_USB_HYDRA_TRANSID_CANHE:
 670        case KVASER_USB_HYDRA_TRANSID_CANHE + 1:
 671        case KVASER_USB_HYDRA_TRANSID_CANHE + 2:
 672        case KVASER_USB_HYDRA_TRANSID_CANHE + 3:
 673        case KVASER_USB_HYDRA_TRANSID_CANHE + 4:
 674                channel = transid & 0x000f;
 675                he = cmd->map_ch_res.he_addr;
 676                card_data->channel_to_he[channel] = he;
 677                break;
 678        case KVASER_USB_HYDRA_TRANSID_SYSDBG:
 679                card_data->sysdbg_he = cmd->map_ch_res.he_addr;
 680                break;
 681        default:
 682                dev_warn(&dev->intf->dev,
 683                         "Unknown CMD_MAP_CHANNEL_RESP transid=0x%x\n",
 684                         transid);
 685                break;
 686        }
 687
 688        return 0;
 689}
 690
 691static int kvaser_usb_hydra_map_channel(struct kvaser_usb *dev, u16 transid,
 692                                        u8 channel, const char *name)
 693{
 694        struct kvaser_cmd *cmd;
 695        int err;
 696
 697        cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
 698        if (!cmd)
 699                return -ENOMEM;
 700
 701        strcpy(cmd->map_ch_req.name, name);
 702        cmd->header.cmd_no = CMD_MAP_CHANNEL_REQ;
 703        kvaser_usb_hydra_set_cmd_dest_he
 704                                (cmd, KVASER_USB_HYDRA_HE_ADDRESS_ROUTER);
 705        cmd->map_ch_req.channel = channel;
 706
 707        kvaser_usb_hydra_set_cmd_transid(cmd, transid);
 708
 709        err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
 710        if (err)
 711                goto end;
 712
 713        err = kvaser_usb_hydra_wait_cmd(dev, CMD_MAP_CHANNEL_RESP, cmd);
 714        if (err)
 715                goto end;
 716
 717        err = kvaser_usb_hydra_map_channel_resp(dev, cmd);
 718        if (err)
 719                goto end;
 720
 721end:
 722        kfree(cmd);
 723
 724        return err;
 725}
 726
 727static int kvaser_usb_hydra_get_single_capability(struct kvaser_usb *dev,
 728                                                  u16 cap_cmd_req, u16 *status)
 729{
 730        struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
 731        struct kvaser_cmd *cmd;
 732        u32 value = 0;
 733        u32 mask = 0;
 734        u16 cap_cmd_res;
 735        int err;
 736        int i;
 737
 738        cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
 739        if (!cmd)
 740                return -ENOMEM;
 741
 742        cmd->header.cmd_no = CMD_GET_CAPABILITIES_REQ;
 743        cmd->cap_req.cap_cmd = cpu_to_le16(cap_cmd_req);
 744
 745        kvaser_usb_hydra_set_cmd_dest_he(cmd, card_data->hydra.sysdbg_he);
 746        kvaser_usb_hydra_set_cmd_transid
 747                                (cmd, kvaser_usb_hydra_get_next_transid(dev));
 748
 749        err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
 750        if (err)
 751                goto end;
 752
 753        err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_CAPABILITIES_RESP, cmd);
 754        if (err)
 755                goto end;
 756
 757        *status = le16_to_cpu(cmd->cap_res.status);
 758
 759        if (*status != KVASER_USB_HYDRA_CAP_STAT_OK)
 760                goto end;
 761
 762        cap_cmd_res = le16_to_cpu(cmd->cap_res.cap_cmd);
 763        switch (cap_cmd_res) {
 764        case KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE:
 765        case KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT:
 766        case KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT:
 767                value = le32_to_cpu(cmd->cap_res.value);
 768                mask = le32_to_cpu(cmd->cap_res.mask);
 769                break;
 770        default:
 771                dev_warn(&dev->intf->dev, "Unknown capability command %u\n",
 772                         cap_cmd_res);
 773                break;
 774        }
 775
 776        for (i = 0; i < dev->nchannels; i++) {
 777                if (BIT(i) & (value & mask)) {
 778                        switch (cap_cmd_res) {
 779                        case KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE:
 780                                card_data->ctrlmode_supported |=
 781                                                CAN_CTRLMODE_LISTENONLY;
 782                                break;
 783                        case KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT:
 784                                card_data->capabilities |=
 785                                                KVASER_USB_CAP_BERR_CAP;
 786                                break;
 787                        case KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT:
 788                                card_data->ctrlmode_supported |=
 789                                                CAN_CTRLMODE_ONE_SHOT;
 790                                break;
 791                        }
 792                }
 793        }
 794
 795end:
 796        kfree(cmd);
 797
 798        return err;
 799}
 800
 801static void kvaser_usb_hydra_start_chip_reply(const struct kvaser_usb *dev,
 802                                              const struct kvaser_cmd *cmd)
 803{
 804        struct kvaser_usb_net_priv *priv;
 805
 806        priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
 807        if (!priv)
 808                return;
 809
 810        if (completion_done(&priv->start_comp) &&
 811            netif_queue_stopped(priv->netdev)) {
 812                netif_wake_queue(priv->netdev);
 813        } else {
 814                netif_start_queue(priv->netdev);
 815                complete(&priv->start_comp);
 816        }
 817}
 818
 819static void kvaser_usb_hydra_stop_chip_reply(const struct kvaser_usb *dev,
 820                                             const struct kvaser_cmd *cmd)
 821{
 822        struct kvaser_usb_net_priv *priv;
 823
 824        priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
 825        if (!priv)
 826                return;
 827
 828        complete(&priv->stop_comp);
 829}
 830
 831static void kvaser_usb_hydra_flush_queue_reply(const struct kvaser_usb *dev,
 832                                               const struct kvaser_cmd *cmd)
 833{
 834        struct kvaser_usb_net_priv *priv;
 835
 836        priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
 837        if (!priv)
 838                return;
 839
 840        complete(&priv->flush_comp);
 841}
 842
 843static void
 844kvaser_usb_hydra_bus_status_to_can_state(const struct kvaser_usb_net_priv *priv,
 845                                         u8 bus_status,
 846                                         const struct can_berr_counter *bec,
 847                                         enum can_state *new_state)
 848{
 849        if (bus_status & KVASER_USB_HYDRA_BUS_BUS_OFF) {
 850                *new_state = CAN_STATE_BUS_OFF;
 851        } else if (bus_status & KVASER_USB_HYDRA_BUS_ERR_PASS) {
 852                *new_state = CAN_STATE_ERROR_PASSIVE;
 853        } else if (bus_status == KVASER_USB_HYDRA_BUS_ERR_ACT) {
 854                if (bec->txerr >= 128 || bec->rxerr >= 128) {
 855                        netdev_warn(priv->netdev,
 856                                    "ERR_ACTIVE but err tx=%u or rx=%u >=128\n",
 857                                    bec->txerr, bec->rxerr);
 858                        *new_state = CAN_STATE_ERROR_PASSIVE;
 859                } else if (bec->txerr >= 96 || bec->rxerr >= 96) {
 860                        *new_state = CAN_STATE_ERROR_WARNING;
 861                } else {
 862                        *new_state = CAN_STATE_ERROR_ACTIVE;
 863                }
 864        }
 865}
 866
 867static void kvaser_usb_hydra_update_state(struct kvaser_usb_net_priv *priv,
 868                                          u8 bus_status,
 869                                          const struct can_berr_counter *bec)
 870{
 871        struct net_device *netdev = priv->netdev;
 872        struct can_frame *cf;
 873        struct sk_buff *skb;
 874        enum can_state new_state, old_state;
 875
 876        old_state = priv->can.state;
 877
 878        kvaser_usb_hydra_bus_status_to_can_state(priv, bus_status, bec,
 879                                                 &new_state);
 880
 881        if (new_state == old_state)
 882                return;
 883
 884        /* Ignore state change if previous state was STOPPED and the new state
 885         * is BUS_OFF. Firmware always report this as BUS_OFF, since firmware
 886         * does not distinguish between BUS_OFF and STOPPED.
 887         */
 888        if (old_state == CAN_STATE_STOPPED && new_state == CAN_STATE_BUS_OFF)
 889                return;
 890
 891        skb = alloc_can_err_skb(netdev, &cf);
 892        if (skb) {
 893                enum can_state tx_state, rx_state;
 894
 895                tx_state = (bec->txerr >= bec->rxerr) ?
 896                                        new_state : CAN_STATE_ERROR_ACTIVE;
 897                rx_state = (bec->txerr <= bec->rxerr) ?
 898                                        new_state : CAN_STATE_ERROR_ACTIVE;
 899                can_change_state(netdev, cf, tx_state, rx_state);
 900        }
 901
 902        if (new_state == CAN_STATE_BUS_OFF && old_state < CAN_STATE_BUS_OFF) {
 903                if (!priv->can.restart_ms)
 904                        kvaser_usb_hydra_send_simple_cmd_async
 905                                                (priv, CMD_STOP_CHIP_REQ);
 906
 907                can_bus_off(netdev);
 908        }
 909
 910        if (!skb) {
 911                netdev_warn(netdev, "No memory left for err_skb\n");
 912                return;
 913        }
 914
 915        if (priv->can.restart_ms &&
 916            old_state >= CAN_STATE_BUS_OFF &&
 917            new_state < CAN_STATE_BUS_OFF)
 918                priv->can.can_stats.restarts++;
 919
 920        cf->data[6] = bec->txerr;
 921        cf->data[7] = bec->rxerr;
 922
 923        netif_rx(skb);
 924}
 925
 926static void kvaser_usb_hydra_state_event(const struct kvaser_usb *dev,
 927                                         const struct kvaser_cmd *cmd)
 928{
 929        struct kvaser_usb_net_priv *priv;
 930        struct can_berr_counter bec;
 931        u8 bus_status;
 932
 933        priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
 934        if (!priv)
 935                return;
 936
 937        bus_status = cmd->chip_state_event.bus_status;
 938        bec.txerr = cmd->chip_state_event.tx_err_counter;
 939        bec.rxerr = cmd->chip_state_event.rx_err_counter;
 940
 941        kvaser_usb_hydra_update_state(priv, bus_status, &bec);
 942        priv->bec.txerr = bec.txerr;
 943        priv->bec.rxerr = bec.rxerr;
 944}
 945
 946static void kvaser_usb_hydra_error_event_parameter(const struct kvaser_usb *dev,
 947                                                   const struct kvaser_cmd *cmd)
 948{
 949        /* info1 will contain the offending cmd_no */
 950        switch (le16_to_cpu(cmd->error_event.info1)) {
 951        case CMD_START_CHIP_REQ:
 952                dev_warn(&dev->intf->dev,
 953                         "CMD_START_CHIP_REQ error in parameter\n");
 954                break;
 955
 956        case CMD_STOP_CHIP_REQ:
 957                dev_warn(&dev->intf->dev,
 958                         "CMD_STOP_CHIP_REQ error in parameter\n");
 959                break;
 960
 961        case CMD_FLUSH_QUEUE:
 962                dev_warn(&dev->intf->dev,
 963                         "CMD_FLUSH_QUEUE error in parameter\n");
 964                break;
 965
 966        case CMD_SET_BUSPARAMS_REQ:
 967                dev_warn(&dev->intf->dev,
 968                         "Set bittiming failed. Error in parameter\n");
 969                break;
 970
 971        case CMD_SET_BUSPARAMS_FD_REQ:
 972                dev_warn(&dev->intf->dev,
 973                         "Set data bittiming failed. Error in parameter\n");
 974                break;
 975
 976        default:
 977                dev_warn(&dev->intf->dev,
 978                         "Unhandled parameter error event cmd_no (%u)\n",
 979                         le16_to_cpu(cmd->error_event.info1));
 980                break;
 981        }
 982}
 983
 984static void kvaser_usb_hydra_error_event(const struct kvaser_usb *dev,
 985                                         const struct kvaser_cmd *cmd)
 986{
 987        switch (cmd->error_event.error_code) {
 988        case KVASER_USB_HYDRA_ERROR_EVENT_PARAM:
 989                kvaser_usb_hydra_error_event_parameter(dev, cmd);
 990                break;
 991
 992        case KVASER_USB_HYDRA_ERROR_EVENT_CAN:
 993                /* Wrong channel mapping?! This should never happen!
 994                 * info1 will contain the offending cmd_no
 995                 */
 996                dev_err(&dev->intf->dev,
 997                        "Received CAN error event for cmd_no (%u)\n",
 998                        le16_to_cpu(cmd->error_event.info1));
 999                break;
1000
1001        default:
1002                dev_warn(&dev->intf->dev,
1003                         "Unhandled error event (%d)\n",
1004                         cmd->error_event.error_code);
1005                break;
1006        }
1007}
1008
1009static void
1010kvaser_usb_hydra_error_frame(struct kvaser_usb_net_priv *priv,
1011                             const struct kvaser_err_frame_data *err_frame_data,
1012                             ktime_t hwtstamp)
1013{
1014        struct net_device *netdev = priv->netdev;
1015        struct net_device_stats *stats = &netdev->stats;
1016        struct can_frame *cf;
1017        struct sk_buff *skb;
1018        struct skb_shared_hwtstamps *shhwtstamps;
1019        struct can_berr_counter bec;
1020        enum can_state new_state, old_state;
1021        u8 bus_status;
1022
1023        priv->can.can_stats.bus_error++;
1024        stats->rx_errors++;
1025
1026        bus_status = err_frame_data->bus_status;
1027        bec.txerr = err_frame_data->tx_err_counter;
1028        bec.rxerr = err_frame_data->rx_err_counter;
1029
1030        old_state = priv->can.state;
1031        kvaser_usb_hydra_bus_status_to_can_state(priv, bus_status, &bec,
1032                                                 &new_state);
1033
1034        skb = alloc_can_err_skb(netdev, &cf);
1035
1036        if (new_state != old_state) {
1037                if (skb) {
1038                        enum can_state tx_state, rx_state;
1039
1040                        tx_state = (bec.txerr >= bec.rxerr) ?
1041                                        new_state : CAN_STATE_ERROR_ACTIVE;
1042                        rx_state = (bec.txerr <= bec.rxerr) ?
1043                                        new_state : CAN_STATE_ERROR_ACTIVE;
1044
1045                        can_change_state(netdev, cf, tx_state, rx_state);
1046
1047                        if (priv->can.restart_ms &&
1048                            old_state >= CAN_STATE_BUS_OFF &&
1049                            new_state < CAN_STATE_BUS_OFF)
1050                                cf->can_id |= CAN_ERR_RESTARTED;
1051                }
1052
1053                if (new_state == CAN_STATE_BUS_OFF) {
1054                        if (!priv->can.restart_ms)
1055                                kvaser_usb_hydra_send_simple_cmd_async
1056                                                (priv, CMD_STOP_CHIP_REQ);
1057
1058                        can_bus_off(netdev);
1059                }
1060        }
1061
1062        if (!skb) {
1063                stats->rx_dropped++;
1064                netdev_warn(netdev, "No memory left for err_skb\n");
1065                return;
1066        }
1067
1068        shhwtstamps = skb_hwtstamps(skb);
1069        shhwtstamps->hwtstamp = hwtstamp;
1070
1071        cf->can_id |= CAN_ERR_BUSERROR;
1072        cf->data[6] = bec.txerr;
1073        cf->data[7] = bec.rxerr;
1074
1075        netif_rx(skb);
1076
1077        priv->bec.txerr = bec.txerr;
1078        priv->bec.rxerr = bec.rxerr;
1079}
1080
1081static void kvaser_usb_hydra_one_shot_fail(struct kvaser_usb_net_priv *priv,
1082                                           const struct kvaser_cmd_ext *cmd)
1083{
1084        struct net_device *netdev = priv->netdev;
1085        struct net_device_stats *stats = &netdev->stats;
1086        struct can_frame *cf;
1087        struct sk_buff *skb;
1088        u32 flags;
1089
1090        skb = alloc_can_err_skb(netdev, &cf);
1091        if (!skb) {
1092                stats->rx_dropped++;
1093                netdev_warn(netdev, "No memory left for err_skb\n");
1094                return;
1095        }
1096
1097        cf->can_id |= CAN_ERR_BUSERROR;
1098        flags = le32_to_cpu(cmd->tx_ack.flags);
1099
1100        if (flags & KVASER_USB_HYDRA_CF_FLAG_OSM_NACK)
1101                cf->can_id |= CAN_ERR_ACK;
1102        if (flags & KVASER_USB_HYDRA_CF_FLAG_ABL) {
1103                cf->can_id |= CAN_ERR_LOSTARB;
1104                priv->can.can_stats.arbitration_lost++;
1105        }
1106
1107        stats->tx_errors++;
1108        netif_rx(skb);
1109}
1110
1111static void kvaser_usb_hydra_tx_acknowledge(const struct kvaser_usb *dev,
1112                                            const struct kvaser_cmd *cmd)
1113{
1114        struct kvaser_usb_tx_urb_context *context;
1115        struct kvaser_usb_net_priv *priv;
1116        unsigned long irq_flags;
1117        unsigned int len;
1118        bool one_shot_fail = false;
1119        bool is_err_frame = false;
1120        u16 transid = kvaser_usb_hydra_get_cmd_transid(cmd);
1121
1122        priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
1123        if (!priv)
1124                return;
1125
1126        if (!netif_device_present(priv->netdev))
1127                return;
1128
1129        if (cmd->header.cmd_no == CMD_EXTENDED) {
1130                struct kvaser_cmd_ext *cmd_ext = (struct kvaser_cmd_ext *)cmd;
1131                u32 flags = le32_to_cpu(cmd_ext->tx_ack.flags);
1132
1133                if (flags & (KVASER_USB_HYDRA_CF_FLAG_OSM_NACK |
1134                             KVASER_USB_HYDRA_CF_FLAG_ABL)) {
1135                        kvaser_usb_hydra_one_shot_fail(priv, cmd_ext);
1136                        one_shot_fail = true;
1137                }
1138
1139                is_err_frame = flags & KVASER_USB_HYDRA_CF_FLAG_TX_ACK &&
1140                               flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME;
1141        }
1142
1143        context = &priv->tx_contexts[transid % dev->max_tx_urbs];
1144
1145        spin_lock_irqsave(&priv->tx_contexts_lock, irq_flags);
1146
1147        len = can_get_echo_skb(priv->netdev, context->echo_index, NULL);
1148        context->echo_index = dev->max_tx_urbs;
1149        --priv->active_tx_contexts;
1150        netif_wake_queue(priv->netdev);
1151
1152        spin_unlock_irqrestore(&priv->tx_contexts_lock, irq_flags);
1153
1154        if (!one_shot_fail && !is_err_frame) {
1155                struct net_device_stats *stats = &priv->netdev->stats;
1156
1157                stats->tx_packets++;
1158                stats->tx_bytes += len;
1159        }
1160}
1161
1162static void kvaser_usb_hydra_rx_msg_std(const struct kvaser_usb *dev,
1163                                        const struct kvaser_cmd *cmd)
1164{
1165        struct kvaser_usb_net_priv *priv = NULL;
1166        struct can_frame *cf;
1167        struct sk_buff *skb;
1168        struct skb_shared_hwtstamps *shhwtstamps;
1169        struct net_device_stats *stats;
1170        u8 flags;
1171        ktime_t hwtstamp;
1172
1173        priv = kvaser_usb_hydra_net_priv_from_cmd(dev, cmd);
1174        if (!priv)
1175                return;
1176
1177        stats = &priv->netdev->stats;
1178
1179        flags = cmd->rx_can.flags;
1180        hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, cmd);
1181
1182        if (flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME) {
1183                kvaser_usb_hydra_error_frame(priv, &cmd->rx_can.err_frame_data,
1184                                             hwtstamp);
1185                return;
1186        }
1187
1188        skb = alloc_can_skb(priv->netdev, &cf);
1189        if (!skb) {
1190                stats->rx_dropped++;
1191                return;
1192        }
1193
1194        shhwtstamps = skb_hwtstamps(skb);
1195        shhwtstamps->hwtstamp = hwtstamp;
1196
1197        cf->can_id = le32_to_cpu(cmd->rx_can.id);
1198
1199        if (cf->can_id &  KVASER_USB_HYDRA_EXTENDED_FRAME_ID) {
1200                cf->can_id &= CAN_EFF_MASK;
1201                cf->can_id |= CAN_EFF_FLAG;
1202        } else {
1203                cf->can_id &= CAN_SFF_MASK;
1204        }
1205
1206        if (flags & KVASER_USB_HYDRA_CF_FLAG_OVERRUN)
1207                kvaser_usb_can_rx_over_error(priv->netdev);
1208
1209        cf->len = can_cc_dlc2len(cmd->rx_can.dlc);
1210
1211        if (flags & KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME) {
1212                cf->can_id |= CAN_RTR_FLAG;
1213        } else {
1214                memcpy(cf->data, cmd->rx_can.data, cf->len);
1215
1216                stats->rx_bytes += cf->len;
1217        }
1218        stats->rx_packets++;
1219
1220        netif_rx(skb);
1221}
1222
1223static void kvaser_usb_hydra_rx_msg_ext(const struct kvaser_usb *dev,
1224                                        const struct kvaser_cmd_ext *cmd)
1225{
1226        struct kvaser_cmd *std_cmd = (struct kvaser_cmd *)cmd;
1227        struct kvaser_usb_net_priv *priv;
1228        struct canfd_frame *cf;
1229        struct sk_buff *skb;
1230        struct skb_shared_hwtstamps *shhwtstamps;
1231        struct net_device_stats *stats;
1232        u32 flags;
1233        u8 dlc;
1234        u32 kcan_header;
1235        ktime_t hwtstamp;
1236
1237        priv = kvaser_usb_hydra_net_priv_from_cmd(dev, std_cmd);
1238        if (!priv)
1239                return;
1240
1241        stats = &priv->netdev->stats;
1242
1243        kcan_header = le32_to_cpu(cmd->rx_can.kcan_header);
1244        dlc = (kcan_header & KVASER_USB_KCAN_DATA_DLC_MASK) >>
1245                KVASER_USB_KCAN_DATA_DLC_SHIFT;
1246
1247        flags = le32_to_cpu(cmd->rx_can.flags);
1248        hwtstamp = kvaser_usb_hydra_ktime_from_rx_cmd(dev->cfg, std_cmd);
1249
1250        if (flags & KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME) {
1251                kvaser_usb_hydra_error_frame(priv, &cmd->rx_can.err_frame_data,
1252                                             hwtstamp);
1253                return;
1254        }
1255
1256        if (flags & KVASER_USB_HYDRA_CF_FLAG_FDF)
1257                skb = alloc_canfd_skb(priv->netdev, &cf);
1258        else
1259                skb = alloc_can_skb(priv->netdev, (struct can_frame **)&cf);
1260
1261        if (!skb) {
1262                stats->rx_dropped++;
1263                return;
1264        }
1265
1266        shhwtstamps = skb_hwtstamps(skb);
1267        shhwtstamps->hwtstamp = hwtstamp;
1268
1269        cf->can_id = le32_to_cpu(cmd->rx_can.id);
1270
1271        if (flags & KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID) {
1272                cf->can_id &= CAN_EFF_MASK;
1273                cf->can_id |= CAN_EFF_FLAG;
1274        } else {
1275                cf->can_id &= CAN_SFF_MASK;
1276        }
1277
1278        if (flags & KVASER_USB_HYDRA_CF_FLAG_OVERRUN)
1279                kvaser_usb_can_rx_over_error(priv->netdev);
1280
1281        if (flags & KVASER_USB_HYDRA_CF_FLAG_FDF) {
1282                cf->len = can_fd_dlc2len(dlc);
1283                if (flags & KVASER_USB_HYDRA_CF_FLAG_BRS)
1284                        cf->flags |= CANFD_BRS;
1285                if (flags & KVASER_USB_HYDRA_CF_FLAG_ESI)
1286                        cf->flags |= CANFD_ESI;
1287        } else {
1288                cf->len = can_cc_dlc2len(dlc);
1289        }
1290
1291        if (flags & KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME) {
1292                cf->can_id |= CAN_RTR_FLAG;
1293        } else {
1294                memcpy(cf->data, cmd->rx_can.kcan_payload, cf->len);
1295
1296                stats->rx_bytes += cf->len;
1297        }
1298        stats->rx_packets++;
1299
1300        netif_rx(skb);
1301}
1302
1303static void kvaser_usb_hydra_handle_cmd_std(const struct kvaser_usb *dev,
1304                                            const struct kvaser_cmd *cmd)
1305{
1306        switch (cmd->header.cmd_no) {
1307        case CMD_START_CHIP_RESP:
1308                kvaser_usb_hydra_start_chip_reply(dev, cmd);
1309                break;
1310
1311        case CMD_STOP_CHIP_RESP:
1312                kvaser_usb_hydra_stop_chip_reply(dev, cmd);
1313                break;
1314
1315        case CMD_FLUSH_QUEUE_RESP:
1316                kvaser_usb_hydra_flush_queue_reply(dev, cmd);
1317                break;
1318
1319        case CMD_CHIP_STATE_EVENT:
1320                kvaser_usb_hydra_state_event(dev, cmd);
1321                break;
1322
1323        case CMD_ERROR_EVENT:
1324                kvaser_usb_hydra_error_event(dev, cmd);
1325                break;
1326
1327        case CMD_TX_ACKNOWLEDGE:
1328                kvaser_usb_hydra_tx_acknowledge(dev, cmd);
1329                break;
1330
1331        case CMD_RX_MESSAGE:
1332                kvaser_usb_hydra_rx_msg_std(dev, cmd);
1333                break;
1334
1335        /* Ignored commands */
1336        case CMD_SET_BUSPARAMS_RESP:
1337        case CMD_SET_BUSPARAMS_FD_RESP:
1338                break;
1339
1340        default:
1341                dev_warn(&dev->intf->dev, "Unhandled command (%d)\n",
1342                         cmd->header.cmd_no);
1343                break;
1344        }
1345}
1346
1347static void kvaser_usb_hydra_handle_cmd_ext(const struct kvaser_usb *dev,
1348                                            const struct kvaser_cmd_ext *cmd)
1349{
1350        switch (cmd->cmd_no_ext) {
1351        case CMD_TX_ACKNOWLEDGE_FD:
1352                kvaser_usb_hydra_tx_acknowledge(dev, (struct kvaser_cmd *)cmd);
1353                break;
1354
1355        case CMD_RX_MESSAGE_FD:
1356                kvaser_usb_hydra_rx_msg_ext(dev, cmd);
1357                break;
1358
1359        default:
1360                dev_warn(&dev->intf->dev, "Unhandled extended command (%d)\n",
1361                         cmd->header.cmd_no);
1362                break;
1363        }
1364}
1365
1366static void kvaser_usb_hydra_handle_cmd(const struct kvaser_usb *dev,
1367                                        const struct kvaser_cmd *cmd)
1368{
1369                if (cmd->header.cmd_no == CMD_EXTENDED)
1370                        kvaser_usb_hydra_handle_cmd_ext
1371                                        (dev, (struct kvaser_cmd_ext *)cmd);
1372                else
1373                        kvaser_usb_hydra_handle_cmd_std(dev, cmd);
1374}
1375
1376static void *
1377kvaser_usb_hydra_frame_to_cmd_ext(const struct kvaser_usb_net_priv *priv,
1378                                  const struct sk_buff *skb, int *cmd_len,
1379                                  u16 transid)
1380{
1381        struct kvaser_usb *dev = priv->dev;
1382        struct kvaser_cmd_ext *cmd;
1383        struct canfd_frame *cf = (struct canfd_frame *)skb->data;
1384        u8 dlc = can_fd_len2dlc(cf->len);
1385        u8 nbr_of_bytes = cf->len;
1386        u32 flags;
1387        u32 id;
1388        u32 kcan_id;
1389        u32 kcan_header;
1390
1391        cmd = kcalloc(1, sizeof(struct kvaser_cmd_ext), GFP_ATOMIC);
1392        if (!cmd)
1393                return NULL;
1394
1395        kvaser_usb_hydra_set_cmd_dest_he
1396                        ((struct kvaser_cmd *)cmd,
1397                         dev->card_data.hydra.channel_to_he[priv->channel]);
1398        kvaser_usb_hydra_set_cmd_transid((struct kvaser_cmd *)cmd, transid);
1399
1400        cmd->header.cmd_no = CMD_EXTENDED;
1401        cmd->cmd_no_ext = CMD_TX_CAN_MESSAGE_FD;
1402
1403        *cmd_len = ALIGN(sizeof(struct kvaser_cmd_ext) -
1404                         sizeof(cmd->tx_can.kcan_payload) + nbr_of_bytes,
1405                         8);
1406
1407        cmd->len = cpu_to_le16(*cmd_len);
1408
1409        cmd->tx_can.databytes = nbr_of_bytes;
1410        cmd->tx_can.dlc = dlc;
1411
1412        if (cf->can_id & CAN_EFF_FLAG) {
1413                id = cf->can_id & CAN_EFF_MASK;
1414                flags = KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID;
1415                kcan_id = (cf->can_id & CAN_EFF_MASK) |
1416                          KVASER_USB_KCAN_DATA_IDE | KVASER_USB_KCAN_DATA_SRR;
1417        } else {
1418                id = cf->can_id & CAN_SFF_MASK;
1419                flags = 0;
1420                kcan_id = cf->can_id & CAN_SFF_MASK;
1421        }
1422
1423        if (cf->can_id & CAN_ERR_FLAG)
1424                flags |= KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME;
1425
1426        kcan_header = ((dlc << KVASER_USB_KCAN_DATA_DLC_SHIFT) &
1427                                KVASER_USB_KCAN_DATA_DLC_MASK) |
1428                        KVASER_USB_KCAN_DATA_AREQ |
1429                        (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT ?
1430                                KVASER_USB_KCAN_DATA_OSM : 0);
1431
1432        if (can_is_canfd_skb(skb)) {
1433                kcan_header |= KVASER_USB_KCAN_DATA_FDF |
1434                               (cf->flags & CANFD_BRS ?
1435                                        KVASER_USB_KCAN_DATA_BRS : 0);
1436        } else {
1437                if (cf->can_id & CAN_RTR_FLAG) {
1438                        kcan_id |= KVASER_USB_KCAN_DATA_RTR;
1439                        cmd->tx_can.databytes = 0;
1440                        flags |= KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME;
1441                }
1442        }
1443
1444        cmd->tx_can.kcan_id = cpu_to_le32(kcan_id);
1445        cmd->tx_can.id = cpu_to_le32(id);
1446        cmd->tx_can.flags = cpu_to_le32(flags);
1447        cmd->tx_can.kcan_header = cpu_to_le32(kcan_header);
1448
1449        memcpy(cmd->tx_can.kcan_payload, cf->data, nbr_of_bytes);
1450
1451        return cmd;
1452}
1453
1454static void *
1455kvaser_usb_hydra_frame_to_cmd_std(const struct kvaser_usb_net_priv *priv,
1456                                  const struct sk_buff *skb, int *cmd_len,
1457                                  u16 transid)
1458{
1459        struct kvaser_usb *dev = priv->dev;
1460        struct kvaser_cmd *cmd;
1461        struct can_frame *cf = (struct can_frame *)skb->data;
1462        u32 flags;
1463        u32 id;
1464
1465        cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_ATOMIC);
1466        if (!cmd)
1467                return NULL;
1468
1469        kvaser_usb_hydra_set_cmd_dest_he
1470                (cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1471        kvaser_usb_hydra_set_cmd_transid(cmd, transid);
1472
1473        cmd->header.cmd_no = CMD_TX_CAN_MESSAGE;
1474
1475        *cmd_len = ALIGN(sizeof(struct kvaser_cmd), 8);
1476
1477        if (cf->can_id & CAN_EFF_FLAG) {
1478                id = (cf->can_id & CAN_EFF_MASK);
1479                id |= KVASER_USB_HYDRA_EXTENDED_FRAME_ID;
1480        } else {
1481                id = cf->can_id & CAN_SFF_MASK;
1482        }
1483
1484        cmd->tx_can.dlc = cf->len;
1485
1486        flags = (cf->can_id & CAN_EFF_FLAG ?
1487                 KVASER_USB_HYDRA_CF_FLAG_EXTENDED_ID : 0);
1488
1489        if (cf->can_id & CAN_RTR_FLAG)
1490                flags |= KVASER_USB_HYDRA_CF_FLAG_REMOTE_FRAME;
1491
1492        flags |= (cf->can_id & CAN_ERR_FLAG ?
1493                  KVASER_USB_HYDRA_CF_FLAG_ERROR_FRAME : 0);
1494
1495        cmd->tx_can.id = cpu_to_le32(id);
1496        cmd->tx_can.flags = flags;
1497
1498        memcpy(cmd->tx_can.data, cf->data, cf->len);
1499
1500        return cmd;
1501}
1502
1503static int kvaser_usb_hydra_set_mode(struct net_device *netdev,
1504                                     enum can_mode mode)
1505{
1506        int err = 0;
1507
1508        switch (mode) {
1509        case CAN_MODE_START:
1510                /* CAN controller automatically recovers from BUS_OFF */
1511                break;
1512        default:
1513                err = -EOPNOTSUPP;
1514        }
1515
1516        return err;
1517}
1518
1519static int kvaser_usb_hydra_set_bittiming(struct net_device *netdev)
1520{
1521        struct kvaser_cmd *cmd;
1522        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1523        struct can_bittiming *bt = &priv->can.bittiming;
1524        struct kvaser_usb *dev = priv->dev;
1525        int tseg1 = bt->prop_seg + bt->phase_seg1;
1526        int tseg2 = bt->phase_seg2;
1527        int sjw = bt->sjw;
1528        int err;
1529
1530        cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1531        if (!cmd)
1532                return -ENOMEM;
1533
1534        cmd->header.cmd_no = CMD_SET_BUSPARAMS_REQ;
1535        cmd->set_busparams_req.bitrate = cpu_to_le32(bt->bitrate);
1536        cmd->set_busparams_req.sjw = (u8)sjw;
1537        cmd->set_busparams_req.tseg1 = (u8)tseg1;
1538        cmd->set_busparams_req.tseg2 = (u8)tseg2;
1539        cmd->set_busparams_req.nsamples = 1;
1540
1541        kvaser_usb_hydra_set_cmd_dest_he
1542                (cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1543        kvaser_usb_hydra_set_cmd_transid
1544                                (cmd, kvaser_usb_hydra_get_next_transid(dev));
1545
1546        err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1547
1548        kfree(cmd);
1549
1550        return err;
1551}
1552
1553static int kvaser_usb_hydra_set_data_bittiming(struct net_device *netdev)
1554{
1555        struct kvaser_cmd *cmd;
1556        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1557        struct can_bittiming *dbt = &priv->can.data_bittiming;
1558        struct kvaser_usb *dev = priv->dev;
1559        int tseg1 = dbt->prop_seg + dbt->phase_seg1;
1560        int tseg2 = dbt->phase_seg2;
1561        int sjw = dbt->sjw;
1562        int err;
1563
1564        cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1565        if (!cmd)
1566                return -ENOMEM;
1567
1568        cmd->header.cmd_no = CMD_SET_BUSPARAMS_FD_REQ;
1569        cmd->set_busparams_req.bitrate_d = cpu_to_le32(dbt->bitrate);
1570        cmd->set_busparams_req.sjw_d = (u8)sjw;
1571        cmd->set_busparams_req.tseg1_d = (u8)tseg1;
1572        cmd->set_busparams_req.tseg2_d = (u8)tseg2;
1573        cmd->set_busparams_req.nsamples_d = 1;
1574
1575        if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
1576                if (priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
1577                        cmd->set_busparams_req.canfd_mode =
1578                                        KVASER_USB_HYDRA_BUS_MODE_NONISO;
1579                else
1580                        cmd->set_busparams_req.canfd_mode =
1581                                        KVASER_USB_HYDRA_BUS_MODE_CANFD_ISO;
1582        }
1583
1584        kvaser_usb_hydra_set_cmd_dest_he
1585                (cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1586        kvaser_usb_hydra_set_cmd_transid
1587                                (cmd, kvaser_usb_hydra_get_next_transid(dev));
1588
1589        err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1590
1591        kfree(cmd);
1592
1593        return err;
1594}
1595
1596static int kvaser_usb_hydra_get_berr_counter(const struct net_device *netdev,
1597                                             struct can_berr_counter *bec)
1598{
1599        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1600        int err;
1601
1602        err = kvaser_usb_hydra_send_simple_cmd(priv->dev,
1603                                               CMD_GET_CHIP_STATE_REQ,
1604                                               priv->channel);
1605        if (err)
1606                return err;
1607
1608        *bec = priv->bec;
1609
1610        return 0;
1611}
1612
1613static int kvaser_usb_hydra_setup_endpoints(struct kvaser_usb *dev)
1614{
1615        const struct usb_host_interface *iface_desc;
1616        struct usb_endpoint_descriptor *ep;
1617        int i;
1618
1619        iface_desc = dev->intf->cur_altsetting;
1620
1621        for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1622                ep = &iface_desc->endpoint[i].desc;
1623
1624                if (!dev->bulk_in && usb_endpoint_is_bulk_in(ep) &&
1625                    ep->bEndpointAddress == KVASER_USB_HYDRA_BULK_EP_IN_ADDR)
1626                        dev->bulk_in = ep;
1627
1628                if (!dev->bulk_out && usb_endpoint_is_bulk_out(ep) &&
1629                    ep->bEndpointAddress == KVASER_USB_HYDRA_BULK_EP_OUT_ADDR)
1630                        dev->bulk_out = ep;
1631
1632                if (dev->bulk_in && dev->bulk_out)
1633                        return 0;
1634        }
1635
1636        return -ENODEV;
1637}
1638
1639static int kvaser_usb_hydra_init_card(struct kvaser_usb *dev)
1640{
1641        int err;
1642        unsigned int i;
1643        struct kvaser_usb_dev_card_data_hydra *card_data =
1644                                                        &dev->card_data.hydra;
1645
1646        card_data->transid = KVASER_USB_HYDRA_MIN_TRANSID;
1647        spin_lock_init(&card_data->transid_lock);
1648
1649        memset(card_data->usb_rx_leftover, 0, KVASER_USB_HYDRA_MAX_CMD_LEN);
1650        card_data->usb_rx_leftover_len = 0;
1651        spin_lock_init(&card_data->usb_rx_leftover_lock);
1652
1653        memset(card_data->channel_to_he, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL,
1654               sizeof(card_data->channel_to_he));
1655        card_data->sysdbg_he = 0;
1656
1657        for (i = 0; i < KVASER_USB_MAX_NET_DEVICES; i++) {
1658                err = kvaser_usb_hydra_map_channel
1659                                        (dev,
1660                                         (KVASER_USB_HYDRA_TRANSID_CANHE | i),
1661                                         i, "CAN");
1662                if (err) {
1663                        dev_err(&dev->intf->dev,
1664                                "CMD_MAP_CHANNEL_REQ failed for CAN%u\n", i);
1665                        return err;
1666                }
1667        }
1668
1669        err = kvaser_usb_hydra_map_channel(dev, KVASER_USB_HYDRA_TRANSID_SYSDBG,
1670                                           0, "SYSDBG");
1671        if (err) {
1672                dev_err(&dev->intf->dev,
1673                        "CMD_MAP_CHANNEL_REQ failed for SYSDBG\n");
1674                return err;
1675        }
1676
1677        return 0;
1678}
1679
1680static int kvaser_usb_hydra_get_software_info(struct kvaser_usb *dev)
1681{
1682        struct kvaser_cmd cmd;
1683        int err;
1684
1685        err = kvaser_usb_hydra_send_simple_cmd(dev, CMD_GET_SOFTWARE_INFO_REQ,
1686                                               -1);
1687        if (err)
1688                return err;
1689
1690        memset(&cmd, 0, sizeof(struct kvaser_cmd));
1691        err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_SOFTWARE_INFO_RESP, &cmd);
1692        if (err)
1693                return err;
1694
1695        dev->max_tx_urbs = min_t(unsigned int, KVASER_USB_MAX_TX_URBS,
1696                                 le16_to_cpu(cmd.sw_info.max_outstanding_tx));
1697
1698        return 0;
1699}
1700
1701static int kvaser_usb_hydra_get_software_details(struct kvaser_usb *dev)
1702{
1703        struct kvaser_cmd *cmd;
1704        int err;
1705        u32 flags;
1706        struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
1707
1708        cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1709        if (!cmd)
1710                return -ENOMEM;
1711
1712        cmd->header.cmd_no = CMD_GET_SOFTWARE_DETAILS_REQ;
1713        cmd->sw_detail_req.use_ext_cmd = 1;
1714        kvaser_usb_hydra_set_cmd_dest_he
1715                                (cmd, KVASER_USB_HYDRA_HE_ADDRESS_ILLEGAL);
1716
1717        kvaser_usb_hydra_set_cmd_transid
1718                                (cmd, kvaser_usb_hydra_get_next_transid(dev));
1719
1720        err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1721        if (err)
1722                goto end;
1723
1724        err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_SOFTWARE_DETAILS_RESP,
1725                                        cmd);
1726        if (err)
1727                goto end;
1728
1729        dev->fw_version = le32_to_cpu(cmd->sw_detail_res.sw_version);
1730        flags = le32_to_cpu(cmd->sw_detail_res.sw_flags);
1731
1732        if (flags & KVASER_USB_HYDRA_SW_FLAG_FW_BAD) {
1733                dev_err(&dev->intf->dev,
1734                        "Bad firmware, device refuse to run!\n");
1735                err = -EINVAL;
1736                goto end;
1737        }
1738
1739        if (flags & KVASER_USB_HYDRA_SW_FLAG_FW_BETA)
1740                dev_info(&dev->intf->dev, "Beta firmware in use\n");
1741
1742        if (flags & KVASER_USB_HYDRA_SW_FLAG_EXT_CAP)
1743                card_data->capabilities |= KVASER_USB_CAP_EXT_CAP;
1744
1745        if (flags & KVASER_USB_HYDRA_SW_FLAG_EXT_CMD)
1746                card_data->capabilities |= KVASER_USB_HYDRA_CAP_EXT_CMD;
1747
1748        if (flags & KVASER_USB_HYDRA_SW_FLAG_CANFD)
1749                card_data->ctrlmode_supported |= CAN_CTRLMODE_FD;
1750
1751        if (flags & KVASER_USB_HYDRA_SW_FLAG_NONISO)
1752                card_data->ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO;
1753
1754        if (flags &  KVASER_USB_HYDRA_SW_FLAG_FREQ_80M)
1755                dev->cfg = &kvaser_usb_hydra_dev_cfg_kcan;
1756        else if (flags & KVASER_USB_HYDRA_SW_FLAG_CAN_FREQ_80M)
1757                dev->cfg = &kvaser_usb_hydra_dev_cfg_rt;
1758        else
1759                dev->cfg = &kvaser_usb_hydra_dev_cfg_flexc;
1760
1761end:
1762        kfree(cmd);
1763
1764        return err;
1765}
1766
1767static int kvaser_usb_hydra_get_card_info(struct kvaser_usb *dev)
1768{
1769        struct kvaser_cmd cmd;
1770        int err;
1771
1772        err = kvaser_usb_hydra_send_simple_cmd(dev, CMD_GET_CARD_INFO_REQ, -1);
1773        if (err)
1774                return err;
1775
1776        memset(&cmd, 0, sizeof(struct kvaser_cmd));
1777        err = kvaser_usb_hydra_wait_cmd(dev, CMD_GET_CARD_INFO_RESP, &cmd);
1778        if (err)
1779                return err;
1780
1781        dev->nchannels = cmd.card_info.nchannels;
1782        if (dev->nchannels > KVASER_USB_MAX_NET_DEVICES)
1783                return -EINVAL;
1784
1785        return 0;
1786}
1787
1788static int kvaser_usb_hydra_get_capabilities(struct kvaser_usb *dev)
1789{
1790        int err;
1791        u16 status;
1792
1793        if (!(dev->card_data.capabilities & KVASER_USB_CAP_EXT_CAP)) {
1794                dev_info(&dev->intf->dev,
1795                         "No extended capability support. Upgrade your device.\n");
1796                return 0;
1797        }
1798
1799        err = kvaser_usb_hydra_get_single_capability
1800                                        (dev,
1801                                         KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE,
1802                                         &status);
1803        if (err)
1804                return err;
1805        if (status)
1806                dev_info(&dev->intf->dev,
1807                         "KVASER_USB_HYDRA_CAP_CMD_LISTEN_MODE failed %u\n",
1808                         status);
1809
1810        err = kvaser_usb_hydra_get_single_capability
1811                                        (dev,
1812                                         KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT,
1813                                         &status);
1814        if (err)
1815                return err;
1816        if (status)
1817                dev_info(&dev->intf->dev,
1818                         "KVASER_USB_HYDRA_CAP_CMD_ERR_REPORT failed %u\n",
1819                         status);
1820
1821        err = kvaser_usb_hydra_get_single_capability
1822                                        (dev, KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT,
1823                                         &status);
1824        if (err)
1825                return err;
1826        if (status)
1827                dev_info(&dev->intf->dev,
1828                         "KVASER_USB_HYDRA_CAP_CMD_ONE_SHOT failed %u\n",
1829                         status);
1830
1831        return 0;
1832}
1833
1834static int kvaser_usb_hydra_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1835{
1836        struct kvaser_usb *dev = priv->dev;
1837        struct kvaser_cmd *cmd;
1838        int err;
1839
1840        if ((priv->can.ctrlmode &
1841            (CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO)) ==
1842            CAN_CTRLMODE_FD_NON_ISO) {
1843                netdev_warn(priv->netdev,
1844                            "CTRLMODE_FD shall be on if CTRLMODE_FD_NON_ISO is on\n");
1845                return -EINVAL;
1846        }
1847
1848        cmd = kcalloc(1, sizeof(struct kvaser_cmd), GFP_KERNEL);
1849        if (!cmd)
1850                return -ENOMEM;
1851
1852        cmd->header.cmd_no = CMD_SET_DRIVERMODE_REQ;
1853        kvaser_usb_hydra_set_cmd_dest_he
1854                (cmd, dev->card_data.hydra.channel_to_he[priv->channel]);
1855        kvaser_usb_hydra_set_cmd_transid
1856                                (cmd, kvaser_usb_hydra_get_next_transid(dev));
1857        if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1858                cmd->set_ctrlmode.mode = KVASER_USB_HYDRA_CTRLMODE_LISTEN;
1859        else
1860                cmd->set_ctrlmode.mode = KVASER_USB_HYDRA_CTRLMODE_NORMAL;
1861
1862        err = kvaser_usb_send_cmd(dev, cmd, kvaser_usb_hydra_cmd_size(cmd));
1863        kfree(cmd);
1864
1865        return err;
1866}
1867
1868static int kvaser_usb_hydra_start_chip(struct kvaser_usb_net_priv *priv)
1869{
1870        int err;
1871
1872        init_completion(&priv->start_comp);
1873
1874        err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_START_CHIP_REQ,
1875                                               priv->channel);
1876        if (err)
1877                return err;
1878
1879        if (!wait_for_completion_timeout(&priv->start_comp,
1880                                         msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1881                return -ETIMEDOUT;
1882
1883        return 0;
1884}
1885
1886static int kvaser_usb_hydra_stop_chip(struct kvaser_usb_net_priv *priv)
1887{
1888        int err;
1889
1890        init_completion(&priv->stop_comp);
1891
1892        /* Make sure we do not report invalid BUS_OFF from CMD_CHIP_STATE_EVENT
1893         * see comment in kvaser_usb_hydra_update_state()
1894         */
1895        priv->can.state = CAN_STATE_STOPPED;
1896
1897        err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_STOP_CHIP_REQ,
1898                                               priv->channel);
1899        if (err)
1900                return err;
1901
1902        if (!wait_for_completion_timeout(&priv->stop_comp,
1903                                         msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1904                return -ETIMEDOUT;
1905
1906        return 0;
1907}
1908
1909static int kvaser_usb_hydra_flush_queue(struct kvaser_usb_net_priv *priv)
1910{
1911        int err;
1912
1913        init_completion(&priv->flush_comp);
1914
1915        err = kvaser_usb_hydra_send_simple_cmd(priv->dev, CMD_FLUSH_QUEUE,
1916                                               priv->channel);
1917        if (err)
1918                return err;
1919
1920        if (!wait_for_completion_timeout(&priv->flush_comp,
1921                                         msecs_to_jiffies(KVASER_USB_TIMEOUT)))
1922                return -ETIMEDOUT;
1923
1924        return 0;
1925}
1926
1927/* A single extended hydra command can be transmitted in multiple transfers
1928 * We have to buffer partial hydra commands, and handle them on next callback.
1929 */
1930static void kvaser_usb_hydra_read_bulk_callback(struct kvaser_usb *dev,
1931                                                void *buf, int len)
1932{
1933        unsigned long irq_flags;
1934        struct kvaser_cmd *cmd;
1935        int pos = 0;
1936        size_t cmd_len;
1937        struct kvaser_usb_dev_card_data_hydra *card_data =
1938                                                        &dev->card_data.hydra;
1939        int usb_rx_leftover_len;
1940        spinlock_t *usb_rx_leftover_lock = &card_data->usb_rx_leftover_lock;
1941
1942        spin_lock_irqsave(usb_rx_leftover_lock, irq_flags);
1943        usb_rx_leftover_len = card_data->usb_rx_leftover_len;
1944        if (usb_rx_leftover_len) {
1945                int remaining_bytes;
1946
1947                cmd = (struct kvaser_cmd *)card_data->usb_rx_leftover;
1948
1949                cmd_len = kvaser_usb_hydra_cmd_size(cmd);
1950
1951                remaining_bytes = min_t(unsigned int, len,
1952                                        cmd_len - usb_rx_leftover_len);
1953                /* Make sure we do not overflow usb_rx_leftover */
1954                if (remaining_bytes + usb_rx_leftover_len >
1955                                                KVASER_USB_HYDRA_MAX_CMD_LEN) {
1956                        dev_err(&dev->intf->dev, "Format error\n");
1957                        spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
1958                        return;
1959                }
1960
1961                memcpy(card_data->usb_rx_leftover + usb_rx_leftover_len, buf,
1962                       remaining_bytes);
1963                pos += remaining_bytes;
1964
1965                if (remaining_bytes + usb_rx_leftover_len == cmd_len) {
1966                        kvaser_usb_hydra_handle_cmd(dev, cmd);
1967                        usb_rx_leftover_len = 0;
1968                } else {
1969                        /* Command still not complete */
1970                        usb_rx_leftover_len += remaining_bytes;
1971                }
1972                card_data->usb_rx_leftover_len = usb_rx_leftover_len;
1973        }
1974        spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
1975
1976        while (pos < len) {
1977                cmd = buf + pos;
1978
1979                cmd_len = kvaser_usb_hydra_cmd_size(cmd);
1980
1981                if (pos + cmd_len > len) {
1982                        /* We got first part of a command */
1983                        int leftover_bytes;
1984
1985                        leftover_bytes = len - pos;
1986                        /* Make sure we do not overflow usb_rx_leftover */
1987                        if (leftover_bytes > KVASER_USB_HYDRA_MAX_CMD_LEN) {
1988                                dev_err(&dev->intf->dev, "Format error\n");
1989                                return;
1990                        }
1991                        spin_lock_irqsave(usb_rx_leftover_lock, irq_flags);
1992                        memcpy(card_data->usb_rx_leftover, buf + pos,
1993                               leftover_bytes);
1994                        card_data->usb_rx_leftover_len = leftover_bytes;
1995                        spin_unlock_irqrestore(usb_rx_leftover_lock, irq_flags);
1996                        break;
1997                }
1998
1999                kvaser_usb_hydra_handle_cmd(dev, cmd);
2000                pos += cmd_len;
2001        }
2002}
2003
2004static void *
2005kvaser_usb_hydra_frame_to_cmd(const struct kvaser_usb_net_priv *priv,
2006                              const struct sk_buff *skb, int *cmd_len,
2007                              u16 transid)
2008{
2009        void *buf;
2010
2011        if (priv->dev->card_data.capabilities & KVASER_USB_HYDRA_CAP_EXT_CMD)
2012                buf = kvaser_usb_hydra_frame_to_cmd_ext(priv, skb, cmd_len,
2013                                                        transid);
2014        else
2015                buf = kvaser_usb_hydra_frame_to_cmd_std(priv, skb, cmd_len,
2016                                                        transid);
2017
2018        return buf;
2019}
2020
2021const struct kvaser_usb_dev_ops kvaser_usb_hydra_dev_ops = {
2022        .dev_set_mode = kvaser_usb_hydra_set_mode,
2023        .dev_set_bittiming = kvaser_usb_hydra_set_bittiming,
2024        .dev_set_data_bittiming = kvaser_usb_hydra_set_data_bittiming,
2025        .dev_get_berr_counter = kvaser_usb_hydra_get_berr_counter,
2026        .dev_setup_endpoints = kvaser_usb_hydra_setup_endpoints,
2027        .dev_init_card = kvaser_usb_hydra_init_card,
2028        .dev_get_software_info = kvaser_usb_hydra_get_software_info,
2029        .dev_get_software_details = kvaser_usb_hydra_get_software_details,
2030        .dev_get_card_info = kvaser_usb_hydra_get_card_info,
2031        .dev_get_capabilities = kvaser_usb_hydra_get_capabilities,
2032        .dev_set_opt_mode = kvaser_usb_hydra_set_opt_mode,
2033        .dev_start_chip = kvaser_usb_hydra_start_chip,
2034        .dev_stop_chip = kvaser_usb_hydra_stop_chip,
2035        .dev_reset_chip = NULL,
2036        .dev_flush_queue = kvaser_usb_hydra_flush_queue,
2037        .dev_read_bulk_callback = kvaser_usb_hydra_read_bulk_callback,
2038        .dev_frame_to_cmd = kvaser_usb_hydra_frame_to_cmd,
2039};
2040
2041static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_kcan = {
2042        .clock = {
2043                .freq = 80 * MEGA /* Hz */,
2044        },
2045        .timestamp_freq = 80,
2046        .bittiming_const = &kvaser_usb_hydra_kcan_bittiming_c,
2047        .data_bittiming_const = &kvaser_usb_hydra_kcan_bittiming_c,
2048};
2049
2050static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_flexc = {
2051        .clock = {
2052                .freq = 24 * MEGA /* Hz */,
2053        },
2054        .timestamp_freq = 1,
2055        .bittiming_const = &kvaser_usb_flexc_bittiming_const,
2056};
2057
2058static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_rt = {
2059        .clock = {
2060                .freq = 80 * MEGA /* Hz */,
2061        },
2062        .timestamp_freq = 24,
2063        .bittiming_const = &kvaser_usb_hydra_rt_bittiming_c,
2064        .data_bittiming_const = &kvaser_usb_hydra_rtd_bittiming_c,
2065};
2066