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