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 *
  10 * Copyright (C) 2002-2006 KVASER AB, Sweden. All rights reserved.
  11 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
  12 * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
  13 */
  14
  15#include <linux/init.h>
  16#include <linux/completion.h>
  17#include <linux/module.h>
  18#include <linux/netdevice.h>
  19#include <linux/usb.h>
  20
  21#include <linux/can.h>
  22#include <linux/can/dev.h>
  23#include <linux/can/error.h>
  24
  25#define MAX_TX_URBS                     16
  26#define MAX_RX_URBS                     4
  27#define START_TIMEOUT                   1000 /* msecs */
  28#define STOP_TIMEOUT                    1000 /* msecs */
  29#define USB_SEND_TIMEOUT                1000 /* msecs */
  30#define USB_RECV_TIMEOUT                1000 /* msecs */
  31#define RX_BUFFER_SIZE                  3072
  32#define CAN_USB_CLOCK                   8000000
  33#define MAX_NET_DEVICES                 3
  34
  35/* Kvaser USB devices */
  36#define KVASER_VENDOR_ID                0x0bfd
  37#define USB_LEAF_DEVEL_PRODUCT_ID       10
  38#define USB_LEAF_LITE_PRODUCT_ID        11
  39#define USB_LEAF_PRO_PRODUCT_ID         12
  40#define USB_LEAF_SPRO_PRODUCT_ID        14
  41#define USB_LEAF_PRO_LS_PRODUCT_ID      15
  42#define USB_LEAF_PRO_SWC_PRODUCT_ID     16
  43#define USB_LEAF_PRO_LIN_PRODUCT_ID     17
  44#define USB_LEAF_SPRO_LS_PRODUCT_ID     18
  45#define USB_LEAF_SPRO_SWC_PRODUCT_ID    19
  46#define USB_MEMO2_DEVEL_PRODUCT_ID      22
  47#define USB_MEMO2_HSHS_PRODUCT_ID       23
  48#define USB_UPRO_HSHS_PRODUCT_ID        24
  49#define USB_LEAF_LITE_GI_PRODUCT_ID     25
  50#define USB_LEAF_PRO_OBDII_PRODUCT_ID   26
  51#define USB_MEMO2_HSLS_PRODUCT_ID       27
  52#define USB_LEAF_LITE_CH_PRODUCT_ID     28
  53#define USB_BLACKBIRD_SPRO_PRODUCT_ID   29
  54#define USB_OEM_MERCURY_PRODUCT_ID      34
  55#define USB_OEM_LEAF_PRODUCT_ID         35
  56#define USB_CAN_R_PRODUCT_ID            39
  57
  58/* USB devices features */
  59#define KVASER_HAS_SILENT_MODE          BIT(0)
  60#define KVASER_HAS_TXRX_ERRORS          BIT(1)
  61
  62/* Message header size */
  63#define MSG_HEADER_LEN                  2
  64
  65/* Can message flags */
  66#define MSG_FLAG_ERROR_FRAME            BIT(0)
  67#define MSG_FLAG_OVERRUN                BIT(1)
  68#define MSG_FLAG_NERR                   BIT(2)
  69#define MSG_FLAG_WAKEUP                 BIT(3)
  70#define MSG_FLAG_REMOTE_FRAME           BIT(4)
  71#define MSG_FLAG_RESERVED               BIT(5)
  72#define MSG_FLAG_TX_ACK                 BIT(6)
  73#define MSG_FLAG_TX_REQUEST             BIT(7)
  74
  75/* Can states */
  76#define M16C_STATE_BUS_RESET            BIT(0)
  77#define M16C_STATE_BUS_ERROR            BIT(4)
  78#define M16C_STATE_BUS_PASSIVE          BIT(5)
  79#define M16C_STATE_BUS_OFF              BIT(6)
  80
  81/* Can msg ids */
  82#define CMD_RX_STD_MESSAGE              12
  83#define CMD_TX_STD_MESSAGE              13
  84#define CMD_RX_EXT_MESSAGE              14
  85#define CMD_TX_EXT_MESSAGE              15
  86#define CMD_SET_BUS_PARAMS              16
  87#define CMD_GET_BUS_PARAMS              17
  88#define CMD_GET_BUS_PARAMS_REPLY        18
  89#define CMD_GET_CHIP_STATE              19
  90#define CMD_CHIP_STATE_EVENT            20
  91#define CMD_SET_CTRL_MODE               21
  92#define CMD_GET_CTRL_MODE               22
  93#define CMD_GET_CTRL_MODE_REPLY         23
  94#define CMD_RESET_CHIP                  24
  95#define CMD_RESET_CARD                  25
  96#define CMD_START_CHIP                  26
  97#define CMD_START_CHIP_REPLY            27
  98#define CMD_STOP_CHIP                   28
  99#define CMD_STOP_CHIP_REPLY             29
 100#define CMD_GET_CARD_INFO2              32
 101#define CMD_GET_CARD_INFO               34
 102#define CMD_GET_CARD_INFO_REPLY         35
 103#define CMD_GET_SOFTWARE_INFO           38
 104#define CMD_GET_SOFTWARE_INFO_REPLY     39
 105#define CMD_ERROR_EVENT                 45
 106#define CMD_FLUSH_QUEUE                 48
 107#define CMD_RESET_ERROR_COUNTER         49
 108#define CMD_TX_ACKNOWLEDGE              50
 109#define CMD_CAN_ERROR_EVENT             51
 110#define CMD_USB_THROTTLE                77
 111#define CMD_LOG_MESSAGE                 106
 112
 113/* error factors */
 114#define M16C_EF_ACKE                    BIT(0)
 115#define M16C_EF_CRCE                    BIT(1)
 116#define M16C_EF_FORME                   BIT(2)
 117#define M16C_EF_STFE                    BIT(3)
 118#define M16C_EF_BITE0                   BIT(4)
 119#define M16C_EF_BITE1                   BIT(5)
 120#define M16C_EF_RCVE                    BIT(6)
 121#define M16C_EF_TRE                     BIT(7)
 122
 123/* bittiming parameters */
 124#define KVASER_USB_TSEG1_MIN            1
 125#define KVASER_USB_TSEG1_MAX            16
 126#define KVASER_USB_TSEG2_MIN            1
 127#define KVASER_USB_TSEG2_MAX            8
 128#define KVASER_USB_SJW_MAX              4
 129#define KVASER_USB_BRP_MIN              1
 130#define KVASER_USB_BRP_MAX              64
 131#define KVASER_USB_BRP_INC              1
 132
 133/* ctrl modes */
 134#define KVASER_CTRL_MODE_NORMAL         1
 135#define KVASER_CTRL_MODE_SILENT         2
 136#define KVASER_CTRL_MODE_SELFRECEPTION  3
 137#define KVASER_CTRL_MODE_OFF            4
 138
 139/* log message */
 140#define KVASER_EXTENDED_FRAME           BIT(31)
 141
 142struct kvaser_msg_simple {
 143        u8 tid;
 144        u8 channel;
 145} __packed;
 146
 147struct kvaser_msg_cardinfo {
 148        u8 tid;
 149        u8 nchannels;
 150        __le32 serial_number;
 151        __le32 padding;
 152        __le32 clock_resolution;
 153        __le32 mfgdate;
 154        u8 ean[8];
 155        u8 hw_revision;
 156        u8 usb_hs_mode;
 157        __le16 padding2;
 158} __packed;
 159
 160struct kvaser_msg_cardinfo2 {
 161        u8 tid;
 162        u8 channel;
 163        u8 pcb_id[24];
 164        __le32 oem_unlock_code;
 165} __packed;
 166
 167struct kvaser_msg_softinfo {
 168        u8 tid;
 169        u8 channel;
 170        __le32 sw_options;
 171        __le32 fw_version;
 172        __le16 max_outstanding_tx;
 173        __le16 padding[9];
 174} __packed;
 175
 176struct kvaser_msg_busparams {
 177        u8 tid;
 178        u8 channel;
 179        __le32 bitrate;
 180        u8 tseg1;
 181        u8 tseg2;
 182        u8 sjw;
 183        u8 no_samp;
 184} __packed;
 185
 186struct kvaser_msg_tx_can {
 187        u8 channel;
 188        u8 tid;
 189        u8 msg[14];
 190        u8 padding;
 191        u8 flags;
 192} __packed;
 193
 194struct kvaser_msg_rx_can {
 195        u8 channel;
 196        u8 flag;
 197        __le16 time[3];
 198        u8 msg[14];
 199} __packed;
 200
 201struct kvaser_msg_chip_state_event {
 202        u8 tid;
 203        u8 channel;
 204        __le16 time[3];
 205        u8 tx_errors_count;
 206        u8 rx_errors_count;
 207        u8 status;
 208        u8 padding[3];
 209} __packed;
 210
 211struct kvaser_msg_tx_acknowledge {
 212        u8 channel;
 213        u8 tid;
 214        __le16 time[3];
 215        u8 flags;
 216        u8 time_offset;
 217} __packed;
 218
 219struct kvaser_msg_error_event {
 220        u8 tid;
 221        u8 flags;
 222        __le16 time[3];
 223        u8 channel;
 224        u8 padding;
 225        u8 tx_errors_count;
 226        u8 rx_errors_count;
 227        u8 status;
 228        u8 error_factor;
 229} __packed;
 230
 231struct kvaser_msg_ctrl_mode {
 232        u8 tid;
 233        u8 channel;
 234        u8 ctrl_mode;
 235        u8 padding[3];
 236} __packed;
 237
 238struct kvaser_msg_flush_queue {
 239        u8 tid;
 240        u8 channel;
 241        u8 flags;
 242        u8 padding[3];
 243} __packed;
 244
 245struct kvaser_msg_log_message {
 246        u8 channel;
 247        u8 flags;
 248        __le16 time[3];
 249        u8 dlc;
 250        u8 time_offset;
 251        __le32 id;
 252        u8 data[8];
 253} __packed;
 254
 255struct kvaser_msg {
 256        u8 len;
 257        u8 id;
 258        union   {
 259                struct kvaser_msg_simple simple;
 260                struct kvaser_msg_cardinfo cardinfo;
 261                struct kvaser_msg_cardinfo2 cardinfo2;
 262                struct kvaser_msg_softinfo softinfo;
 263                struct kvaser_msg_busparams busparams;
 264                struct kvaser_msg_tx_can tx_can;
 265                struct kvaser_msg_rx_can rx_can;
 266                struct kvaser_msg_chip_state_event chip_state_event;
 267                struct kvaser_msg_tx_acknowledge tx_acknowledge;
 268                struct kvaser_msg_error_event error_event;
 269                struct kvaser_msg_ctrl_mode ctrl_mode;
 270                struct kvaser_msg_flush_queue flush_queue;
 271                struct kvaser_msg_log_message log_message;
 272        } u;
 273} __packed;
 274
 275struct kvaser_usb_tx_urb_context {
 276        struct kvaser_usb_net_priv *priv;
 277        u32 echo_index;
 278        int dlc;
 279};
 280
 281struct kvaser_usb {
 282        struct usb_device *udev;
 283        struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
 284
 285        struct usb_endpoint_descriptor *bulk_in, *bulk_out;
 286        struct usb_anchor rx_submitted;
 287
 288        u32 fw_version;
 289        unsigned int nchannels;
 290
 291        bool rxinitdone;
 292        void *rxbuf[MAX_RX_URBS];
 293        dma_addr_t rxbuf_dma[MAX_RX_URBS];
 294};
 295
 296struct kvaser_usb_net_priv {
 297        struct can_priv can;
 298
 299        atomic_t active_tx_urbs;
 300        struct usb_anchor tx_submitted;
 301        struct kvaser_usb_tx_urb_context tx_contexts[MAX_TX_URBS];
 302
 303        struct completion start_comp, stop_comp;
 304
 305        struct kvaser_usb *dev;
 306        struct net_device *netdev;
 307        int channel;
 308
 309        struct can_berr_counter bec;
 310};
 311
 312static const struct usb_device_id kvaser_usb_table[] = {
 313        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
 314        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
 315        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
 316                .driver_info = KVASER_HAS_TXRX_ERRORS |
 317                               KVASER_HAS_SILENT_MODE },
 318        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
 319                .driver_info = KVASER_HAS_TXRX_ERRORS |
 320                               KVASER_HAS_SILENT_MODE },
 321        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
 322                .driver_info = KVASER_HAS_TXRX_ERRORS |
 323                               KVASER_HAS_SILENT_MODE },
 324        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
 325                .driver_info = KVASER_HAS_TXRX_ERRORS |
 326                               KVASER_HAS_SILENT_MODE },
 327        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
 328                .driver_info = KVASER_HAS_TXRX_ERRORS |
 329                               KVASER_HAS_SILENT_MODE },
 330        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
 331                .driver_info = KVASER_HAS_TXRX_ERRORS |
 332                               KVASER_HAS_SILENT_MODE },
 333        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
 334                .driver_info = KVASER_HAS_TXRX_ERRORS |
 335                               KVASER_HAS_SILENT_MODE },
 336        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
 337                .driver_info = KVASER_HAS_TXRX_ERRORS |
 338                               KVASER_HAS_SILENT_MODE },
 339        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
 340                .driver_info = KVASER_HAS_TXRX_ERRORS |
 341                               KVASER_HAS_SILENT_MODE },
 342        { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
 343                .driver_info = KVASER_HAS_TXRX_ERRORS },
 344        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
 345        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
 346                .driver_info = KVASER_HAS_TXRX_ERRORS |
 347                               KVASER_HAS_SILENT_MODE },
 348        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
 349                .driver_info = KVASER_HAS_TXRX_ERRORS },
 350        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
 351                .driver_info = KVASER_HAS_TXRX_ERRORS },
 352        { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
 353                .driver_info = KVASER_HAS_TXRX_ERRORS },
 354        { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
 355                .driver_info = KVASER_HAS_TXRX_ERRORS },
 356        { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
 357                .driver_info = KVASER_HAS_TXRX_ERRORS },
 358        { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
 359                .driver_info = KVASER_HAS_TXRX_ERRORS },
 360        { }
 361};
 362MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
 363
 364static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
 365                                      struct kvaser_msg *msg)
 366{
 367        int actual_len;
 368
 369        return usb_bulk_msg(dev->udev,
 370                            usb_sndbulkpipe(dev->udev,
 371                                        dev->bulk_out->bEndpointAddress),
 372                            msg, msg->len, &actual_len,
 373                            USB_SEND_TIMEOUT);
 374}
 375
 376static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
 377                               struct kvaser_msg *msg)
 378{
 379        struct kvaser_msg *tmp;
 380        void *buf;
 381        int actual_len;
 382        int err;
 383        int pos = 0;
 384
 385        buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
 386        if (!buf)
 387                return -ENOMEM;
 388
 389        err = usb_bulk_msg(dev->udev,
 390                           usb_rcvbulkpipe(dev->udev,
 391                                           dev->bulk_in->bEndpointAddress),
 392                           buf, RX_BUFFER_SIZE, &actual_len,
 393                           USB_RECV_TIMEOUT);
 394        if (err < 0)
 395                goto end;
 396
 397        while (pos <= actual_len - MSG_HEADER_LEN) {
 398                tmp = buf + pos;
 399
 400                if (!tmp->len)
 401                        break;
 402
 403                if (pos + tmp->len > actual_len) {
 404                        dev_err(dev->udev->dev.parent, "Format error\n");
 405                        break;
 406                }
 407
 408                if (tmp->id == id) {
 409                        memcpy(msg, tmp, tmp->len);
 410                        goto end;
 411                }
 412
 413                pos += tmp->len;
 414        }
 415
 416        err = -EINVAL;
 417
 418end:
 419        kfree(buf);
 420
 421        return err;
 422}
 423
 424static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
 425                                      u8 msg_id, int channel)
 426{
 427        struct kvaser_msg *msg;
 428        int rc;
 429
 430        msg = kmalloc(sizeof(*msg), GFP_KERNEL);
 431        if (!msg)
 432                return -ENOMEM;
 433
 434        msg->id = msg_id;
 435        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
 436        msg->u.simple.channel = channel;
 437        msg->u.simple.tid = 0xff;
 438
 439        rc = kvaser_usb_send_msg(dev, msg);
 440
 441        kfree(msg);
 442        return rc;
 443}
 444
 445static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
 446{
 447        struct kvaser_msg msg;
 448        int err;
 449
 450        err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
 451        if (err)
 452                return err;
 453
 454        err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
 455        if (err)
 456                return err;
 457
 458        dev->fw_version = le32_to_cpu(msg.u.softinfo.fw_version);
 459
 460        return 0;
 461}
 462
 463static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
 464{
 465        struct kvaser_msg msg;
 466        int err;
 467
 468        err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
 469        if (err)
 470                return err;
 471
 472        err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
 473        if (err)
 474                return err;
 475
 476        dev->nchannels = msg.u.cardinfo.nchannels;
 477
 478        return 0;
 479}
 480
 481static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
 482                                      const struct kvaser_msg *msg)
 483{
 484        struct net_device_stats *stats;
 485        struct kvaser_usb_tx_urb_context *context;
 486        struct kvaser_usb_net_priv *priv;
 487        struct sk_buff *skb;
 488        struct can_frame *cf;
 489        u8 channel = msg->u.tx_acknowledge.channel;
 490        u8 tid = msg->u.tx_acknowledge.tid;
 491
 492        if (channel >= dev->nchannels) {
 493                dev_err(dev->udev->dev.parent,
 494                        "Invalid channel number (%d)\n", channel);
 495                return;
 496        }
 497
 498        priv = dev->nets[channel];
 499
 500        if (!netif_device_present(priv->netdev))
 501                return;
 502
 503        stats = &priv->netdev->stats;
 504
 505        context = &priv->tx_contexts[tid % MAX_TX_URBS];
 506
 507        /* Sometimes the state change doesn't come after a bus-off event */
 508        if (priv->can.restart_ms &&
 509            (priv->can.state >= CAN_STATE_BUS_OFF)) {
 510                skb = alloc_can_err_skb(priv->netdev, &cf);
 511                if (skb) {
 512                        cf->can_id |= CAN_ERR_RESTARTED;
 513                        netif_rx(skb);
 514
 515                        stats->rx_packets++;
 516                        stats->rx_bytes += cf->can_dlc;
 517                } else {
 518                        netdev_err(priv->netdev,
 519                                   "No memory left for err_skb\n");
 520                }
 521
 522                priv->can.can_stats.restarts++;
 523                netif_carrier_on(priv->netdev);
 524
 525                priv->can.state = CAN_STATE_ERROR_ACTIVE;
 526        }
 527
 528        stats->tx_packets++;
 529        stats->tx_bytes += context->dlc;
 530        can_get_echo_skb(priv->netdev, context->echo_index);
 531
 532        context->echo_index = MAX_TX_URBS;
 533        atomic_dec(&priv->active_tx_urbs);
 534
 535        netif_wake_queue(priv->netdev);
 536}
 537
 538static void kvaser_usb_simple_msg_callback(struct urb *urb)
 539{
 540        struct net_device *netdev = urb->context;
 541
 542        kfree(urb->transfer_buffer);
 543
 544        if (urb->status)
 545                netdev_warn(netdev, "urb status received: %d\n",
 546                            urb->status);
 547}
 548
 549static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
 550                                       u8 msg_id)
 551{
 552        struct kvaser_usb *dev = priv->dev;
 553        struct net_device *netdev = priv->netdev;
 554        struct kvaser_msg *msg;
 555        struct urb *urb;
 556        void *buf;
 557        int err;
 558
 559        urb = usb_alloc_urb(0, GFP_ATOMIC);
 560        if (!urb) {
 561                netdev_err(netdev, "No memory left for URBs\n");
 562                return -ENOMEM;
 563        }
 564
 565        buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
 566        if (!buf) {
 567                usb_free_urb(urb);
 568                return -ENOMEM;
 569        }
 570
 571        msg = (struct kvaser_msg *)buf;
 572        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
 573        msg->id = msg_id;
 574        msg->u.simple.channel = priv->channel;
 575
 576        usb_fill_bulk_urb(urb, dev->udev,
 577                          usb_sndbulkpipe(dev->udev,
 578                                          dev->bulk_out->bEndpointAddress),
 579                          buf, msg->len,
 580                          kvaser_usb_simple_msg_callback, priv);
 581        usb_anchor_urb(urb, &priv->tx_submitted);
 582
 583        err = usb_submit_urb(urb, GFP_ATOMIC);
 584        if (err) {
 585                netdev_err(netdev, "Error transmitting URB\n");
 586                usb_unanchor_urb(urb);
 587                usb_free_urb(urb);
 588                kfree(buf);
 589                return err;
 590        }
 591
 592        usb_free_urb(urb);
 593
 594        return 0;
 595}
 596
 597static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
 598{
 599        int i;
 600
 601        usb_kill_anchored_urbs(&priv->tx_submitted);
 602        atomic_set(&priv->active_tx_urbs, 0);
 603
 604        for (i = 0; i < MAX_TX_URBS; i++)
 605                priv->tx_contexts[i].echo_index = MAX_TX_URBS;
 606}
 607
 608static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
 609                                const struct kvaser_msg *msg)
 610{
 611        struct can_frame *cf;
 612        struct sk_buff *skb;
 613        struct net_device_stats *stats;
 614        struct kvaser_usb_net_priv *priv;
 615        unsigned int new_state;
 616        u8 channel, status, txerr, rxerr, error_factor;
 617
 618        switch (msg->id) {
 619        case CMD_CAN_ERROR_EVENT:
 620                channel = msg->u.error_event.channel;
 621                status =  msg->u.error_event.status;
 622                txerr = msg->u.error_event.tx_errors_count;
 623                rxerr = msg->u.error_event.rx_errors_count;
 624                error_factor = msg->u.error_event.error_factor;
 625                break;
 626        case CMD_LOG_MESSAGE:
 627                channel = msg->u.log_message.channel;
 628                status = msg->u.log_message.data[0];
 629                txerr = msg->u.log_message.data[2];
 630                rxerr = msg->u.log_message.data[3];
 631                error_factor = msg->u.log_message.data[1];
 632                break;
 633        case CMD_CHIP_STATE_EVENT:
 634                channel = msg->u.chip_state_event.channel;
 635                status =  msg->u.chip_state_event.status;
 636                txerr = msg->u.chip_state_event.tx_errors_count;
 637                rxerr = msg->u.chip_state_event.rx_errors_count;
 638                error_factor = 0;
 639                break;
 640        default:
 641                dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
 642                        msg->id);
 643                return;
 644        }
 645
 646        if (channel >= dev->nchannels) {
 647                dev_err(dev->udev->dev.parent,
 648                        "Invalid channel number (%d)\n", channel);
 649                return;
 650        }
 651
 652        priv = dev->nets[channel];
 653        stats = &priv->netdev->stats;
 654
 655        if (status & M16C_STATE_BUS_RESET) {
 656                kvaser_usb_unlink_tx_urbs(priv);
 657                return;
 658        }
 659
 660        skb = alloc_can_err_skb(priv->netdev, &cf);
 661        if (!skb) {
 662                stats->rx_dropped++;
 663                return;
 664        }
 665
 666        new_state = priv->can.state;
 667
 668        netdev_dbg(priv->netdev, "Error status: 0x%02x\n", status);
 669
 670        if (status & M16C_STATE_BUS_OFF) {
 671                cf->can_id |= CAN_ERR_BUSOFF;
 672
 673                priv->can.can_stats.bus_off++;
 674                if (!priv->can.restart_ms)
 675                        kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
 676
 677                netif_carrier_off(priv->netdev);
 678
 679                new_state = CAN_STATE_BUS_OFF;
 680        } else if (status & M16C_STATE_BUS_PASSIVE) {
 681                if (priv->can.state != CAN_STATE_ERROR_PASSIVE) {
 682                        cf->can_id |= CAN_ERR_CRTL;
 683
 684                        if (txerr || rxerr)
 685                                cf->data[1] = (txerr > rxerr)
 686                                                ? CAN_ERR_CRTL_TX_PASSIVE
 687                                                : CAN_ERR_CRTL_RX_PASSIVE;
 688                        else
 689                                cf->data[1] = CAN_ERR_CRTL_TX_PASSIVE |
 690                                              CAN_ERR_CRTL_RX_PASSIVE;
 691
 692                        priv->can.can_stats.error_passive++;
 693                }
 694
 695                new_state = CAN_STATE_ERROR_PASSIVE;
 696        }
 697
 698        if (status == M16C_STATE_BUS_ERROR) {
 699                if ((priv->can.state < CAN_STATE_ERROR_WARNING) &&
 700                    ((txerr >= 96) || (rxerr >= 96))) {
 701                        cf->can_id |= CAN_ERR_CRTL;
 702                        cf->data[1] = (txerr > rxerr)
 703                                        ? CAN_ERR_CRTL_TX_WARNING
 704                                        : CAN_ERR_CRTL_RX_WARNING;
 705
 706                        priv->can.can_stats.error_warning++;
 707                        new_state = CAN_STATE_ERROR_WARNING;
 708                } else if (priv->can.state > CAN_STATE_ERROR_ACTIVE) {
 709                        cf->can_id |= CAN_ERR_PROT;
 710                        cf->data[2] = CAN_ERR_PROT_ACTIVE;
 711
 712                        new_state = CAN_STATE_ERROR_ACTIVE;
 713                }
 714        }
 715
 716        if (!status) {
 717                cf->can_id |= CAN_ERR_PROT;
 718                cf->data[2] = CAN_ERR_PROT_ACTIVE;
 719
 720                new_state = CAN_STATE_ERROR_ACTIVE;
 721        }
 722
 723        if (priv->can.restart_ms &&
 724            (priv->can.state >= CAN_STATE_BUS_OFF) &&
 725            (new_state < CAN_STATE_BUS_OFF)) {
 726                cf->can_id |= CAN_ERR_RESTARTED;
 727                netif_carrier_on(priv->netdev);
 728
 729                priv->can.can_stats.restarts++;
 730        }
 731
 732        if (error_factor) {
 733                priv->can.can_stats.bus_error++;
 734                stats->rx_errors++;
 735
 736                cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
 737
 738                if (error_factor & M16C_EF_ACKE)
 739                        cf->data[3] |= (CAN_ERR_PROT_LOC_ACK);
 740                if (error_factor & M16C_EF_CRCE)
 741                        cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
 742                                        CAN_ERR_PROT_LOC_CRC_DEL);
 743                if (error_factor & M16C_EF_FORME)
 744                        cf->data[2] |= CAN_ERR_PROT_FORM;
 745                if (error_factor & M16C_EF_STFE)
 746                        cf->data[2] |= CAN_ERR_PROT_STUFF;
 747                if (error_factor & M16C_EF_BITE0)
 748                        cf->data[2] |= CAN_ERR_PROT_BIT0;
 749                if (error_factor & M16C_EF_BITE1)
 750                        cf->data[2] |= CAN_ERR_PROT_BIT1;
 751                if (error_factor & M16C_EF_TRE)
 752                        cf->data[2] |= CAN_ERR_PROT_TX;
 753        }
 754
 755        cf->data[6] = txerr;
 756        cf->data[7] = rxerr;
 757
 758        priv->bec.txerr = txerr;
 759        priv->bec.rxerr = rxerr;
 760
 761        priv->can.state = new_state;
 762
 763        netif_rx(skb);
 764
 765        stats->rx_packets++;
 766        stats->rx_bytes += cf->can_dlc;
 767}
 768
 769static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
 770                                  const struct kvaser_msg *msg)
 771{
 772        struct can_frame *cf;
 773        struct sk_buff *skb;
 774        struct net_device_stats *stats = &priv->netdev->stats;
 775
 776        if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
 777                                         MSG_FLAG_NERR)) {
 778                netdev_err(priv->netdev, "Unknow error (flags: 0x%02x)\n",
 779                           msg->u.rx_can.flag);
 780
 781                stats->rx_errors++;
 782                return;
 783        }
 784
 785        if (msg->u.rx_can.flag & MSG_FLAG_OVERRUN) {
 786                skb = alloc_can_err_skb(priv->netdev, &cf);
 787                if (!skb) {
 788                        stats->rx_dropped++;
 789                        return;
 790                }
 791
 792                cf->can_id |= CAN_ERR_CRTL;
 793                cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
 794
 795                stats->rx_over_errors++;
 796                stats->rx_errors++;
 797
 798                netif_rx(skb);
 799
 800                stats->rx_packets++;
 801                stats->rx_bytes += cf->can_dlc;
 802        }
 803}
 804
 805static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
 806                                  const struct kvaser_msg *msg)
 807{
 808        struct kvaser_usb_net_priv *priv;
 809        struct can_frame *cf;
 810        struct sk_buff *skb;
 811        struct net_device_stats *stats;
 812        u8 channel = msg->u.rx_can.channel;
 813
 814        if (channel >= dev->nchannels) {
 815                dev_err(dev->udev->dev.parent,
 816                        "Invalid channel number (%d)\n", channel);
 817                return;
 818        }
 819
 820        priv = dev->nets[channel];
 821        stats = &priv->netdev->stats;
 822
 823        if ((msg->u.rx_can.flag & MSG_FLAG_ERROR_FRAME) &&
 824            (msg->id == CMD_LOG_MESSAGE)) {
 825                kvaser_usb_rx_error(dev, msg);
 826                return;
 827        } else if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
 828                                         MSG_FLAG_NERR |
 829                                         MSG_FLAG_OVERRUN)) {
 830                kvaser_usb_rx_can_err(priv, msg);
 831                return;
 832        } else if (msg->u.rx_can.flag & ~MSG_FLAG_REMOTE_FRAME) {
 833                netdev_warn(priv->netdev,
 834                            "Unhandled frame (flags: 0x%02x)",
 835                            msg->u.rx_can.flag);
 836                return;
 837        }
 838
 839        skb = alloc_can_skb(priv->netdev, &cf);
 840        if (!skb) {
 841                stats->tx_dropped++;
 842                return;
 843        }
 844
 845        if (msg->id == CMD_LOG_MESSAGE) {
 846                cf->can_id = le32_to_cpu(msg->u.log_message.id);
 847                if (cf->can_id & KVASER_EXTENDED_FRAME)
 848                        cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
 849                else
 850                        cf->can_id &= CAN_SFF_MASK;
 851
 852                cf->can_dlc = get_can_dlc(msg->u.log_message.dlc);
 853
 854                if (msg->u.log_message.flags & MSG_FLAG_REMOTE_FRAME)
 855                        cf->can_id |= CAN_RTR_FLAG;
 856                else
 857                        memcpy(cf->data, &msg->u.log_message.data,
 858                               cf->can_dlc);
 859        } else {
 860                cf->can_id = ((msg->u.rx_can.msg[0] & 0x1f) << 6) |
 861                             (msg->u.rx_can.msg[1] & 0x3f);
 862
 863                if (msg->id == CMD_RX_EXT_MESSAGE) {
 864                        cf->can_id <<= 18;
 865                        cf->can_id |= ((msg->u.rx_can.msg[2] & 0x0f) << 14) |
 866                                      ((msg->u.rx_can.msg[3] & 0xff) << 6) |
 867                                      (msg->u.rx_can.msg[4] & 0x3f);
 868                        cf->can_id |= CAN_EFF_FLAG;
 869                }
 870
 871                cf->can_dlc = get_can_dlc(msg->u.rx_can.msg[5]);
 872
 873                if (msg->u.rx_can.flag & MSG_FLAG_REMOTE_FRAME)
 874                        cf->can_id |= CAN_RTR_FLAG;
 875                else
 876                        memcpy(cf->data, &msg->u.rx_can.msg[6],
 877                               cf->can_dlc);
 878        }
 879
 880        netif_rx(skb);
 881
 882        stats->rx_packets++;
 883        stats->rx_bytes += cf->can_dlc;
 884}
 885
 886static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev,
 887                                        const struct kvaser_msg *msg)
 888{
 889        struct kvaser_usb_net_priv *priv;
 890        u8 channel = msg->u.simple.channel;
 891
 892        if (channel >= dev->nchannels) {
 893                dev_err(dev->udev->dev.parent,
 894                        "Invalid channel number (%d)\n", channel);
 895                return;
 896        }
 897
 898        priv = dev->nets[channel];
 899
 900        if (completion_done(&priv->start_comp) &&
 901            netif_queue_stopped(priv->netdev)) {
 902                netif_wake_queue(priv->netdev);
 903        } else {
 904                netif_start_queue(priv->netdev);
 905                complete(&priv->start_comp);
 906        }
 907}
 908
 909static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev,
 910                                       const struct kvaser_msg *msg)
 911{
 912        struct kvaser_usb_net_priv *priv;
 913        u8 channel = msg->u.simple.channel;
 914
 915        if (channel >= dev->nchannels) {
 916                dev_err(dev->udev->dev.parent,
 917                        "Invalid channel number (%d)\n", channel);
 918                return;
 919        }
 920
 921        priv = dev->nets[channel];
 922
 923        complete(&priv->stop_comp);
 924}
 925
 926static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
 927                                      const struct kvaser_msg *msg)
 928{
 929        switch (msg->id) {
 930        case CMD_START_CHIP_REPLY:
 931                kvaser_usb_start_chip_reply(dev, msg);
 932                break;
 933
 934        case CMD_STOP_CHIP_REPLY:
 935                kvaser_usb_stop_chip_reply(dev, msg);
 936                break;
 937
 938        case CMD_RX_STD_MESSAGE:
 939        case CMD_RX_EXT_MESSAGE:
 940        case CMD_LOG_MESSAGE:
 941                kvaser_usb_rx_can_msg(dev, msg);
 942                break;
 943
 944        case CMD_CHIP_STATE_EVENT:
 945        case CMD_CAN_ERROR_EVENT:
 946                kvaser_usb_rx_error(dev, msg);
 947                break;
 948
 949        case CMD_TX_ACKNOWLEDGE:
 950                kvaser_usb_tx_acknowledge(dev, msg);
 951                break;
 952
 953        default:
 954                dev_warn(dev->udev->dev.parent,
 955                         "Unhandled message (%d)\n", msg->id);
 956                break;
 957        }
 958}
 959
 960static void kvaser_usb_read_bulk_callback(struct urb *urb)
 961{
 962        struct kvaser_usb *dev = urb->context;
 963        struct kvaser_msg *msg;
 964        int pos = 0;
 965        int err, i;
 966
 967        switch (urb->status) {
 968        case 0:
 969                break;
 970        case -ENOENT:
 971        case -ESHUTDOWN:
 972                return;
 973        default:
 974                dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n",
 975                         urb->status);
 976                goto resubmit_urb;
 977        }
 978
 979        while (pos <= urb->actual_length - MSG_HEADER_LEN) {
 980                msg = urb->transfer_buffer + pos;
 981
 982                if (!msg->len)
 983                        break;
 984
 985                if (pos + msg->len > urb->actual_length) {
 986                        dev_err(dev->udev->dev.parent, "Format error\n");
 987                        break;
 988                }
 989
 990                kvaser_usb_handle_message(dev, msg);
 991
 992                pos += msg->len;
 993        }
 994
 995resubmit_urb:
 996        usb_fill_bulk_urb(urb, dev->udev,
 997                          usb_rcvbulkpipe(dev->udev,
 998                                          dev->bulk_in->bEndpointAddress),
 999                          urb->transfer_buffer, RX_BUFFER_SIZE,
1000                          kvaser_usb_read_bulk_callback, dev);
1001
1002        err = usb_submit_urb(urb, GFP_ATOMIC);
1003        if (err == -ENODEV) {
1004                for (i = 0; i < dev->nchannels; i++) {
1005                        if (!dev->nets[i])
1006                                continue;
1007
1008                        netif_device_detach(dev->nets[i]->netdev);
1009                }
1010        } else if (err) {
1011                dev_err(dev->udev->dev.parent,
1012                        "Failed resubmitting read bulk urb: %d\n", err);
1013        }
1014
1015        return;
1016}
1017
1018static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
1019{
1020        int i, err = 0;
1021
1022        if (dev->rxinitdone)
1023                return 0;
1024
1025        for (i = 0; i < MAX_RX_URBS; i++) {
1026                struct urb *urb = NULL;
1027                u8 *buf = NULL;
1028                dma_addr_t buf_dma;
1029
1030                urb = usb_alloc_urb(0, GFP_KERNEL);
1031                if (!urb) {
1032                        dev_warn(dev->udev->dev.parent,
1033                                 "No memory left for URBs\n");
1034                        err = -ENOMEM;
1035                        break;
1036                }
1037
1038                buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE,
1039                                         GFP_KERNEL, &buf_dma);
1040                if (!buf) {
1041                        dev_warn(dev->udev->dev.parent,
1042                                 "No memory left for USB buffer\n");
1043                        usb_free_urb(urb);
1044                        err = -ENOMEM;
1045                        break;
1046                }
1047
1048                usb_fill_bulk_urb(urb, dev->udev,
1049                                  usb_rcvbulkpipe(dev->udev,
1050                                          dev->bulk_in->bEndpointAddress),
1051                                  buf, RX_BUFFER_SIZE,
1052                                  kvaser_usb_read_bulk_callback,
1053                                  dev);
1054                urb->transfer_dma = buf_dma;
1055                urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1056                usb_anchor_urb(urb, &dev->rx_submitted);
1057
1058                err = usb_submit_urb(urb, GFP_KERNEL);
1059                if (err) {
1060                        usb_unanchor_urb(urb);
1061                        usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
1062                                          buf_dma);
1063                        usb_free_urb(urb);
1064                        break;
1065                }
1066
1067                dev->rxbuf[i] = buf;
1068                dev->rxbuf_dma[i] = buf_dma;
1069
1070                usb_free_urb(urb);
1071        }
1072
1073        if (i == 0) {
1074                dev_warn(dev->udev->dev.parent,
1075                         "Cannot setup read URBs, error %d\n", err);
1076                return err;
1077        } else if (i < MAX_RX_URBS) {
1078                dev_warn(dev->udev->dev.parent,
1079                         "RX performances may be slow\n");
1080        }
1081
1082        dev->rxinitdone = true;
1083
1084        return 0;
1085}
1086
1087static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1088{
1089        struct kvaser_msg *msg;
1090        int rc;
1091
1092        msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1093        if (!msg)
1094                return -ENOMEM;
1095
1096        msg->id = CMD_SET_CTRL_MODE;
1097        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode);
1098        msg->u.ctrl_mode.tid = 0xff;
1099        msg->u.ctrl_mode.channel = priv->channel;
1100
1101        if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1102                msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1103        else
1104                msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1105
1106        rc = kvaser_usb_send_msg(priv->dev, msg);
1107
1108        kfree(msg);
1109        return rc;
1110}
1111
1112static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv)
1113{
1114        int err;
1115
1116        init_completion(&priv->start_comp);
1117
1118        err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP,
1119                                         priv->channel);
1120        if (err)
1121                return err;
1122
1123        if (!wait_for_completion_timeout(&priv->start_comp,
1124                                         msecs_to_jiffies(START_TIMEOUT)))
1125                return -ETIMEDOUT;
1126
1127        return 0;
1128}
1129
1130static int kvaser_usb_open(struct net_device *netdev)
1131{
1132        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1133        struct kvaser_usb *dev = priv->dev;
1134        int err;
1135
1136        err = open_candev(netdev);
1137        if (err)
1138                return err;
1139
1140        err = kvaser_usb_setup_rx_urbs(dev);
1141        if (err)
1142                goto error;
1143
1144        err = kvaser_usb_set_opt_mode(priv);
1145        if (err)
1146                goto error;
1147
1148        err = kvaser_usb_start_chip(priv);
1149        if (err) {
1150                netdev_warn(netdev, "Cannot start device, error %d\n", err);
1151                goto error;
1152        }
1153
1154        priv->can.state = CAN_STATE_ERROR_ACTIVE;
1155
1156        return 0;
1157
1158error:
1159        close_candev(netdev);
1160        return err;
1161}
1162
1163static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1164{
1165        int i;
1166
1167        usb_kill_anchored_urbs(&dev->rx_submitted);
1168
1169        for (i = 0; i < MAX_RX_URBS; i++)
1170                usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
1171                                  dev->rxbuf[i],
1172                                  dev->rxbuf_dma[i]);
1173
1174        for (i = 0; i < MAX_NET_DEVICES; i++) {
1175                struct kvaser_usb_net_priv *priv = dev->nets[i];
1176
1177                if (priv)
1178                        kvaser_usb_unlink_tx_urbs(priv);
1179        }
1180}
1181
1182static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv)
1183{
1184        int err;
1185
1186        init_completion(&priv->stop_comp);
1187
1188        err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP,
1189                                         priv->channel);
1190        if (err)
1191                return err;
1192
1193        if (!wait_for_completion_timeout(&priv->stop_comp,
1194                                         msecs_to_jiffies(STOP_TIMEOUT)))
1195                return -ETIMEDOUT;
1196
1197        return 0;
1198}
1199
1200static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv)
1201{
1202        struct kvaser_msg *msg;
1203        int rc;
1204
1205        msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1206        if (!msg)
1207                return -ENOMEM;
1208
1209        msg->id = CMD_FLUSH_QUEUE;
1210        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue);
1211        msg->u.flush_queue.channel = priv->channel;
1212        msg->u.flush_queue.flags = 0x00;
1213
1214        rc = kvaser_usb_send_msg(priv->dev, msg);
1215
1216        kfree(msg);
1217        return rc;
1218}
1219
1220static int kvaser_usb_close(struct net_device *netdev)
1221{
1222        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1223        struct kvaser_usb *dev = priv->dev;
1224        int err;
1225
1226        netif_stop_queue(netdev);
1227
1228        err = kvaser_usb_flush_queue(priv);
1229        if (err)
1230                netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
1231
1232        if (kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel))
1233                netdev_warn(netdev, "Cannot reset card, error %d\n", err);
1234
1235        err = kvaser_usb_stop_chip(priv);
1236        if (err)
1237                netdev_warn(netdev, "Cannot stop device, error %d\n", err);
1238
1239        priv->can.state = CAN_STATE_STOPPED;
1240        close_candev(priv->netdev);
1241
1242        return 0;
1243}
1244
1245static void kvaser_usb_write_bulk_callback(struct urb *urb)
1246{
1247        struct kvaser_usb_tx_urb_context *context = urb->context;
1248        struct kvaser_usb_net_priv *priv;
1249        struct net_device *netdev;
1250
1251        if (WARN_ON(!context))
1252                return;
1253
1254        priv = context->priv;
1255        netdev = priv->netdev;
1256
1257        kfree(urb->transfer_buffer);
1258
1259        if (!netif_device_present(netdev))
1260                return;
1261
1262        if (urb->status)
1263                netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1264}
1265
1266static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1267                                         struct net_device *netdev)
1268{
1269        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1270        struct kvaser_usb *dev = priv->dev;
1271        struct net_device_stats *stats = &netdev->stats;
1272        struct can_frame *cf = (struct can_frame *)skb->data;
1273        struct kvaser_usb_tx_urb_context *context = NULL;
1274        struct urb *urb;
1275        void *buf;
1276        struct kvaser_msg *msg;
1277        int i, err;
1278        int ret = NETDEV_TX_OK;
1279
1280        if (can_dropped_invalid_skb(netdev, skb))
1281                return NETDEV_TX_OK;
1282
1283        urb = usb_alloc_urb(0, GFP_ATOMIC);
1284        if (!urb) {
1285                netdev_err(netdev, "No memory left for URBs\n");
1286                stats->tx_dropped++;
1287                goto nourbmem;
1288        }
1289
1290        buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1291        if (!buf) {
1292                stats->tx_dropped++;
1293                goto nobufmem;
1294        }
1295
1296        msg = buf;
1297        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
1298        msg->u.tx_can.flags = 0;
1299        msg->u.tx_can.channel = priv->channel;
1300
1301        if (cf->can_id & CAN_EFF_FLAG) {
1302                msg->id = CMD_TX_EXT_MESSAGE;
1303                msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1304                msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1305                msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1306                msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1307                msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1308        } else {
1309                msg->id = CMD_TX_STD_MESSAGE;
1310                msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1311                msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1312        }
1313
1314        msg->u.tx_can.msg[5] = cf->can_dlc;
1315        memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1316
1317        if (cf->can_id & CAN_RTR_FLAG)
1318                msg->u.tx_can.flags |= MSG_FLAG_REMOTE_FRAME;
1319
1320        for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) {
1321                if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
1322                        context = &priv->tx_contexts[i];
1323                        break;
1324                }
1325        }
1326
1327        if (!context) {
1328                netdev_warn(netdev, "cannot find free context\n");
1329                ret =  NETDEV_TX_BUSY;
1330                goto releasebuf;
1331        }
1332
1333        context->priv = priv;
1334        context->echo_index = i;
1335        context->dlc = cf->can_dlc;
1336
1337        msg->u.tx_can.tid = context->echo_index;
1338
1339        usb_fill_bulk_urb(urb, dev->udev,
1340                          usb_sndbulkpipe(dev->udev,
1341                                          dev->bulk_out->bEndpointAddress),
1342                          buf, msg->len,
1343                          kvaser_usb_write_bulk_callback, context);
1344        usb_anchor_urb(urb, &priv->tx_submitted);
1345
1346        can_put_echo_skb(skb, netdev, context->echo_index);
1347
1348        atomic_inc(&priv->active_tx_urbs);
1349
1350        if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
1351                netif_stop_queue(netdev);
1352
1353        err = usb_submit_urb(urb, GFP_ATOMIC);
1354        if (unlikely(err)) {
1355                can_free_echo_skb(netdev, context->echo_index);
1356
1357                skb = NULL; /* set to NULL to avoid double free in
1358                             * dev_kfree_skb(skb) */
1359
1360                atomic_dec(&priv->active_tx_urbs);
1361                usb_unanchor_urb(urb);
1362
1363                stats->tx_dropped++;
1364
1365                if (err == -ENODEV)
1366                        netif_device_detach(netdev);
1367                else
1368                        netdev_warn(netdev, "Failed tx_urb %d\n", err);
1369
1370                goto releasebuf;
1371        }
1372
1373        usb_free_urb(urb);
1374
1375        return NETDEV_TX_OK;
1376
1377releasebuf:
1378        kfree(buf);
1379nobufmem:
1380        usb_free_urb(urb);
1381nourbmem:
1382        dev_kfree_skb(skb);
1383        return ret;
1384}
1385
1386static const struct net_device_ops kvaser_usb_netdev_ops = {
1387        .ndo_open = kvaser_usb_open,
1388        .ndo_stop = kvaser_usb_close,
1389        .ndo_start_xmit = kvaser_usb_start_xmit,
1390};
1391
1392static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1393        .name = "kvaser_usb",
1394        .tseg1_min = KVASER_USB_TSEG1_MIN,
1395        .tseg1_max = KVASER_USB_TSEG1_MAX,
1396        .tseg2_min = KVASER_USB_TSEG2_MIN,
1397        .tseg2_max = KVASER_USB_TSEG2_MAX,
1398        .sjw_max = KVASER_USB_SJW_MAX,
1399        .brp_min = KVASER_USB_BRP_MIN,
1400        .brp_max = KVASER_USB_BRP_MAX,
1401        .brp_inc = KVASER_USB_BRP_INC,
1402};
1403
1404static int kvaser_usb_set_bittiming(struct net_device *netdev)
1405{
1406        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1407        struct can_bittiming *bt = &priv->can.bittiming;
1408        struct kvaser_usb *dev = priv->dev;
1409        struct kvaser_msg *msg;
1410        int rc;
1411
1412        msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1413        if (!msg)
1414                return -ENOMEM;
1415
1416        msg->id = CMD_SET_BUS_PARAMS;
1417        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1418        msg->u.busparams.channel = priv->channel;
1419        msg->u.busparams.tid = 0xff;
1420        msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1421        msg->u.busparams.sjw = bt->sjw;
1422        msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1423        msg->u.busparams.tseg2 = bt->phase_seg2;
1424
1425        if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1426                msg->u.busparams.no_samp = 3;
1427        else
1428                msg->u.busparams.no_samp = 1;
1429
1430        rc = kvaser_usb_send_msg(dev, msg);
1431
1432        kfree(msg);
1433        return rc;
1434}
1435
1436static int kvaser_usb_set_mode(struct net_device *netdev,
1437                               enum can_mode mode)
1438{
1439        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1440        int err;
1441
1442        switch (mode) {
1443        case CAN_MODE_START:
1444                err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1445                if (err)
1446                        return err;
1447                break;
1448        default:
1449                return -EOPNOTSUPP;
1450        }
1451
1452        return 0;
1453}
1454
1455static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1456                                       struct can_berr_counter *bec)
1457{
1458        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1459
1460        *bec = priv->bec;
1461
1462        return 0;
1463}
1464
1465static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1466{
1467        int i;
1468
1469        for (i = 0; i < dev->nchannels; i++) {
1470                if (!dev->nets[i])
1471                        continue;
1472
1473                unregister_netdev(dev->nets[i]->netdev);
1474        }
1475
1476        kvaser_usb_unlink_all_urbs(dev);
1477
1478        for (i = 0; i < dev->nchannels; i++) {
1479                if (!dev->nets[i])
1480                        continue;
1481
1482                free_candev(dev->nets[i]->netdev);
1483        }
1484}
1485
1486static int kvaser_usb_init_one(struct usb_interface *intf,
1487                               const struct usb_device_id *id, int channel)
1488{
1489        struct kvaser_usb *dev = usb_get_intfdata(intf);
1490        struct net_device *netdev;
1491        struct kvaser_usb_net_priv *priv;
1492        int i, err;
1493
1494        netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
1495        if (!netdev) {
1496                dev_err(&intf->dev, "Cannot alloc candev\n");
1497                return -ENOMEM;
1498        }
1499
1500        priv = netdev_priv(netdev);
1501
1502        init_completion(&priv->start_comp);
1503        init_completion(&priv->stop_comp);
1504
1505        init_usb_anchor(&priv->tx_submitted);
1506        atomic_set(&priv->active_tx_urbs, 0);
1507
1508        for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++)
1509                priv->tx_contexts[i].echo_index = MAX_TX_URBS;
1510
1511        priv->dev = dev;
1512        priv->netdev = netdev;
1513        priv->channel = channel;
1514
1515        priv->can.state = CAN_STATE_STOPPED;
1516        priv->can.clock.freq = CAN_USB_CLOCK;
1517        priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1518        priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1519        priv->can.do_set_mode = kvaser_usb_set_mode;
1520        if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1521                priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1522        priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1523        if (id->driver_info & KVASER_HAS_SILENT_MODE)
1524                priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1525
1526        netdev->flags |= IFF_ECHO;
1527
1528        netdev->netdev_ops = &kvaser_usb_netdev_ops;
1529
1530        SET_NETDEV_DEV(netdev, &intf->dev);
1531
1532        dev->nets[channel] = priv;
1533
1534        err = register_candev(netdev);
1535        if (err) {
1536                dev_err(&intf->dev, "Failed to register can device\n");
1537                free_candev(netdev);
1538                dev->nets[channel] = NULL;
1539                return err;
1540        }
1541
1542        netdev_dbg(netdev, "device registered\n");
1543
1544        return 0;
1545}
1546
1547static void kvaser_usb_get_endpoints(const struct usb_interface *intf,
1548                                     struct usb_endpoint_descriptor **in,
1549                                     struct usb_endpoint_descriptor **out)
1550{
1551        const struct usb_host_interface *iface_desc;
1552        struct usb_endpoint_descriptor *endpoint;
1553        int i;
1554
1555        iface_desc = &intf->altsetting[0];
1556
1557        for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1558                endpoint = &iface_desc->endpoint[i].desc;
1559
1560                if (usb_endpoint_is_bulk_in(endpoint))
1561                        *in = endpoint;
1562
1563                if (usb_endpoint_is_bulk_out(endpoint))
1564                        *out = endpoint;
1565        }
1566}
1567
1568static int kvaser_usb_probe(struct usb_interface *intf,
1569                            const struct usb_device_id *id)
1570{
1571        struct kvaser_usb *dev;
1572        int err = -ENOMEM;
1573        int i;
1574
1575        dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1576        if (!dev)
1577                return -ENOMEM;
1578
1579        kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
1580        if (!dev->bulk_in || !dev->bulk_out) {
1581                dev_err(&intf->dev, "Cannot get usb endpoint(s)");
1582                return err;
1583        }
1584
1585        dev->udev = interface_to_usbdev(intf);
1586
1587        init_usb_anchor(&dev->rx_submitted);
1588
1589        usb_set_intfdata(intf, dev);
1590
1591        for (i = 0; i < MAX_NET_DEVICES; i++)
1592                kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, i);
1593
1594        err = kvaser_usb_get_software_info(dev);
1595        if (err) {
1596                dev_err(&intf->dev,
1597                        "Cannot get software infos, error %d\n", err);
1598                return err;
1599        }
1600
1601        err = kvaser_usb_get_card_info(dev);
1602        if (err) {
1603                dev_err(&intf->dev,
1604                        "Cannot get card infos, error %d\n", err);
1605                return err;
1606        }
1607
1608        dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
1609                ((dev->fw_version >> 24) & 0xff),
1610                ((dev->fw_version >> 16) & 0xff),
1611                (dev->fw_version & 0xffff));
1612
1613        for (i = 0; i < dev->nchannels; i++) {
1614                err = kvaser_usb_init_one(intf, id, i);
1615                if (err) {
1616                        kvaser_usb_remove_interfaces(dev);
1617                        return err;
1618                }
1619        }
1620
1621        return 0;
1622}
1623
1624static void kvaser_usb_disconnect(struct usb_interface *intf)
1625{
1626        struct kvaser_usb *dev = usb_get_intfdata(intf);
1627
1628        usb_set_intfdata(intf, NULL);
1629
1630        if (!dev)
1631                return;
1632
1633        kvaser_usb_remove_interfaces(dev);
1634}
1635
1636static struct usb_driver kvaser_usb_driver = {
1637        .name = "kvaser_usb",
1638        .probe = kvaser_usb_probe,
1639        .disconnect = kvaser_usb_disconnect,
1640        .id_table = kvaser_usb_table,
1641};
1642
1643module_usb_driver(kvaser_usb_driver);
1644
1645MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1646MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1647MODULE_LICENSE("GPL v2");
1648