linux/drivers/net/can/usb/esd_usb2.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * CAN driver for esd CAN-USB/2 and CAN-USB/Micro
   4 *
   5 * Copyright (C) 2010-2012 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
   6 */
   7#include <linux/signal.h>
   8#include <linux/slab.h>
   9#include <linux/module.h>
  10#include <linux/netdevice.h>
  11#include <linux/usb.h>
  12
  13#include <linux/can.h>
  14#include <linux/can/dev.h>
  15#include <linux/can/error.h>
  16
  17MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd.eu>");
  18MODULE_DESCRIPTION("CAN driver for esd CAN-USB/2 and CAN-USB/Micro interfaces");
  19MODULE_LICENSE("GPL v2");
  20
  21/* Define these values to match your devices */
  22#define USB_ESDGMBH_VENDOR_ID   0x0ab4
  23#define USB_CANUSB2_PRODUCT_ID  0x0010
  24#define USB_CANUSBM_PRODUCT_ID  0x0011
  25
  26#define ESD_USB2_CAN_CLOCK      60000000
  27#define ESD_USBM_CAN_CLOCK      36000000
  28#define ESD_USB2_MAX_NETS       2
  29
  30/* USB2 commands */
  31#define CMD_VERSION             1 /* also used for VERSION_REPLY */
  32#define CMD_CAN_RX              2 /* device to host only */
  33#define CMD_CAN_TX              3 /* also used for TX_DONE */
  34#define CMD_SETBAUD             4 /* also used for SETBAUD_REPLY */
  35#define CMD_TS                  5 /* also used for TS_REPLY */
  36#define CMD_IDADD               6 /* also used for IDADD_REPLY */
  37
  38/* esd CAN message flags - dlc field */
  39#define ESD_RTR                 0x10
  40
  41/* esd CAN message flags - id field */
  42#define ESD_EXTID               0x20000000
  43#define ESD_EVENT               0x40000000
  44#define ESD_IDMASK              0x1fffffff
  45
  46/* esd CAN event ids used by this driver */
  47#define ESD_EV_CAN_ERROR_EXT    2
  48
  49/* baudrate message flags */
  50#define ESD_USB2_UBR            0x80000000
  51#define ESD_USB2_LOM            0x40000000
  52#define ESD_USB2_NO_BAUDRATE    0x7fffffff
  53#define ESD_USB2_TSEG1_MIN      1
  54#define ESD_USB2_TSEG1_MAX      16
  55#define ESD_USB2_TSEG1_SHIFT    16
  56#define ESD_USB2_TSEG2_MIN      1
  57#define ESD_USB2_TSEG2_MAX      8
  58#define ESD_USB2_TSEG2_SHIFT    20
  59#define ESD_USB2_SJW_MAX        4
  60#define ESD_USB2_SJW_SHIFT      14
  61#define ESD_USBM_SJW_SHIFT      24
  62#define ESD_USB2_BRP_MIN        1
  63#define ESD_USB2_BRP_MAX        1024
  64#define ESD_USB2_BRP_INC        1
  65#define ESD_USB2_3_SAMPLES      0x00800000
  66
  67/* esd IDADD message */
  68#define ESD_ID_ENABLE           0x80
  69#define ESD_MAX_ID_SEGMENT      64
  70
  71/* SJA1000 ECC register (emulated by usb2 firmware) */
  72#define SJA1000_ECC_SEG         0x1F
  73#define SJA1000_ECC_DIR         0x20
  74#define SJA1000_ECC_ERR         0x06
  75#define SJA1000_ECC_BIT         0x00
  76#define SJA1000_ECC_FORM        0x40
  77#define SJA1000_ECC_STUFF       0x80
  78#define SJA1000_ECC_MASK        0xc0
  79
  80/* esd bus state event codes */
  81#define ESD_BUSSTATE_MASK       0xc0
  82#define ESD_BUSSTATE_WARN       0x40
  83#define ESD_BUSSTATE_ERRPASSIVE 0x80
  84#define ESD_BUSSTATE_BUSOFF     0xc0
  85
  86#define RX_BUFFER_SIZE          1024
  87#define MAX_RX_URBS             4
  88#define MAX_TX_URBS             16 /* must be power of 2 */
  89
  90struct header_msg {
  91        u8 len; /* len is always the total message length in 32bit words */
  92        u8 cmd;
  93        u8 rsvd[2];
  94};
  95
  96struct version_msg {
  97        u8 len;
  98        u8 cmd;
  99        u8 rsvd;
 100        u8 flags;
 101        __le32 drv_version;
 102};
 103
 104struct version_reply_msg {
 105        u8 len;
 106        u8 cmd;
 107        u8 nets;
 108        u8 features;
 109        __le32 version;
 110        u8 name[16];
 111        __le32 rsvd;
 112        __le32 ts;
 113};
 114
 115struct rx_msg {
 116        u8 len;
 117        u8 cmd;
 118        u8 net;
 119        u8 dlc;
 120        __le32 ts;
 121        __le32 id; /* upper 3 bits contain flags */
 122        u8 data[8];
 123};
 124
 125struct tx_msg {
 126        u8 len;
 127        u8 cmd;
 128        u8 net;
 129        u8 dlc;
 130        u32 hnd;        /* opaque handle, not used by device */
 131        __le32 id; /* upper 3 bits contain flags */
 132        u8 data[8];
 133};
 134
 135struct tx_done_msg {
 136        u8 len;
 137        u8 cmd;
 138        u8 net;
 139        u8 status;
 140        u32 hnd;        /* opaque handle, not used by device */
 141        __le32 ts;
 142};
 143
 144struct id_filter_msg {
 145        u8 len;
 146        u8 cmd;
 147        u8 net;
 148        u8 option;
 149        __le32 mask[ESD_MAX_ID_SEGMENT + 1];
 150};
 151
 152struct set_baudrate_msg {
 153        u8 len;
 154        u8 cmd;
 155        u8 net;
 156        u8 rsvd;
 157        __le32 baud;
 158};
 159
 160/* Main message type used between library and application */
 161struct __attribute__ ((packed)) esd_usb2_msg {
 162        union {
 163                struct header_msg hdr;
 164                struct version_msg version;
 165                struct version_reply_msg version_reply;
 166                struct rx_msg rx;
 167                struct tx_msg tx;
 168                struct tx_done_msg txdone;
 169                struct set_baudrate_msg setbaud;
 170                struct id_filter_msg filter;
 171        } msg;
 172};
 173
 174static struct usb_device_id esd_usb2_table[] = {
 175        {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)},
 176        {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSBM_PRODUCT_ID)},
 177        {}
 178};
 179MODULE_DEVICE_TABLE(usb, esd_usb2_table);
 180
 181struct esd_usb2_net_priv;
 182
 183struct esd_tx_urb_context {
 184        struct esd_usb2_net_priv *priv;
 185        u32 echo_index;
 186        int len;        /* CAN payload length */
 187};
 188
 189struct esd_usb2 {
 190        struct usb_device *udev;
 191        struct esd_usb2_net_priv *nets[ESD_USB2_MAX_NETS];
 192
 193        struct usb_anchor rx_submitted;
 194
 195        int net_count;
 196        u32 version;
 197        int rxinitdone;
 198        void *rxbuf[MAX_RX_URBS];
 199        dma_addr_t rxbuf_dma[MAX_RX_URBS];
 200};
 201
 202struct esd_usb2_net_priv {
 203        struct can_priv can; /* must be the first member */
 204
 205        atomic_t active_tx_jobs;
 206        struct usb_anchor tx_submitted;
 207        struct esd_tx_urb_context tx_contexts[MAX_TX_URBS];
 208
 209        struct esd_usb2 *usb2;
 210        struct net_device *netdev;
 211        int index;
 212        u8 old_state;
 213        struct can_berr_counter bec;
 214};
 215
 216static void esd_usb2_rx_event(struct esd_usb2_net_priv *priv,
 217                              struct esd_usb2_msg *msg)
 218{
 219        struct net_device_stats *stats = &priv->netdev->stats;
 220        struct can_frame *cf;
 221        struct sk_buff *skb;
 222        u32 id = le32_to_cpu(msg->msg.rx.id) & ESD_IDMASK;
 223
 224        if (id == ESD_EV_CAN_ERROR_EXT) {
 225                u8 state = msg->msg.rx.data[0];
 226                u8 ecc = msg->msg.rx.data[1];
 227                u8 rxerr = msg->msg.rx.data[2];
 228                u8 txerr = msg->msg.rx.data[3];
 229
 230                skb = alloc_can_err_skb(priv->netdev, &cf);
 231                if (skb == NULL) {
 232                        stats->rx_dropped++;
 233                        return;
 234                }
 235
 236                if (state != priv->old_state) {
 237                        priv->old_state = state;
 238
 239                        switch (state & ESD_BUSSTATE_MASK) {
 240                        case ESD_BUSSTATE_BUSOFF:
 241                                priv->can.state = CAN_STATE_BUS_OFF;
 242                                cf->can_id |= CAN_ERR_BUSOFF;
 243                                priv->can.can_stats.bus_off++;
 244                                can_bus_off(priv->netdev);
 245                                break;
 246                        case ESD_BUSSTATE_WARN:
 247                                priv->can.state = CAN_STATE_ERROR_WARNING;
 248                                priv->can.can_stats.error_warning++;
 249                                break;
 250                        case ESD_BUSSTATE_ERRPASSIVE:
 251                                priv->can.state = CAN_STATE_ERROR_PASSIVE;
 252                                priv->can.can_stats.error_passive++;
 253                                break;
 254                        default:
 255                                priv->can.state = CAN_STATE_ERROR_ACTIVE;
 256                                break;
 257                        }
 258                } else {
 259                        priv->can.can_stats.bus_error++;
 260                        stats->rx_errors++;
 261
 262                        cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
 263
 264                        switch (ecc & SJA1000_ECC_MASK) {
 265                        case SJA1000_ECC_BIT:
 266                                cf->data[2] |= CAN_ERR_PROT_BIT;
 267                                break;
 268                        case SJA1000_ECC_FORM:
 269                                cf->data[2] |= CAN_ERR_PROT_FORM;
 270                                break;
 271                        case SJA1000_ECC_STUFF:
 272                                cf->data[2] |= CAN_ERR_PROT_STUFF;
 273                                break;
 274                        default:
 275                                cf->data[3] = ecc & SJA1000_ECC_SEG;
 276                                break;
 277                        }
 278
 279                        /* Error occurred during transmission? */
 280                        if (!(ecc & SJA1000_ECC_DIR))
 281                                cf->data[2] |= CAN_ERR_PROT_TX;
 282
 283                        if (priv->can.state == CAN_STATE_ERROR_WARNING ||
 284                            priv->can.state == CAN_STATE_ERROR_PASSIVE) {
 285                                cf->data[1] = (txerr > rxerr) ?
 286                                        CAN_ERR_CRTL_TX_PASSIVE :
 287                                        CAN_ERR_CRTL_RX_PASSIVE;
 288                        }
 289                        cf->data[6] = txerr;
 290                        cf->data[7] = rxerr;
 291                }
 292
 293                priv->bec.txerr = txerr;
 294                priv->bec.rxerr = rxerr;
 295
 296                stats->rx_packets++;
 297                stats->rx_bytes += cf->len;
 298                netif_rx(skb);
 299        }
 300}
 301
 302static void esd_usb2_rx_can_msg(struct esd_usb2_net_priv *priv,
 303                                struct esd_usb2_msg *msg)
 304{
 305        struct net_device_stats *stats = &priv->netdev->stats;
 306        struct can_frame *cf;
 307        struct sk_buff *skb;
 308        int i;
 309        u32 id;
 310
 311        if (!netif_device_present(priv->netdev))
 312                return;
 313
 314        id = le32_to_cpu(msg->msg.rx.id);
 315
 316        if (id & ESD_EVENT) {
 317                esd_usb2_rx_event(priv, msg);
 318        } else {
 319                skb = alloc_can_skb(priv->netdev, &cf);
 320                if (skb == NULL) {
 321                        stats->rx_dropped++;
 322                        return;
 323                }
 324
 325                cf->can_id = id & ESD_IDMASK;
 326                can_frame_set_cc_len(cf, msg->msg.rx.dlc & ~ESD_RTR,
 327                                     priv->can.ctrlmode);
 328
 329                if (id & ESD_EXTID)
 330                        cf->can_id |= CAN_EFF_FLAG;
 331
 332                if (msg->msg.rx.dlc & ESD_RTR) {
 333                        cf->can_id |= CAN_RTR_FLAG;
 334                } else {
 335                        for (i = 0; i < cf->len; i++)
 336                                cf->data[i] = msg->msg.rx.data[i];
 337                }
 338
 339                stats->rx_packets++;
 340                stats->rx_bytes += cf->len;
 341                netif_rx(skb);
 342        }
 343
 344        return;
 345}
 346
 347static void esd_usb2_tx_done_msg(struct esd_usb2_net_priv *priv,
 348                                 struct esd_usb2_msg *msg)
 349{
 350        struct net_device_stats *stats = &priv->netdev->stats;
 351        struct net_device *netdev = priv->netdev;
 352        struct esd_tx_urb_context *context;
 353
 354        if (!netif_device_present(netdev))
 355                return;
 356
 357        context = &priv->tx_contexts[msg->msg.txdone.hnd & (MAX_TX_URBS - 1)];
 358
 359        if (!msg->msg.txdone.status) {
 360                stats->tx_packets++;
 361                stats->tx_bytes += context->len;
 362                can_get_echo_skb(netdev, context->echo_index, NULL);
 363        } else {
 364                stats->tx_errors++;
 365                can_free_echo_skb(netdev, context->echo_index, NULL);
 366        }
 367
 368        /* Release context */
 369        context->echo_index = MAX_TX_URBS;
 370        atomic_dec(&priv->active_tx_jobs);
 371
 372        netif_wake_queue(netdev);
 373}
 374
 375static void esd_usb2_read_bulk_callback(struct urb *urb)
 376{
 377        struct esd_usb2 *dev = urb->context;
 378        int retval;
 379        int pos = 0;
 380        int i;
 381
 382        switch (urb->status) {
 383        case 0: /* success */
 384                break;
 385
 386        case -ENOENT:
 387        case -EPIPE:
 388        case -EPROTO:
 389        case -ESHUTDOWN:
 390                return;
 391
 392        default:
 393                dev_info(dev->udev->dev.parent,
 394                         "Rx URB aborted (%d)\n", urb->status);
 395                goto resubmit_urb;
 396        }
 397
 398        while (pos < urb->actual_length) {
 399                struct esd_usb2_msg *msg;
 400
 401                msg = (struct esd_usb2_msg *)(urb->transfer_buffer + pos);
 402
 403                switch (msg->msg.hdr.cmd) {
 404                case CMD_CAN_RX:
 405                        if (msg->msg.rx.net >= dev->net_count) {
 406                                dev_err(dev->udev->dev.parent, "format error\n");
 407                                break;
 408                        }
 409
 410                        esd_usb2_rx_can_msg(dev->nets[msg->msg.rx.net], msg);
 411                        break;
 412
 413                case CMD_CAN_TX:
 414                        if (msg->msg.txdone.net >= dev->net_count) {
 415                                dev_err(dev->udev->dev.parent, "format error\n");
 416                                break;
 417                        }
 418
 419                        esd_usb2_tx_done_msg(dev->nets[msg->msg.txdone.net],
 420                                             msg);
 421                        break;
 422                }
 423
 424                pos += msg->msg.hdr.len << 2;
 425
 426                if (pos > urb->actual_length) {
 427                        dev_err(dev->udev->dev.parent, "format error\n");
 428                        break;
 429                }
 430        }
 431
 432resubmit_urb:
 433        usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
 434                          urb->transfer_buffer, RX_BUFFER_SIZE,
 435                          esd_usb2_read_bulk_callback, dev);
 436
 437        retval = usb_submit_urb(urb, GFP_ATOMIC);
 438        if (retval == -ENODEV) {
 439                for (i = 0; i < dev->net_count; i++) {
 440                        if (dev->nets[i])
 441                                netif_device_detach(dev->nets[i]->netdev);
 442                }
 443        } else if (retval) {
 444                dev_err(dev->udev->dev.parent,
 445                        "failed resubmitting read bulk urb: %d\n", retval);
 446        }
 447
 448        return;
 449}
 450
 451/*
 452 * callback for bulk IN urb
 453 */
 454static void esd_usb2_write_bulk_callback(struct urb *urb)
 455{
 456        struct esd_tx_urb_context *context = urb->context;
 457        struct esd_usb2_net_priv *priv;
 458        struct net_device *netdev;
 459        size_t size = sizeof(struct esd_usb2_msg);
 460
 461        WARN_ON(!context);
 462
 463        priv = context->priv;
 464        netdev = priv->netdev;
 465
 466        /* free up our allocated buffer */
 467        usb_free_coherent(urb->dev, size,
 468                          urb->transfer_buffer, urb->transfer_dma);
 469
 470        if (!netif_device_present(netdev))
 471                return;
 472
 473        if (urb->status)
 474                netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
 475
 476        netif_trans_update(netdev);
 477}
 478
 479static ssize_t show_firmware(struct device *d,
 480                             struct device_attribute *attr, char *buf)
 481{
 482        struct usb_interface *intf = to_usb_interface(d);
 483        struct esd_usb2 *dev = usb_get_intfdata(intf);
 484
 485        return sprintf(buf, "%d.%d.%d\n",
 486                       (dev->version >> 12) & 0xf,
 487                       (dev->version >> 8) & 0xf,
 488                       dev->version & 0xff);
 489}
 490static DEVICE_ATTR(firmware, 0444, show_firmware, NULL);
 491
 492static ssize_t show_hardware(struct device *d,
 493                             struct device_attribute *attr, char *buf)
 494{
 495        struct usb_interface *intf = to_usb_interface(d);
 496        struct esd_usb2 *dev = usb_get_intfdata(intf);
 497
 498        return sprintf(buf, "%d.%d.%d\n",
 499                       (dev->version >> 28) & 0xf,
 500                       (dev->version >> 24) & 0xf,
 501                       (dev->version >> 16) & 0xff);
 502}
 503static DEVICE_ATTR(hardware, 0444, show_hardware, NULL);
 504
 505static ssize_t show_nets(struct device *d,
 506                         struct device_attribute *attr, char *buf)
 507{
 508        struct usb_interface *intf = to_usb_interface(d);
 509        struct esd_usb2 *dev = usb_get_intfdata(intf);
 510
 511        return sprintf(buf, "%d", dev->net_count);
 512}
 513static DEVICE_ATTR(nets, 0444, show_nets, NULL);
 514
 515static int esd_usb2_send_msg(struct esd_usb2 *dev, struct esd_usb2_msg *msg)
 516{
 517        int actual_length;
 518
 519        return usb_bulk_msg(dev->udev,
 520                            usb_sndbulkpipe(dev->udev, 2),
 521                            msg,
 522                            msg->msg.hdr.len << 2,
 523                            &actual_length,
 524                            1000);
 525}
 526
 527static int esd_usb2_wait_msg(struct esd_usb2 *dev,
 528                             struct esd_usb2_msg *msg)
 529{
 530        int actual_length;
 531
 532        return usb_bulk_msg(dev->udev,
 533                            usb_rcvbulkpipe(dev->udev, 1),
 534                            msg,
 535                            sizeof(*msg),
 536                            &actual_length,
 537                            1000);
 538}
 539
 540static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev)
 541{
 542        int i, err = 0;
 543
 544        if (dev->rxinitdone)
 545                return 0;
 546
 547        for (i = 0; i < MAX_RX_URBS; i++) {
 548                struct urb *urb = NULL;
 549                u8 *buf = NULL;
 550                dma_addr_t buf_dma;
 551
 552                /* create a URB, and a buffer for it */
 553                urb = usb_alloc_urb(0, GFP_KERNEL);
 554                if (!urb) {
 555                        err = -ENOMEM;
 556                        break;
 557                }
 558
 559                buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
 560                                         &buf_dma);
 561                if (!buf) {
 562                        dev_warn(dev->udev->dev.parent,
 563                                 "No memory left for USB buffer\n");
 564                        err = -ENOMEM;
 565                        goto freeurb;
 566                }
 567
 568                urb->transfer_dma = buf_dma;
 569
 570                usb_fill_bulk_urb(urb, dev->udev,
 571                                  usb_rcvbulkpipe(dev->udev, 1),
 572                                  buf, RX_BUFFER_SIZE,
 573                                  esd_usb2_read_bulk_callback, dev);
 574                urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
 575                usb_anchor_urb(urb, &dev->rx_submitted);
 576
 577                err = usb_submit_urb(urb, GFP_KERNEL);
 578                if (err) {
 579                        usb_unanchor_urb(urb);
 580                        usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
 581                                          urb->transfer_dma);
 582                        goto freeurb;
 583                }
 584
 585                dev->rxbuf[i] = buf;
 586                dev->rxbuf_dma[i] = buf_dma;
 587
 588freeurb:
 589                /* Drop reference, USB core will take care of freeing it */
 590                usb_free_urb(urb);
 591                if (err)
 592                        break;
 593        }
 594
 595        /* Did we submit any URBs */
 596        if (i == 0) {
 597                dev_err(dev->udev->dev.parent, "couldn't setup read URBs\n");
 598                return err;
 599        }
 600
 601        /* Warn if we've couldn't transmit all the URBs */
 602        if (i < MAX_RX_URBS) {
 603                dev_warn(dev->udev->dev.parent,
 604                         "rx performance may be slow\n");
 605        }
 606
 607        dev->rxinitdone = 1;
 608        return 0;
 609}
 610
 611/*
 612 * Start interface
 613 */
 614static int esd_usb2_start(struct esd_usb2_net_priv *priv)
 615{
 616        struct esd_usb2 *dev = priv->usb2;
 617        struct net_device *netdev = priv->netdev;
 618        struct esd_usb2_msg *msg;
 619        int err, i;
 620
 621        msg = kmalloc(sizeof(*msg), GFP_KERNEL);
 622        if (!msg) {
 623                err = -ENOMEM;
 624                goto out;
 625        }
 626
 627        /*
 628         * Enable all IDs
 629         * The IDADD message takes up to 64 32 bit bitmasks (2048 bits).
 630         * Each bit represents one 11 bit CAN identifier. A set bit
 631         * enables reception of the corresponding CAN identifier. A cleared
 632         * bit disabled this identifier. An additional bitmask value
 633         * following the CAN 2.0A bits is used to enable reception of
 634         * extended CAN frames. Only the LSB of this final mask is checked
 635         * for the complete 29 bit ID range. The IDADD message also allows
 636         * filter configuration for an ID subset. In this case you can add
 637         * the number of the starting bitmask (0..64) to the filter.option
 638         * field followed by only some bitmasks.
 639         */
 640        msg->msg.hdr.cmd = CMD_IDADD;
 641        msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
 642        msg->msg.filter.net = priv->index;
 643        msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
 644        for (i = 0; i < ESD_MAX_ID_SEGMENT; i++)
 645                msg->msg.filter.mask[i] = cpu_to_le32(0xffffffff);
 646        /* enable 29bit extended IDs */
 647        msg->msg.filter.mask[ESD_MAX_ID_SEGMENT] = cpu_to_le32(0x00000001);
 648
 649        err = esd_usb2_send_msg(dev, msg);
 650        if (err)
 651                goto out;
 652
 653        err = esd_usb2_setup_rx_urbs(dev);
 654        if (err)
 655                goto out;
 656
 657        priv->can.state = CAN_STATE_ERROR_ACTIVE;
 658
 659out:
 660        if (err == -ENODEV)
 661                netif_device_detach(netdev);
 662        if (err)
 663                netdev_err(netdev, "couldn't start device: %d\n", err);
 664
 665        kfree(msg);
 666        return err;
 667}
 668
 669static void unlink_all_urbs(struct esd_usb2 *dev)
 670{
 671        struct esd_usb2_net_priv *priv;
 672        int i, j;
 673
 674        usb_kill_anchored_urbs(&dev->rx_submitted);
 675
 676        for (i = 0; i < MAX_RX_URBS; ++i)
 677                usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
 678                                  dev->rxbuf[i], dev->rxbuf_dma[i]);
 679
 680        for (i = 0; i < dev->net_count; i++) {
 681                priv = dev->nets[i];
 682                if (priv) {
 683                        usb_kill_anchored_urbs(&priv->tx_submitted);
 684                        atomic_set(&priv->active_tx_jobs, 0);
 685
 686                        for (j = 0; j < MAX_TX_URBS; j++)
 687                                priv->tx_contexts[j].echo_index = MAX_TX_URBS;
 688                }
 689        }
 690}
 691
 692static int esd_usb2_open(struct net_device *netdev)
 693{
 694        struct esd_usb2_net_priv *priv = netdev_priv(netdev);
 695        int err;
 696
 697        /* common open */
 698        err = open_candev(netdev);
 699        if (err)
 700                return err;
 701
 702        /* finally start device */
 703        err = esd_usb2_start(priv);
 704        if (err) {
 705                netdev_warn(netdev, "couldn't start device: %d\n", err);
 706                close_candev(netdev);
 707                return err;
 708        }
 709
 710        netif_start_queue(netdev);
 711
 712        return 0;
 713}
 714
 715static netdev_tx_t esd_usb2_start_xmit(struct sk_buff *skb,
 716                                      struct net_device *netdev)
 717{
 718        struct esd_usb2_net_priv *priv = netdev_priv(netdev);
 719        struct esd_usb2 *dev = priv->usb2;
 720        struct esd_tx_urb_context *context = NULL;
 721        struct net_device_stats *stats = &netdev->stats;
 722        struct can_frame *cf = (struct can_frame *)skb->data;
 723        struct esd_usb2_msg *msg;
 724        struct urb *urb;
 725        u8 *buf;
 726        int i, err;
 727        int ret = NETDEV_TX_OK;
 728        size_t size = sizeof(struct esd_usb2_msg);
 729
 730        if (can_dropped_invalid_skb(netdev, skb))
 731                return NETDEV_TX_OK;
 732
 733        /* create a URB, and a buffer for it, and copy the data to the URB */
 734        urb = usb_alloc_urb(0, GFP_ATOMIC);
 735        if (!urb) {
 736                stats->tx_dropped++;
 737                dev_kfree_skb(skb);
 738                goto nourbmem;
 739        }
 740
 741        buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC,
 742                                 &urb->transfer_dma);
 743        if (!buf) {
 744                netdev_err(netdev, "No memory left for USB buffer\n");
 745                stats->tx_dropped++;
 746                dev_kfree_skb(skb);
 747                goto nobufmem;
 748        }
 749
 750        msg = (struct esd_usb2_msg *)buf;
 751
 752        msg->msg.hdr.len = 3; /* minimal length */
 753        msg->msg.hdr.cmd = CMD_CAN_TX;
 754        msg->msg.tx.net = priv->index;
 755        msg->msg.tx.dlc = can_get_cc_dlc(cf, priv->can.ctrlmode);
 756        msg->msg.tx.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
 757
 758        if (cf->can_id & CAN_RTR_FLAG)
 759                msg->msg.tx.dlc |= ESD_RTR;
 760
 761        if (cf->can_id & CAN_EFF_FLAG)
 762                msg->msg.tx.id |= cpu_to_le32(ESD_EXTID);
 763
 764        for (i = 0; i < cf->len; i++)
 765                msg->msg.tx.data[i] = cf->data[i];
 766
 767        msg->msg.hdr.len += (cf->len + 3) >> 2;
 768
 769        for (i = 0; i < MAX_TX_URBS; i++) {
 770                if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
 771                        context = &priv->tx_contexts[i];
 772                        break;
 773                }
 774        }
 775
 776        /*
 777         * This may never happen.
 778         */
 779        if (!context) {
 780                netdev_warn(netdev, "couldn't find free context\n");
 781                ret = NETDEV_TX_BUSY;
 782                goto releasebuf;
 783        }
 784
 785        context->priv = priv;
 786        context->echo_index = i;
 787        context->len = cf->len;
 788
 789        /* hnd must not be 0 - MSB is stripped in txdone handling */
 790        msg->msg.tx.hnd = 0x80000000 | i; /* returned in TX done message */
 791
 792        usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
 793                          msg->msg.hdr.len << 2,
 794                          esd_usb2_write_bulk_callback, context);
 795
 796        urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
 797
 798        usb_anchor_urb(urb, &priv->tx_submitted);
 799
 800        can_put_echo_skb(skb, netdev, context->echo_index, 0);
 801
 802        atomic_inc(&priv->active_tx_jobs);
 803
 804        /* Slow down tx path */
 805        if (atomic_read(&priv->active_tx_jobs) >= MAX_TX_URBS)
 806                netif_stop_queue(netdev);
 807
 808        err = usb_submit_urb(urb, GFP_ATOMIC);
 809        if (err) {
 810                can_free_echo_skb(netdev, context->echo_index, NULL);
 811
 812                atomic_dec(&priv->active_tx_jobs);
 813                usb_unanchor_urb(urb);
 814
 815                stats->tx_dropped++;
 816
 817                if (err == -ENODEV)
 818                        netif_device_detach(netdev);
 819                else
 820                        netdev_warn(netdev, "failed tx_urb %d\n", err);
 821
 822                goto releasebuf;
 823        }
 824
 825        netif_trans_update(netdev);
 826
 827        /*
 828         * Release our reference to this URB, the USB core will eventually free
 829         * it entirely.
 830         */
 831        usb_free_urb(urb);
 832
 833        return NETDEV_TX_OK;
 834
 835releasebuf:
 836        usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
 837
 838nobufmem:
 839        usb_free_urb(urb);
 840
 841nourbmem:
 842        return ret;
 843}
 844
 845static int esd_usb2_close(struct net_device *netdev)
 846{
 847        struct esd_usb2_net_priv *priv = netdev_priv(netdev);
 848        struct esd_usb2_msg *msg;
 849        int i;
 850
 851        msg = kmalloc(sizeof(*msg), GFP_KERNEL);
 852        if (!msg)
 853                return -ENOMEM;
 854
 855        /* Disable all IDs (see esd_usb2_start()) */
 856        msg->msg.hdr.cmd = CMD_IDADD;
 857        msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
 858        msg->msg.filter.net = priv->index;
 859        msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
 860        for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++)
 861                msg->msg.filter.mask[i] = 0;
 862        if (esd_usb2_send_msg(priv->usb2, msg) < 0)
 863                netdev_err(netdev, "sending idadd message failed\n");
 864
 865        /* set CAN controller to reset mode */
 866        msg->msg.hdr.len = 2;
 867        msg->msg.hdr.cmd = CMD_SETBAUD;
 868        msg->msg.setbaud.net = priv->index;
 869        msg->msg.setbaud.rsvd = 0;
 870        msg->msg.setbaud.baud = cpu_to_le32(ESD_USB2_NO_BAUDRATE);
 871        if (esd_usb2_send_msg(priv->usb2, msg) < 0)
 872                netdev_err(netdev, "sending setbaud message failed\n");
 873
 874        priv->can.state = CAN_STATE_STOPPED;
 875
 876        netif_stop_queue(netdev);
 877
 878        close_candev(netdev);
 879
 880        kfree(msg);
 881
 882        return 0;
 883}
 884
 885static const struct net_device_ops esd_usb2_netdev_ops = {
 886        .ndo_open = esd_usb2_open,
 887        .ndo_stop = esd_usb2_close,
 888        .ndo_start_xmit = esd_usb2_start_xmit,
 889        .ndo_change_mtu = can_change_mtu,
 890};
 891
 892static const struct can_bittiming_const esd_usb2_bittiming_const = {
 893        .name = "esd_usb2",
 894        .tseg1_min = ESD_USB2_TSEG1_MIN,
 895        .tseg1_max = ESD_USB2_TSEG1_MAX,
 896        .tseg2_min = ESD_USB2_TSEG2_MIN,
 897        .tseg2_max = ESD_USB2_TSEG2_MAX,
 898        .sjw_max = ESD_USB2_SJW_MAX,
 899        .brp_min = ESD_USB2_BRP_MIN,
 900        .brp_max = ESD_USB2_BRP_MAX,
 901        .brp_inc = ESD_USB2_BRP_INC,
 902};
 903
 904static int esd_usb2_set_bittiming(struct net_device *netdev)
 905{
 906        struct esd_usb2_net_priv *priv = netdev_priv(netdev);
 907        struct can_bittiming *bt = &priv->can.bittiming;
 908        struct esd_usb2_msg *msg;
 909        int err;
 910        u32 canbtr;
 911        int sjw_shift;
 912
 913        canbtr = ESD_USB2_UBR;
 914        if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
 915                canbtr |= ESD_USB2_LOM;
 916
 917        canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1);
 918
 919        if (le16_to_cpu(priv->usb2->udev->descriptor.idProduct) ==
 920            USB_CANUSBM_PRODUCT_ID)
 921                sjw_shift = ESD_USBM_SJW_SHIFT;
 922        else
 923                sjw_shift = ESD_USB2_SJW_SHIFT;
 924
 925        canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1))
 926                << sjw_shift;
 927        canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1)
 928                   & (ESD_USB2_TSEG1_MAX - 1))
 929                << ESD_USB2_TSEG1_SHIFT;
 930        canbtr |= ((bt->phase_seg2 - 1) & (ESD_USB2_TSEG2_MAX - 1))
 931                << ESD_USB2_TSEG2_SHIFT;
 932        if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
 933                canbtr |= ESD_USB2_3_SAMPLES;
 934
 935        msg = kmalloc(sizeof(*msg), GFP_KERNEL);
 936        if (!msg)
 937                return -ENOMEM;
 938
 939        msg->msg.hdr.len = 2;
 940        msg->msg.hdr.cmd = CMD_SETBAUD;
 941        msg->msg.setbaud.net = priv->index;
 942        msg->msg.setbaud.rsvd = 0;
 943        msg->msg.setbaud.baud = cpu_to_le32(canbtr);
 944
 945        netdev_info(netdev, "setting BTR=%#x\n", canbtr);
 946
 947        err = esd_usb2_send_msg(priv->usb2, msg);
 948
 949        kfree(msg);
 950        return err;
 951}
 952
 953static int esd_usb2_get_berr_counter(const struct net_device *netdev,
 954                                     struct can_berr_counter *bec)
 955{
 956        struct esd_usb2_net_priv *priv = netdev_priv(netdev);
 957
 958        bec->txerr = priv->bec.txerr;
 959        bec->rxerr = priv->bec.rxerr;
 960
 961        return 0;
 962}
 963
 964static int esd_usb2_set_mode(struct net_device *netdev, enum can_mode mode)
 965{
 966        switch (mode) {
 967        case CAN_MODE_START:
 968                netif_wake_queue(netdev);
 969                break;
 970
 971        default:
 972                return -EOPNOTSUPP;
 973        }
 974
 975        return 0;
 976}
 977
 978static int esd_usb2_probe_one_net(struct usb_interface *intf, int index)
 979{
 980        struct esd_usb2 *dev = usb_get_intfdata(intf);
 981        struct net_device *netdev;
 982        struct esd_usb2_net_priv *priv;
 983        int err = 0;
 984        int i;
 985
 986        netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
 987        if (!netdev) {
 988                dev_err(&intf->dev, "couldn't alloc candev\n");
 989                err = -ENOMEM;
 990                goto done;
 991        }
 992
 993        priv = netdev_priv(netdev);
 994
 995        init_usb_anchor(&priv->tx_submitted);
 996        atomic_set(&priv->active_tx_jobs, 0);
 997
 998        for (i = 0; i < MAX_TX_URBS; i++)
 999                priv->tx_contexts[i].echo_index = MAX_TX_URBS;
1000
1001        priv->usb2 = dev;
1002        priv->netdev = netdev;
1003        priv->index = index;
1004
1005        priv->can.state = CAN_STATE_STOPPED;
1006        priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY |
1007                CAN_CTRLMODE_CC_LEN8_DLC;
1008
1009        if (le16_to_cpu(dev->udev->descriptor.idProduct) ==
1010            USB_CANUSBM_PRODUCT_ID)
1011                priv->can.clock.freq = ESD_USBM_CAN_CLOCK;
1012        else {
1013                priv->can.clock.freq = ESD_USB2_CAN_CLOCK;
1014                priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
1015        }
1016
1017        priv->can.bittiming_const = &esd_usb2_bittiming_const;
1018        priv->can.do_set_bittiming = esd_usb2_set_bittiming;
1019        priv->can.do_set_mode = esd_usb2_set_mode;
1020        priv->can.do_get_berr_counter = esd_usb2_get_berr_counter;
1021
1022        netdev->flags |= IFF_ECHO; /* we support local echo */
1023
1024        netdev->netdev_ops = &esd_usb2_netdev_ops;
1025
1026        SET_NETDEV_DEV(netdev, &intf->dev);
1027        netdev->dev_id = index;
1028
1029        err = register_candev(netdev);
1030        if (err) {
1031                dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
1032                free_candev(netdev);
1033                err = -ENOMEM;
1034                goto done;
1035        }
1036
1037        dev->nets[index] = priv;
1038        netdev_info(netdev, "device %s registered\n", netdev->name);
1039
1040done:
1041        return err;
1042}
1043
1044/*
1045 * probe function for new USB2 devices
1046 *
1047 * check version information and number of available
1048 * CAN interfaces
1049 */
1050static int esd_usb2_probe(struct usb_interface *intf,
1051                         const struct usb_device_id *id)
1052{
1053        struct esd_usb2 *dev;
1054        struct esd_usb2_msg *msg;
1055        int i, err;
1056
1057        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1058        if (!dev) {
1059                err = -ENOMEM;
1060                goto done;
1061        }
1062
1063        dev->udev = interface_to_usbdev(intf);
1064
1065        init_usb_anchor(&dev->rx_submitted);
1066
1067        usb_set_intfdata(intf, dev);
1068
1069        msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1070        if (!msg) {
1071                err = -ENOMEM;
1072                goto free_msg;
1073        }
1074
1075        /* query number of CAN interfaces (nets) */
1076        msg->msg.hdr.cmd = CMD_VERSION;
1077        msg->msg.hdr.len = 2;
1078        msg->msg.version.rsvd = 0;
1079        msg->msg.version.flags = 0;
1080        msg->msg.version.drv_version = 0;
1081
1082        err = esd_usb2_send_msg(dev, msg);
1083        if (err < 0) {
1084                dev_err(&intf->dev, "sending version message failed\n");
1085                goto free_msg;
1086        }
1087
1088        err = esd_usb2_wait_msg(dev, msg);
1089        if (err < 0) {
1090                dev_err(&intf->dev, "no version message answer\n");
1091                goto free_msg;
1092        }
1093
1094        dev->net_count = (int)msg->msg.version_reply.nets;
1095        dev->version = le32_to_cpu(msg->msg.version_reply.version);
1096
1097        if (device_create_file(&intf->dev, &dev_attr_firmware))
1098                dev_err(&intf->dev,
1099                        "Couldn't create device file for firmware\n");
1100
1101        if (device_create_file(&intf->dev, &dev_attr_hardware))
1102                dev_err(&intf->dev,
1103                        "Couldn't create device file for hardware\n");
1104
1105        if (device_create_file(&intf->dev, &dev_attr_nets))
1106                dev_err(&intf->dev,
1107                        "Couldn't create device file for nets\n");
1108
1109        /* do per device probing */
1110        for (i = 0; i < dev->net_count; i++)
1111                esd_usb2_probe_one_net(intf, i);
1112
1113free_msg:
1114        kfree(msg);
1115        if (err)
1116                kfree(dev);
1117done:
1118        return err;
1119}
1120
1121/*
1122 * called by the usb core when the device is removed from the system
1123 */
1124static void esd_usb2_disconnect(struct usb_interface *intf)
1125{
1126        struct esd_usb2 *dev = usb_get_intfdata(intf);
1127        struct net_device *netdev;
1128        int i;
1129
1130        device_remove_file(&intf->dev, &dev_attr_firmware);
1131        device_remove_file(&intf->dev, &dev_attr_hardware);
1132        device_remove_file(&intf->dev, &dev_attr_nets);
1133
1134        usb_set_intfdata(intf, NULL);
1135
1136        if (dev) {
1137                for (i = 0; i < dev->net_count; i++) {
1138                        if (dev->nets[i]) {
1139                                netdev = dev->nets[i]->netdev;
1140                                unregister_netdev(netdev);
1141                                free_candev(netdev);
1142                        }
1143                }
1144                unlink_all_urbs(dev);
1145                kfree(dev);
1146        }
1147}
1148
1149/* usb specific object needed to register this driver with the usb subsystem */
1150static struct usb_driver esd_usb2_driver = {
1151        .name = "esd_usb2",
1152        .probe = esd_usb2_probe,
1153        .disconnect = esd_usb2_disconnect,
1154        .id_table = esd_usb2_table,
1155};
1156
1157module_usb_driver(esd_usb2_driver);
1158