linux/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
<<
>>
Prefs
   1/*
   2 * CAN driver for PEAK System PCAN-USB FD / PCAN-USB Pro FD adapter
   3 *
   4 * Copyright (C) 2013-2014 Stephane Grosjean <s.grosjean@peak-system.com>
   5 *
   6 * This program is free software; you can redistribute it and/or modify it
   7 * under the terms of the GNU General Public License as published
   8 * by the Free Software Foundation; version 2 of the License.
   9 *
  10 * This program is distributed in the hope that it will be useful, but
  11 * WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13 * General Public License for more details.
  14 */
  15#include <linux/netdevice.h>
  16#include <linux/usb.h>
  17#include <linux/module.h>
  18
  19#include <linux/can.h>
  20#include <linux/can/dev.h>
  21#include <linux/can/error.h>
  22#include <linux/can/dev/peak_canfd.h>
  23
  24#include "pcan_usb_core.h"
  25#include "pcan_usb_pro.h"
  26
  27MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB FD adapter");
  28MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB Pro FD adapter");
  29
  30#define PCAN_USBPROFD_CHANNEL_COUNT     2
  31#define PCAN_USBFD_CHANNEL_COUNT        1
  32
  33/* PCAN-USB Pro FD adapter internal clock (Hz) */
  34#define PCAN_UFD_CRYSTAL_HZ             80000000
  35
  36#define PCAN_UFD_CMD_BUFFER_SIZE        512
  37#define PCAN_UFD_LOSPD_PKT_SIZE         64
  38
  39/* PCAN-USB Pro FD command timeout (ms.) */
  40#define PCAN_UFD_CMD_TIMEOUT_MS         1000
  41
  42/* PCAN-USB Pro FD rx/tx buffers size */
  43#define PCAN_UFD_RX_BUFFER_SIZE         2048
  44#define PCAN_UFD_TX_BUFFER_SIZE         512
  45
  46/* read some versions info from the hw devcie */
  47struct __packed pcan_ufd_fw_info {
  48        __le16  size_of;        /* sizeof this */
  49        __le16  type;           /* type of this structure */
  50        u8      hw_type;        /* Type of hardware (HW_TYPE_xxx) */
  51        u8      bl_version[3];  /* Bootloader version */
  52        u8      hw_version;     /* Hardware version (PCB) */
  53        u8      fw_version[3];  /* Firmware version */
  54        __le32  dev_id[2];      /* "device id" per CAN */
  55        __le32  ser_no;         /* S/N */
  56        __le32  flags;          /* special functions */
  57};
  58
  59/* handle device specific info used by the netdevices */
  60struct pcan_usb_fd_if {
  61        struct peak_usb_device  *dev[PCAN_USB_MAX_CHANNEL];
  62        struct pcan_ufd_fw_info fw_info;
  63        struct peak_time_ref    time_ref;
  64        int                     cm_ignore_count;
  65        int                     dev_opened_count;
  66};
  67
  68/* device information */
  69struct pcan_usb_fd_device {
  70        struct peak_usb_device  dev;
  71        struct can_berr_counter bec;
  72        struct pcan_usb_fd_if   *usb_if;
  73        u8                      *cmd_buffer_addr;
  74};
  75
  76/* Extended USB commands (non uCAN commands) */
  77
  78/* Clock Modes command */
  79#define PCAN_UFD_CMD_CLK_SET            0x80
  80
  81#define PCAN_UFD_CLK_80MHZ              0x0
  82#define PCAN_UFD_CLK_60MHZ              0x1
  83#define PCAN_UFD_CLK_40MHZ              0x2
  84#define PCAN_UFD_CLK_30MHZ              0x3
  85#define PCAN_UFD_CLK_24MHZ              0x4
  86#define PCAN_UFD_CLK_20MHZ              0x5
  87#define PCAN_UFD_CLK_DEF                PCAN_UFD_CLK_80MHZ
  88
  89struct __packed pcan_ufd_clock {
  90        __le16  opcode_channel;
  91
  92        u8      mode;
  93        u8      unused[5];
  94};
  95
  96/* LED control command */
  97#define PCAN_UFD_CMD_LED_SET            0x86
  98
  99#define PCAN_UFD_LED_DEV                0x00
 100#define PCAN_UFD_LED_FAST               0x01
 101#define PCAN_UFD_LED_SLOW               0x02
 102#define PCAN_UFD_LED_ON                 0x03
 103#define PCAN_UFD_LED_OFF                0x04
 104#define PCAN_UFD_LED_DEF                PCAN_UFD_LED_DEV
 105
 106struct __packed pcan_ufd_led {
 107        __le16  opcode_channel;
 108
 109        u8      mode;
 110        u8      unused[5];
 111};
 112
 113/* Extended usage of uCAN commands CMD_xxx_xx_OPTION for PCAN-USB Pro FD */
 114#define PCAN_UFD_FLTEXT_CALIBRATION     0x8000
 115
 116struct __packed pcan_ufd_options {
 117        __le16  opcode_channel;
 118
 119        __le16  ucan_mask;
 120        u16     unused;
 121        __le16  usb_mask;
 122};
 123
 124/* Extended usage of uCAN messages for PCAN-USB Pro FD */
 125#define PCAN_UFD_MSG_CALIBRATION        0x100
 126
 127struct __packed pcan_ufd_ts_msg {
 128        __le16  size;
 129        __le16  type;
 130        __le32  ts_low;
 131        __le32  ts_high;
 132        __le16  usb_frame_index;
 133        u16     unused;
 134};
 135
 136#define PCAN_UFD_MSG_OVERRUN            0x101
 137
 138#define PCAN_UFD_OVMSG_CHANNEL(o)       ((o)->channel & 0xf)
 139
 140struct __packed pcan_ufd_ovr_msg {
 141        __le16  size;
 142        __le16  type;
 143        __le32  ts_low;
 144        __le32  ts_high;
 145        u8      channel;
 146        u8      unused[3];
 147};
 148
 149static inline int pufd_omsg_get_channel(struct pcan_ufd_ovr_msg *om)
 150{
 151        return om->channel & 0xf;
 152}
 153
 154/* Clock mode frequency values */
 155static const u32 pcan_usb_fd_clk_freq[6] = {
 156        [PCAN_UFD_CLK_80MHZ] = 80000000,
 157        [PCAN_UFD_CLK_60MHZ] = 60000000,
 158        [PCAN_UFD_CLK_40MHZ] = 40000000,
 159        [PCAN_UFD_CLK_30MHZ] = 30000000,
 160        [PCAN_UFD_CLK_24MHZ] = 24000000,
 161        [PCAN_UFD_CLK_20MHZ] = 20000000
 162};
 163
 164/* return a device USB interface */
 165static inline
 166struct pcan_usb_fd_if *pcan_usb_fd_dev_if(struct peak_usb_device *dev)
 167{
 168        struct pcan_usb_fd_device *pdev =
 169                        container_of(dev, struct pcan_usb_fd_device, dev);
 170        return pdev->usb_if;
 171}
 172
 173/* return a device USB commands buffer */
 174static inline void *pcan_usb_fd_cmd_buffer(struct peak_usb_device *dev)
 175{
 176        struct pcan_usb_fd_device *pdev =
 177                        container_of(dev, struct pcan_usb_fd_device, dev);
 178        return pdev->cmd_buffer_addr;
 179}
 180
 181/* send PCAN-USB Pro FD commands synchronously */
 182static int pcan_usb_fd_send_cmd(struct peak_usb_device *dev, void *cmd_tail)
 183{
 184        void *cmd_head = pcan_usb_fd_cmd_buffer(dev);
 185        int err = 0;
 186        u8 *packet_ptr;
 187        int i, n = 1, packet_len;
 188        ptrdiff_t cmd_len;
 189
 190        /* usb device unregistered? */
 191        if (!(dev->state & PCAN_USB_STATE_CONNECTED))
 192                return 0;
 193
 194        /* if a packet is not filled completely by commands, the command list
 195         * is terminated with an "end of collection" record.
 196         */
 197        cmd_len = cmd_tail - cmd_head;
 198        if (cmd_len <= (PCAN_UFD_CMD_BUFFER_SIZE - sizeof(u64))) {
 199                memset(cmd_tail, 0xff, sizeof(u64));
 200                cmd_len += sizeof(u64);
 201        }
 202
 203        packet_ptr = cmd_head;
 204
 205        /* firmware is not able to re-assemble 512 bytes buffer in full-speed */
 206        if ((dev->udev->speed != USB_SPEED_HIGH) &&
 207            (cmd_len > PCAN_UFD_LOSPD_PKT_SIZE)) {
 208                packet_len = PCAN_UFD_LOSPD_PKT_SIZE;
 209                n += cmd_len / packet_len;
 210        } else {
 211                packet_len = cmd_len;
 212        }
 213
 214        for (i = 0; i < n; i++) {
 215                err = usb_bulk_msg(dev->udev,
 216                                   usb_sndbulkpipe(dev->udev,
 217                                                   PCAN_USBPRO_EP_CMDOUT),
 218                                   packet_ptr, packet_len,
 219                                   NULL, PCAN_UFD_CMD_TIMEOUT_MS);
 220                if (err) {
 221                        netdev_err(dev->netdev,
 222                                   "sending command failure: %d\n", err);
 223                        break;
 224                }
 225
 226                packet_ptr += packet_len;
 227        }
 228
 229        return err;
 230}
 231
 232/* build the commands list in the given buffer, to enter operational mode */
 233static int pcan_usb_fd_build_restart_cmd(struct peak_usb_device *dev, u8 *buf)
 234{
 235        struct pucan_wr_err_cnt *prc;
 236        struct pucan_command *cmd;
 237        u8 *pc = buf;
 238
 239        /* 1st, reset error counters: */
 240        prc = (struct pucan_wr_err_cnt *)pc;
 241        prc->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
 242                                                       PUCAN_CMD_WR_ERR_CNT);
 243
 244        /* select both counters */
 245        prc->sel_mask = cpu_to_le16(PUCAN_WRERRCNT_TE|PUCAN_WRERRCNT_RE);
 246
 247        /* and reset their values */
 248        prc->tx_counter = 0;
 249        prc->rx_counter = 0;
 250
 251        /* moves the pointer forward */
 252        pc += sizeof(struct pucan_wr_err_cnt);
 253
 254        /* add command to switch from ISO to non-ISO mode, if fw allows it */
 255        if (dev->can.ctrlmode_supported & CAN_CTRLMODE_FD_NON_ISO) {
 256                struct pucan_options *puo = (struct pucan_options *)pc;
 257
 258                puo->opcode_channel =
 259                        (dev->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO) ?
 260                        pucan_cmd_opcode_channel(dev->ctrl_idx,
 261                                                 PUCAN_CMD_CLR_DIS_OPTION) :
 262                        pucan_cmd_opcode_channel(dev->ctrl_idx,
 263                                                 PUCAN_CMD_SET_EN_OPTION);
 264
 265                puo->options = cpu_to_le16(PUCAN_OPTION_CANDFDISO);
 266
 267                /* to be sure that no other extended bits will be taken into
 268                 * account
 269                 */
 270                puo->unused = 0;
 271
 272                /* moves the pointer forward */
 273                pc += sizeof(struct pucan_options);
 274        }
 275
 276        /* next, go back to operational mode */
 277        cmd = (struct pucan_command *)pc;
 278        cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
 279                                (dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) ?
 280                                                PUCAN_CMD_LISTEN_ONLY_MODE :
 281                                                PUCAN_CMD_NORMAL_MODE);
 282        pc += sizeof(struct pucan_command);
 283
 284        return pc - buf;
 285}
 286
 287/* set CAN bus on/off */
 288static int pcan_usb_fd_set_bus(struct peak_usb_device *dev, u8 onoff)
 289{
 290        u8 *pc = pcan_usb_fd_cmd_buffer(dev);
 291        int l;
 292
 293        if (onoff) {
 294                /* build the cmds list to enter operational mode */
 295                l = pcan_usb_fd_build_restart_cmd(dev, pc);
 296        } else {
 297                struct pucan_command *cmd = (struct pucan_command *)pc;
 298
 299                /* build cmd to go back to reset mode */
 300                cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
 301                                                        PUCAN_CMD_RESET_MODE);
 302                l = sizeof(struct pucan_command);
 303        }
 304
 305        /* send the command */
 306        return pcan_usb_fd_send_cmd(dev, pc + l);
 307}
 308
 309/* set filtering masks:
 310 *
 311 *      idx  in range [0..63] selects a row #idx, all rows otherwise
 312 *      mask in range [0..0xffffffff] defines up to 32 CANIDs in the row(s)
 313 *
 314 *      Each bit of this 64 x 32 bits array defines a CANID value:
 315 *
 316 *      bit[i,j] = 1 implies that CANID=(i x 32)+j will be received, while
 317 *      bit[i,j] = 0 implies that CANID=(i x 32)+j will be discarded.
 318 */
 319static int pcan_usb_fd_set_filter_std(struct peak_usb_device *dev, int idx,
 320                                      u32 mask)
 321{
 322        struct pucan_filter_std *cmd = pcan_usb_fd_cmd_buffer(dev);
 323        int i, n;
 324
 325        /* select all rows when idx is out of range [0..63] */
 326        if ((idx < 0) || (idx >= (1 << PUCAN_FLTSTD_ROW_IDX_BITS))) {
 327                n = 1 << PUCAN_FLTSTD_ROW_IDX_BITS;
 328                idx = 0;
 329
 330        /* select the row (and only the row) otherwise */
 331        } else {
 332                n = idx + 1;
 333        }
 334
 335        for (i = idx; i < n; i++, cmd++) {
 336                cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
 337                                                        PUCAN_CMD_FILTER_STD);
 338                cmd->idx = cpu_to_le16(i);
 339                cmd->mask = cpu_to_le32(mask);
 340        }
 341
 342        /* send the command */
 343        return pcan_usb_fd_send_cmd(dev, cmd);
 344}
 345
 346/* set/unset options
 347 *
 348 *      onoff   set(1)/unset(0) options
 349 *      mask    each bit defines a kind of options to set/unset
 350 */
 351static int pcan_usb_fd_set_options(struct peak_usb_device *dev,
 352                                   bool onoff, u16 ucan_mask, u16 usb_mask)
 353{
 354        struct pcan_ufd_options *cmd = pcan_usb_fd_cmd_buffer(dev);
 355
 356        cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
 357                                        (onoff) ? PUCAN_CMD_SET_EN_OPTION :
 358                                                  PUCAN_CMD_CLR_DIS_OPTION);
 359
 360        cmd->ucan_mask = cpu_to_le16(ucan_mask);
 361        cmd->usb_mask = cpu_to_le16(usb_mask);
 362
 363        /* send the command */
 364        return pcan_usb_fd_send_cmd(dev, ++cmd);
 365}
 366
 367/* setup LED control */
 368static int pcan_usb_fd_set_can_led(struct peak_usb_device *dev, u8 led_mode)
 369{
 370        struct pcan_ufd_led *cmd = pcan_usb_fd_cmd_buffer(dev);
 371
 372        cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
 373                                                       PCAN_UFD_CMD_LED_SET);
 374        cmd->mode = led_mode;
 375
 376        /* send the command */
 377        return pcan_usb_fd_send_cmd(dev, ++cmd);
 378}
 379
 380/* set CAN clock domain */
 381static int pcan_usb_fd_set_clock_domain(struct peak_usb_device *dev,
 382                                        u8 clk_mode)
 383{
 384        struct pcan_ufd_clock *cmd = pcan_usb_fd_cmd_buffer(dev);
 385
 386        cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
 387                                                       PCAN_UFD_CMD_CLK_SET);
 388        cmd->mode = clk_mode;
 389
 390        /* send the command */
 391        return pcan_usb_fd_send_cmd(dev, ++cmd);
 392}
 393
 394/* set bittiming for CAN and CAN-FD header */
 395static int pcan_usb_fd_set_bittiming_slow(struct peak_usb_device *dev,
 396                                          struct can_bittiming *bt)
 397{
 398        struct pucan_timing_slow *cmd = pcan_usb_fd_cmd_buffer(dev);
 399
 400        cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
 401                                                       PUCAN_CMD_TIMING_SLOW);
 402        cmd->sjw_t = PUCAN_TSLOW_SJW_T(bt->sjw - 1,
 403                                dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES);
 404
 405        cmd->tseg2 = PUCAN_TSLOW_TSEG2(bt->phase_seg2 - 1);
 406        cmd->tseg1 = PUCAN_TSLOW_TSEG1(bt->prop_seg + bt->phase_seg1 - 1);
 407        cmd->brp = cpu_to_le16(PUCAN_TSLOW_BRP(bt->brp - 1));
 408
 409        cmd->ewl = 96;  /* default */
 410
 411        /* send the command */
 412        return pcan_usb_fd_send_cmd(dev, ++cmd);
 413}
 414
 415/* set CAN-FD bittiming for data */
 416static int pcan_usb_fd_set_bittiming_fast(struct peak_usb_device *dev,
 417                                          struct can_bittiming *bt)
 418{
 419        struct pucan_timing_fast *cmd = pcan_usb_fd_cmd_buffer(dev);
 420
 421        cmd->opcode_channel = pucan_cmd_opcode_channel(dev->ctrl_idx,
 422                                                       PUCAN_CMD_TIMING_FAST);
 423        cmd->sjw = PUCAN_TFAST_SJW(bt->sjw - 1);
 424        cmd->tseg2 = PUCAN_TFAST_TSEG2(bt->phase_seg2 - 1);
 425        cmd->tseg1 = PUCAN_TFAST_TSEG1(bt->prop_seg + bt->phase_seg1 - 1);
 426        cmd->brp = cpu_to_le16(PUCAN_TFAST_BRP(bt->brp - 1));
 427
 428        /* send the command */
 429        return pcan_usb_fd_send_cmd(dev, ++cmd);
 430}
 431
 432/* handle restart but in asynchronously way
 433 * (uses PCAN-USB Pro code to complete asynchronous request)
 434 */
 435static int pcan_usb_fd_restart_async(struct peak_usb_device *dev,
 436                                     struct urb *urb, u8 *buf)
 437{
 438        u8 *pc = buf;
 439
 440        /* build the entire cmds list in the provided buffer, to go back into
 441         * operational mode.
 442         */
 443        pc += pcan_usb_fd_build_restart_cmd(dev, pc);
 444
 445        /* add EOC */
 446        memset(pc, 0xff, sizeof(struct pucan_command));
 447        pc += sizeof(struct pucan_command);
 448
 449        /* complete the URB */
 450        usb_fill_bulk_urb(urb, dev->udev,
 451                          usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
 452                          buf, pc - buf,
 453                          pcan_usb_pro_restart_complete, dev);
 454
 455        /* and submit it. */
 456        return usb_submit_urb(urb, GFP_ATOMIC);
 457}
 458
 459static int pcan_usb_fd_drv_loaded(struct peak_usb_device *dev, bool loaded)
 460{
 461        struct pcan_usb_fd_device *pdev =
 462                        container_of(dev, struct pcan_usb_fd_device, dev);
 463
 464        pdev->cmd_buffer_addr[0] = 0;
 465        pdev->cmd_buffer_addr[1] = !!loaded;
 466
 467        return pcan_usb_pro_send_req(dev,
 468                                PCAN_USBPRO_REQ_FCT,
 469                                PCAN_USBPRO_FCT_DRVLD,
 470                                pdev->cmd_buffer_addr,
 471                                PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
 472}
 473
 474static int pcan_usb_fd_decode_canmsg(struct pcan_usb_fd_if *usb_if,
 475                                     struct pucan_msg *rx_msg)
 476{
 477        struct pucan_rx_msg *rm = (struct pucan_rx_msg *)rx_msg;
 478        struct peak_usb_device *dev = usb_if->dev[pucan_msg_get_channel(rm)];
 479        struct net_device *netdev = dev->netdev;
 480        struct canfd_frame *cfd;
 481        struct sk_buff *skb;
 482        const u16 rx_msg_flags = le16_to_cpu(rm->flags);
 483
 484        if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) {
 485                /* CANFD frame case */
 486                skb = alloc_canfd_skb(netdev, &cfd);
 487                if (!skb)
 488                        return -ENOMEM;
 489
 490                if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH)
 491                        cfd->flags |= CANFD_BRS;
 492
 493                if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND)
 494                        cfd->flags |= CANFD_ESI;
 495
 496                cfd->len = can_dlc2len(get_canfd_dlc(pucan_msg_get_dlc(rm)));
 497        } else {
 498                /* CAN 2.0 frame case */
 499                skb = alloc_can_skb(netdev, (struct can_frame **)&cfd);
 500                if (!skb)
 501                        return -ENOMEM;
 502
 503                cfd->len = get_can_dlc(pucan_msg_get_dlc(rm));
 504        }
 505
 506        cfd->can_id = le32_to_cpu(rm->can_id);
 507
 508        if (rx_msg_flags & PUCAN_MSG_EXT_ID)
 509                cfd->can_id |= CAN_EFF_FLAG;
 510
 511        if (rx_msg_flags & PUCAN_MSG_RTR)
 512                cfd->can_id |= CAN_RTR_FLAG;
 513        else
 514                memcpy(cfd->data, rm->d, cfd->len);
 515
 516        peak_usb_netif_rx(skb, &usb_if->time_ref,
 517                          le32_to_cpu(rm->ts_low), le32_to_cpu(rm->ts_high));
 518
 519        netdev->stats.rx_packets++;
 520        netdev->stats.rx_bytes += cfd->len;
 521
 522        return 0;
 523}
 524
 525/* handle uCAN status message */
 526static int pcan_usb_fd_decode_status(struct pcan_usb_fd_if *usb_if,
 527                                     struct pucan_msg *rx_msg)
 528{
 529        struct pucan_status_msg *sm = (struct pucan_status_msg *)rx_msg;
 530        struct peak_usb_device *dev = usb_if->dev[pucan_stmsg_get_channel(sm)];
 531        struct pcan_usb_fd_device *pdev =
 532                        container_of(dev, struct pcan_usb_fd_device, dev);
 533        enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
 534        enum can_state rx_state, tx_state;
 535        struct net_device *netdev = dev->netdev;
 536        struct can_frame *cf;
 537        struct sk_buff *skb;
 538
 539        /* nothing should be sent while in BUS_OFF state */
 540        if (dev->can.state == CAN_STATE_BUS_OFF)
 541                return 0;
 542
 543        if (sm->channel_p_w_b & PUCAN_BUS_BUSOFF) {
 544                new_state = CAN_STATE_BUS_OFF;
 545        } else if (sm->channel_p_w_b & PUCAN_BUS_PASSIVE) {
 546                new_state = CAN_STATE_ERROR_PASSIVE;
 547        } else if (sm->channel_p_w_b & PUCAN_BUS_WARNING) {
 548                new_state = CAN_STATE_ERROR_WARNING;
 549        } else {
 550                /* no error bit (so, no error skb, back to active state) */
 551                dev->can.state = CAN_STATE_ERROR_ACTIVE;
 552                pdev->bec.txerr = 0;
 553                pdev->bec.rxerr = 0;
 554                return 0;
 555        }
 556
 557        /* state hasn't changed */
 558        if (new_state == dev->can.state)
 559                return 0;
 560
 561        /* handle bus state change */
 562        tx_state = (pdev->bec.txerr >= pdev->bec.rxerr) ? new_state : 0;
 563        rx_state = (pdev->bec.txerr <= pdev->bec.rxerr) ? new_state : 0;
 564
 565        /* allocate an skb to store the error frame */
 566        skb = alloc_can_err_skb(netdev, &cf);
 567        if (skb)
 568                can_change_state(netdev, cf, tx_state, rx_state);
 569
 570        /* things must be done even in case of OOM */
 571        if (new_state == CAN_STATE_BUS_OFF)
 572                can_bus_off(netdev);
 573
 574        if (!skb)
 575                return -ENOMEM;
 576
 577        peak_usb_netif_rx(skb, &usb_if->time_ref,
 578                          le32_to_cpu(sm->ts_low), le32_to_cpu(sm->ts_high));
 579
 580        netdev->stats.rx_packets++;
 581        netdev->stats.rx_bytes += cf->can_dlc;
 582
 583        return 0;
 584}
 585
 586/* handle uCAN error message */
 587static int pcan_usb_fd_decode_error(struct pcan_usb_fd_if *usb_if,
 588                                    struct pucan_msg *rx_msg)
 589{
 590        struct pucan_error_msg *er = (struct pucan_error_msg *)rx_msg;
 591        struct peak_usb_device *dev = usb_if->dev[pucan_ermsg_get_channel(er)];
 592        struct pcan_usb_fd_device *pdev =
 593                        container_of(dev, struct pcan_usb_fd_device, dev);
 594
 595        /* keep a trace of tx and rx error counters for later use */
 596        pdev->bec.txerr = er->tx_err_cnt;
 597        pdev->bec.rxerr = er->rx_err_cnt;
 598
 599        return 0;
 600}
 601
 602/* handle uCAN overrun message */
 603static int pcan_usb_fd_decode_overrun(struct pcan_usb_fd_if *usb_if,
 604                                      struct pucan_msg *rx_msg)
 605{
 606        struct pcan_ufd_ovr_msg *ov = (struct pcan_ufd_ovr_msg *)rx_msg;
 607        struct peak_usb_device *dev = usb_if->dev[pufd_omsg_get_channel(ov)];
 608        struct net_device *netdev = dev->netdev;
 609        struct can_frame *cf;
 610        struct sk_buff *skb;
 611
 612        /* allocate an skb to store the error frame */
 613        skb = alloc_can_err_skb(netdev, &cf);
 614        if (!skb)
 615                return -ENOMEM;
 616
 617        cf->can_id |= CAN_ERR_CRTL;
 618        cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
 619
 620        peak_usb_netif_rx(skb, &usb_if->time_ref,
 621                          le32_to_cpu(ov->ts_low), le32_to_cpu(ov->ts_high));
 622
 623        netdev->stats.rx_over_errors++;
 624        netdev->stats.rx_errors++;
 625
 626        return 0;
 627}
 628
 629/* handle USB calibration message */
 630static void pcan_usb_fd_decode_ts(struct pcan_usb_fd_if *usb_if,
 631                                  struct pucan_msg *rx_msg)
 632{
 633        struct pcan_ufd_ts_msg *ts = (struct pcan_ufd_ts_msg *)rx_msg;
 634
 635        /* should wait until clock is stabilized */
 636        if (usb_if->cm_ignore_count > 0)
 637                usb_if->cm_ignore_count--;
 638        else
 639                peak_usb_set_ts_now(&usb_if->time_ref, le32_to_cpu(ts->ts_low));
 640}
 641
 642/* callback for bulk IN urb */
 643static int pcan_usb_fd_decode_buf(struct peak_usb_device *dev, struct urb *urb)
 644{
 645        struct pcan_usb_fd_if *usb_if = pcan_usb_fd_dev_if(dev);
 646        struct net_device *netdev = dev->netdev;
 647        struct pucan_msg *rx_msg;
 648        u8 *msg_ptr, *msg_end;
 649        int err = 0;
 650
 651        /* loop reading all the records from the incoming message */
 652        msg_ptr = urb->transfer_buffer;
 653        msg_end = urb->transfer_buffer + urb->actual_length;
 654        for (; msg_ptr < msg_end;) {
 655                u16 rx_msg_type, rx_msg_size;
 656
 657                rx_msg = (struct pucan_msg *)msg_ptr;
 658                if (!rx_msg->size) {
 659                        /* null packet found: end of list */
 660                        break;
 661                }
 662
 663                rx_msg_size = le16_to_cpu(rx_msg->size);
 664                rx_msg_type = le16_to_cpu(rx_msg->type);
 665
 666                /* check if the record goes out of current packet */
 667                if (msg_ptr + rx_msg_size > msg_end) {
 668                        netdev_err(netdev,
 669                                   "got frag rec: should inc usb rx buf sze\n");
 670                        err = -EBADMSG;
 671                        break;
 672                }
 673
 674                switch (rx_msg_type) {
 675                case PUCAN_MSG_CAN_RX:
 676                        err = pcan_usb_fd_decode_canmsg(usb_if, rx_msg);
 677                        if (err < 0)
 678                                goto fail;
 679                        break;
 680
 681                case PCAN_UFD_MSG_CALIBRATION:
 682                        pcan_usb_fd_decode_ts(usb_if, rx_msg);
 683                        break;
 684
 685                case PUCAN_MSG_ERROR:
 686                        err = pcan_usb_fd_decode_error(usb_if, rx_msg);
 687                        if (err < 0)
 688                                goto fail;
 689                        break;
 690
 691                case PUCAN_MSG_STATUS:
 692                        err = pcan_usb_fd_decode_status(usb_if, rx_msg);
 693                        if (err < 0)
 694                                goto fail;
 695                        break;
 696
 697                case PCAN_UFD_MSG_OVERRUN:
 698                        err = pcan_usb_fd_decode_overrun(usb_if, rx_msg);
 699                        if (err < 0)
 700                                goto fail;
 701                        break;
 702
 703                default:
 704                        netdev_err(netdev,
 705                                   "unhandled msg type 0x%02x (%d): ignored\n",
 706                                   rx_msg_type, rx_msg_type);
 707                        break;
 708                }
 709
 710                msg_ptr += rx_msg_size;
 711        }
 712
 713fail:
 714        if (err)
 715                pcan_dump_mem("received msg",
 716                              urb->transfer_buffer, urb->actual_length);
 717        return err;
 718}
 719
 720/* CAN/CANFD frames encoding callback */
 721static int pcan_usb_fd_encode_msg(struct peak_usb_device *dev,
 722                                  struct sk_buff *skb, u8 *obuf, size_t *size)
 723{
 724        struct pucan_tx_msg *tx_msg = (struct pucan_tx_msg *)obuf;
 725        struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
 726        u16 tx_msg_size, tx_msg_flags;
 727        u8 can_dlc;
 728
 729        tx_msg_size = ALIGN(sizeof(struct pucan_tx_msg) + cfd->len, 4);
 730        tx_msg->size = cpu_to_le16(tx_msg_size);
 731        tx_msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX);
 732
 733        tx_msg_flags = 0;
 734        if (cfd->can_id & CAN_EFF_FLAG) {
 735                tx_msg_flags |= PUCAN_MSG_EXT_ID;
 736                tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_EFF_MASK);
 737        } else {
 738                tx_msg->can_id = cpu_to_le32(cfd->can_id & CAN_SFF_MASK);
 739        }
 740
 741        if (can_is_canfd_skb(skb)) {
 742                /* considering a CANFD frame */
 743                can_dlc = can_len2dlc(cfd->len);
 744
 745                tx_msg_flags |= PUCAN_MSG_EXT_DATA_LEN;
 746
 747                if (cfd->flags & CANFD_BRS)
 748                        tx_msg_flags |= PUCAN_MSG_BITRATE_SWITCH;
 749
 750                if (cfd->flags & CANFD_ESI)
 751                        tx_msg_flags |= PUCAN_MSG_ERROR_STATE_IND;
 752        } else {
 753                /* CAND 2.0 frames */
 754                can_dlc = cfd->len;
 755
 756                if (cfd->can_id & CAN_RTR_FLAG)
 757                        tx_msg_flags |= PUCAN_MSG_RTR;
 758        }
 759
 760        tx_msg->flags = cpu_to_le16(tx_msg_flags);
 761        tx_msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(dev->ctrl_idx, can_dlc);
 762        memcpy(tx_msg->d, cfd->data, cfd->len);
 763
 764        /* add null size message to tag the end (messages are 32-bits aligned)
 765         */
 766        tx_msg = (struct pucan_tx_msg *)(obuf + tx_msg_size);
 767
 768        tx_msg->size = 0;
 769
 770        /* set the whole size of the USB packet to send */
 771        *size = tx_msg_size + sizeof(u32);
 772
 773        return 0;
 774}
 775
 776/* start the interface (last chance before set bus on) */
 777static int pcan_usb_fd_start(struct peak_usb_device *dev)
 778{
 779        struct pcan_usb_fd_device *pdev =
 780                        container_of(dev, struct pcan_usb_fd_device, dev);
 781        int err;
 782
 783        /* set filter mode: all acceptance */
 784        err = pcan_usb_fd_set_filter_std(dev, -1, 0xffffffff);
 785        if (err)
 786                return err;
 787
 788        /* opening first device: */
 789        if (pdev->usb_if->dev_opened_count == 0) {
 790                /* reset time_ref */
 791                peak_usb_init_time_ref(&pdev->usb_if->time_ref,
 792                                       &pcan_usb_pro_fd);
 793
 794                /* enable USB calibration messages */
 795                err = pcan_usb_fd_set_options(dev, 1,
 796                                              PUCAN_OPTION_ERROR,
 797                                              PCAN_UFD_FLTEXT_CALIBRATION);
 798        }
 799
 800        pdev->usb_if->dev_opened_count++;
 801
 802        /* reset cached error counters */
 803        pdev->bec.txerr = 0;
 804        pdev->bec.rxerr = 0;
 805
 806        return err;
 807}
 808
 809/* socket callback used to copy berr counters values receieved through USB */
 810static int pcan_usb_fd_get_berr_counter(const struct net_device *netdev,
 811                                        struct can_berr_counter *bec)
 812{
 813        struct peak_usb_device *dev = netdev_priv(netdev);
 814        struct pcan_usb_fd_device *pdev =
 815                        container_of(dev, struct pcan_usb_fd_device, dev);
 816
 817        *bec = pdev->bec;
 818
 819        /* must return 0 */
 820        return 0;
 821}
 822
 823/* stop interface (last chance before set bus off) */
 824static int pcan_usb_fd_stop(struct peak_usb_device *dev)
 825{
 826        struct pcan_usb_fd_device *pdev =
 827                        container_of(dev, struct pcan_usb_fd_device, dev);
 828
 829        /* turn off special msgs for that interface if no other dev opened */
 830        if (pdev->usb_if->dev_opened_count == 1)
 831                pcan_usb_fd_set_options(dev, 0,
 832                                        PUCAN_OPTION_ERROR,
 833                                        PCAN_UFD_FLTEXT_CALIBRATION);
 834        pdev->usb_if->dev_opened_count--;
 835
 836        return 0;
 837}
 838
 839/* called when probing, to initialize a device object */
 840static int pcan_usb_fd_init(struct peak_usb_device *dev)
 841{
 842        struct pcan_usb_fd_device *pdev =
 843                        container_of(dev, struct pcan_usb_fd_device, dev);
 844        int i, err = -ENOMEM;
 845
 846        /* do this for 1st channel only */
 847        if (!dev->prev_siblings) {
 848                /* allocate netdevices common structure attached to first one */
 849                pdev->usb_if = kzalloc(sizeof(*pdev->usb_if), GFP_KERNEL);
 850                if (!pdev->usb_if)
 851                        goto err_out;
 852
 853                /* allocate command buffer once for all for the interface */
 854                pdev->cmd_buffer_addr = kmalloc(PCAN_UFD_CMD_BUFFER_SIZE,
 855                                                GFP_KERNEL);
 856                if (!pdev->cmd_buffer_addr)
 857                        goto err_out_1;
 858
 859                /* number of ts msgs to ignore before taking one into account */
 860                pdev->usb_if->cm_ignore_count = 5;
 861
 862                err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
 863                                            PCAN_USBPRO_INFO_FW,
 864                                            &pdev->usb_if->fw_info,
 865                                            sizeof(pdev->usb_if->fw_info));
 866                if (err) {
 867                        dev_err(dev->netdev->dev.parent,
 868                                "unable to read %s firmware info (err %d)\n",
 869                                dev->adapter->name, err);
 870                        goto err_out_2;
 871                }
 872
 873                /* explicit use of dev_xxx() instead of netdev_xxx() here:
 874                 * information displayed are related to the device itself, not
 875                 * to the canx (channel) device.
 876                 */
 877                dev_info(dev->netdev->dev.parent,
 878                         "PEAK-System %s v%u fw v%u.%u.%u (%u channels)\n",
 879                         dev->adapter->name, pdev->usb_if->fw_info.hw_version,
 880                         pdev->usb_if->fw_info.fw_version[0],
 881                         pdev->usb_if->fw_info.fw_version[1],
 882                         pdev->usb_if->fw_info.fw_version[2],
 883                         dev->adapter->ctrl_count);
 884
 885                /* check for ability to switch between ISO/non-ISO modes */
 886                if (pdev->usb_if->fw_info.fw_version[0] >= 2) {
 887                        /* firmware >= 2.x supports ISO/non-ISO switching */
 888                        dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD_NON_ISO;
 889                } else {
 890                        /* firmware < 2.x only supports fixed(!) non-ISO */
 891                        dev->can.ctrlmode |= CAN_CTRLMODE_FD_NON_ISO;
 892                }
 893
 894                /* tell the hardware the can driver is running */
 895                err = pcan_usb_fd_drv_loaded(dev, 1);
 896                if (err) {
 897                        dev_err(dev->netdev->dev.parent,
 898                                "unable to tell %s driver is loaded (err %d)\n",
 899                                dev->adapter->name, err);
 900                        goto err_out_2;
 901                }
 902        } else {
 903                /* otherwise, simply copy previous sibling's values */
 904                struct pcan_usb_fd_device *ppdev =
 905                        container_of(dev->prev_siblings,
 906                                     struct pcan_usb_fd_device, dev);
 907
 908                pdev->usb_if = ppdev->usb_if;
 909                pdev->cmd_buffer_addr = ppdev->cmd_buffer_addr;
 910
 911                /* do a copy of the ctrlmode[_supported] too */
 912                dev->can.ctrlmode = ppdev->dev.can.ctrlmode;
 913                dev->can.ctrlmode_supported = ppdev->dev.can.ctrlmode_supported;
 914        }
 915
 916        pdev->usb_if->dev[dev->ctrl_idx] = dev;
 917        dev->device_number =
 918                le32_to_cpu(pdev->usb_if->fw_info.dev_id[dev->ctrl_idx]);
 919
 920        /* set clock domain */
 921        for (i = 0; i < ARRAY_SIZE(pcan_usb_fd_clk_freq); i++)
 922                if (dev->adapter->clock.freq == pcan_usb_fd_clk_freq[i])
 923                        break;
 924
 925        if (i >= ARRAY_SIZE(pcan_usb_fd_clk_freq)) {
 926                dev_warn(dev->netdev->dev.parent,
 927                         "incompatible clock frequencies\n");
 928                err = -EINVAL;
 929                goto err_out_2;
 930        }
 931
 932        pcan_usb_fd_set_clock_domain(dev, i);
 933
 934        /* set LED in default state (end of init phase) */
 935        pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_DEF);
 936
 937        return 0;
 938
 939err_out_2:
 940        kfree(pdev->cmd_buffer_addr);
 941err_out_1:
 942        kfree(pdev->usb_if);
 943err_out:
 944        return err;
 945}
 946
 947/* called when driver module is being unloaded */
 948static void pcan_usb_fd_exit(struct peak_usb_device *dev)
 949{
 950        struct pcan_usb_fd_device *pdev =
 951                        container_of(dev, struct pcan_usb_fd_device, dev);
 952
 953        /* when rmmod called before unplug and if down, should reset things
 954         * before leaving
 955         */
 956        if (dev->can.state != CAN_STATE_STOPPED) {
 957                /* set bus off on the corresponding channel */
 958                pcan_usb_fd_set_bus(dev, 0);
 959        }
 960
 961        /* switch off corresponding CAN LEDs */
 962        pcan_usb_fd_set_can_led(dev, PCAN_UFD_LED_OFF);
 963
 964        /* if channel #0 (only) */
 965        if (dev->ctrl_idx == 0) {
 966                /* turn off calibration message if any device were opened */
 967                if (pdev->usb_if->dev_opened_count > 0)
 968                        pcan_usb_fd_set_options(dev, 0,
 969                                                PUCAN_OPTION_ERROR,
 970                                                PCAN_UFD_FLTEXT_CALIBRATION);
 971
 972                /* tell USB adapter that the driver is being unloaded */
 973                pcan_usb_fd_drv_loaded(dev, 0);
 974        }
 975}
 976
 977/* called when the USB adapter is unplugged */
 978static void pcan_usb_fd_free(struct peak_usb_device *dev)
 979{
 980        /* last device: can free shared objects now */
 981        if (!dev->prev_siblings && !dev->next_siblings) {
 982                struct pcan_usb_fd_device *pdev =
 983                        container_of(dev, struct pcan_usb_fd_device, dev);
 984
 985                /* free commands buffer */
 986                kfree(pdev->cmd_buffer_addr);
 987
 988                /* free usb interface object */
 989                kfree(pdev->usb_if);
 990        }
 991}
 992
 993/* describes the PCAN-USB FD adapter */
 994static const struct can_bittiming_const pcan_usb_fd_const = {
 995        .name = "pcan_usb_fd",
 996        .tseg1_min = 1,
 997        .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
 998        .tseg2_min = 1,
 999        .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1000        .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1001        .brp_min = 1,
1002        .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1003        .brp_inc = 1,
1004};
1005
1006static const struct can_bittiming_const pcan_usb_fd_data_const = {
1007        .name = "pcan_usb_fd",
1008        .tseg1_min = 1,
1009        .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1010        .tseg2_min = 1,
1011        .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1012        .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1013        .brp_min = 1,
1014        .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1015        .brp_inc = 1,
1016};
1017
1018const struct peak_usb_adapter pcan_usb_fd = {
1019        .name = "PCAN-USB FD",
1020        .device_id = PCAN_USBFD_PRODUCT_ID,
1021        .ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
1022        .ctrlmode_supported = CAN_CTRLMODE_FD |
1023                        CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1024        .clock = {
1025                .freq = PCAN_UFD_CRYSTAL_HZ,
1026        },
1027        .bittiming_const = &pcan_usb_fd_const,
1028        .data_bittiming_const = &pcan_usb_fd_data_const,
1029
1030        /* size of device private data */
1031        .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1032
1033        /* timestamps usage */
1034        .ts_used_bits = 32,
1035        .ts_period = 1000000, /* calibration period in ts. */
1036        .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1037        .us_per_ts_shift = 0,
1038
1039        /* give here messages in/out endpoints */
1040        .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1041        .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1042
1043        /* size of rx/tx usb buffers */
1044        .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1045        .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1046
1047        /* device callbacks */
1048        .intf_probe = pcan_usb_pro_probe,       /* same as PCAN-USB Pro */
1049        .dev_init = pcan_usb_fd_init,
1050
1051        .dev_exit = pcan_usb_fd_exit,
1052        .dev_free = pcan_usb_fd_free,
1053        .dev_set_bus = pcan_usb_fd_set_bus,
1054        .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1055        .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1056        .dev_decode_buf = pcan_usb_fd_decode_buf,
1057        .dev_start = pcan_usb_fd_start,
1058        .dev_stop = pcan_usb_fd_stop,
1059        .dev_restart_async = pcan_usb_fd_restart_async,
1060        .dev_encode_msg = pcan_usb_fd_encode_msg,
1061
1062        .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1063};
1064
1065/* describes the PCAN-CHIP USB */
1066static const struct can_bittiming_const pcan_usb_chip_const = {
1067        .name = "pcan_chip_usb",
1068        .tseg1_min = 1,
1069        .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1070        .tseg2_min = 1,
1071        .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1072        .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1073        .brp_min = 1,
1074        .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1075        .brp_inc = 1,
1076};
1077
1078static const struct can_bittiming_const pcan_usb_chip_data_const = {
1079        .name = "pcan_chip_usb",
1080        .tseg1_min = 1,
1081        .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1082        .tseg2_min = 1,
1083        .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1084        .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1085        .brp_min = 1,
1086        .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1087        .brp_inc = 1,
1088};
1089
1090const struct peak_usb_adapter pcan_usb_chip = {
1091        .name = "PCAN-Chip USB",
1092        .device_id = PCAN_USBCHIP_PRODUCT_ID,
1093        .ctrl_count = PCAN_USBFD_CHANNEL_COUNT,
1094        .ctrlmode_supported = CAN_CTRLMODE_FD |
1095                CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1096        .clock = {
1097                .freq = PCAN_UFD_CRYSTAL_HZ,
1098        },
1099        .bittiming_const = &pcan_usb_chip_const,
1100        .data_bittiming_const = &pcan_usb_chip_data_const,
1101
1102        /* size of device private data */
1103        .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1104
1105        /* timestamps usage */
1106        .ts_used_bits = 32,
1107        .ts_period = 1000000, /* calibration period in ts. */
1108        .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1109        .us_per_ts_shift = 0,
1110
1111        /* give here messages in/out endpoints */
1112        .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1113        .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0},
1114
1115        /* size of rx/tx usb buffers */
1116        .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1117        .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1118
1119        /* device callbacks */
1120        .intf_probe = pcan_usb_pro_probe,       /* same as PCAN-USB Pro */
1121        .dev_init = pcan_usb_fd_init,
1122
1123        .dev_exit = pcan_usb_fd_exit,
1124        .dev_free = pcan_usb_fd_free,
1125        .dev_set_bus = pcan_usb_fd_set_bus,
1126        .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1127        .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1128        .dev_decode_buf = pcan_usb_fd_decode_buf,
1129        .dev_start = pcan_usb_fd_start,
1130        .dev_stop = pcan_usb_fd_stop,
1131        .dev_restart_async = pcan_usb_fd_restart_async,
1132        .dev_encode_msg = pcan_usb_fd_encode_msg,
1133
1134        .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1135};
1136
1137/* describes the PCAN-USB Pro FD adapter */
1138static const struct can_bittiming_const pcan_usb_pro_fd_const = {
1139        .name = "pcan_usb_pro_fd",
1140        .tseg1_min = 1,
1141        .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1142        .tseg2_min = 1,
1143        .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1144        .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1145        .brp_min = 1,
1146        .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1147        .brp_inc = 1,
1148};
1149
1150static const struct can_bittiming_const pcan_usb_pro_fd_data_const = {
1151        .name = "pcan_usb_pro_fd",
1152        .tseg1_min = 1,
1153        .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1154        .tseg2_min = 1,
1155        .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1156        .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1157        .brp_min = 1,
1158        .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1159        .brp_inc = 1,
1160};
1161
1162const struct peak_usb_adapter pcan_usb_pro_fd = {
1163        .name = "PCAN-USB Pro FD",
1164        .device_id = PCAN_USBPROFD_PRODUCT_ID,
1165        .ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1166        .ctrlmode_supported = CAN_CTRLMODE_FD |
1167                        CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1168        .clock = {
1169                .freq = PCAN_UFD_CRYSTAL_HZ,
1170        },
1171        .bittiming_const = &pcan_usb_pro_fd_const,
1172        .data_bittiming_const = &pcan_usb_pro_fd_data_const,
1173
1174        /* size of device private data */
1175        .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1176
1177        /* timestamps usage */
1178        .ts_used_bits = 32,
1179        .ts_period = 1000000, /* calibration period in ts. */
1180        .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1181        .us_per_ts_shift = 0,
1182
1183        /* give here messages in/out endpoints */
1184        .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1185        .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1186
1187        /* size of rx/tx usb buffers */
1188        .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1189        .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1190
1191        /* device callbacks */
1192        .intf_probe = pcan_usb_pro_probe,       /* same as PCAN-USB Pro */
1193        .dev_init = pcan_usb_fd_init,
1194
1195        .dev_exit = pcan_usb_fd_exit,
1196        .dev_free = pcan_usb_fd_free,
1197        .dev_set_bus = pcan_usb_fd_set_bus,
1198        .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1199        .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1200        .dev_decode_buf = pcan_usb_fd_decode_buf,
1201        .dev_start = pcan_usb_fd_start,
1202        .dev_stop = pcan_usb_fd_stop,
1203        .dev_restart_async = pcan_usb_fd_restart_async,
1204        .dev_encode_msg = pcan_usb_fd_encode_msg,
1205
1206        .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1207};
1208
1209/* describes the PCAN-USB X6 adapter */
1210static const struct can_bittiming_const pcan_usb_x6_const = {
1211        .name = "pcan_usb_x6",
1212        .tseg1_min = 1,
1213        .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
1214        .tseg2_min = 1,
1215        .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
1216        .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
1217        .brp_min = 1,
1218        .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
1219        .brp_inc = 1,
1220};
1221
1222static const struct can_bittiming_const pcan_usb_x6_data_const = {
1223        .name = "pcan_usb_x6",
1224        .tseg1_min = 1,
1225        .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
1226        .tseg2_min = 1,
1227        .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
1228        .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
1229        .brp_min = 1,
1230        .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
1231        .brp_inc = 1,
1232};
1233
1234const struct peak_usb_adapter pcan_usb_x6 = {
1235        .name = "PCAN-USB X6",
1236        .device_id = PCAN_USBX6_PRODUCT_ID,
1237        .ctrl_count = PCAN_USBPROFD_CHANNEL_COUNT,
1238        .ctrlmode_supported = CAN_CTRLMODE_FD |
1239                        CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1240        .clock = {
1241                .freq = PCAN_UFD_CRYSTAL_HZ,
1242        },
1243        .bittiming_const = &pcan_usb_x6_const,
1244        .data_bittiming_const = &pcan_usb_x6_data_const,
1245
1246        /* size of device private data */
1247        .sizeof_dev_private = sizeof(struct pcan_usb_fd_device),
1248
1249        /* timestamps usage */
1250        .ts_used_bits = 32,
1251        .ts_period = 1000000, /* calibration period in ts. */
1252        .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1253        .us_per_ts_shift = 0,
1254
1255        /* give here messages in/out endpoints */
1256        .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1257        .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1258
1259        /* size of rx/tx usb buffers */
1260        .rx_buffer_size = PCAN_UFD_RX_BUFFER_SIZE,
1261        .tx_buffer_size = PCAN_UFD_TX_BUFFER_SIZE,
1262
1263        /* device callbacks */
1264        .intf_probe = pcan_usb_pro_probe,       /* same as PCAN-USB Pro */
1265        .dev_init = pcan_usb_fd_init,
1266
1267        .dev_exit = pcan_usb_fd_exit,
1268        .dev_free = pcan_usb_fd_free,
1269        .dev_set_bus = pcan_usb_fd_set_bus,
1270        .dev_set_bittiming = pcan_usb_fd_set_bittiming_slow,
1271        .dev_set_data_bittiming = pcan_usb_fd_set_bittiming_fast,
1272        .dev_decode_buf = pcan_usb_fd_decode_buf,
1273        .dev_start = pcan_usb_fd_start,
1274        .dev_stop = pcan_usb_fd_stop,
1275        .dev_restart_async = pcan_usb_fd_restart_async,
1276        .dev_encode_msg = pcan_usb_fd_encode_msg,
1277
1278        .do_get_berr_counter = pcan_usb_fd_get_berr_counter,
1279};
1280