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