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#define USB_LEAF_LITE_V2_PRODUCT_ID     288
  57#define USB_MINI_PCIE_HS_PRODUCT_ID     289
  58
  59/* USB devices features */
  60#define KVASER_HAS_SILENT_MODE          BIT(0)
  61#define KVASER_HAS_TXRX_ERRORS          BIT(1)
  62
  63/* Message header size */
  64#define MSG_HEADER_LEN                  2
  65
  66/* Can message flags */
  67#define MSG_FLAG_ERROR_FRAME            BIT(0)
  68#define MSG_FLAG_OVERRUN                BIT(1)
  69#define MSG_FLAG_NERR                   BIT(2)
  70#define MSG_FLAG_WAKEUP                 BIT(3)
  71#define MSG_FLAG_REMOTE_FRAME           BIT(4)
  72#define MSG_FLAG_RESERVED               BIT(5)
  73#define MSG_FLAG_TX_ACK                 BIT(6)
  74#define MSG_FLAG_TX_REQUEST             BIT(7)
  75
  76/* Can states */
  77#define M16C_STATE_BUS_RESET            BIT(0)
  78#define M16C_STATE_BUS_ERROR            BIT(4)
  79#define M16C_STATE_BUS_PASSIVE          BIT(5)
  80#define M16C_STATE_BUS_OFF              BIT(6)
  81
  82/* Can msg ids */
  83#define CMD_RX_STD_MESSAGE              12
  84#define CMD_TX_STD_MESSAGE              13
  85#define CMD_RX_EXT_MESSAGE              14
  86#define CMD_TX_EXT_MESSAGE              15
  87#define CMD_SET_BUS_PARAMS              16
  88#define CMD_GET_BUS_PARAMS              17
  89#define CMD_GET_BUS_PARAMS_REPLY        18
  90#define CMD_GET_CHIP_STATE              19
  91#define CMD_CHIP_STATE_EVENT            20
  92#define CMD_SET_CTRL_MODE               21
  93#define CMD_GET_CTRL_MODE               22
  94#define CMD_GET_CTRL_MODE_REPLY         23
  95#define CMD_RESET_CHIP                  24
  96#define CMD_RESET_CARD                  25
  97#define CMD_START_CHIP                  26
  98#define CMD_START_CHIP_REPLY            27
  99#define CMD_STOP_CHIP                   28
 100#define CMD_STOP_CHIP_REPLY             29
 101#define CMD_GET_CARD_INFO2              32
 102#define CMD_GET_CARD_INFO               34
 103#define CMD_GET_CARD_INFO_REPLY         35
 104#define CMD_GET_SOFTWARE_INFO           38
 105#define CMD_GET_SOFTWARE_INFO_REPLY     39
 106#define CMD_ERROR_EVENT                 45
 107#define CMD_FLUSH_QUEUE                 48
 108#define CMD_RESET_ERROR_COUNTER         49
 109#define CMD_TX_ACKNOWLEDGE              50
 110#define CMD_CAN_ERROR_EVENT             51
 111#define CMD_USB_THROTTLE                77
 112#define CMD_LOG_MESSAGE                 106
 113
 114/* error factors */
 115#define M16C_EF_ACKE                    BIT(0)
 116#define M16C_EF_CRCE                    BIT(1)
 117#define M16C_EF_FORME                   BIT(2)
 118#define M16C_EF_STFE                    BIT(3)
 119#define M16C_EF_BITE0                   BIT(4)
 120#define M16C_EF_BITE1                   BIT(5)
 121#define M16C_EF_RCVE                    BIT(6)
 122#define M16C_EF_TRE                     BIT(7)
 123
 124/* bittiming parameters */
 125#define KVASER_USB_TSEG1_MIN            1
 126#define KVASER_USB_TSEG1_MAX            16
 127#define KVASER_USB_TSEG2_MIN            1
 128#define KVASER_USB_TSEG2_MAX            8
 129#define KVASER_USB_SJW_MAX              4
 130#define KVASER_USB_BRP_MIN              1
 131#define KVASER_USB_BRP_MAX              64
 132#define KVASER_USB_BRP_INC              1
 133
 134/* ctrl modes */
 135#define KVASER_CTRL_MODE_NORMAL         1
 136#define KVASER_CTRL_MODE_SILENT         2
 137#define KVASER_CTRL_MODE_SELFRECEPTION  3
 138#define KVASER_CTRL_MODE_OFF            4
 139
 140/* log message */
 141#define KVASER_EXTENDED_FRAME           BIT(31)
 142
 143struct kvaser_msg_simple {
 144        u8 tid;
 145        u8 channel;
 146} __packed;
 147
 148struct kvaser_msg_cardinfo {
 149        u8 tid;
 150        u8 nchannels;
 151        __le32 serial_number;
 152        __le32 padding;
 153        __le32 clock_resolution;
 154        __le32 mfgdate;
 155        u8 ean[8];
 156        u8 hw_revision;
 157        u8 usb_hs_mode;
 158        __le16 padding2;
 159} __packed;
 160
 161struct kvaser_msg_cardinfo2 {
 162        u8 tid;
 163        u8 channel;
 164        u8 pcb_id[24];
 165        __le32 oem_unlock_code;
 166} __packed;
 167
 168struct kvaser_msg_softinfo {
 169        u8 tid;
 170        u8 channel;
 171        __le32 sw_options;
 172        __le32 fw_version;
 173        __le16 max_outstanding_tx;
 174        __le16 padding[9];
 175} __packed;
 176
 177struct kvaser_msg_busparams {
 178        u8 tid;
 179        u8 channel;
 180        __le32 bitrate;
 181        u8 tseg1;
 182        u8 tseg2;
 183        u8 sjw;
 184        u8 no_samp;
 185} __packed;
 186
 187struct kvaser_msg_tx_can {
 188        u8 channel;
 189        u8 tid;
 190        u8 msg[14];
 191        u8 padding;
 192        u8 flags;
 193} __packed;
 194
 195struct kvaser_msg_rx_can {
 196        u8 channel;
 197        u8 flag;
 198        __le16 time[3];
 199        u8 msg[14];
 200} __packed;
 201
 202struct kvaser_msg_chip_state_event {
 203        u8 tid;
 204        u8 channel;
 205        __le16 time[3];
 206        u8 tx_errors_count;
 207        u8 rx_errors_count;
 208        u8 status;
 209        u8 padding[3];
 210} __packed;
 211
 212struct kvaser_msg_tx_acknowledge {
 213        u8 channel;
 214        u8 tid;
 215        __le16 time[3];
 216        u8 flags;
 217        u8 time_offset;
 218} __packed;
 219
 220struct kvaser_msg_error_event {
 221        u8 tid;
 222        u8 flags;
 223        __le16 time[3];
 224        u8 channel;
 225        u8 padding;
 226        u8 tx_errors_count;
 227        u8 rx_errors_count;
 228        u8 status;
 229        u8 error_factor;
 230} __packed;
 231
 232struct kvaser_msg_ctrl_mode {
 233        u8 tid;
 234        u8 channel;
 235        u8 ctrl_mode;
 236        u8 padding[3];
 237} __packed;
 238
 239struct kvaser_msg_flush_queue {
 240        u8 tid;
 241        u8 channel;
 242        u8 flags;
 243        u8 padding[3];
 244} __packed;
 245
 246struct kvaser_msg_log_message {
 247        u8 channel;
 248        u8 flags;
 249        __le16 time[3];
 250        u8 dlc;
 251        u8 time_offset;
 252        __le32 id;
 253        u8 data[8];
 254} __packed;
 255
 256struct kvaser_msg {
 257        u8 len;
 258        u8 id;
 259        union   {
 260                struct kvaser_msg_simple simple;
 261                struct kvaser_msg_cardinfo cardinfo;
 262                struct kvaser_msg_cardinfo2 cardinfo2;
 263                struct kvaser_msg_softinfo softinfo;
 264                struct kvaser_msg_busparams busparams;
 265                struct kvaser_msg_tx_can tx_can;
 266                struct kvaser_msg_rx_can rx_can;
 267                struct kvaser_msg_chip_state_event chip_state_event;
 268                struct kvaser_msg_tx_acknowledge tx_acknowledge;
 269                struct kvaser_msg_error_event error_event;
 270                struct kvaser_msg_ctrl_mode ctrl_mode;
 271                struct kvaser_msg_flush_queue flush_queue;
 272                struct kvaser_msg_log_message log_message;
 273        } u;
 274} __packed;
 275
 276struct kvaser_usb_tx_urb_context {
 277        struct kvaser_usb_net_priv *priv;
 278        u32 echo_index;
 279        int dlc;
 280};
 281
 282struct kvaser_usb {
 283        struct usb_device *udev;
 284        struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
 285
 286        struct usb_endpoint_descriptor *bulk_in, *bulk_out;
 287        struct usb_anchor rx_submitted;
 288
 289        u32 fw_version;
 290        unsigned int nchannels;
 291
 292        bool rxinitdone;
 293        void *rxbuf[MAX_RX_URBS];
 294        dma_addr_t rxbuf_dma[MAX_RX_URBS];
 295};
 296
 297struct kvaser_usb_net_priv {
 298        struct can_priv can;
 299
 300        atomic_t active_tx_urbs;
 301        struct usb_anchor tx_submitted;
 302        struct kvaser_usb_tx_urb_context tx_contexts[MAX_TX_URBS];
 303
 304        struct completion start_comp, stop_comp;
 305
 306        struct kvaser_usb *dev;
 307        struct net_device *netdev;
 308        int channel;
 309
 310        struct can_berr_counter bec;
 311};
 312
 313static const struct usb_device_id kvaser_usb_table[] = {
 314        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
 315        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
 316        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
 317                .driver_info = KVASER_HAS_TXRX_ERRORS |
 318                               KVASER_HAS_SILENT_MODE },
 319        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
 320                .driver_info = KVASER_HAS_TXRX_ERRORS |
 321                               KVASER_HAS_SILENT_MODE },
 322        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
 323                .driver_info = KVASER_HAS_TXRX_ERRORS |
 324                               KVASER_HAS_SILENT_MODE },
 325        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
 326                .driver_info = KVASER_HAS_TXRX_ERRORS |
 327                               KVASER_HAS_SILENT_MODE },
 328        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
 329                .driver_info = KVASER_HAS_TXRX_ERRORS |
 330                               KVASER_HAS_SILENT_MODE },
 331        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
 332                .driver_info = KVASER_HAS_TXRX_ERRORS |
 333                               KVASER_HAS_SILENT_MODE },
 334        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
 335                .driver_info = KVASER_HAS_TXRX_ERRORS |
 336                               KVASER_HAS_SILENT_MODE },
 337        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
 338                .driver_info = KVASER_HAS_TXRX_ERRORS |
 339                               KVASER_HAS_SILENT_MODE },
 340        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
 341                .driver_info = KVASER_HAS_TXRX_ERRORS |
 342                               KVASER_HAS_SILENT_MODE },
 343        { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
 344                .driver_info = KVASER_HAS_TXRX_ERRORS },
 345        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
 346        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
 347                .driver_info = KVASER_HAS_TXRX_ERRORS |
 348                               KVASER_HAS_SILENT_MODE },
 349        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
 350                .driver_info = KVASER_HAS_TXRX_ERRORS },
 351        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
 352                .driver_info = KVASER_HAS_TXRX_ERRORS },
 353        { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
 354                .driver_info = KVASER_HAS_TXRX_ERRORS },
 355        { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
 356                .driver_info = KVASER_HAS_TXRX_ERRORS },
 357        { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
 358                .driver_info = KVASER_HAS_TXRX_ERRORS },
 359        { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
 360                .driver_info = KVASER_HAS_TXRX_ERRORS },
 361        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) },
 362        { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) },
 363        { }
 364};
 365MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
 366
 367static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
 368                                      struct kvaser_msg *msg)
 369{
 370        int actual_len;
 371
 372        return usb_bulk_msg(dev->udev,
 373                            usb_sndbulkpipe(dev->udev,
 374                                        dev->bulk_out->bEndpointAddress),
 375                            msg, msg->len, &actual_len,
 376                            USB_SEND_TIMEOUT);
 377}
 378
 379static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
 380                               struct kvaser_msg *msg)
 381{
 382        struct kvaser_msg *tmp;
 383        void *buf;
 384        int actual_len;
 385        int err;
 386        int pos;
 387        unsigned long to = jiffies + msecs_to_jiffies(USB_RECV_TIMEOUT);
 388
 389        buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
 390        if (!buf)
 391                return -ENOMEM;
 392
 393        do {
 394                err = usb_bulk_msg(dev->udev,
 395                                   usb_rcvbulkpipe(dev->udev,
 396                                        dev->bulk_in->bEndpointAddress),
 397                                   buf, RX_BUFFER_SIZE, &actual_len,
 398                                   USB_RECV_TIMEOUT);
 399                if (err < 0)
 400                        goto end;
 401
 402                pos = 0;
 403                while (pos <= actual_len - MSG_HEADER_LEN) {
 404                        tmp = buf + pos;
 405
 406                        if (!tmp->len)
 407                                break;
 408
 409                        if (pos + tmp->len > actual_len) {
 410                                dev_err(dev->udev->dev.parent,
 411                                        "Format error\n");
 412                                break;
 413                        }
 414
 415                        if (tmp->id == id) {
 416                                memcpy(msg, tmp, tmp->len);
 417                                goto end;
 418                        }
 419
 420                        pos += tmp->len;
 421                }
 422        } while (time_before(jiffies, to));
 423
 424        err = -EINVAL;
 425
 426end:
 427        kfree(buf);
 428
 429        return err;
 430}
 431
 432static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
 433                                      u8 msg_id, int channel)
 434{
 435        struct kvaser_msg *msg;
 436        int rc;
 437
 438        msg = kmalloc(sizeof(*msg), GFP_KERNEL);
 439        if (!msg)
 440                return -ENOMEM;
 441
 442        msg->id = msg_id;
 443        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
 444        msg->u.simple.channel = channel;
 445        msg->u.simple.tid = 0xff;
 446
 447        rc = kvaser_usb_send_msg(dev, msg);
 448
 449        kfree(msg);
 450        return rc;
 451}
 452
 453static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
 454{
 455        struct kvaser_msg msg;
 456        int err;
 457
 458        err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
 459        if (err)
 460                return err;
 461
 462        err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
 463        if (err)
 464                return err;
 465
 466        dev->fw_version = le32_to_cpu(msg.u.softinfo.fw_version);
 467
 468        return 0;
 469}
 470
 471static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
 472{
 473        struct kvaser_msg msg;
 474        int err;
 475
 476        err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
 477        if (err)
 478                return err;
 479
 480        err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
 481        if (err)
 482                return err;
 483
 484        dev->nchannels = msg.u.cardinfo.nchannels;
 485        if (dev->nchannels > MAX_NET_DEVICES)
 486                return -EINVAL;
 487
 488        return 0;
 489}
 490
 491static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
 492                                      const struct kvaser_msg *msg)
 493{
 494        struct net_device_stats *stats;
 495        struct kvaser_usb_tx_urb_context *context;
 496        struct kvaser_usb_net_priv *priv;
 497        struct sk_buff *skb;
 498        struct can_frame *cf;
 499        u8 channel = msg->u.tx_acknowledge.channel;
 500        u8 tid = msg->u.tx_acknowledge.tid;
 501
 502        if (channel >= dev->nchannels) {
 503                dev_err(dev->udev->dev.parent,
 504                        "Invalid channel number (%d)\n", channel);
 505                return;
 506        }
 507
 508        priv = dev->nets[channel];
 509
 510        if (!netif_device_present(priv->netdev))
 511                return;
 512
 513        stats = &priv->netdev->stats;
 514
 515        context = &priv->tx_contexts[tid % MAX_TX_URBS];
 516
 517        /* Sometimes the state change doesn't come after a bus-off event */
 518        if (priv->can.restart_ms &&
 519            (priv->can.state >= CAN_STATE_BUS_OFF)) {
 520                skb = alloc_can_err_skb(priv->netdev, &cf);
 521                if (skb) {
 522                        cf->can_id |= CAN_ERR_RESTARTED;
 523
 524                        stats->rx_packets++;
 525                        stats->rx_bytes += cf->can_dlc;
 526                        netif_rx(skb);
 527                } else {
 528                        netdev_err(priv->netdev,
 529                                   "No memory left for err_skb\n");
 530                }
 531
 532                priv->can.can_stats.restarts++;
 533                netif_carrier_on(priv->netdev);
 534
 535                priv->can.state = CAN_STATE_ERROR_ACTIVE;
 536        }
 537
 538        stats->tx_packets++;
 539        stats->tx_bytes += context->dlc;
 540        can_get_echo_skb(priv->netdev, context->echo_index);
 541
 542        context->echo_index = MAX_TX_URBS;
 543        atomic_dec(&priv->active_tx_urbs);
 544
 545        netif_wake_queue(priv->netdev);
 546}
 547
 548static void kvaser_usb_simple_msg_callback(struct urb *urb)
 549{
 550        struct net_device *netdev = urb->context;
 551
 552        kfree(urb->transfer_buffer);
 553
 554        if (urb->status)
 555                netdev_warn(netdev, "urb status received: %d\n",
 556                            urb->status);
 557}
 558
 559static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
 560                                       u8 msg_id)
 561{
 562        struct kvaser_usb *dev = priv->dev;
 563        struct net_device *netdev = priv->netdev;
 564        struct kvaser_msg *msg;
 565        struct urb *urb;
 566        void *buf;
 567        int err;
 568
 569        urb = usb_alloc_urb(0, GFP_ATOMIC);
 570        if (!urb) {
 571                netdev_err(netdev, "No memory left for URBs\n");
 572                return -ENOMEM;
 573        }
 574
 575        buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
 576        if (!buf) {
 577                usb_free_urb(urb);
 578                return -ENOMEM;
 579        }
 580
 581        msg = (struct kvaser_msg *)buf;
 582        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
 583        msg->id = msg_id;
 584        msg->u.simple.channel = priv->channel;
 585
 586        usb_fill_bulk_urb(urb, dev->udev,
 587                          usb_sndbulkpipe(dev->udev,
 588                                          dev->bulk_out->bEndpointAddress),
 589                          buf, msg->len,
 590                          kvaser_usb_simple_msg_callback, netdev);
 591        usb_anchor_urb(urb, &priv->tx_submitted);
 592
 593        err = usb_submit_urb(urb, GFP_ATOMIC);
 594        if (err) {
 595                netdev_err(netdev, "Error transmitting URB\n");
 596                usb_unanchor_urb(urb);
 597                usb_free_urb(urb);
 598                kfree(buf);
 599                return err;
 600        }
 601
 602        usb_free_urb(urb);
 603
 604        return 0;
 605}
 606
 607static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
 608{
 609        int i;
 610
 611        usb_kill_anchored_urbs(&priv->tx_submitted);
 612        atomic_set(&priv->active_tx_urbs, 0);
 613
 614        for (i = 0; i < MAX_TX_URBS; i++)
 615                priv->tx_contexts[i].echo_index = MAX_TX_URBS;
 616}
 617
 618static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
 619                                const struct kvaser_msg *msg)
 620{
 621        struct can_frame *cf;
 622        struct sk_buff *skb;
 623        struct net_device_stats *stats;
 624        struct kvaser_usb_net_priv *priv;
 625        unsigned int new_state;
 626        u8 channel, status, txerr, rxerr, error_factor;
 627
 628        switch (msg->id) {
 629        case CMD_CAN_ERROR_EVENT:
 630                channel = msg->u.error_event.channel;
 631                status =  msg->u.error_event.status;
 632                txerr = msg->u.error_event.tx_errors_count;
 633                rxerr = msg->u.error_event.rx_errors_count;
 634                error_factor = msg->u.error_event.error_factor;
 635                break;
 636        case CMD_LOG_MESSAGE:
 637                channel = msg->u.log_message.channel;
 638                status = msg->u.log_message.data[0];
 639                txerr = msg->u.log_message.data[2];
 640                rxerr = msg->u.log_message.data[3];
 641                error_factor = msg->u.log_message.data[1];
 642                break;
 643        case CMD_CHIP_STATE_EVENT:
 644                channel = msg->u.chip_state_event.channel;
 645                status =  msg->u.chip_state_event.status;
 646                txerr = msg->u.chip_state_event.tx_errors_count;
 647                rxerr = msg->u.chip_state_event.rx_errors_count;
 648                error_factor = 0;
 649                break;
 650        default:
 651                dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
 652                        msg->id);
 653                return;
 654        }
 655
 656        if (channel >= dev->nchannels) {
 657                dev_err(dev->udev->dev.parent,
 658                        "Invalid channel number (%d)\n", channel);
 659                return;
 660        }
 661
 662        priv = dev->nets[channel];
 663        stats = &priv->netdev->stats;
 664
 665        skb = alloc_can_err_skb(priv->netdev, &cf);
 666        if (!skb) {
 667                stats->rx_dropped++;
 668                return;
 669        }
 670
 671        new_state = priv->can.state;
 672
 673        netdev_dbg(priv->netdev, "Error status: 0x%02x\n", status);
 674
 675        if (status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) {
 676                cf->can_id |= CAN_ERR_BUSOFF;
 677
 678                priv->can.can_stats.bus_off++;
 679                if (!priv->can.restart_ms)
 680                        kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
 681
 682                netif_carrier_off(priv->netdev);
 683
 684                new_state = CAN_STATE_BUS_OFF;
 685        } else if (status & M16C_STATE_BUS_PASSIVE) {
 686                if (priv->can.state != CAN_STATE_ERROR_PASSIVE) {
 687                        cf->can_id |= CAN_ERR_CRTL;
 688
 689                        if (txerr || rxerr)
 690                                cf->data[1] = (txerr > rxerr)
 691                                                ? CAN_ERR_CRTL_TX_PASSIVE
 692                                                : CAN_ERR_CRTL_RX_PASSIVE;
 693                        else
 694                                cf->data[1] = CAN_ERR_CRTL_TX_PASSIVE |
 695                                              CAN_ERR_CRTL_RX_PASSIVE;
 696
 697                        priv->can.can_stats.error_passive++;
 698                }
 699
 700                new_state = CAN_STATE_ERROR_PASSIVE;
 701        } else if (status & M16C_STATE_BUS_ERROR) {
 702                if ((priv->can.state < CAN_STATE_ERROR_WARNING) &&
 703                    ((txerr >= 96) || (rxerr >= 96))) {
 704                        cf->can_id |= CAN_ERR_CRTL;
 705                        cf->data[1] = (txerr > rxerr)
 706                                        ? CAN_ERR_CRTL_TX_WARNING
 707                                        : CAN_ERR_CRTL_RX_WARNING;
 708
 709                        priv->can.can_stats.error_warning++;
 710                        new_state = CAN_STATE_ERROR_WARNING;
 711                } else if ((priv->can.state > CAN_STATE_ERROR_ACTIVE) &&
 712                           ((txerr < 96) && (rxerr < 96))) {
 713                        cf->can_id |= CAN_ERR_PROT;
 714                        cf->data[2] = CAN_ERR_PROT_ACTIVE;
 715
 716                        new_state = CAN_STATE_ERROR_ACTIVE;
 717                }
 718        }
 719
 720        if (!status) {
 721                cf->can_id |= CAN_ERR_PROT;
 722                cf->data[2] = CAN_ERR_PROT_ACTIVE;
 723
 724                new_state = CAN_STATE_ERROR_ACTIVE;
 725        }
 726
 727        if (priv->can.restart_ms &&
 728            (priv->can.state >= CAN_STATE_BUS_OFF) &&
 729            (new_state < CAN_STATE_BUS_OFF)) {
 730                cf->can_id |= CAN_ERR_RESTARTED;
 731                netif_carrier_on(priv->netdev);
 732
 733                priv->can.can_stats.restarts++;
 734        }
 735
 736        if (error_factor) {
 737                priv->can.can_stats.bus_error++;
 738                stats->rx_errors++;
 739
 740                cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
 741
 742                if (error_factor & M16C_EF_ACKE)
 743                        cf->data[3] |= (CAN_ERR_PROT_LOC_ACK);
 744                if (error_factor & M16C_EF_CRCE)
 745                        cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
 746                                        CAN_ERR_PROT_LOC_CRC_DEL);
 747                if (error_factor & M16C_EF_FORME)
 748                        cf->data[2] |= CAN_ERR_PROT_FORM;
 749                if (error_factor & M16C_EF_STFE)
 750                        cf->data[2] |= CAN_ERR_PROT_STUFF;
 751                if (error_factor & M16C_EF_BITE0)
 752                        cf->data[2] |= CAN_ERR_PROT_BIT0;
 753                if (error_factor & M16C_EF_BITE1)
 754                        cf->data[2] |= CAN_ERR_PROT_BIT1;
 755                if (error_factor & M16C_EF_TRE)
 756                        cf->data[2] |= CAN_ERR_PROT_TX;
 757        }
 758
 759        cf->data[6] = txerr;
 760        cf->data[7] = rxerr;
 761
 762        priv->bec.txerr = txerr;
 763        priv->bec.rxerr = rxerr;
 764
 765        priv->can.state = new_state;
 766
 767        stats->rx_packets++;
 768        stats->rx_bytes += cf->can_dlc;
 769        netif_rx(skb);
 770}
 771
 772static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
 773                                  const struct kvaser_msg *msg)
 774{
 775        struct can_frame *cf;
 776        struct sk_buff *skb;
 777        struct net_device_stats *stats = &priv->netdev->stats;
 778
 779        if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
 780                                         MSG_FLAG_NERR)) {
 781                netdev_err(priv->netdev, "Unknow error (flags: 0x%02x)\n",
 782                           msg->u.rx_can.flag);
 783
 784                stats->rx_errors++;
 785                return;
 786        }
 787
 788        if (msg->u.rx_can.flag & MSG_FLAG_OVERRUN) {
 789                skb = alloc_can_err_skb(priv->netdev, &cf);
 790                if (!skb) {
 791                        stats->rx_dropped++;
 792                        return;
 793                }
 794
 795                cf->can_id |= CAN_ERR_CRTL;
 796                cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
 797
 798                stats->rx_over_errors++;
 799                stats->rx_errors++;
 800
 801                stats->rx_packets++;
 802                stats->rx_bytes += cf->can_dlc;
 803                netif_rx(skb);
 804        }
 805}
 806
 807static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
 808                                  const struct kvaser_msg *msg)
 809{
 810        struct kvaser_usb_net_priv *priv;
 811        struct can_frame *cf;
 812        struct sk_buff *skb;
 813        struct net_device_stats *stats;
 814        u8 channel = msg->u.rx_can.channel;
 815
 816        if (channel >= dev->nchannels) {
 817                dev_err(dev->udev->dev.parent,
 818                        "Invalid channel number (%d)\n", channel);
 819                return;
 820        }
 821
 822        priv = dev->nets[channel];
 823        stats = &priv->netdev->stats;
 824
 825        if ((msg->u.rx_can.flag & MSG_FLAG_ERROR_FRAME) &&
 826            (msg->id == CMD_LOG_MESSAGE)) {
 827                kvaser_usb_rx_error(dev, msg);
 828                return;
 829        } else if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
 830                                         MSG_FLAG_NERR |
 831                                         MSG_FLAG_OVERRUN)) {
 832                kvaser_usb_rx_can_err(priv, msg);
 833                return;
 834        } else if (msg->u.rx_can.flag & ~MSG_FLAG_REMOTE_FRAME) {
 835                netdev_warn(priv->netdev,
 836                            "Unhandled frame (flags: 0x%02x)",
 837                            msg->u.rx_can.flag);
 838                return;
 839        }
 840
 841        skb = alloc_can_skb(priv->netdev, &cf);
 842        if (!skb) {
 843                stats->tx_dropped++;
 844                return;
 845        }
 846
 847        if (msg->id == CMD_LOG_MESSAGE) {
 848                cf->can_id = le32_to_cpu(msg->u.log_message.id);
 849                if (cf->can_id & KVASER_EXTENDED_FRAME)
 850                        cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG;
 851                else
 852                        cf->can_id &= CAN_SFF_MASK;
 853
 854                cf->can_dlc = get_can_dlc(msg->u.log_message.dlc);
 855
 856                if (msg->u.log_message.flags & MSG_FLAG_REMOTE_FRAME)
 857                        cf->can_id |= CAN_RTR_FLAG;
 858                else
 859                        memcpy(cf->data, &msg->u.log_message.data,
 860                               cf->can_dlc);
 861        } else {
 862                cf->can_id = ((msg->u.rx_can.msg[0] & 0x1f) << 6) |
 863                             (msg->u.rx_can.msg[1] & 0x3f);
 864
 865                if (msg->id == CMD_RX_EXT_MESSAGE) {
 866                        cf->can_id <<= 18;
 867                        cf->can_id |= ((msg->u.rx_can.msg[2] & 0x0f) << 14) |
 868                                      ((msg->u.rx_can.msg[3] & 0xff) << 6) |
 869                                      (msg->u.rx_can.msg[4] & 0x3f);
 870                        cf->can_id |= CAN_EFF_FLAG;
 871                }
 872
 873                cf->can_dlc = get_can_dlc(msg->u.rx_can.msg[5]);
 874
 875                if (msg->u.rx_can.flag & MSG_FLAG_REMOTE_FRAME)
 876                        cf->can_id |= CAN_RTR_FLAG;
 877                else
 878                        memcpy(cf->data, &msg->u.rx_can.msg[6],
 879                               cf->can_dlc);
 880        }
 881
 882        stats->rx_packets++;
 883        stats->rx_bytes += cf->can_dlc;
 884        netif_rx(skb);
 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        /* reset tx contexts */
1241        kvaser_usb_unlink_tx_urbs(priv);
1242
1243        priv->can.state = CAN_STATE_STOPPED;
1244        close_candev(priv->netdev);
1245
1246        return 0;
1247}
1248
1249static void kvaser_usb_write_bulk_callback(struct urb *urb)
1250{
1251        struct kvaser_usb_tx_urb_context *context = urb->context;
1252        struct kvaser_usb_net_priv *priv;
1253        struct net_device *netdev;
1254
1255        if (WARN_ON(!context))
1256                return;
1257
1258        priv = context->priv;
1259        netdev = priv->netdev;
1260
1261        kfree(urb->transfer_buffer);
1262
1263        if (!netif_device_present(netdev))
1264                return;
1265
1266        if (urb->status)
1267                netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1268}
1269
1270static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1271                                         struct net_device *netdev)
1272{
1273        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1274        struct kvaser_usb *dev = priv->dev;
1275        struct net_device_stats *stats = &netdev->stats;
1276        struct can_frame *cf = (struct can_frame *)skb->data;
1277        struct kvaser_usb_tx_urb_context *context = NULL;
1278        struct urb *urb;
1279        void *buf;
1280        struct kvaser_msg *msg;
1281        int i, err;
1282        int ret = NETDEV_TX_OK;
1283
1284        if (can_dropped_invalid_skb(netdev, skb))
1285                return NETDEV_TX_OK;
1286
1287        urb = usb_alloc_urb(0, GFP_ATOMIC);
1288        if (!urb) {
1289                netdev_err(netdev, "No memory left for URBs\n");
1290                stats->tx_dropped++;
1291                dev_kfree_skb(skb);
1292                return NETDEV_TX_OK;
1293        }
1294
1295        buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1296        if (!buf) {
1297                stats->tx_dropped++;
1298                dev_kfree_skb(skb);
1299                goto nobufmem;
1300        }
1301
1302        msg = buf;
1303        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
1304        msg->u.tx_can.flags = 0;
1305        msg->u.tx_can.channel = priv->channel;
1306
1307        if (cf->can_id & CAN_EFF_FLAG) {
1308                msg->id = CMD_TX_EXT_MESSAGE;
1309                msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1310                msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1311                msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1312                msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1313                msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1314        } else {
1315                msg->id = CMD_TX_STD_MESSAGE;
1316                msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1317                msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1318        }
1319
1320        msg->u.tx_can.msg[5] = cf->can_dlc;
1321        memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1322
1323        if (cf->can_id & CAN_RTR_FLAG)
1324                msg->u.tx_can.flags |= MSG_FLAG_REMOTE_FRAME;
1325
1326        for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) {
1327                if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
1328                        context = &priv->tx_contexts[i];
1329                        break;
1330                }
1331        }
1332
1333        /* This should never happen; it implies a flow control bug */
1334        if (!context) {
1335                netdev_warn(netdev, "cannot find free context\n");
1336                ret =  NETDEV_TX_BUSY;
1337                goto releasebuf;
1338        }
1339
1340        context->priv = priv;
1341        context->echo_index = i;
1342        context->dlc = cf->can_dlc;
1343
1344        msg->u.tx_can.tid = context->echo_index;
1345
1346        usb_fill_bulk_urb(urb, dev->udev,
1347                          usb_sndbulkpipe(dev->udev,
1348                                          dev->bulk_out->bEndpointAddress),
1349                          buf, msg->len,
1350                          kvaser_usb_write_bulk_callback, context);
1351        usb_anchor_urb(urb, &priv->tx_submitted);
1352
1353        can_put_echo_skb(skb, netdev, context->echo_index);
1354
1355        atomic_inc(&priv->active_tx_urbs);
1356
1357        if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
1358                netif_stop_queue(netdev);
1359
1360        err = usb_submit_urb(urb, GFP_ATOMIC);
1361        if (unlikely(err)) {
1362                can_free_echo_skb(netdev, context->echo_index);
1363
1364                atomic_dec(&priv->active_tx_urbs);
1365                usb_unanchor_urb(urb);
1366
1367                stats->tx_dropped++;
1368
1369                if (err == -ENODEV)
1370                        netif_device_detach(netdev);
1371                else
1372                        netdev_warn(netdev, "Failed tx_urb %d\n", err);
1373
1374                goto releasebuf;
1375        }
1376
1377        usb_free_urb(urb);
1378
1379        return NETDEV_TX_OK;
1380
1381releasebuf:
1382        kfree(buf);
1383nobufmem:
1384        usb_free_urb(urb);
1385        return ret;
1386}
1387
1388static const struct net_device_ops kvaser_usb_netdev_ops = {
1389        .ndo_open = kvaser_usb_open,
1390        .ndo_stop = kvaser_usb_close,
1391        .ndo_start_xmit = kvaser_usb_start_xmit,
1392        .ndo_change_mtu = can_change_mtu,
1393};
1394
1395static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1396        .name = "kvaser_usb",
1397        .tseg1_min = KVASER_USB_TSEG1_MIN,
1398        .tseg1_max = KVASER_USB_TSEG1_MAX,
1399        .tseg2_min = KVASER_USB_TSEG2_MIN,
1400        .tseg2_max = KVASER_USB_TSEG2_MAX,
1401        .sjw_max = KVASER_USB_SJW_MAX,
1402        .brp_min = KVASER_USB_BRP_MIN,
1403        .brp_max = KVASER_USB_BRP_MAX,
1404        .brp_inc = KVASER_USB_BRP_INC,
1405};
1406
1407static int kvaser_usb_set_bittiming(struct net_device *netdev)
1408{
1409        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1410        struct can_bittiming *bt = &priv->can.bittiming;
1411        struct kvaser_usb *dev = priv->dev;
1412        struct kvaser_msg *msg;
1413        int rc;
1414
1415        msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1416        if (!msg)
1417                return -ENOMEM;
1418
1419        msg->id = CMD_SET_BUS_PARAMS;
1420        msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1421        msg->u.busparams.channel = priv->channel;
1422        msg->u.busparams.tid = 0xff;
1423        msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1424        msg->u.busparams.sjw = bt->sjw;
1425        msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1426        msg->u.busparams.tseg2 = bt->phase_seg2;
1427
1428        if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1429                msg->u.busparams.no_samp = 3;
1430        else
1431                msg->u.busparams.no_samp = 1;
1432
1433        rc = kvaser_usb_send_msg(dev, msg);
1434
1435        kfree(msg);
1436        return rc;
1437}
1438
1439static int kvaser_usb_set_mode(struct net_device *netdev,
1440                               enum can_mode mode)
1441{
1442        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1443        int err;
1444
1445        switch (mode) {
1446        case CAN_MODE_START:
1447                err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1448                if (err)
1449                        return err;
1450                break;
1451        default:
1452                return -EOPNOTSUPP;
1453        }
1454
1455        return 0;
1456}
1457
1458static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1459                                       struct can_berr_counter *bec)
1460{
1461        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1462
1463        *bec = priv->bec;
1464
1465        return 0;
1466}
1467
1468static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1469{
1470        int i;
1471
1472        for (i = 0; i < dev->nchannels; i++) {
1473                if (!dev->nets[i])
1474                        continue;
1475
1476                unregister_netdev(dev->nets[i]->netdev);
1477        }
1478
1479        kvaser_usb_unlink_all_urbs(dev);
1480
1481        for (i = 0; i < dev->nchannels; i++) {
1482                if (!dev->nets[i])
1483                        continue;
1484
1485                free_candev(dev->nets[i]->netdev);
1486        }
1487}
1488
1489static int kvaser_usb_init_one(struct usb_interface *intf,
1490                               const struct usb_device_id *id, int channel)
1491{
1492        struct kvaser_usb *dev = usb_get_intfdata(intf);
1493        struct net_device *netdev;
1494        struct kvaser_usb_net_priv *priv;
1495        int i, err;
1496
1497        err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel);
1498        if (err)
1499                return err;
1500
1501        netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
1502        if (!netdev) {
1503                dev_err(&intf->dev, "Cannot alloc candev\n");
1504                return -ENOMEM;
1505        }
1506
1507        priv = netdev_priv(netdev);
1508
1509        init_completion(&priv->start_comp);
1510        init_completion(&priv->stop_comp);
1511
1512        init_usb_anchor(&priv->tx_submitted);
1513        atomic_set(&priv->active_tx_urbs, 0);
1514
1515        for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++)
1516                priv->tx_contexts[i].echo_index = MAX_TX_URBS;
1517
1518        priv->dev = dev;
1519        priv->netdev = netdev;
1520        priv->channel = channel;
1521
1522        priv->can.state = CAN_STATE_STOPPED;
1523        priv->can.clock.freq = CAN_USB_CLOCK;
1524        priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1525        priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1526        priv->can.do_set_mode = kvaser_usb_set_mode;
1527        if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1528                priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1529        priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1530        if (id->driver_info & KVASER_HAS_SILENT_MODE)
1531                priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1532
1533        netdev->flags |= IFF_ECHO;
1534
1535        netdev->netdev_ops = &kvaser_usb_netdev_ops;
1536
1537        SET_NETDEV_DEV(netdev, &intf->dev);
1538        netdev->dev_id = channel;
1539
1540        dev->nets[channel] = priv;
1541
1542        err = register_candev(netdev);
1543        if (err) {
1544                dev_err(&intf->dev, "Failed to register can device\n");
1545                free_candev(netdev);
1546                dev->nets[channel] = NULL;
1547                return err;
1548        }
1549
1550        netdev_dbg(netdev, "device registered\n");
1551
1552        return 0;
1553}
1554
1555static int kvaser_usb_get_endpoints(const struct usb_interface *intf,
1556                                    struct usb_endpoint_descriptor **in,
1557                                    struct usb_endpoint_descriptor **out)
1558{
1559        const struct usb_host_interface *iface_desc;
1560        struct usb_endpoint_descriptor *endpoint;
1561        int i;
1562
1563        iface_desc = &intf->altsetting[0];
1564
1565        for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1566                endpoint = &iface_desc->endpoint[i].desc;
1567
1568                if (!*in && usb_endpoint_is_bulk_in(endpoint))
1569                        *in = endpoint;
1570
1571                if (!*out && usb_endpoint_is_bulk_out(endpoint))
1572                        *out = endpoint;
1573
1574                /* use first bulk endpoint for in and out */
1575                if (*in && *out)
1576                        return 0;
1577        }
1578
1579        return -ENODEV;
1580}
1581
1582static int kvaser_usb_probe(struct usb_interface *intf,
1583                            const struct usb_device_id *id)
1584{
1585        struct kvaser_usb *dev;
1586        int err = -ENOMEM;
1587        int i, retry = 3;
1588
1589        dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1590        if (!dev)
1591                return -ENOMEM;
1592
1593        err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
1594        if (err) {
1595                dev_err(&intf->dev, "Cannot get usb endpoint(s)");
1596                return err;
1597        }
1598
1599        dev->udev = interface_to_usbdev(intf);
1600
1601        init_usb_anchor(&dev->rx_submitted);
1602
1603        usb_set_intfdata(intf, dev);
1604
1605        /* On some x86 laptops, plugging a Kvaser device again after
1606         * an unplug makes the firmware always ignore the very first
1607         * command. For such a case, provide some room for retries
1608         * instead of completely exiting the driver.
1609         */
1610        do {
1611                err = kvaser_usb_get_software_info(dev);
1612        } while (--retry && err == -ETIMEDOUT);
1613
1614        if (err) {
1615                dev_err(&intf->dev,
1616                        "Cannot get software infos, error %d\n", err);
1617                return err;
1618        }
1619
1620        err = kvaser_usb_get_card_info(dev);
1621        if (err) {
1622                dev_err(&intf->dev,
1623                        "Cannot get card infos, error %d\n", err);
1624                return err;
1625        }
1626
1627        dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
1628                ((dev->fw_version >> 24) & 0xff),
1629                ((dev->fw_version >> 16) & 0xff),
1630                (dev->fw_version & 0xffff));
1631
1632        for (i = 0; i < dev->nchannels; i++) {
1633                err = kvaser_usb_init_one(intf, id, i);
1634                if (err) {
1635                        kvaser_usb_remove_interfaces(dev);
1636                        return err;
1637                }
1638        }
1639
1640        return 0;
1641}
1642
1643static void kvaser_usb_disconnect(struct usb_interface *intf)
1644{
1645        struct kvaser_usb *dev = usb_get_intfdata(intf);
1646
1647        usb_set_intfdata(intf, NULL);
1648
1649        if (!dev)
1650                return;
1651
1652        kvaser_usb_remove_interfaces(dev);
1653}
1654
1655static struct usb_driver kvaser_usb_driver = {
1656        .name = "kvaser_usb",
1657        .probe = kvaser_usb_probe,
1658        .disconnect = kvaser_usb_disconnect,
1659        .id_table = kvaser_usb_table,
1660};
1661
1662module_usb_driver(kvaser_usb_driver);
1663
1664MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1665MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1666MODULE_LICENSE("GPL v2");
1667
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.