linux/drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/* Parts of this driver are based on the following:
   3 *  - Kvaser linux leaf driver (version 4.78)
   4 *  - CAN driver for esd CAN-USB/2
   5 *  - Kvaser linux usbcanII driver (version 5.3)
   6 *  - Kvaser linux mhydra driver (version 5.24)
   7 *
   8 * Copyright (C) 2002-2018 KVASER AB, Sweden. All rights reserved.
   9 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
  10 * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
  11 * Copyright (C) 2015 Valeo S.A.
  12 */
  13
  14#include <linux/completion.h>
  15#include <linux/device.h>
  16#include <linux/gfp.h>
  17#include <linux/if.h>
  18#include <linux/kernel.h>
  19#include <linux/module.h>
  20#include <linux/netdevice.h>
  21#include <linux/spinlock.h>
  22#include <linux/types.h>
  23#include <linux/usb.h>
  24
  25#include <linux/can.h>
  26#include <linux/can/dev.h>
  27#include <linux/can/error.h>
  28#include <linux/can/netlink.h>
  29
  30#include "kvaser_usb.h"
  31
  32/* Kvaser USB vendor id. */
  33#define KVASER_VENDOR_ID                        0x0bfd
  34
  35/* Kvaser Leaf USB devices product ids */
  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#define USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID     290
  59#define USB_USBCAN_LIGHT_2HS_PRODUCT_ID         291
  60#define USB_MINI_PCIE_2HS_PRODUCT_ID            292
  61
  62/* Kvaser USBCan-II devices product ids */
  63#define USB_USBCAN_REVB_PRODUCT_ID              2
  64#define USB_VCI2_PRODUCT_ID                     3
  65#define USB_USBCAN2_PRODUCT_ID                  4
  66#define USB_MEMORATOR_PRODUCT_ID                5
  67
  68/* Kvaser Minihydra USB devices product ids */
  69#define USB_BLACKBIRD_V2_PRODUCT_ID             258
  70#define USB_MEMO_PRO_5HS_PRODUCT_ID             260
  71#define USB_USBCAN_PRO_5HS_PRODUCT_ID           261
  72#define USB_USBCAN_LIGHT_4HS_PRODUCT_ID         262
  73#define USB_LEAF_PRO_HS_V2_PRODUCT_ID           263
  74#define USB_USBCAN_PRO_2HS_V2_PRODUCT_ID        264
  75#define USB_MEMO_2HS_PRODUCT_ID                 265
  76#define USB_MEMO_PRO_2HS_V2_PRODUCT_ID          266
  77#define USB_HYBRID_CANLIN_PRODUCT_ID            267
  78#define USB_ATI_USBCAN_PRO_2HS_V2_PRODUCT_ID    268
  79#define USB_ATI_MEMO_PRO_2HS_V2_PRODUCT_ID      269
  80#define USB_HYBRID_PRO_CANLIN_PRODUCT_ID        270
  81
  82static inline bool kvaser_is_leaf(const struct usb_device_id *id)
  83{
  84        return (id->idProduct >= USB_LEAF_DEVEL_PRODUCT_ID &&
  85                id->idProduct <= USB_CAN_R_PRODUCT_ID) ||
  86                (id->idProduct >= USB_LEAF_LITE_V2_PRODUCT_ID &&
  87                 id->idProduct <= USB_MINI_PCIE_2HS_PRODUCT_ID);
  88}
  89
  90static inline bool kvaser_is_usbcan(const struct usb_device_id *id)
  91{
  92        return id->idProduct >= USB_USBCAN_REVB_PRODUCT_ID &&
  93               id->idProduct <= USB_MEMORATOR_PRODUCT_ID;
  94}
  95
  96static inline bool kvaser_is_hydra(const struct usb_device_id *id)
  97{
  98        return id->idProduct >= USB_BLACKBIRD_V2_PRODUCT_ID &&
  99               id->idProduct <= USB_HYBRID_PRO_CANLIN_PRODUCT_ID;
 100}
 101
 102static const struct usb_device_id kvaser_usb_table[] = {
 103        /* Leaf USB product IDs */
 104        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
 105        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
 106        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
 107                .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
 108                               KVASER_USB_HAS_SILENT_MODE },
 109        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
 110                .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
 111                               KVASER_USB_HAS_SILENT_MODE },
 112        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
 113                .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
 114                               KVASER_USB_HAS_SILENT_MODE },
 115        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
 116                .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
 117                               KVASER_USB_HAS_SILENT_MODE },
 118        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
 119                .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
 120                               KVASER_USB_HAS_SILENT_MODE },
 121        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
 122                .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
 123                               KVASER_USB_HAS_SILENT_MODE },
 124        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
 125                .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
 126                               KVASER_USB_HAS_SILENT_MODE },
 127        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
 128                .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
 129                               KVASER_USB_HAS_SILENT_MODE },
 130        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
 131                .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
 132                               KVASER_USB_HAS_SILENT_MODE },
 133        { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
 134                .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
 135        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
 136        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
 137                .driver_info = KVASER_USB_HAS_TXRX_ERRORS |
 138                               KVASER_USB_HAS_SILENT_MODE },
 139        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
 140                .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
 141        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
 142                .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
 143        { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
 144                .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
 145        { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
 146                .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
 147        { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
 148                .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
 149        { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
 150                .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
 151        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) },
 152        { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) },
 153        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID) },
 154        { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_2HS_PRODUCT_ID) },
 155        { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_2HS_PRODUCT_ID) },
 156
 157        /* USBCANII USB product IDs */
 158        { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID),
 159                .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
 160        { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID),
 161                .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
 162        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID),
 163                .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
 164        { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID),
 165                .driver_info = KVASER_USB_HAS_TXRX_ERRORS },
 166
 167        /* Minihydra USB product IDs */
 168        { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_V2_PRODUCT_ID) },
 169        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO_PRO_5HS_PRODUCT_ID) },
 170        { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_PRO_5HS_PRODUCT_ID) },
 171        { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_4HS_PRODUCT_ID) },
 172        { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_HS_V2_PRODUCT_ID) },
 173        { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_PRO_2HS_V2_PRODUCT_ID) },
 174        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO_2HS_PRODUCT_ID) },
 175        { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO_PRO_2HS_V2_PRODUCT_ID) },
 176        { USB_DEVICE(KVASER_VENDOR_ID, USB_HYBRID_CANLIN_PRODUCT_ID) },
 177        { USB_DEVICE(KVASER_VENDOR_ID, USB_ATI_USBCAN_PRO_2HS_V2_PRODUCT_ID) },
 178        { USB_DEVICE(KVASER_VENDOR_ID, USB_ATI_MEMO_PRO_2HS_V2_PRODUCT_ID) },
 179        { USB_DEVICE(KVASER_VENDOR_ID, USB_HYBRID_PRO_CANLIN_PRODUCT_ID) },
 180        { }
 181};
 182MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
 183
 184int kvaser_usb_send_cmd(const struct kvaser_usb *dev, void *cmd, int len)
 185{
 186        int actual_len; /* Not used */
 187
 188        return usb_bulk_msg(dev->udev,
 189                            usb_sndbulkpipe(dev->udev,
 190                                            dev->bulk_out->bEndpointAddress),
 191                            cmd, len, &actual_len, KVASER_USB_TIMEOUT);
 192}
 193
 194int kvaser_usb_recv_cmd(const struct kvaser_usb *dev, void *cmd, int len,
 195                        int *actual_len)
 196{
 197        return usb_bulk_msg(dev->udev,
 198                            usb_rcvbulkpipe(dev->udev,
 199                                            dev->bulk_in->bEndpointAddress),
 200                            cmd, len, actual_len, KVASER_USB_TIMEOUT);
 201}
 202
 203static void kvaser_usb_send_cmd_callback(struct urb *urb)
 204{
 205        struct net_device *netdev = urb->context;
 206
 207        kfree(urb->transfer_buffer);
 208
 209        if (urb->status)
 210                netdev_warn(netdev, "urb status received: %d\n", urb->status);
 211}
 212
 213int kvaser_usb_send_cmd_async(struct kvaser_usb_net_priv *priv, void *cmd,
 214                              int len)
 215{
 216        struct kvaser_usb *dev = priv->dev;
 217        struct net_device *netdev = priv->netdev;
 218        struct urb *urb;
 219        int err;
 220
 221        urb = usb_alloc_urb(0, GFP_ATOMIC);
 222        if (!urb)
 223                return -ENOMEM;
 224
 225        usb_fill_bulk_urb(urb, dev->udev,
 226                          usb_sndbulkpipe(dev->udev,
 227                                          dev->bulk_out->bEndpointAddress),
 228                          cmd, len, kvaser_usb_send_cmd_callback, netdev);
 229        usb_anchor_urb(urb, &priv->tx_submitted);
 230
 231        err = usb_submit_urb(urb, GFP_ATOMIC);
 232        if (err) {
 233                netdev_err(netdev, "Error transmitting URB\n");
 234                usb_unanchor_urb(urb);
 235        }
 236        usb_free_urb(urb);
 237
 238        return 0;
 239}
 240
 241int kvaser_usb_can_rx_over_error(struct net_device *netdev)
 242{
 243        struct net_device_stats *stats = &netdev->stats;
 244        struct can_frame *cf;
 245        struct sk_buff *skb;
 246
 247        stats->rx_over_errors++;
 248        stats->rx_errors++;
 249
 250        skb = alloc_can_err_skb(netdev, &cf);
 251        if (!skb) {
 252                stats->rx_dropped++;
 253                netdev_warn(netdev, "No memory left for err_skb\n");
 254                return -ENOMEM;
 255        }
 256
 257        cf->can_id |= CAN_ERR_CRTL;
 258        cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
 259
 260        stats->rx_packets++;
 261        stats->rx_bytes += cf->can_dlc;
 262        netif_rx(skb);
 263
 264        return 0;
 265}
 266
 267static void kvaser_usb_read_bulk_callback(struct urb *urb)
 268{
 269        struct kvaser_usb *dev = urb->context;
 270        int err;
 271        unsigned int i;
 272
 273        switch (urb->status) {
 274        case 0:
 275                break;
 276        case -ENOENT:
 277        case -EPIPE:
 278        case -EPROTO:
 279        case -ESHUTDOWN:
 280                return;
 281        default:
 282                dev_info(&dev->intf->dev, "Rx URB aborted (%d)\n", urb->status);
 283                goto resubmit_urb;
 284        }
 285
 286        dev->ops->dev_read_bulk_callback(dev, urb->transfer_buffer,
 287                                         urb->actual_length);
 288
 289resubmit_urb:
 290        usb_fill_bulk_urb(urb, dev->udev,
 291                          usb_rcvbulkpipe(dev->udev,
 292                                          dev->bulk_in->bEndpointAddress),
 293                          urb->transfer_buffer, KVASER_USB_RX_BUFFER_SIZE,
 294                          kvaser_usb_read_bulk_callback, dev);
 295
 296        err = usb_submit_urb(urb, GFP_ATOMIC);
 297        if (err == -ENODEV) {
 298                for (i = 0; i < dev->nchannels; i++) {
 299                        if (!dev->nets[i])
 300                                continue;
 301
 302                        netif_device_detach(dev->nets[i]->netdev);
 303                }
 304        } else if (err) {
 305                dev_err(&dev->intf->dev,
 306                        "Failed resubmitting read bulk urb: %d\n", err);
 307        }
 308}
 309
 310static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
 311{
 312        int i, err = 0;
 313
 314        if (dev->rxinitdone)
 315                return 0;
 316
 317        for (i = 0; i < KVASER_USB_MAX_RX_URBS; i++) {
 318                struct urb *urb = NULL;
 319                u8 *buf = NULL;
 320                dma_addr_t buf_dma;
 321
 322                urb = usb_alloc_urb(0, GFP_KERNEL);
 323                if (!urb) {
 324                        err = -ENOMEM;
 325                        break;
 326                }
 327
 328                buf = usb_alloc_coherent(dev->udev, KVASER_USB_RX_BUFFER_SIZE,
 329                                         GFP_KERNEL, &buf_dma);
 330                if (!buf) {
 331                        dev_warn(&dev->intf->dev,
 332                                 "No memory left for USB buffer\n");
 333                        usb_free_urb(urb);
 334                        err = -ENOMEM;
 335                        break;
 336                }
 337
 338                usb_fill_bulk_urb(urb, dev->udev,
 339                                  usb_rcvbulkpipe
 340                                        (dev->udev,
 341                                         dev->bulk_in->bEndpointAddress),
 342                                  buf, KVASER_USB_RX_BUFFER_SIZE,
 343                                  kvaser_usb_read_bulk_callback, dev);
 344                urb->transfer_dma = buf_dma;
 345                urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
 346                usb_anchor_urb(urb, &dev->rx_submitted);
 347
 348                err = usb_submit_urb(urb, GFP_KERNEL);
 349                if (err) {
 350                        usb_unanchor_urb(urb);
 351                        usb_free_coherent(dev->udev,
 352                                          KVASER_USB_RX_BUFFER_SIZE, buf,
 353                                          buf_dma);
 354                        usb_free_urb(urb);
 355                        break;
 356                }
 357
 358                dev->rxbuf[i] = buf;
 359                dev->rxbuf_dma[i] = buf_dma;
 360
 361                usb_free_urb(urb);
 362        }
 363
 364        if (i == 0) {
 365                dev_warn(&dev->intf->dev, "Cannot setup read URBs, error %d\n",
 366                         err);
 367                return err;
 368        } else if (i < KVASER_USB_MAX_RX_URBS) {
 369                dev_warn(&dev->intf->dev, "RX performances may be slow\n");
 370        }
 371
 372        dev->rxinitdone = true;
 373
 374        return 0;
 375}
 376
 377static int kvaser_usb_open(struct net_device *netdev)
 378{
 379        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
 380        struct kvaser_usb *dev = priv->dev;
 381        int err;
 382
 383        err = open_candev(netdev);
 384        if (err)
 385                return err;
 386
 387        err = kvaser_usb_setup_rx_urbs(dev);
 388        if (err)
 389                goto error;
 390
 391        err = dev->ops->dev_set_opt_mode(priv);
 392        if (err)
 393                goto error;
 394
 395        err = dev->ops->dev_start_chip(priv);
 396        if (err) {
 397                netdev_warn(netdev, "Cannot start device, error %d\n", err);
 398                goto error;
 399        }
 400
 401        priv->can.state = CAN_STATE_ERROR_ACTIVE;
 402
 403        return 0;
 404
 405error:
 406        close_candev(netdev);
 407        return err;
 408}
 409
 410static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv)
 411{
 412        int i, max_tx_urbs;
 413
 414        max_tx_urbs = priv->dev->max_tx_urbs;
 415
 416        priv->active_tx_contexts = 0;
 417        for (i = 0; i < max_tx_urbs; i++)
 418                priv->tx_contexts[i].echo_index = max_tx_urbs;
 419}
 420
 421/* This method might sleep. Do not call it in the atomic context
 422 * of URB completions.
 423 */
 424static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
 425{
 426        usb_kill_anchored_urbs(&priv->tx_submitted);
 427        kvaser_usb_reset_tx_urb_contexts(priv);
 428}
 429
 430static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
 431{
 432        int i;
 433
 434        usb_kill_anchored_urbs(&dev->rx_submitted);
 435
 436        for (i = 0; i < KVASER_USB_MAX_RX_URBS; i++)
 437                usb_free_coherent(dev->udev, KVASER_USB_RX_BUFFER_SIZE,
 438                                  dev->rxbuf[i], dev->rxbuf_dma[i]);
 439
 440        for (i = 0; i < dev->nchannels; i++) {
 441                struct kvaser_usb_net_priv *priv = dev->nets[i];
 442
 443                if (priv)
 444                        kvaser_usb_unlink_tx_urbs(priv);
 445        }
 446}
 447
 448static int kvaser_usb_close(struct net_device *netdev)
 449{
 450        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
 451        struct kvaser_usb *dev = priv->dev;
 452        int err;
 453
 454        netif_stop_queue(netdev);
 455
 456        err = dev->ops->dev_flush_queue(priv);
 457        if (err)
 458                netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
 459
 460        if (dev->ops->dev_reset_chip) {
 461                err = dev->ops->dev_reset_chip(dev, priv->channel);
 462                if (err)
 463                        netdev_warn(netdev, "Cannot reset card, error %d\n",
 464                                    err);
 465        }
 466
 467        err = dev->ops->dev_stop_chip(priv);
 468        if (err)
 469                netdev_warn(netdev, "Cannot stop device, error %d\n", err);
 470
 471        /* reset tx contexts */
 472        kvaser_usb_unlink_tx_urbs(priv);
 473
 474        priv->can.state = CAN_STATE_STOPPED;
 475        close_candev(priv->netdev);
 476
 477        return 0;
 478}
 479
 480static void kvaser_usb_write_bulk_callback(struct urb *urb)
 481{
 482        struct kvaser_usb_tx_urb_context *context = urb->context;
 483        struct kvaser_usb_net_priv *priv;
 484        struct net_device *netdev;
 485
 486        if (WARN_ON(!context))
 487                return;
 488
 489        priv = context->priv;
 490        netdev = priv->netdev;
 491
 492        kfree(urb->transfer_buffer);
 493
 494        if (!netif_device_present(netdev))
 495                return;
 496
 497        if (urb->status)
 498                netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
 499}
 500
 501static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
 502                                         struct net_device *netdev)
 503{
 504        struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
 505        struct kvaser_usb *dev = priv->dev;
 506        struct net_device_stats *stats = &netdev->stats;
 507        struct kvaser_usb_tx_urb_context *context = NULL;
 508        struct urb *urb;
 509        void *buf;
 510        int cmd_len = 0;
 511        int err, ret = NETDEV_TX_OK;
 512        unsigned int i;
 513        unsigned long flags;
 514
 515        if (can_dropped_invalid_skb(netdev, skb))
 516                return NETDEV_TX_OK;
 517
 518        urb = usb_alloc_urb(0, GFP_ATOMIC);
 519        if (!urb) {
 520                stats->tx_dropped++;
 521                dev_kfree_skb(skb);
 522                return NETDEV_TX_OK;
 523        }
 524
 525        spin_lock_irqsave(&priv->tx_contexts_lock, flags);
 526        for (i = 0; i < dev->max_tx_urbs; i++) {
 527                if (priv->tx_contexts[i].echo_index == dev->max_tx_urbs) {
 528                        context = &priv->tx_contexts[i];
 529
 530                        context->echo_index = i;
 531                        ++priv->active_tx_contexts;
 532                        if (priv->active_tx_contexts >= (int)dev->max_tx_urbs)
 533                                netif_stop_queue(netdev);
 534
 535                        break;
 536                }
 537        }
 538        spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
 539
 540        /* This should never happen; it implies a flow control bug */
 541        if (!context) {
 542                netdev_warn(netdev, "cannot find free context\n");
 543
 544                ret = NETDEV_TX_BUSY;
 545                goto freeurb;
 546        }
 547
 548        buf = dev->ops->dev_frame_to_cmd(priv, skb, &context->dlc, &cmd_len,
 549                                         context->echo_index);
 550        if (!buf) {
 551                stats->tx_dropped++;
 552                dev_kfree_skb(skb);
 553                spin_lock_irqsave(&priv->tx_contexts_lock, flags);
 554
 555                context->echo_index = dev->max_tx_urbs;
 556                --priv->active_tx_contexts;
 557                netif_wake_queue(netdev);
 558
 559                spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
 560                goto freeurb;
 561        }
 562
 563        context->priv = priv;
 564
 565        can_put_echo_skb(skb, netdev, context->echo_index);
 566
 567        usb_fill_bulk_urb(urb, dev->udev,
 568                          usb_sndbulkpipe(dev->udev,
 569                                          dev->bulk_out->bEndpointAddress),
 570                          buf, cmd_len, kvaser_usb_write_bulk_callback,
 571                          context);
 572        usb_anchor_urb(urb, &priv->tx_submitted);
 573
 574        err = usb_submit_urb(urb, GFP_ATOMIC);
 575        if (unlikely(err)) {
 576                spin_lock_irqsave(&priv->tx_contexts_lock, flags);
 577
 578                can_free_echo_skb(netdev, context->echo_index);
 579                context->echo_index = dev->max_tx_urbs;
 580                --priv->active_tx_contexts;
 581                netif_wake_queue(netdev);
 582
 583                spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
 584
 585                usb_unanchor_urb(urb);
 586                kfree(buf);
 587
 588                stats->tx_dropped++;
 589
 590                if (err == -ENODEV)
 591                        netif_device_detach(netdev);
 592                else
 593                        netdev_warn(netdev, "Failed tx_urb %d\n", err);
 594
 595                goto freeurb;
 596        }
 597
 598        ret = NETDEV_TX_OK;
 599
 600freeurb:
 601        usb_free_urb(urb);
 602        return ret;
 603}
 604
 605static const struct net_device_ops kvaser_usb_netdev_ops = {
 606        .ndo_open = kvaser_usb_open,
 607        .ndo_stop = kvaser_usb_close,
 608        .ndo_start_xmit = kvaser_usb_start_xmit,
 609        .ndo_change_mtu = can_change_mtu,
 610};
 611
 612static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
 613{
 614        int i;
 615
 616        for (i = 0; i < dev->nchannels; i++) {
 617                if (!dev->nets[i])
 618                        continue;
 619
 620                unregister_candev(dev->nets[i]->netdev);
 621        }
 622
 623        kvaser_usb_unlink_all_urbs(dev);
 624
 625        for (i = 0; i < dev->nchannels; i++) {
 626                if (!dev->nets[i])
 627                        continue;
 628
 629                free_candev(dev->nets[i]->netdev);
 630        }
 631}
 632
 633static int kvaser_usb_init_one(struct kvaser_usb *dev,
 634                               const struct usb_device_id *id, int channel)
 635{
 636        struct net_device *netdev;
 637        struct kvaser_usb_net_priv *priv;
 638        int err;
 639
 640        if (dev->ops->dev_reset_chip) {
 641                err = dev->ops->dev_reset_chip(dev, channel);
 642                if (err)
 643                        return err;
 644        }
 645
 646        netdev = alloc_candev(sizeof(*priv) +
 647                              dev->max_tx_urbs * sizeof(*priv->tx_contexts),
 648                              dev->max_tx_urbs);
 649        if (!netdev) {
 650                dev_err(&dev->intf->dev, "Cannot alloc candev\n");
 651                return -ENOMEM;
 652        }
 653
 654        priv = netdev_priv(netdev);
 655
 656        init_usb_anchor(&priv->tx_submitted);
 657        init_completion(&priv->start_comp);
 658        init_completion(&priv->stop_comp);
 659        priv->can.ctrlmode_supported = 0;
 660
 661        priv->dev = dev;
 662        priv->netdev = netdev;
 663        priv->channel = channel;
 664
 665        spin_lock_init(&priv->tx_contexts_lock);
 666        kvaser_usb_reset_tx_urb_contexts(priv);
 667
 668        priv->can.state = CAN_STATE_STOPPED;
 669        priv->can.clock.freq = dev->cfg->clock.freq;
 670        priv->can.bittiming_const = dev->cfg->bittiming_const;
 671        priv->can.do_set_bittiming = dev->ops->dev_set_bittiming;
 672        priv->can.do_set_mode = dev->ops->dev_set_mode;
 673        if ((id->driver_info & KVASER_USB_HAS_TXRX_ERRORS) ||
 674            (priv->dev->card_data.capabilities & KVASER_USB_CAP_BERR_CAP))
 675                priv->can.do_get_berr_counter = dev->ops->dev_get_berr_counter;
 676        if (id->driver_info & KVASER_USB_HAS_SILENT_MODE)
 677                priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
 678
 679        priv->can.ctrlmode_supported |= dev->card_data.ctrlmode_supported;
 680
 681        if (priv->can.ctrlmode_supported & CAN_CTRLMODE_FD) {
 682                priv->can.data_bittiming_const = dev->cfg->data_bittiming_const;
 683                priv->can.do_set_data_bittiming =
 684                                        dev->ops->dev_set_data_bittiming;
 685        }
 686
 687        netdev->flags |= IFF_ECHO;
 688
 689        netdev->netdev_ops = &kvaser_usb_netdev_ops;
 690
 691        SET_NETDEV_DEV(netdev, &dev->intf->dev);
 692        netdev->dev_id = channel;
 693
 694        dev->nets[channel] = priv;
 695
 696        err = register_candev(netdev);
 697        if (err) {
 698                dev_err(&dev->intf->dev, "Failed to register CAN device\n");
 699                free_candev(netdev);
 700                dev->nets[channel] = NULL;
 701                return err;
 702        }
 703
 704        netdev_dbg(netdev, "device registered\n");
 705
 706        return 0;
 707}
 708
 709static int kvaser_usb_probe(struct usb_interface *intf,
 710                            const struct usb_device_id *id)
 711{
 712        struct kvaser_usb *dev;
 713        int err;
 714        int i;
 715
 716        dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
 717        if (!dev)
 718                return -ENOMEM;
 719
 720        if (kvaser_is_leaf(id)) {
 721                dev->card_data.leaf.family = KVASER_LEAF;
 722                dev->ops = &kvaser_usb_leaf_dev_ops;
 723        } else if (kvaser_is_usbcan(id)) {
 724                dev->card_data.leaf.family = KVASER_USBCAN;
 725                dev->ops = &kvaser_usb_leaf_dev_ops;
 726        } else if (kvaser_is_hydra(id)) {
 727                dev->ops = &kvaser_usb_hydra_dev_ops;
 728        } else {
 729                dev_err(&intf->dev,
 730                        "Product ID (%d) is not a supported Kvaser USB device\n",
 731                        id->idProduct);
 732                return -ENODEV;
 733        }
 734
 735        dev->intf = intf;
 736
 737        err = dev->ops->dev_setup_endpoints(dev);
 738        if (err) {
 739                dev_err(&intf->dev, "Cannot get usb endpoint(s)");
 740                return err;
 741        }
 742
 743        dev->udev = interface_to_usbdev(intf);
 744
 745        init_usb_anchor(&dev->rx_submitted);
 746
 747        usb_set_intfdata(intf, dev);
 748
 749        dev->card_data.ctrlmode_supported = 0;
 750        dev->card_data.capabilities = 0;
 751        err = dev->ops->dev_init_card(dev);
 752        if (err) {
 753                dev_err(&intf->dev,
 754                        "Failed to initialize card, error %d\n", err);
 755                return err;
 756        }
 757
 758        err = dev->ops->dev_get_software_info(dev);
 759        if (err) {
 760                dev_err(&intf->dev,
 761                        "Cannot get software info, error %d\n", err);
 762                return err;
 763        }
 764
 765        if (dev->ops->dev_get_software_details) {
 766                err = dev->ops->dev_get_software_details(dev);
 767                if (err) {
 768                        dev_err(&intf->dev,
 769                                "Cannot get software details, error %d\n", err);
 770                        return err;
 771                }
 772        }
 773
 774        if (WARN_ON(!dev->cfg))
 775                return -ENODEV;
 776
 777        dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
 778                ((dev->fw_version >> 24) & 0xff),
 779                ((dev->fw_version >> 16) & 0xff),
 780                (dev->fw_version & 0xffff));
 781
 782        dev_dbg(&intf->dev, "Max outstanding tx = %d URBs\n", dev->max_tx_urbs);
 783
 784        err = dev->ops->dev_get_card_info(dev);
 785        if (err) {
 786                dev_err(&intf->dev, "Cannot get card info, error %d\n", err);
 787                return err;
 788        }
 789
 790        if (dev->ops->dev_get_capabilities) {
 791                err = dev->ops->dev_get_capabilities(dev);
 792                if (err) {
 793                        dev_err(&intf->dev,
 794                                "Cannot get capabilities, error %d\n", err);
 795                        kvaser_usb_remove_interfaces(dev);
 796                        return err;
 797                }
 798        }
 799
 800        for (i = 0; i < dev->nchannels; i++) {
 801                err = kvaser_usb_init_one(dev, id, i);
 802                if (err) {
 803                        kvaser_usb_remove_interfaces(dev);
 804                        return err;
 805                }
 806        }
 807
 808        return 0;
 809}
 810
 811static void kvaser_usb_disconnect(struct usb_interface *intf)
 812{
 813        struct kvaser_usb *dev = usb_get_intfdata(intf);
 814
 815        usb_set_intfdata(intf, NULL);
 816
 817        if (!dev)
 818                return;
 819
 820        kvaser_usb_remove_interfaces(dev);
 821}
 822
 823static struct usb_driver kvaser_usb_driver = {
 824        .name = "kvaser_usb",
 825        .probe = kvaser_usb_probe,
 826        .disconnect = kvaser_usb_disconnect,
 827        .id_table = kvaser_usb_table,
 828};
 829
 830module_usb_driver(kvaser_usb_driver);
 831
 832MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
 833MODULE_AUTHOR("Kvaser AB <support@kvaser.com>");
 834MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
 835MODULE_LICENSE("GPL v2");
 836