linux/drivers/net/can/usb/kvaser_usb.c
<<
>>
Prefs
   1/*
   2 * This program is free software; you can redistribute it and/or
   3 * modify it under the terms of the GNU General Public License as
   4 * published by the Free Software Foundation version 2.
   5 *
   6 * Parts of this driver are based on the following:
   7 *  - Kvaser linux leaf driver (version 4.78)
   8 *  - CAN driver for esd CAN-USB/2
   9 *  - Kvaser linux usbcanII driver (version 5.3)
  10 *
  11 * Copyright (C) 2002-2006 KVASER AB, Sweden. All rights reserved.
  12 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
  13 * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
  14 * Copyright (C) 2015 Valeo S.A.
  15 */
  16
  17#include <linux/spinlock.h>
  18#include <linux/kernel.h>
  19#include <linux/completion.h>
  20#include <linux/module.h>
  21#include <linux/netdevice.h>
  22#include <linux/usb.h>
  23
  24#include <linux/can.h>
  25#include <linux/can/dev.h>
  26#include <linux/can/error.h>
  27
  28#define MAX_RX_URBS                     4
  29#define START_TIMEOUT                   1000 /* msecs */
  30#define STOP_TIMEOUT                    1000 /* msecs */
  31#define USB_SEND_TIMEOUT                1000 /* msecs */
  32#define USB_RECV_TIMEOUT                1000 /* msecs */
  33#define RX_BUFFER_SIZE                  3072
  34#define CAN_USB_CLOCK                   8000000
  35#define MAX_NET_DEVICES                 3
  36#define MAX_USBCAN_NET_DEVICES          2
  37
  38/* Kvaser Leaf USB devices */
  39#define KVASER_VENDOR_ID                0x0bfd
  40#define USB_LEAF_DEVEL_PRODUCT_ID       10
  41#define USB_LEAF_LITE_PRODUCT_ID        11
  42#define USB_LEAF_PRO_PRODUCT_ID         12
  43#define USB_LEAF_SPRO_PRODUCT_ID        14
  44#define USB_LEAF_PRO_LS_PRODUCT_ID      15
  45#define USB_LEAF_PRO_SWC_PRODUCT_ID     16
  46#define USB_LEAF_PRO_LIN_PRODUCT_ID     17
  47#define USB_LEAF_SPRO_LS_PRODUCT_ID     18
  48#define USB_LEAF_SPRO_SWC_PRODUCT_ID    19
  49#define USB_MEMO2_DEVEL_PRODUCT_ID      22
  50#define USB_MEMO2_HSHS_PRODUCT_ID       23
  51#define USB_UPRO_HSHS_PRODUCT_ID        24
  52#define USB_LEAF_LITE_GI_PRODUCT_ID     25
  53#define USB_LEAF_PRO_OBDII_PRODUCT_ID   26
  54#define USB_MEMO2_HSLS_PRODUCT_ID       27
  55#define USB_LEAF_LITE_CH_PRODUCT_ID     28
  56#define USB_BLACKBIRD_SPRO_PRODUCT_ID   29
  57#define USB_OEM_MERCURY_PRODUCT_ID      34
  58#define USB_OEM_LEAF_PRODUCT_ID         35
  59#define USB_CAN_R_PRODUCT_ID            39
  60#define USB_LEAF_LITE_V2_PRODUCT_ID     288
  61#define USB_MINI_PCIE_HS_PRODUCT_ID     289
  62#define USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID 290
  63#define USB_USBCAN_LIGHT_2HS_PRODUCT_ID 291
  64#define USB_MINI_PCIE_2HS_PRODUCT_ID    292
  65
  66static inline bool kvaser_is_leaf(const struct usb_device_id *id)
  67{
  68        return id->idProduct >= USB_LEAF_DEVEL_PRODUCT_ID &&
  69               id->idProduct <= USB_MINI_PCIE_2HS_PRODUCT_ID;
  70}
  71
  72/* Kvaser USBCan-II devices */
  73#define USB_USBCAN_REVB_PRODUCT_ID      2
  74#define USB_VCI2_PRODUCT_ID             3
  75#define USB_USBCAN2_PRODUCT_ID          4
  76#define USB_MEMORATOR_PRODUCT_ID        5
  77
  78static inline bool kvaser_is_usbcan(const struct usb_device_id *id)
  79{
  80        return id->idProduct >= USB_USBCAN_REVB_PRODUCT_ID &&
  81               id->idProduct <= USB_MEMORATOR_PRODUCT_ID;
  82}
  83
  84/* USB devices features */
  85#define KVASER_HAS_SILENT_MODE          BIT(0)
  86#define KVASER_HAS_TXRX_ERRORS          BIT(1)
  87
  88/* Message header size */
  89#define MSG_HEADER_LEN                  2
  90
  91/* Can message flags */
  92#define MSG_FLAG_ERROR_FRAME            BIT(0)
  93#define MSG_FLAG_OVERRUN                BIT(1)
  94#define MSG_FLAG_NERR                   BIT(2)
  95#define MSG_FLAG_WAKEUP                 BIT(3)
  96#define MSG_FLAG_REMOTE_FRAME           BIT(4)
  97#define MSG_FLAG_RESERVED               BIT(5)
  98#define MSG_FLAG_TX_ACK                 BIT(6)
  99#define MSG_FLAG_TX_REQUEST             BIT(7)
 100
 101/* Can states (M16C CxSTRH register) */
 102#define M16C_STATE_BUS_RESET            BIT(0)
 103#define M16C_STATE_BUS_ERROR            BIT(4)
 104#define M16C_STATE_BUS_PASSIVE          BIT(5)
 105#define M16C_STATE_BUS_OFF              BIT(6)
 106
 107/* Can msg ids */
 108#define CMD_RX_STD_MESSAGE              12
 109#define CMD_TX_STD_MESSAGE              13
 110#define CMD_RX_EXT_MESSAGE              14
 111#define CMD_TX_EXT_MESSAGE              15
 112#define CMD_SET_BUS_PARAMS              16
 113#define CMD_GET_BUS_PARAMS              17
 114#define CMD_GET_BUS_PARAMS_REPLY        18
 115#define CMD_GET_CHIP_STATE              19
 116#define CMD_CHIP_STATE_EVENT            20
 117#define CMD_SET_CTRL_MODE               21
 118#define CMD_GET_CTRL_MODE               22
 119#define CMD_GET_CTRL_MODE_REPLY         23
 120#define CMD_RESET_CHIP                  24
 121#define CMD_RESET_CARD                  25
 122#define CMD_START_CHIP                  26
 123#define CMD_START_CHIP_REPLY            27
 124#define CMD_STOP_CHIP                   28
 125#define CMD_STOP_CHIP_REPLY             29
 126
 127#define CMD_LEAF_GET_CARD_INFO2         32
 128#define CMD_USBCAN_RESET_CLOCK          32
 129#define CMD_USBCAN_CLOCK_OVERFLOW_EVENT 33
 130
 131#define CMD_GET_CARD_INFO               34
 132#define CMD_GET_CARD_INFO_REPLY         35
 133#define CMD_GET_SOFTWARE_INFO           38
 134#define CMD_GET_SOFTWARE_INFO_REPLY     39
 135#define CMD_ERROR_EVENT                 45
 136#define CMD_FLUSH_QUEUE                 48
 137#define CMD_RESET_ERROR_COUNTER         49
 138#define CMD_TX_ACKNOWLEDGE              50
 139#define CMD_CAN_ERROR_EVENT             51
 140
 141#define CMD_LEAF_USB_THROTTLE           77
 142#define CMD_LEAF_LOG_MESSAGE            106
 143
 144/* error factors */
 145#define M16C_EF_ACKE                    BIT(0)
 146#define M16C_EF_CRCE                    BIT(1)
 147#define M16C_EF_FORME                   BIT(2)
 148#define M16C_EF_STFE                    BIT(3)
 149#define M16C_EF_BITE0                   BIT(4)
 150#define M16C_EF_BITE1                   BIT(5)
 151#define M16C_EF_RCVE                    BIT(6)
 152#define M16C_EF_TRE                     BIT(7)
 153
 154/* Only Leaf-based devices can report M16C error factors,
 155 * thus define our own error status flags for USBCANII
 156 */
 157#define USBCAN_ERROR_STATE_NONE         0
 158#define USBCAN_ERROR_STATE_TX_ERROR     BIT(0)
 159#define USBCAN_ERROR_STATE_RX_ERROR     BIT(1)
 160#define USBCAN_ERROR_STATE_BUSERROR     BIT(2)
 161
 162/* bittiming parameters */
 163#define KVASER_USB_TSEG1_MIN            1
 164#define KVASER_USB_TSEG1_MAX            16
 165#define KVASER_USB_TSEG2_MIN            1
 166#define KVASER_USB_TSEG2_MAX            8
 167#define KVASER_USB_SJW_MAX              4
 168#define KVASER_USB_BRP_MIN              1
 169#define KVASER_USB_BRP_MAX              64
 170#define KVASER_USB_BRP_INC              1
 171
 172/* ctrl modes */
 173#define KVASER_CTRL_MODE_NORMAL         1
 174#define KVASER_CTRL_MODE_SILENT         2
 175#define KVASER_CTRL_MODE_SELFRECEPTION  3
 176#define KVASER_CTRL_MODE_OFF            4
 177
 178/* Extended CAN identifier flag */
 179#define KVASER_EXTENDED_FRAME           BIT(31)
 180
 181/* Kvaser USB CAN dongles are divided into two major families:
 182 * - Leaf: Based on Renesas M32C, running firmware labeled as 'filo'
 183 * - UsbcanII: Based on Renesas M16C, running firmware labeled as 'helios'
 184 */
 185enum kvaser_usb_family {
 186        KVASER_LEAF,
 187        KVASER_USBCAN,
 188};
 189
 190struct kvaser_msg_simple {
 191        u8 tid;
 192        u8 channel;
 193} __packed;
 194
 195struct kvaser_msg_cardinfo {
 196        u8 tid;
 197        u8 nchannels;
 198        union {
 199                struct {
 200                        __le32 serial_number;
 201                        __le32 padding;
 202                } __packed leaf0;
 203                struct {
 204                        __le32 serial_number_low;
 205                        __le32 serial_number_high;
 206                } __packed usbcan0;
 207        } __packed;
 208        __le32 clock_resolution;
 209        __le32 mfgdate;
 210        u8 ean[8];
 211        u8 hw_revision;
 212        union {
 213                struct {
 214                        u8 usb_hs_mode;
 215                } __packed leaf1;
 216                struct {
 217                        u8 padding;
 218                } __packed usbcan1;
 219        } __packed;
 220        __le16 padding;
 221} __packed;
 222
 223struct kvaser_msg_cardinfo2 {
 224        u8 tid;
 225        u8 reserved;
 226        u8 pcb_id[24];
 227        __le32 oem_unlock_code;
 228} __packed;
 229
 230struct leaf_msg_softinfo {
 231        u8 tid;
 232        u8 padding0;
 233        __le32 sw_options;
 234        __le32 fw_version;
 235        __le16 max_outstanding_tx;
 236        __le16 padding1[9];
 237} __packed;
 238
 239struct usbcan_msg_softinfo {
 240        u8 tid;
 241        u8 fw_name[5];
 242        __le16 max_outstanding_tx;
 243        u8 padding[6];
 244        __le32 fw_version;
 245        __le16 checksum;
 246        __le16 sw_options;
 247} __packed;
 248
 249struct kvaser_msg_busparams {
 250        u8 tid;
 251        u8 channel;
 252        __le32 bitrate;
 253        u8 tseg1;
 254        u8 tseg2;
 255        u8 sjw;
 256        u8 no_samp;
 257} __packed;
 258
 259struct kvaser_msg_tx_can {
 260        u8 channel;
 261        u8 tid;
 262        u8 msg[14];
 263        union {
 264                struct {
 265                        u8 padding;
 266                        u8 flags;
 267                } __packed leaf;
 268                struct {
 269                        u8 flags;
 270                        u8 padding;
 271                } __packed usbcan;
 272        } __packed;
 273} __packed;
 274
 275struct kvaser_msg_rx_can_header {
 276        u8 channel;
 277        u8 flag;
 278} __packed;
 279
 280struct leaf_msg_rx_can {
 281        u8 channel;
 282        u8 flag;
 283
 284        __le16 time[3];
 285        u8 msg[14];
 286} __packed;
 287
 288struct usbcan_msg_rx_can {
 289        u8 channel;
 290        u8 flag;
 291
 292        u8 msg[14];
 293        __le16 time;
 294} __packed;
 295
 296struct leaf_msg_chip_state_event {
 297        u8 tid;
 298        u8 channel;
 299
 300        __le16 time[3];
 301        u8 tx_errors_count;
 302        u8 rx_errors_count;
 303
 304        u8 status;
 305        u8 padding[3];
 306} __packed;
 307
 308struct usbcan_msg_chip_state_event {
 309        u8 tid;
 310        u8 channel;
 311
 312        u8 tx_errors_count;
 313        u8 rx_errors_count;
 314        __le16 time;
 315
 316        u8 status;
 317        u8 padding[3];
 318} __packed;
 319
 320struct kvaser_msg_tx_acknowledge_header {
 321        u8 channel;
 322        u8 tid;
 323} __packed;
 324
 325struct leaf_msg_tx_acknowledge {
 326        u8 channel;
 327        u8 tid;
 328
 329        __le16 time[3];
 330        u8 flags;
 331        u8 time_offset;
 332} __packed;
 333
 334struct usbcan_msg_tx_acknowledge {
 335        u8 channel;
 336        u8 tid;
 337
 338        __le16 time;
 339        __le16 padding;
 340} __packed;
 341
 342struct leaf_msg_error_event {
 343        u8 tid;
 344        u8 flags;
 345        __le16 time[3];
 346        u8 channel;
 347        u8 padding;
 348        u8 tx_errors_count;
 349        u8 rx_errors_count;
 350        u8 status;
 351        u8 error_factor;
 352} __packed;
 353
 354struct usbcan_msg_error_event {
 355        u8 tid;
 356        u8 padding;
 357        u8 tx_errors_count_ch0;
 358        u8 rx_errors_count_ch0;
 359        u8 tx_errors_count_ch1;
 360        u8 rx_errors_count_ch1;
 361        u8 status_ch0;
 362        u8 status_ch1;
 363        __le16 time;
 364} __packed;
 365
 366struct kvaser_msg_ctrl_mode {
 367        u8 tid;
 368        u8 channel;
 369        u8 ctrl_mode;
 370        u8 padding[3];
 371} __packed;
 372
 373struct kvaser_msg_flush_queue {
 374        u8 tid;
 375        u8 channel;
 376        u8 flags;
 377        u8 padding[3];
 378} __packed;
 379
 380struct leaf_msg_log_message {
 381        u8 channel;
 382        u8 flags;
 383        __le16 time[3];
 384        u8 dlc;
 385        u8 time_offset;
 386        __le32 id;
 387        u8 data[8];
 388} __packed;
 389
 390struct kvaser_msg {
 391        u8 len;
 392        u8 id;
 393        union   {
 394                struct kvaser_msg_simple simple;
 395                struct kvaser_msg_cardinfo cardinfo;
 396                struct kvaser_msg_cardinfo2 cardinfo2;
 397                struct kvaser_msg_busparams busparams;
 398
 399                struct kvaser_msg_rx_can_header rx_can_header;
 400                struct kvaser_msg_tx_acknowledge_header tx_acknowledge_header;
 401
 402                union {
 403                        struct leaf_msg_softinfo softinfo;
 404                        struct leaf_msg_rx_can rx_can;
 405                        struct leaf_msg_chip_state_event chip_state_event;
 406                        struct leaf_msg_tx_acknowledge tx_acknowledge;
 407                        struct leaf_msg_error_event error_event;
 408                        struct leaf_msg_log_message log_message;
 409                } __packed leaf;
 410
 411                union {
 412                        struct usbcan_msg_softinfo softinfo;
 413                        struct usbcan_msg_rx_can rx_can;
 414                        struct usbcan_msg_chip_state_event chip_state_event;
 415                        struct usbcan_msg_tx_acknowledge tx_acknowledge;
 416                        struct usbcan_msg_error_event error_event;
 417                } __packed usbcan;
 418
 419                struct kvaser_msg_tx_can tx_can;
 420                struct kvaser_msg_ctrl_mode ctrl_mode;
 421                struct kvaser_msg_flush_queue flush_queue;
 422        } u;
 423} __packed;
 424
 425/* Summary of a kvaser error event, for a unified Leaf/Usbcan error
 426 * handling. Some discrepancies between the two families exist:
 427 *
 428 * - USBCAN firmware does not report M16C "error factors"
 429 * - USBCAN controllers has difficulties reporting if the raised error
 430 *   event is for ch0 or ch1. They leave such arbitration to the OS
 431 *   driver by letting it compare error counters with previous values
 432 *   and decide the error event's channel. Thus for USBCAN, the channel
 433 *   field is only advisory.
 434 */
 435struct kvaser_usb_error_summary {
 436        u8 channel, status, txerr, rxerr;
 437        union {
 438                struct {
 439                        u8 error_factor;
 440                } leaf;
 441                struct {
 442                        u8 other_ch_status;
 443                        u8 error_state;
 444                } usbcan;
 445        };
 446};
 447
 448/* Context for an outstanding, not yet ACKed, transmission */
 449struct kvaser_usb_tx_urb_context {
 450        struct kvaser_usb_net_priv *priv;
 451        u32 echo_index;
 452        int dlc;
 453};
 454
 455struct kvaser_usb {
 456        struct usb_device *udev;
 457        struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
 458
 459        struct usb_endpoint_descriptor *bulk_in, *bulk_out;
 460        struct usb_anchor rx_submitted;
 461
 462        /* @max_tx_urbs: Firmware-reported maximum number of oustanding,
 463         * not yet ACKed, transmissions on this device. This value is
 464         * also used as a sentinel for marking free tx contexts.
 465         */
 466        u32 fw_version;
 467        unsigned int nchannels;
 468        unsigned int max_tx_urbs;
 469        enum kvaser_usb_family family;
 470
 471        bool rxinitdone;
 472        void *rxbuf[MAX_RX_URBS];
 473        dma_addr_t rxbuf_dma[MAX_RX_URBS];
 474};
 475
 476struct kvaser_usb_net_priv {
 477        struct can_priv can;
 478        struct can_berr_counter bec;
 479
 480        struct kvaser_usb *dev;
 481        struct net_device *netdev;
 482        int channel;
 483
 484        struct completion start_comp, stop_comp;
 485        struct usb_anchor tx_submitted;
 486
 487        spinlock_t tx_contexts_lock;
 488        int active_tx_contexts;
 489        struct kvaser_usb_tx_urb_context tx_contexts[];
 490};
 491
 492static const struct usb_device_id kvaser_usb_table[] = {
 493        /* Leaf family IDs */
 494        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
 495        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
 496        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
 497                .driver_info = KVASER_HAS_TXRX_ERRORS |
 498                               KVASER_HAS_SILENT_MODE },
 499        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
 500                .driver_info = KVASER_HAS_TXRX_ERRORS |
 501                               KVASER_HAS_SILENT_MODE },
 502        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
 503                .driver_info = KVASER_HAS_TXRX_ERRORS |
 504                               KVASER_HAS_SILENT_MODE },
 505        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
 506                .driver_info = KVASER_HAS_TXRX_ERRORS |
 507                               KVASER_HAS_SILENT_MODE },
 508        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
 509                .driver_info = KVASER_HAS_TXRX_ERRORS |
 510                               KVASER_HAS_SILENT_MODE },
 511        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
 512                .driver_info = KVASER_HAS_TXRX_ERRORS |
 513                               KVASER_HAS_SILENT_MODE },
 514        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
 515                .driver_info = KVASER_HAS_TXRX_ERRORS |
 516                               KVASER_HAS_SILENT_MODE },
 517        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
 518                .driver_info = KVASER_HAS_TXRX_ERRORS |
 519                               KVASER_HAS_SILENT_MODE },
 520        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
 521                .driver_info = KVASER_HAS_TXRX_ERRORS |
 522                               KVASER_HAS_SILENT_MODE },
 523        { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
 524                .driver_info = KVASER_HAS_TXRX_ERRORS },
 525        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
 526        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
 527                .driver_info = KVASER_HAS_TXRX_ERRORS |
 528                               KVASER_HAS_SILENT_MODE },
 529        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
 530                .driver_info = KVASER_HAS_TXRX_ERRORS },
 531        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
 532                .driver_info = KVASER_HAS_TXRX_ERRORS },
 533        { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
 534                .driver_info = KVASER_HAS_TXRX_ERRORS },
 535        { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
 536                .driver_info = KVASER_HAS_TXRX_ERRORS },
 537        { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
 538                .driver_info = KVASER_HAS_TXRX_ERRORS },
 539        { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
 540                .driver_info = KVASER_HAS_TXRX_ERRORS },
 541        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) },
 542        { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) },
 543        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID) },
 544        { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_2HS_PRODUCT_ID) },
 545        { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_2HS_PRODUCT_ID) },
 546
 547        /* USBCANII family IDs */
 548        { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID),
 549                .driver_info = KVASER_HAS_TXRX_ERRORS },
 550        { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID),
 551                .driver_info = KVASER_HAS_TXRX_ERRORS },
 552        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID),
 553                .driver_info = KVASER_HAS_TXRX_ERRORS },
 554        { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID),
 555                .driver_info = KVASER_HAS_TXRX_ERRORS },
 556
 557        { }
 558};
 559MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
 560
 561static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
 562                                      struct kvaser_msg *msg)
 563{
 564        int actual_len;
 565
 566        return usb_bulk_msg(dev->udev,
 567                            usb_sndbulkpipe(dev->udev,
 568                                        dev->bulk_out->bEndpointAddress),
 569                            msg, msg->len, &actual_len,
 570                            USB_SEND_TIMEOUT);
 571}
 572
 573static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
 574                               struct kvaser_msg *msg)
 575{
 576        struct kvaser_msg *tmp;
 577        void *buf;
 578        int actual_len;
 579        int err;
 580        int pos;
 581        unsigned long to = jiffies + msecs_to_jiffies(USB_RECV_TIMEOUT);
 582
 583        buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
 584        if (!buf)
 585                return -ENOMEM;
 586
 587        do {
 588                err = usb_bulk_msg(dev->udev,
 589                                   usb_rcvbulkpipe(dev->udev,
 590                                        dev->bulk_in->bEndpointAddress),
 591                                   buf, RX_BUFFER_SIZE, &actual_len,
 592                                   USB_RECV_TIMEOUT);
 593                if (err < 0)
 594                        goto end;
 595
 596                pos = 0;
 597                while (pos <= actual_len - MSG_HEADER_LEN) {
 598                        tmp = buf + pos;
 599
 600                        /* Handle messages crossing the USB endpoint max packet
 601                         * size boundary. Check kvaser_usb_read_bulk_callback()
 602                         * for further details.
 603                         */
 604                        if (tmp->len == 0) {
 605                                pos = round_up(pos, le16_to_cpu(dev->bulk_in->
 606                                                                wMaxPacketSize));
 607                                continue;
 608                        }
 609
 610                        if (pos + tmp->len > actual_len) {
 611                                dev_err(dev->udev->dev.parent,
 612                                        "Format error\n");
 613                                break;
 614                        }
 615
 616                        if (tmp->id == id) {
 617                                memcpy(msg, tmp, tmp->len);
 618                                goto end;
 619                        }
 620
 621                        pos += tmp->len;
 622                }
 623        } while (time_before(jiffies, to));
 624
 625        err = -EINVAL;
 626
 627end:
 628        kfree(buf);
 629
 630        return err;
 631}
 632
 633static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
 634                                      u8 msg_id, int channel)
 635{
 636        struct kvaser_msg *msg;
 637        int rc;
 638
 639        msg = kmalloc(sizeof(*msg), GFP_KERNEL);
 640        if (!msg)
 641                return -ENOMEM;
 642
 643        msg->id = msg_id;
 644        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
 645        msg->u.simple.channel = channel;
 646        msg->u.simple.tid = 0xff;
 647
 648        rc = kvaser_usb_send_msg(dev, msg);
 649
 650        kfree(msg);
 651        return rc;
 652}
 653
 654static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
 655{
 656        struct kvaser_msg msg;
 657        int err;
 658
 659        err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
 660        if (err)
 661                return err;
 662
 663        err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
 664        if (err)
 665                return err;
 666
 667        switch (dev->family) {
 668        case KVASER_LEAF:
 669                dev->fw_version = le32_to_cpu(msg.u.leaf.softinfo.fw_version);
 670                dev->max_tx_urbs =
 671                        le16_to_cpu(msg.u.leaf.softinfo.max_outstanding_tx);
 672                break;
 673        case KVASER_USBCAN:
 674                dev->fw_version = le32_to_cpu(msg.u.usbcan.softinfo.fw_version);
 675                dev->max_tx_urbs =
 676                        le16_to_cpu(msg.u.usbcan.softinfo.max_outstanding_tx);
 677                break;
 678        }
 679
 680        return 0;
 681}
 682
 683static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
 684{
 685        struct kvaser_msg msg;
 686        int err;
 687
 688        err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
 689        if (err)
 690                return err;
 691
 692        err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
 693        if (err)
 694                return err;
 695
 696        dev->nchannels = msg.u.cardinfo.nchannels;
 697        if ((dev->nchannels > MAX_NET_DEVICES) ||
 698            (dev->family == KVASER_USBCAN &&
 699             dev->nchannels > MAX_USBCAN_NET_DEVICES))
 700                return -EINVAL;
 701
 702        return 0;
 703}
 704
 705static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
 706                                      const struct kvaser_msg *msg)
 707{
 708        struct net_device_stats *stats;
 709        struct kvaser_usb_tx_urb_context *context;
 710        struct kvaser_usb_net_priv *priv;
 711        struct sk_buff *skb;
 712        struct can_frame *cf;
 713        unsigned long flags;
 714        u8 channel, tid;
 715
 716        channel = msg->u.tx_acknowledge_header.channel;
 717        tid = msg->u.tx_acknowledge_header.tid;
 718
 719        if (channel >= dev->nchannels) {
 720                dev_err(dev->udev->dev.parent,
 721                        "Invalid channel number (%d)\n", channel);
 722                return;
 723        }
 724
 725        priv = dev->nets[channel];
 726
 727        if (!netif_device_present(priv->netdev))
 728                return;
 729
 730        stats = &priv->netdev->stats;
 731
 732        context = &priv->tx_contexts[tid % dev->max_tx_urbs];
 733
 734        /* Sometimes the state change doesn't come after a bus-off event */
 735        if (priv->can.restart_ms &&
 736            (priv->can.state >= CAN_STATE_BUS_OFF)) {
 737                skb = alloc_can_err_skb(priv->netdev, &cf);
 738                if (skb) {
 739                        cf->can_id |= CAN_ERR_RESTARTED;
 740
 741                        stats->rx_packets++;
 742                        stats->rx_bytes += cf->can_dlc;
 743                        netif_rx(skb);
 744                } else {
 745                        netdev_err(priv->netdev,
 746                                   "No memory left for err_skb\n");
 747                }
 748
 749                priv->can.can_stats.restarts++;
 750                netif_carrier_on(priv->netdev);
 751
 752                priv->can.state = CAN_STATE_ERROR_ACTIVE;
 753        }
 754
 755        stats->tx_packets++;
 756        stats->tx_bytes += context->dlc;
 757
 758        spin_lock_irqsave(&priv->tx_contexts_lock, flags);
 759
 760        can_get_echo_skb(priv->netdev, context->echo_index);
 761        context->echo_index = dev->max_tx_urbs;
 762        --priv->active_tx_contexts;
 763        netif_wake_queue(priv->netdev);
 764
 765        spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
 766}
 767
 768static void kvaser_usb_simple_msg_callback(struct urb *urb)
 769{
 770        struct net_device *netdev = urb->context;
 771
 772        kfree(urb->transfer_buffer);
 773
 774        if (urb->status)
 775                netdev_warn(netdev, "urb status received: %d\n",
 776                            urb->status);
 777}
 778
 779static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
 780                                       u8 msg_id)
 781{
 782        struct kvaser_usb *dev = priv->dev;
 783        struct net_device *netdev = priv->netdev;
 784        struct kvaser_msg *msg;
 785        struct urb *urb;
 786        void *buf;
 787        int err;
 788
 789        urb = usb_alloc_urb(0, GFP_ATOMIC);
 790        if (!urb)
 791                return -ENOMEM;
 792
 793        buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
 794        if (!buf) {
 795                usb_free_urb(urb);
 796                return -ENOMEM;
 797        }
 798
 799        msg = (struct kvaser_msg *)buf;
 800        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
 801        msg->id = msg_id;
 802        msg->u.simple.channel = priv->channel;
 803
 804        usb_fill_bulk_urb(urb, dev->udev,
 805                          usb_sndbulkpipe(dev->udev,
 806                                          dev->bulk_out->bEndpointAddress),
 807                          buf, msg->len,
 808                          kvaser_usb_simple_msg_callback, netdev);
 809        usb_anchor_urb(urb, &priv->tx_submitted);
 810
 811        err = usb_submit_urb(urb, GFP_ATOMIC);
 812        if (err) {
 813                netdev_err(netdev, "Error transmitting URB\n");
 814                usb_unanchor_urb(urb);
 815                usb_free_urb(urb);
 816                return err;
 817        }
 818
 819        usb_free_urb(urb);
 820
 821        return 0;
 822}
 823
 824static void kvaser_usb_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
 825                                                 const struct kvaser_usb_error_summary *es,
 826                                                 struct can_frame *cf)
 827{
 828        struct kvaser_usb *dev = priv->dev;
 829        struct net_device_stats *stats = &priv->netdev->stats;
 830        enum can_state cur_state, new_state, tx_state, rx_state;
 831
 832        netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status);
 833
 834        new_state = cur_state = priv->can.state;
 835
 836        if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET))
 837                new_state = CAN_STATE_BUS_OFF;
 838        else if (es->status & M16C_STATE_BUS_PASSIVE)
 839                new_state = CAN_STATE_ERROR_PASSIVE;
 840        else if (es->status & M16C_STATE_BUS_ERROR) {
 841                /* Guard against spurious error events after a busoff */
 842                if (cur_state < CAN_STATE_BUS_OFF) {
 843                        if ((es->txerr >= 128) || (es->rxerr >= 128))
 844                                new_state = CAN_STATE_ERROR_PASSIVE;
 845                        else if ((es->txerr >= 96) || (es->rxerr >= 96))
 846                                new_state = CAN_STATE_ERROR_WARNING;
 847                        else if (cur_state > CAN_STATE_ERROR_ACTIVE)
 848                                new_state = CAN_STATE_ERROR_ACTIVE;
 849                }
 850        }
 851
 852        if (!es->status)
 853                new_state = CAN_STATE_ERROR_ACTIVE;
 854
 855        if (new_state != cur_state) {
 856                tx_state = (es->txerr >= es->rxerr) ? new_state : 0;
 857                rx_state = (es->txerr <= es->rxerr) ? new_state : 0;
 858
 859                can_change_state(priv->netdev, cf, tx_state, rx_state);
 860        }
 861
 862        if (priv->can.restart_ms &&
 863            (cur_state >= CAN_STATE_BUS_OFF) &&
 864            (new_state < CAN_STATE_BUS_OFF)) {
 865                priv->can.can_stats.restarts++;
 866        }
 867
 868        switch (dev->family) {
 869        case KVASER_LEAF:
 870                if (es->leaf.error_factor) {
 871                        priv->can.can_stats.bus_error++;
 872                        stats->rx_errors++;
 873                }
 874                break;
 875        case KVASER_USBCAN:
 876                if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR)
 877                        stats->tx_errors++;
 878                if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR)
 879                        stats->rx_errors++;
 880                if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) {
 881                        priv->can.can_stats.bus_error++;
 882                }
 883                break;
 884        }
 885
 886        priv->bec.txerr = es->txerr;
 887        priv->bec.rxerr = es->rxerr;
 888}
 889
 890static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
 891                                const struct kvaser_usb_error_summary *es)
 892{
 893        struct can_frame *cf, tmp_cf = { .can_id = CAN_ERR_FLAG, .can_dlc = CAN_ERR_DLC };
 894        struct sk_buff *skb;
 895        struct net_device_stats *stats;
 896        struct kvaser_usb_net_priv *priv;
 897        enum can_state old_state, new_state;
 898
 899        if (es->channel >= dev->nchannels) {
 900                dev_err(dev->udev->dev.parent,
 901                        "Invalid channel number (%d)\n", es->channel);
 902                return;
 903        }
 904
 905        priv = dev->nets[es->channel];
 906        stats = &priv->netdev->stats;
 907
 908        /* Update all of the can interface's state and error counters before
 909         * trying any memory allocation that can actually fail with -ENOMEM.
 910         *
 911         * We send a temporary stack-allocated error can frame to
 912         * can_change_state() for the very same reason.
 913         *
 914         * TODO: Split can_change_state() responsibility between updating the
 915         * can interface's state and counters, and the setting up of can error
 916         * frame ID and data to userspace. Remove stack allocation afterwards.
 917         */
 918        old_state = priv->can.state;
 919        kvaser_usb_rx_error_update_can_state(priv, es, &tmp_cf);
 920        new_state = priv->can.state;
 921
 922        skb = alloc_can_err_skb(priv->netdev, &cf);
 923        if (!skb) {
 924                stats->rx_dropped++;
 925                return;
 926        }
 927        memcpy(cf, &tmp_cf, sizeof(*cf));
 928
 929        if (new_state != old_state) {
 930                if (es->status &
 931                    (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
 932                        if (!priv->can.restart_ms)
 933                                kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
 934                        netif_carrier_off(priv->netdev);
 935                }
 936
 937                if (priv->can.restart_ms &&
 938                    (old_state >= CAN_STATE_BUS_OFF) &&
 939                    (new_state < CAN_STATE_BUS_OFF)) {
 940                        cf->can_id |= CAN_ERR_RESTARTED;
 941                        netif_carrier_on(priv->netdev);
 942                }
 943        }
 944
 945        switch (dev->family) {
 946        case KVASER_LEAF:
 947                if (es->leaf.error_factor) {
 948                        cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
 949
 950                        if (es->leaf.error_factor & M16C_EF_ACKE)
 951                                cf->data[3] = CAN_ERR_PROT_LOC_ACK;
 952                        if (es->leaf.error_factor & M16C_EF_CRCE)
 953                                cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
 954                        if (es->leaf.error_factor & M16C_EF_FORME)
 955                                cf->data[2] |= CAN_ERR_PROT_FORM;
 956                        if (es->leaf.error_factor & M16C_EF_STFE)
 957                                cf->data[2] |= CAN_ERR_PROT_STUFF;
 958                        if (es->leaf.error_factor & M16C_EF_BITE0)
 959                                cf->data[2] |= CAN_ERR_PROT_BIT0;
 960                        if (es->leaf.error_factor & M16C_EF_BITE1)
 961                                cf->data[2] |= CAN_ERR_PROT_BIT1;
 962                        if (es->leaf.error_factor & M16C_EF_TRE)
 963                                cf->data[2] |= CAN_ERR_PROT_TX;
 964                }
 965                break;
 966        case KVASER_USBCAN:
 967                if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) {
 968                        cf->can_id |= CAN_ERR_BUSERROR;
 969                }
 970                break;
 971        }
 972
 973        cf->data[6] = es->txerr;
 974        cf->data[7] = es->rxerr;
 975
 976        stats->rx_packets++;
 977        stats->rx_bytes += cf->can_dlc;
 978        netif_rx(skb);
 979}
 980
 981/* For USBCAN, report error to userspace iff the channels's errors counter
 982 * has changed, or we're the only channel seeing a bus error state.
 983 */
 984static void kvaser_usbcan_conditionally_rx_error(const struct kvaser_usb *dev,
 985                                                 struct kvaser_usb_error_summary *es)
 986{
 987        struct kvaser_usb_net_priv *priv;
 988        int channel;
 989        bool report_error;
 990
 991        channel = es->channel;
 992        if (channel >= dev->nchannels) {
 993                dev_err(dev->udev->dev.parent,
 994                        "Invalid channel number (%d)\n", channel);
 995                return;
 996        }
 997
 998        priv = dev->nets[channel];
 999        report_error = false;
1000
1001        if (es->txerr != priv->bec.txerr) {
1002                es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR;
1003                report_error = true;
1004        }
1005        if (es->rxerr != priv->bec.rxerr) {
1006                es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR;
1007                report_error = true;
1008        }
1009        if ((es->status & M16C_STATE_BUS_ERROR) &&
1010            !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) {
1011                es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR;
1012                report_error = true;
1013        }
1014
1015        if (report_error)
1016                kvaser_usb_rx_error(dev, es);
1017}
1018
1019static void kvaser_usbcan_rx_error(const struct kvaser_usb *dev,
1020                                   const struct kvaser_msg *msg)
1021{
1022        struct kvaser_usb_error_summary es = { };
1023
1024        switch (msg->id) {
1025        /* Sometimes errors are sent as unsolicited chip state events */
1026        case CMD_CHIP_STATE_EVENT:
1027                es.channel = msg->u.usbcan.chip_state_event.channel;
1028                es.status =  msg->u.usbcan.chip_state_event.status;
1029                es.txerr = msg->u.usbcan.chip_state_event.tx_errors_count;
1030                es.rxerr = msg->u.usbcan.chip_state_event.rx_errors_count;
1031                kvaser_usbcan_conditionally_rx_error(dev, &es);
1032                break;
1033
1034        case CMD_CAN_ERROR_EVENT:
1035                es.channel = 0;
1036                es.status = msg->u.usbcan.error_event.status_ch0;
1037                es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch0;
1038                es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch0;
1039                es.usbcan.other_ch_status =
1040                        msg->u.usbcan.error_event.status_ch1;
1041                kvaser_usbcan_conditionally_rx_error(dev, &es);
1042
1043                /* The USBCAN firmware supports up to 2 channels.
1044                 * Now that ch0 was checked, check if ch1 has any errors.
1045                 */
1046                if (dev->nchannels == MAX_USBCAN_NET_DEVICES) {
1047                        es.channel = 1;
1048                        es.status = msg->u.usbcan.error_event.status_ch1;
1049                        es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch1;
1050                        es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch1;
1051                        es.usbcan.other_ch_status =
1052                                msg->u.usbcan.error_event.status_ch0;
1053                        kvaser_usbcan_conditionally_rx_error(dev, &es);
1054                }
1055                break;
1056
1057        default:
1058                dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
1059                        msg->id);
1060        }
1061}
1062
1063static void kvaser_leaf_rx_error(const struct kvaser_usb *dev,
1064                                 const struct kvaser_msg *msg)
1065{
1066        struct kvaser_usb_error_summary es = { };
1067
1068        switch (msg->id) {
1069        case CMD_CAN_ERROR_EVENT:
1070                es.channel = msg->u.leaf.error_event.channel;
1071                es.status =  msg->u.leaf.error_event.status;
1072                es.txerr = msg->u.leaf.error_event.tx_errors_count;
1073                es.rxerr = msg->u.leaf.error_event.rx_errors_count;
1074                es.leaf.error_factor = msg->u.leaf.error_event.error_factor;
1075                break;
1076        case CMD_LEAF_LOG_MESSAGE:
1077                es.channel = msg->u.leaf.log_message.channel;
1078                es.status = msg->u.leaf.log_message.data[0];
1079                es.txerr = msg->u.leaf.log_message.data[2];
1080                es.rxerr = msg->u.leaf.log_message.data[3];
1081                es.leaf.error_factor = msg->u.leaf.log_message.data[1];
1082                break;
1083        case CMD_CHIP_STATE_EVENT:
1084                es.channel = msg->u.leaf.chip_state_event.channel;
1085                es.status =  msg->u.leaf.chip_state_event.status;
1086                es.txerr = msg->u.leaf.chip_state_event.tx_errors_count;
1087                es.rxerr = msg->u.leaf.chip_state_event.rx_errors_count;
1088                es.leaf.error_factor = 0;
1089                break;
1090        default:
1091                dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
1092                        msg->id);
1093                return;
1094        }
1095
1096        kvaser_usb_rx_error(dev, &es);
1097}
1098
1099static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
1100                                  const struct kvaser_msg *msg)
1101{
1102        struct can_frame *cf;
1103        struct sk_buff *skb;
1104        struct net_device_stats *stats = &priv->netdev->stats;
1105
1106        if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1107                                         MSG_FLAG_NERR)) {
1108                netdev_err(priv->netdev, "Unknown error (flags: 0x%02x)\n",
1109                           msg->u.rx_can_header.flag);
1110
1111                stats->rx_errors++;
1112                return;
1113        }
1114
1115        if (msg->u.rx_can_header.flag & MSG_FLAG_OVERRUN) {
1116                stats->rx_over_errors++;
1117                stats->rx_errors++;
1118
1119                skb = alloc_can_err_skb(priv->netdev, &cf);
1120                if (!skb) {
1121                        stats->rx_dropped++;
1122                        return;
1123                }
1124
1125                cf->can_id |= CAN_ERR_CRTL;
1126                cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
1127
1128                stats->rx_packets++;
1129                stats->rx_bytes += cf->can_dlc;
1130                netif_rx(skb);
1131        }
1132}
1133
1134static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
1135                                  const struct kvaser_msg *msg)
1136{
1137        struct kvaser_usb_net_priv *priv;
1138        struct can_frame *cf;
1139        struct sk_buff *skb;
1140        struct net_device_stats *stats;
1141        u8 channel = msg->u.rx_can_header.channel;
1142        const u8 *rx_msg = NULL;        /* GCC */
1143
1144        if (channel >= dev->nchannels) {
1145                dev_err(dev->udev->dev.parent,
1146                        "Invalid channel number (%d)\n", channel);
1147                return;
1148        }
1149
1150        priv = dev->nets[channel];
1151        stats = &priv->netdev->stats;
1152
1153        if ((msg->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) &&
1154            (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE)) {
1155                kvaser_leaf_rx_error(dev, msg);
1156                return;
1157        } else if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME |
1158                                                MSG_FLAG_NERR |
1159                                                MSG_FLAG_OVERRUN)) {
1160                kvaser_usb_rx_can_err(priv, msg);
1161                return;
1162        } else if (msg->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) {
1163                netdev_warn(priv->netdev,
1164                            "Unhandled frame (flags: 0x%02x)",
1165                            msg->u.rx_can_header.flag);
1166                return;
1167        }
1168
1169        switch (dev->family) {
1170        case KVASER_LEAF:
1171                rx_msg = msg->u.leaf.rx_can.msg;
1172                break;
1173        case KVASER_USBCAN:
1174                rx_msg = msg->u.usbcan.rx_can.msg;
1175                break;
1176        }
1177
1178        skb = alloc_can_skb(priv->netdev, &cf);
1179        if (!skb) {
1180                stats->tx_dropped++;
1181                return;
1182        }
1183
1184        if (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE) {
1185                cf->can_id = le32_to_cpu(msg->u.leaf.log_message.id);
1186                if (cf->can_id & KVASER_EXTENDED_FRAME)
1187                        cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
1188                else
1189                        cf->can_id &= CAN_SFF_MASK;
1190
1191                cf->can_dlc = get_can_dlc(msg->u.leaf.log_message.dlc);
1192
1193                if (msg->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME)
1194                        cf->can_id |= CAN_RTR_FLAG;
1195                else
1196                        memcpy(cf->data, &msg->u.leaf.log_message.data,
1197                               cf->can_dlc);
1198        } else {
1199                cf->can_id = ((rx_msg[0] & 0x1f) << 6) | (rx_msg[1] & 0x3f);
1200
1201                if (msg->id == CMD_RX_EXT_MESSAGE) {
1202                        cf->can_id <<= 18;
1203                        cf->can_id |= ((rx_msg[2] & 0x0f) << 14) |
1204                                      ((rx_msg[3] & 0xff) << 6) |
1205                                      (rx_msg[4] & 0x3f);
1206                        cf->can_id |= CAN_EFF_FLAG;
1207                }
1208
1209                cf->can_dlc = get_can_dlc(rx_msg[5]);
1210
1211                if (msg->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME)
1212                        cf->can_id |= CAN_RTR_FLAG;
1213                else
1214                        memcpy(cf->data, &rx_msg[6],
1215                               cf->can_dlc);
1216        }
1217
1218        stats->rx_packets++;
1219        stats->rx_bytes += cf->can_dlc;
1220        netif_rx(skb);
1221}
1222
1223static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev,
1224                                        const struct kvaser_msg *msg)
1225{
1226        struct kvaser_usb_net_priv *priv;
1227        u8 channel = msg->u.simple.channel;
1228
1229        if (channel >= dev->nchannels) {
1230                dev_err(dev->udev->dev.parent,
1231                        "Invalid channel number (%d)\n", channel);
1232                return;
1233        }
1234
1235        priv = dev->nets[channel];
1236
1237        if (completion_done(&priv->start_comp) &&
1238            netif_queue_stopped(priv->netdev)) {
1239                netif_wake_queue(priv->netdev);
1240        } else {
1241                netif_start_queue(priv->netdev);
1242                complete(&priv->start_comp);
1243        }
1244}
1245
1246static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev,
1247                                       const struct kvaser_msg *msg)
1248{
1249        struct kvaser_usb_net_priv *priv;
1250        u8 channel = msg->u.simple.channel;
1251
1252        if (channel >= dev->nchannels) {
1253                dev_err(dev->udev->dev.parent,
1254                        "Invalid channel number (%d)\n", channel);
1255                return;
1256        }
1257
1258        priv = dev->nets[channel];
1259
1260        complete(&priv->stop_comp);
1261}
1262
1263static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
1264                                      const struct kvaser_msg *msg)
1265{
1266        switch (msg->id) {
1267        case CMD_START_CHIP_REPLY:
1268                kvaser_usb_start_chip_reply(dev, msg);
1269                break;
1270
1271        case CMD_STOP_CHIP_REPLY:
1272                kvaser_usb_stop_chip_reply(dev, msg);
1273                break;
1274
1275        case CMD_RX_STD_MESSAGE:
1276        case CMD_RX_EXT_MESSAGE:
1277                kvaser_usb_rx_can_msg(dev, msg);
1278                break;
1279
1280        case CMD_LEAF_LOG_MESSAGE:
1281                if (dev->family != KVASER_LEAF)
1282                        goto warn;
1283                kvaser_usb_rx_can_msg(dev, msg);
1284                break;
1285
1286        case CMD_CHIP_STATE_EVENT:
1287        case CMD_CAN_ERROR_EVENT:
1288                if (dev->family == KVASER_LEAF)
1289                        kvaser_leaf_rx_error(dev, msg);
1290                else
1291                        kvaser_usbcan_rx_error(dev, msg);
1292                break;
1293
1294        case CMD_TX_ACKNOWLEDGE:
1295                kvaser_usb_tx_acknowledge(dev, msg);
1296                break;
1297
1298        /* Ignored messages */
1299        case CMD_USBCAN_CLOCK_OVERFLOW_EVENT:
1300                if (dev->family != KVASER_USBCAN)
1301                        goto warn;
1302                break;
1303
1304        default:
1305warn:           dev_warn(dev->udev->dev.parent,
1306                         "Unhandled message (%d)\n", msg->id);
1307                break;
1308        }
1309}
1310
1311static void kvaser_usb_read_bulk_callback(struct urb *urb)
1312{
1313        struct kvaser_usb *dev = urb->context;
1314        struct kvaser_msg *msg;
1315        int pos = 0;
1316        int err, i;
1317
1318        switch (urb->status) {
1319        case 0:
1320                break;
1321        case -ENOENT:
1322        case -ESHUTDOWN:
1323                return;
1324        default:
1325                dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n",
1326                         urb->status);
1327                goto resubmit_urb;
1328        }
1329
1330        while (pos <= urb->actual_length - MSG_HEADER_LEN) {
1331                msg = urb->transfer_buffer + pos;
1332
1333                /* The Kvaser firmware can only read and write messages that
1334                 * does not cross the USB's endpoint wMaxPacketSize boundary.
1335                 * If a follow-up command crosses such boundary, firmware puts
1336                 * a placeholder zero-length command in its place then aligns
1337                 * the real command to the next max packet size.
1338                 *
1339                 * Handle such cases or we're going to miss a significant
1340                 * number of events in case of a heavy rx load on the bus.
1341                 */
1342                if (msg->len == 0) {
1343                        pos = round_up(pos, le16_to_cpu(dev->bulk_in->
1344                                                        wMaxPacketSize));
1345                        continue;
1346                }
1347
1348                if (pos + msg->len > urb->actual_length) {
1349                        dev_err(dev->udev->dev.parent, "Format error\n");
1350                        break;
1351                }
1352
1353                kvaser_usb_handle_message(dev, msg);
1354                pos += msg->len;
1355        }
1356
1357resubmit_urb:
1358        usb_fill_bulk_urb(urb, dev->udev,
1359                          usb_rcvbulkpipe(dev->udev,
1360                                          dev->bulk_in->bEndpointAddress),
1361                          urb->transfer_buffer, RX_BUFFER_SIZE,
1362                          kvaser_usb_read_bulk_callback, dev);
1363
1364        err = usb_submit_urb(urb, GFP_ATOMIC);
1365        if (err == -ENODEV) {
1366                for (i = 0; i < dev->nchannels; i++) {
1367                        if (!dev->nets[i])
1368                                continue;
1369
1370                        netif_device_detach(dev->nets[i]->netdev);
1371                }
1372        } else if (err) {
1373                dev_err(dev->udev->dev.parent,
1374                        "Failed resubmitting read bulk urb: %d\n", err);
1375        }
1376
1377        return;
1378}
1379
1380static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
1381{
1382        int i, err = 0;
1383
1384        if (dev->rxinitdone)
1385                return 0;
1386
1387        for (i = 0; i < MAX_RX_URBS; i++) {
1388                struct urb *urb = NULL;
1389                u8 *buf = NULL;
1390                dma_addr_t buf_dma;
1391
1392                urb = usb_alloc_urb(0, GFP_KERNEL);
1393                if (!urb) {
1394                        err = -ENOMEM;
1395                        break;
1396                }
1397
1398                buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE,
1399                                         GFP_KERNEL, &buf_dma);
1400                if (!buf) {
1401                        dev_warn(dev->udev->dev.parent,
1402                                 "No memory left for USB buffer\n");
1403                        usb_free_urb(urb);
1404                        err = -ENOMEM;
1405                        break;
1406                }
1407
1408                usb_fill_bulk_urb(urb, dev->udev,
1409                                  usb_rcvbulkpipe(dev->udev,
1410                                          dev->bulk_in->bEndpointAddress),
1411                                  buf, RX_BUFFER_SIZE,
1412                                  kvaser_usb_read_bulk_callback,
1413                                  dev);
1414                urb->transfer_dma = buf_dma;
1415                urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1416                usb_anchor_urb(urb, &dev->rx_submitted);
1417
1418                err = usb_submit_urb(urb, GFP_KERNEL);
1419                if (err) {
1420                        usb_unanchor_urb(urb);
1421                        usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
1422                                          buf_dma);
1423                        usb_free_urb(urb);
1424                        break;
1425                }
1426
1427                dev->rxbuf[i] = buf;
1428                dev->rxbuf_dma[i] = buf_dma;
1429
1430                usb_free_urb(urb);
1431        }
1432
1433        if (i == 0) {
1434                dev_warn(dev->udev->dev.parent,
1435                         "Cannot setup read URBs, error %d\n", err);
1436                return err;
1437        } else if (i < MAX_RX_URBS) {
1438                dev_warn(dev->udev->dev.parent,
1439                         "RX performances may be slow\n");
1440        }
1441
1442        dev->rxinitdone = true;
1443
1444        return 0;
1445}
1446
1447static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1448{
1449        struct kvaser_msg *msg;
1450        int rc;
1451
1452        msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1453        if (!msg)
1454                return -ENOMEM;
1455
1456        msg->id = CMD_SET_CTRL_MODE;
1457        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode);
1458        msg->u.ctrl_mode.tid = 0xff;
1459        msg->u.ctrl_mode.channel = priv->channel;
1460
1461        if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1462                msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1463        else
1464                msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1465
1466        rc = kvaser_usb_send_msg(priv->dev, msg);
1467
1468        kfree(msg);
1469        return rc;
1470}
1471
1472static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv)
1473{
1474        int err;
1475
1476        init_completion(&priv->start_comp);
1477
1478        err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP,
1479                                         priv->channel);
1480        if (err)
1481                return err;
1482
1483        if (!wait_for_completion_timeout(&priv->start_comp,
1484                                         msecs_to_jiffies(START_TIMEOUT)))
1485                return -ETIMEDOUT;
1486
1487        return 0;
1488}
1489
1490static int kvaser_usb_open(struct net_device *netdev)
1491{
1492        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1493        struct kvaser_usb *dev = priv->dev;
1494        int err;
1495
1496        err = open_candev(netdev);
1497        if (err)
1498                return err;
1499
1500        err = kvaser_usb_setup_rx_urbs(dev);
1501        if (err)
1502                goto error;
1503
1504        err = kvaser_usb_set_opt_mode(priv);
1505        if (err)
1506                goto error;
1507
1508        err = kvaser_usb_start_chip(priv);
1509        if (err) {
1510                netdev_warn(netdev, "Cannot start device, error %d\n", err);
1511                goto error;
1512        }
1513
1514        priv->can.state = CAN_STATE_ERROR_ACTIVE;
1515
1516        return 0;
1517
1518error:
1519        close_candev(netdev);
1520        return err;
1521}
1522
1523static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv)
1524{
1525        int i, max_tx_urbs;
1526
1527        max_tx_urbs = priv->dev->max_tx_urbs;
1528
1529        priv->active_tx_contexts = 0;
1530        for (i = 0; i < max_tx_urbs; i++)
1531                priv->tx_contexts[i].echo_index = max_tx_urbs;
1532}
1533
1534/* This method might sleep. Do not call it in the atomic context
1535 * of URB completions.
1536 */
1537static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
1538{
1539        usb_kill_anchored_urbs(&priv->tx_submitted);
1540        kvaser_usb_reset_tx_urb_contexts(priv);
1541}
1542
1543static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1544{
1545        int i;
1546
1547        usb_kill_anchored_urbs(&dev->rx_submitted);
1548
1549        for (i = 0; i < MAX_RX_URBS; i++)
1550                usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
1551                                  dev->rxbuf[i],
1552                                  dev->rxbuf_dma[i]);
1553
1554        for (i = 0; i < dev->nchannels; i++) {
1555                struct kvaser_usb_net_priv *priv = dev->nets[i];
1556
1557                if (priv)
1558                        kvaser_usb_unlink_tx_urbs(priv);
1559        }
1560}
1561
1562static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv)
1563{
1564        int err;
1565
1566        init_completion(&priv->stop_comp);
1567
1568        err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP,
1569                                         priv->channel);
1570        if (err)
1571                return err;
1572
1573        if (!wait_for_completion_timeout(&priv->stop_comp,
1574                                         msecs_to_jiffies(STOP_TIMEOUT)))
1575                return -ETIMEDOUT;
1576
1577        return 0;
1578}
1579
1580static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv)
1581{
1582        struct kvaser_msg *msg;
1583        int rc;
1584
1585        msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1586        if (!msg)
1587                return -ENOMEM;
1588
1589        msg->id = CMD_FLUSH_QUEUE;
1590        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue);
1591        msg->u.flush_queue.channel = priv->channel;
1592        msg->u.flush_queue.flags = 0x00;
1593
1594        rc = kvaser_usb_send_msg(priv->dev, msg);
1595
1596        kfree(msg);
1597        return rc;
1598}
1599
1600static int kvaser_usb_close(struct net_device *netdev)
1601{
1602        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1603        struct kvaser_usb *dev = priv->dev;
1604        int err;
1605
1606        netif_stop_queue(netdev);
1607
1608        err = kvaser_usb_flush_queue(priv);
1609        if (err)
1610                netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
1611
1612        if (kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel))
1613                netdev_warn(netdev, "Cannot reset card, error %d\n", err);
1614
1615        err = kvaser_usb_stop_chip(priv);
1616        if (err)
1617                netdev_warn(netdev, "Cannot stop device, error %d\n", err);
1618
1619        /* reset tx contexts */
1620        kvaser_usb_unlink_tx_urbs(priv);
1621
1622        priv->can.state = CAN_STATE_STOPPED;
1623        close_candev(priv->netdev);
1624
1625        return 0;
1626}
1627
1628static void kvaser_usb_write_bulk_callback(struct urb *urb)
1629{
1630        struct kvaser_usb_tx_urb_context *context = urb->context;
1631        struct kvaser_usb_net_priv *priv;
1632        struct net_device *netdev;
1633
1634        if (WARN_ON(!context))
1635                return;
1636
1637        priv = context->priv;
1638        netdev = priv->netdev;
1639
1640        kfree(urb->transfer_buffer);
1641
1642        if (!netif_device_present(netdev))
1643                return;
1644
1645        if (urb->status)
1646                netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1647}
1648
1649static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1650                                         struct net_device *netdev)
1651{
1652        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1653        struct kvaser_usb *dev = priv->dev;
1654        struct net_device_stats *stats = &netdev->stats;
1655        struct can_frame *cf = (struct can_frame *)skb->data;
1656        struct kvaser_usb_tx_urb_context *context = NULL;
1657        struct urb *urb;
1658        void *buf;
1659        struct kvaser_msg *msg;
1660        int i, err, ret = NETDEV_TX_OK;
1661        u8 *msg_tx_can_flags = NULL;            /* GCC */
1662        unsigned long flags;
1663
1664        if (can_dropped_invalid_skb(netdev, skb))
1665                return NETDEV_TX_OK;
1666
1667        urb = usb_alloc_urb(0, GFP_ATOMIC);
1668        if (!urb) {
1669                stats->tx_dropped++;
1670                dev_kfree_skb(skb);
1671                return NETDEV_TX_OK;
1672        }
1673
1674        buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1675        if (!buf) {
1676                stats->tx_dropped++;
1677                dev_kfree_skb(skb);
1678                goto freeurb;
1679        }
1680
1681        msg = buf;
1682        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
1683        msg->u.tx_can.channel = priv->channel;
1684
1685        switch (dev->family) {
1686        case KVASER_LEAF:
1687                msg_tx_can_flags = &msg->u.tx_can.leaf.flags;
1688                break;
1689        case KVASER_USBCAN:
1690                msg_tx_can_flags = &msg->u.tx_can.usbcan.flags;
1691                break;
1692        }
1693
1694        *msg_tx_can_flags = 0;
1695
1696        if (cf->can_id & CAN_EFF_FLAG) {
1697                msg->id = CMD_TX_EXT_MESSAGE;
1698                msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1699                msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1700                msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1701                msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1702                msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1703        } else {
1704                msg->id = CMD_TX_STD_MESSAGE;
1705                msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1706                msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1707        }
1708
1709        msg->u.tx_can.msg[5] = cf->can_dlc;
1710        memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1711
1712        if (cf->can_id & CAN_RTR_FLAG)
1713                *msg_tx_can_flags |= MSG_FLAG_REMOTE_FRAME;
1714
1715        spin_lock_irqsave(&priv->tx_contexts_lock, flags);
1716        for (i = 0; i < dev->max_tx_urbs; i++) {
1717                if (priv->tx_contexts[i].echo_index == dev->max_tx_urbs) {
1718                        context = &priv->tx_contexts[i];
1719
1720                        context->echo_index = i;
1721                        can_put_echo_skb(skb, netdev, context->echo_index);
1722                        ++priv->active_tx_contexts;
1723                        if (priv->active_tx_contexts >= dev->max_tx_urbs)
1724                                netif_stop_queue(netdev);
1725
1726                        break;
1727                }
1728        }
1729        spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
1730
1731        /* This should never happen; it implies a flow control bug */
1732        if (!context) {
1733                netdev_warn(netdev, "cannot find free context\n");
1734
1735                kfree(buf);
1736                ret =  NETDEV_TX_BUSY;
1737                goto freeurb;
1738        }
1739
1740        context->priv = priv;
1741        context->dlc = cf->can_dlc;
1742
1743        msg->u.tx_can.tid = context->echo_index;
1744
1745        usb_fill_bulk_urb(urb, dev->udev,
1746                          usb_sndbulkpipe(dev->udev,
1747                                          dev->bulk_out->bEndpointAddress),
1748                          buf, msg->len,
1749                          kvaser_usb_write_bulk_callback, context);
1750        usb_anchor_urb(urb, &priv->tx_submitted);
1751
1752        err = usb_submit_urb(urb, GFP_ATOMIC);
1753        if (unlikely(err)) {
1754                spin_lock_irqsave(&priv->tx_contexts_lock, flags);
1755
1756                can_free_echo_skb(netdev, context->echo_index);
1757                context->echo_index = dev->max_tx_urbs;
1758                --priv->active_tx_contexts;
1759                netif_wake_queue(netdev);
1760
1761                spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
1762
1763                usb_unanchor_urb(urb);
1764
1765                stats->tx_dropped++;
1766
1767                if (err == -ENODEV)
1768                        netif_device_detach(netdev);
1769                else
1770                        netdev_warn(netdev, "Failed tx_urb %d\n", err);
1771
1772                goto freeurb;
1773        }
1774
1775        ret = NETDEV_TX_OK;
1776
1777freeurb:
1778        usb_free_urb(urb);
1779        return ret;
1780}
1781
1782static const struct net_device_ops kvaser_usb_netdev_ops = {
1783        .ndo_open = kvaser_usb_open,
1784        .ndo_stop = kvaser_usb_close,
1785        .ndo_start_xmit = kvaser_usb_start_xmit,
1786        .ndo_change_mtu = can_change_mtu,
1787};
1788
1789static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1790        .name = "kvaser_usb",
1791        .tseg1_min = KVASER_USB_TSEG1_MIN,
1792        .tseg1_max = KVASER_USB_TSEG1_MAX,
1793        .tseg2_min = KVASER_USB_TSEG2_MIN,
1794        .tseg2_max = KVASER_USB_TSEG2_MAX,
1795        .sjw_max = KVASER_USB_SJW_MAX,
1796        .brp_min = KVASER_USB_BRP_MIN,
1797        .brp_max = KVASER_USB_BRP_MAX,
1798        .brp_inc = KVASER_USB_BRP_INC,
1799};
1800
1801static int kvaser_usb_set_bittiming(struct net_device *netdev)
1802{
1803        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1804        struct can_bittiming *bt = &priv->can.bittiming;
1805        struct kvaser_usb *dev = priv->dev;
1806        struct kvaser_msg *msg;
1807        int rc;
1808
1809        msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1810        if (!msg)
1811                return -ENOMEM;
1812
1813        msg->id = CMD_SET_BUS_PARAMS;
1814        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1815        msg->u.busparams.channel = priv->channel;
1816        msg->u.busparams.tid = 0xff;
1817        msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1818        msg->u.busparams.sjw = bt->sjw;
1819        msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1820        msg->u.busparams.tseg2 = bt->phase_seg2;
1821
1822        if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1823                msg->u.busparams.no_samp = 3;
1824        else
1825                msg->u.busparams.no_samp = 1;
1826
1827        rc = kvaser_usb_send_msg(dev, msg);
1828
1829        kfree(msg);
1830        return rc;
1831}
1832
1833static int kvaser_usb_set_mode(struct net_device *netdev,
1834                               enum can_mode mode)
1835{
1836        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1837        int err;
1838
1839        switch (mode) {
1840        case CAN_MODE_START:
1841                err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1842                if (err)
1843                        return err;
1844                break;
1845        default:
1846                return -EOPNOTSUPP;
1847        }
1848
1849        return 0;
1850}
1851
1852static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1853                                       struct can_berr_counter *bec)
1854{
1855        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1856
1857        *bec = priv->bec;
1858
1859        return 0;
1860}
1861
1862static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1863{
1864        int i;
1865
1866        for (i = 0; i < dev->nchannels; i++) {
1867                if (!dev->nets[i])
1868                        continue;
1869
1870                unregister_candev(dev->nets[i]->netdev);
1871        }
1872
1873        kvaser_usb_unlink_all_urbs(dev);
1874
1875        for (i = 0; i < dev->nchannels; i++) {
1876                if (!dev->nets[i])
1877                        continue;
1878
1879                free_candev(dev->nets[i]->netdev);
1880        }
1881}
1882
1883static int kvaser_usb_init_one(struct usb_interface *intf,
1884                               const struct usb_device_id *id, int channel)
1885{
1886        struct kvaser_usb *dev = usb_get_intfdata(intf);
1887        struct net_device *netdev;
1888        struct kvaser_usb_net_priv *priv;
1889        int err;
1890
1891        err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel);
1892        if (err)
1893                return err;
1894
1895        netdev = alloc_candev(sizeof(*priv) +
1896                              dev->max_tx_urbs * sizeof(*priv->tx_contexts),
1897                              dev->max_tx_urbs);
1898        if (!netdev) {
1899                dev_err(&intf->dev, "Cannot alloc candev\n");
1900                return -ENOMEM;
1901        }
1902
1903        priv = netdev_priv(netdev);
1904
1905        init_usb_anchor(&priv->tx_submitted);
1906        init_completion(&priv->start_comp);
1907        init_completion(&priv->stop_comp);
1908
1909        priv->dev = dev;
1910        priv->netdev = netdev;
1911        priv->channel = channel;
1912
1913        spin_lock_init(&priv->tx_contexts_lock);
1914        kvaser_usb_reset_tx_urb_contexts(priv);
1915
1916        priv->can.state = CAN_STATE_STOPPED;
1917        priv->can.clock.freq = CAN_USB_CLOCK;
1918        priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1919        priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1920        priv->can.do_set_mode = kvaser_usb_set_mode;
1921        if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1922                priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1923        priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1924        if (id->driver_info & KVASER_HAS_SILENT_MODE)
1925                priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1926
1927        netdev->flags |= IFF_ECHO;
1928
1929        netdev->netdev_ops = &kvaser_usb_netdev_ops;
1930
1931        SET_NETDEV_DEV(netdev, &intf->dev);
1932        netdev->dev_id = channel;
1933
1934        dev->nets[channel] = priv;
1935
1936        err = register_candev(netdev);
1937        if (err) {
1938                dev_err(&intf->dev, "Failed to register can device\n");
1939                free_candev(netdev);
1940                dev->nets[channel] = NULL;
1941                return err;
1942        }
1943
1944        netdev_dbg(netdev, "device registered\n");
1945
1946        return 0;
1947}
1948
1949static int kvaser_usb_get_endpoints(const struct usb_interface *intf,
1950                                    struct usb_endpoint_descriptor **in,
1951                                    struct usb_endpoint_descriptor **out)
1952{
1953        const struct usb_host_interface *iface_desc;
1954        struct usb_endpoint_descriptor *endpoint;
1955        int i;
1956
1957        iface_desc = &intf->altsetting[0];
1958
1959        for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1960                endpoint = &iface_desc->endpoint[i].desc;
1961
1962                if (!*in && usb_endpoint_is_bulk_in(endpoint))
1963                        *in = endpoint;
1964
1965                if (!*out && usb_endpoint_is_bulk_out(endpoint))
1966                        *out = endpoint;
1967
1968                /* use first bulk endpoint for in and out */
1969                if (*in && *out)
1970                        return 0;
1971        }
1972
1973        return -ENODEV;
1974}
1975
1976static int kvaser_usb_probe(struct usb_interface *intf,
1977                            const struct usb_device_id *id)
1978{
1979        struct kvaser_usb *dev;
1980        int err = -ENOMEM;
1981        int i, retry = 3;
1982
1983        dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1984        if (!dev)
1985                return -ENOMEM;
1986
1987        if (kvaser_is_leaf(id)) {
1988                dev->family = KVASER_LEAF;
1989        } else if (kvaser_is_usbcan(id)) {
1990                dev->family = KVASER_USBCAN;
1991        } else {
1992                dev_err(&intf->dev,
1993                        "Product ID (%d) does not belong to any known Kvaser USB family",
1994                        id->idProduct);
1995                return -ENODEV;
1996        }
1997
1998        err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
1999        if (err) {
2000                dev_err(&intf->dev, "Cannot get usb endpoint(s)");
2001                return err;
2002        }
2003
2004        dev->udev = interface_to_usbdev(intf);
2005
2006        init_usb_anchor(&dev->rx_submitted);
2007
2008        usb_set_intfdata(intf, dev);
2009
2010        /* On some x86 laptops, plugging a Kvaser device again after
2011         * an unplug makes the firmware always ignore the very first
2012         * command. For such a case, provide some room for retries
2013         * instead of completely exiting the driver.
2014         */
2015        do {
2016                err = kvaser_usb_get_software_info(dev);
2017        } while (--retry && err == -ETIMEDOUT);
2018
2019        if (err) {
2020                dev_err(&intf->dev,
2021                        "Cannot get software infos, error %d\n", err);
2022                return err;
2023        }
2024
2025        dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
2026                ((dev->fw_version >> 24) & 0xff),
2027                ((dev->fw_version >> 16) & 0xff),
2028                (dev->fw_version & 0xffff));
2029
2030        dev_dbg(&intf->dev, "Max oustanding tx = %d URBs\n", dev->max_tx_urbs);
2031
2032        err = kvaser_usb_get_card_info(dev);
2033        if (err) {
2034                dev_err(&intf->dev,
2035                        "Cannot get card infos, error %d\n", err);
2036                return err;
2037        }
2038
2039        for (i = 0; i < dev->nchannels; i++) {
2040                err = kvaser_usb_init_one(intf, id, i);
2041                if (err) {
2042                        kvaser_usb_remove_interfaces(dev);
2043                        return err;
2044                }
2045        }
2046
2047        return 0;
2048}
2049
2050static void kvaser_usb_disconnect(struct usb_interface *intf)
2051{
2052        struct kvaser_usb *dev = usb_get_intfdata(intf);
2053
2054        usb_set_intfdata(intf, NULL);
2055
2056        if (!dev)
2057                return;
2058
2059        kvaser_usb_remove_interfaces(dev);
2060}
2061
2062static struct usb_driver kvaser_usb_driver = {
2063        .name = "kvaser_usb",
2064        .probe = kvaser_usb_probe,
2065        .disconnect = kvaser_usb_disconnect,
2066        .id_table = kvaser_usb_table,
2067};
2068
2069module_usb_driver(kvaser_usb_driver);
2070
2071MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
2072MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
2073MODULE_LICENSE("GPL v2");
2074
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.