linux/drivers/net/can/usb/peak_usb/pcan_usb_core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * CAN driver for PEAK System USB adapters
   4 * Derived from the PCAN project file driver/src/pcan_usb_core.c
   5 *
   6 * Copyright (C) 2003-2010 PEAK System-Technik GmbH
   7 * Copyright (C) 2010-2012 Stephane Grosjean <s.grosjean@peak-system.com>
   8 *
   9 * Many thanks to Klaus Hitschler <klaus.hitschler@gmx.de>
  10 */
  11#include <linux/init.h>
  12#include <linux/signal.h>
  13#include <linux/slab.h>
  14#include <linux/module.h>
  15#include <linux/netdevice.h>
  16#include <linux/usb.h>
  17
  18#include <linux/can.h>
  19#include <linux/can/dev.h>
  20#include <linux/can/error.h>
  21
  22#include "pcan_usb_core.h"
  23
  24MODULE_AUTHOR("Stephane Grosjean <s.grosjean@peak-system.com>");
  25MODULE_DESCRIPTION("CAN driver for PEAK-System USB adapters");
  26MODULE_LICENSE("GPL v2");
  27
  28/* Table of devices that work with this driver */
  29static struct usb_device_id peak_usb_table[] = {
  30        {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USB_PRODUCT_ID)},
  31        {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPRO_PRODUCT_ID)},
  32        {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBFD_PRODUCT_ID)},
  33        {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPROFD_PRODUCT_ID)},
  34        {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBCHIP_PRODUCT_ID)},
  35        {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBX6_PRODUCT_ID)},
  36        {} /* Terminating entry */
  37};
  38
  39MODULE_DEVICE_TABLE(usb, peak_usb_table);
  40
  41/* List of supported PCAN-USB adapters (NULL terminated list) */
  42static const struct peak_usb_adapter *const peak_usb_adapters_list[] = {
  43        &pcan_usb,
  44        &pcan_usb_pro,
  45        &pcan_usb_fd,
  46        &pcan_usb_pro_fd,
  47        &pcan_usb_chip,
  48        &pcan_usb_x6,
  49};
  50
  51/*
  52 * dump memory
  53 */
  54#define DUMP_WIDTH      16
  55void pcan_dump_mem(char *prompt, void *p, int l)
  56{
  57        pr_info("%s dumping %s (%d bytes):\n",
  58                PCAN_USB_DRIVER_NAME, prompt ? prompt : "memory", l);
  59        print_hex_dump(KERN_INFO, PCAN_USB_DRIVER_NAME " ", DUMP_PREFIX_NONE,
  60                       DUMP_WIDTH, 1, p, l, false);
  61}
  62
  63/*
  64 * initialize a time_ref object with usb adapter own settings
  65 */
  66void peak_usb_init_time_ref(struct peak_time_ref *time_ref,
  67                            const struct peak_usb_adapter *adapter)
  68{
  69        if (time_ref) {
  70                memset(time_ref, 0, sizeof(struct peak_time_ref));
  71                time_ref->adapter = adapter;
  72        }
  73}
  74
  75/*
  76 * sometimes, another now may be  more recent than current one...
  77 */
  78void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
  79{
  80        time_ref->ts_dev_2 = ts_now;
  81
  82        /* should wait at least two passes before computing */
  83        if (ktime_to_ns(time_ref->tv_host) > 0) {
  84                u32 delta_ts = time_ref->ts_dev_2 - time_ref->ts_dev_1;
  85
  86                if (time_ref->ts_dev_2 < time_ref->ts_dev_1)
  87                        delta_ts &= (1 << time_ref->adapter->ts_used_bits) - 1;
  88
  89                time_ref->ts_total += delta_ts;
  90        }
  91}
  92
  93/*
  94 * register device timestamp as now
  95 */
  96void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
  97{
  98        if (ktime_to_ns(time_ref->tv_host_0) == 0) {
  99                /* use monotonic clock to correctly compute further deltas */
 100                time_ref->tv_host_0 = ktime_get();
 101                time_ref->tv_host = ktime_set(0, 0);
 102        } else {
 103                /*
 104                 * delta_us should not be >= 2^32 => delta should be < 4294s
 105                 * handle 32-bits wrapping here: if count of s. reaches 4200,
 106                 * reset counters and change time base
 107                 */
 108                if (ktime_to_ns(time_ref->tv_host)) {
 109                        ktime_t delta = ktime_sub(time_ref->tv_host,
 110                                                  time_ref->tv_host_0);
 111                        if (ktime_to_ns(delta) > (4200ull * NSEC_PER_SEC)) {
 112                                time_ref->tv_host_0 = time_ref->tv_host;
 113                                time_ref->ts_total = 0;
 114                        }
 115                }
 116
 117                time_ref->tv_host = ktime_get();
 118                time_ref->tick_count++;
 119        }
 120
 121        time_ref->ts_dev_1 = time_ref->ts_dev_2;
 122        peak_usb_update_ts_now(time_ref, ts_now);
 123}
 124
 125/*
 126 * compute time according to current ts and time_ref data
 127 */
 128void peak_usb_get_ts_time(struct peak_time_ref *time_ref, u32 ts, ktime_t *time)
 129{
 130        /* protect from getting time before setting now */
 131        if (ktime_to_ns(time_ref->tv_host)) {
 132                u64 delta_us;
 133
 134                delta_us = ts - time_ref->ts_dev_2;
 135                if (ts < time_ref->ts_dev_2)
 136                        delta_us &= (1 << time_ref->adapter->ts_used_bits) - 1;
 137
 138                delta_us += time_ref->ts_total;
 139
 140                delta_us *= time_ref->adapter->us_per_ts_scale;
 141                delta_us >>= time_ref->adapter->us_per_ts_shift;
 142
 143                *time = ktime_add_us(time_ref->tv_host_0, delta_us);
 144        } else {
 145                *time = ktime_get();
 146        }
 147}
 148
 149/*
 150 * post received skb after having set any hw timestamp
 151 */
 152int peak_usb_netif_rx(struct sk_buff *skb,
 153                      struct peak_time_ref *time_ref, u32 ts_low)
 154{
 155        struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
 156
 157        peak_usb_get_ts_time(time_ref, ts_low, &hwts->hwtstamp);
 158
 159        return netif_rx(skb);
 160}
 161
 162/*
 163 * callback for bulk Rx urb
 164 */
 165static void peak_usb_read_bulk_callback(struct urb *urb)
 166{
 167        struct peak_usb_device *dev = urb->context;
 168        struct net_device *netdev;
 169        int err;
 170
 171        netdev = dev->netdev;
 172
 173        if (!netif_device_present(netdev))
 174                return;
 175
 176        /* check reception status */
 177        switch (urb->status) {
 178        case 0:
 179                /* success */
 180                break;
 181
 182        case -EILSEQ:
 183        case -ENOENT:
 184        case -ECONNRESET:
 185        case -ESHUTDOWN:
 186                return;
 187
 188        default:
 189                if (net_ratelimit())
 190                        netdev_err(netdev,
 191                                   "Rx urb aborted (%d)\n", urb->status);
 192                goto resubmit_urb;
 193        }
 194
 195        /* protect from any incoming empty msgs */
 196        if ((urb->actual_length > 0) && (dev->adapter->dev_decode_buf)) {
 197                /* handle these kinds of msgs only if _start callback called */
 198                if (dev->state & PCAN_USB_STATE_STARTED) {
 199                        err = dev->adapter->dev_decode_buf(dev, urb);
 200                        if (err)
 201                                pcan_dump_mem("received usb message",
 202                                              urb->transfer_buffer,
 203                                              urb->transfer_buffer_length);
 204                }
 205        }
 206
 207resubmit_urb:
 208        usb_fill_bulk_urb(urb, dev->udev,
 209                usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
 210                urb->transfer_buffer, dev->adapter->rx_buffer_size,
 211                peak_usb_read_bulk_callback, dev);
 212
 213        usb_anchor_urb(urb, &dev->rx_submitted);
 214        err = usb_submit_urb(urb, GFP_ATOMIC);
 215        if (!err)
 216                return;
 217
 218        usb_unanchor_urb(urb);
 219
 220        if (err == -ENODEV)
 221                netif_device_detach(netdev);
 222        else
 223                netdev_err(netdev, "failed resubmitting read bulk urb: %d\n",
 224                           err);
 225}
 226
 227/*
 228 * callback for bulk Tx urb
 229 */
 230static void peak_usb_write_bulk_callback(struct urb *urb)
 231{
 232        struct peak_tx_urb_context *context = urb->context;
 233        struct peak_usb_device *dev;
 234        struct net_device *netdev;
 235
 236        BUG_ON(!context);
 237
 238        dev = context->dev;
 239        netdev = dev->netdev;
 240
 241        atomic_dec(&dev->active_tx_urbs);
 242
 243        if (!netif_device_present(netdev))
 244                return;
 245
 246        /* check tx status */
 247        switch (urb->status) {
 248        case 0:
 249                /* transmission complete */
 250                netdev->stats.tx_packets++;
 251                netdev->stats.tx_bytes += context->data_len;
 252
 253                /* prevent tx timeout */
 254                netif_trans_update(netdev);
 255                break;
 256
 257        default:
 258                if (net_ratelimit())
 259                        netdev_err(netdev, "Tx urb aborted (%d)\n",
 260                                   urb->status);
 261        case -EPROTO:
 262        case -ENOENT:
 263        case -ECONNRESET:
 264        case -ESHUTDOWN:
 265
 266                break;
 267        }
 268
 269        /* should always release echo skb and corresponding context */
 270        can_get_echo_skb(netdev, context->echo_index);
 271        context->echo_index = PCAN_USB_MAX_TX_URBS;
 272
 273        /* do wakeup tx queue in case of success only */
 274        if (!urb->status)
 275                netif_wake_queue(netdev);
 276}
 277
 278/*
 279 * called by netdev to send one skb on the CAN interface.
 280 */
 281static netdev_tx_t peak_usb_ndo_start_xmit(struct sk_buff *skb,
 282                                           struct net_device *netdev)
 283{
 284        struct peak_usb_device *dev = netdev_priv(netdev);
 285        struct peak_tx_urb_context *context = NULL;
 286        struct net_device_stats *stats = &netdev->stats;
 287        struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
 288        struct urb *urb;
 289        u8 *obuf;
 290        int i, err;
 291        size_t size = dev->adapter->tx_buffer_size;
 292
 293        if (can_dropped_invalid_skb(netdev, skb))
 294                return NETDEV_TX_OK;
 295
 296        for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++)
 297                if (dev->tx_contexts[i].echo_index == PCAN_USB_MAX_TX_URBS) {
 298                        context = dev->tx_contexts + i;
 299                        break;
 300                }
 301
 302        if (!context) {
 303                /* should not occur except during restart */
 304                return NETDEV_TX_BUSY;
 305        }
 306
 307        urb = context->urb;
 308        obuf = urb->transfer_buffer;
 309
 310        err = dev->adapter->dev_encode_msg(dev, skb, obuf, &size);
 311        if (err) {
 312                if (net_ratelimit())
 313                        netdev_err(netdev, "packet dropped\n");
 314                dev_kfree_skb(skb);
 315                stats->tx_dropped++;
 316                return NETDEV_TX_OK;
 317        }
 318
 319        context->echo_index = i;
 320
 321        /* Note: this works with CANFD frames too */
 322        context->data_len = cfd->len;
 323
 324        usb_anchor_urb(urb, &dev->tx_submitted);
 325
 326        can_put_echo_skb(skb, netdev, context->echo_index);
 327
 328        atomic_inc(&dev->active_tx_urbs);
 329
 330        err = usb_submit_urb(urb, GFP_ATOMIC);
 331        if (err) {
 332                can_free_echo_skb(netdev, context->echo_index);
 333
 334                usb_unanchor_urb(urb);
 335
 336                /* this context is not used in fact */
 337                context->echo_index = PCAN_USB_MAX_TX_URBS;
 338
 339                atomic_dec(&dev->active_tx_urbs);
 340
 341                switch (err) {
 342                case -ENODEV:
 343                        netif_device_detach(netdev);
 344                        break;
 345                default:
 346                        netdev_warn(netdev, "tx urb submitting failed err=%d\n",
 347                                    err);
 348                        /* fall through */
 349                case -ENOENT:
 350                        /* cable unplugged */
 351                        stats->tx_dropped++;
 352                }
 353        } else {
 354                netif_trans_update(netdev);
 355
 356                /* slow down tx path */
 357                if (atomic_read(&dev->active_tx_urbs) >= PCAN_USB_MAX_TX_URBS)
 358                        netif_stop_queue(netdev);
 359        }
 360
 361        return NETDEV_TX_OK;
 362}
 363
 364/*
 365 * start the CAN interface.
 366 * Rx and Tx urbs are allocated here. Rx urbs are submitted here.
 367 */
 368static int peak_usb_start(struct peak_usb_device *dev)
 369{
 370        struct net_device *netdev = dev->netdev;
 371        int err, i;
 372
 373        for (i = 0; i < PCAN_USB_MAX_RX_URBS; i++) {
 374                struct urb *urb;
 375                u8 *buf;
 376
 377                /* create a URB, and a buffer for it, to receive usb messages */
 378                urb = usb_alloc_urb(0, GFP_KERNEL);
 379                if (!urb) {
 380                        err = -ENOMEM;
 381                        break;
 382                }
 383
 384                buf = kmalloc(dev->adapter->rx_buffer_size, GFP_KERNEL);
 385                if (!buf) {
 386                        usb_free_urb(urb);
 387                        err = -ENOMEM;
 388                        break;
 389                }
 390
 391                usb_fill_bulk_urb(urb, dev->udev,
 392                        usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
 393                        buf, dev->adapter->rx_buffer_size,
 394                        peak_usb_read_bulk_callback, dev);
 395
 396                /* ask last usb_free_urb() to also kfree() transfer_buffer */
 397                urb->transfer_flags |= URB_FREE_BUFFER;
 398                usb_anchor_urb(urb, &dev->rx_submitted);
 399
 400                err = usb_submit_urb(urb, GFP_KERNEL);
 401                if (err) {
 402                        if (err == -ENODEV)
 403                                netif_device_detach(dev->netdev);
 404
 405                        usb_unanchor_urb(urb);
 406                        kfree(buf);
 407                        usb_free_urb(urb);
 408                        break;
 409                }
 410
 411                /* drop reference, USB core will take care of freeing it */
 412                usb_free_urb(urb);
 413        }
 414
 415        /* did we submit any URBs? Warn if we was not able to submit all urbs */
 416        if (i < PCAN_USB_MAX_RX_URBS) {
 417                if (i == 0) {
 418                        netdev_err(netdev, "couldn't setup any rx URB\n");
 419                        return err;
 420                }
 421
 422                netdev_warn(netdev, "rx performance may be slow\n");
 423        }
 424
 425        /* pre-alloc tx buffers and corresponding urbs */
 426        for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
 427                struct peak_tx_urb_context *context;
 428                struct urb *urb;
 429                u8 *buf;
 430
 431                /* create a URB and a buffer for it, to transmit usb messages */
 432                urb = usb_alloc_urb(0, GFP_KERNEL);
 433                if (!urb) {
 434                        err = -ENOMEM;
 435                        break;
 436                }
 437
 438                buf = kmalloc(dev->adapter->tx_buffer_size, GFP_KERNEL);
 439                if (!buf) {
 440                        usb_free_urb(urb);
 441                        err = -ENOMEM;
 442                        break;
 443                }
 444
 445                context = dev->tx_contexts + i;
 446                context->dev = dev;
 447                context->urb = urb;
 448
 449                usb_fill_bulk_urb(urb, dev->udev,
 450                        usb_sndbulkpipe(dev->udev, dev->ep_msg_out),
 451                        buf, dev->adapter->tx_buffer_size,
 452                        peak_usb_write_bulk_callback, context);
 453
 454                /* ask last usb_free_urb() to also kfree() transfer_buffer */
 455                urb->transfer_flags |= URB_FREE_BUFFER;
 456        }
 457
 458        /* warn if we were not able to allocate enough tx contexts */
 459        if (i < PCAN_USB_MAX_TX_URBS) {
 460                if (i == 0) {
 461                        netdev_err(netdev, "couldn't setup any tx URB\n");
 462                        goto err_tx;
 463                }
 464
 465                netdev_warn(netdev, "tx performance may be slow\n");
 466        }
 467
 468        if (dev->adapter->dev_start) {
 469                err = dev->adapter->dev_start(dev);
 470                if (err)
 471                        goto err_adapter;
 472        }
 473
 474        dev->state |= PCAN_USB_STATE_STARTED;
 475
 476        /* can set bus on now */
 477        if (dev->adapter->dev_set_bus) {
 478                err = dev->adapter->dev_set_bus(dev, 1);
 479                if (err)
 480                        goto err_adapter;
 481        }
 482
 483        dev->can.state = CAN_STATE_ERROR_ACTIVE;
 484
 485        return 0;
 486
 487err_adapter:
 488        if (err == -ENODEV)
 489                netif_device_detach(dev->netdev);
 490
 491        netdev_warn(netdev, "couldn't submit control: %d\n", err);
 492
 493        for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
 494                usb_free_urb(dev->tx_contexts[i].urb);
 495                dev->tx_contexts[i].urb = NULL;
 496        }
 497err_tx:
 498        usb_kill_anchored_urbs(&dev->rx_submitted);
 499
 500        return err;
 501}
 502
 503/*
 504 * called by netdev to open the corresponding CAN interface.
 505 */
 506static int peak_usb_ndo_open(struct net_device *netdev)
 507{
 508        struct peak_usb_device *dev = netdev_priv(netdev);
 509        int err;
 510
 511        /* common open */
 512        err = open_candev(netdev);
 513        if (err)
 514                return err;
 515
 516        /* finally start device */
 517        err = peak_usb_start(dev);
 518        if (err) {
 519                netdev_err(netdev, "couldn't start device: %d\n", err);
 520                close_candev(netdev);
 521                return err;
 522        }
 523
 524        netif_start_queue(netdev);
 525
 526        return 0;
 527}
 528
 529/*
 530 * unlink in-flight Rx and Tx urbs and free their memory.
 531 */
 532static void peak_usb_unlink_all_urbs(struct peak_usb_device *dev)
 533{
 534        int i;
 535
 536        /* free all Rx (submitted) urbs */
 537        usb_kill_anchored_urbs(&dev->rx_submitted);
 538
 539        /* free unsubmitted Tx urbs first */
 540        for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
 541                struct urb *urb = dev->tx_contexts[i].urb;
 542
 543                if (!urb ||
 544                    dev->tx_contexts[i].echo_index != PCAN_USB_MAX_TX_URBS) {
 545                        /*
 546                         * this urb is already released or always submitted,
 547                         * let usb core free by itself
 548                         */
 549                        continue;
 550                }
 551
 552                usb_free_urb(urb);
 553                dev->tx_contexts[i].urb = NULL;
 554        }
 555
 556        /* then free all submitted Tx urbs */
 557        usb_kill_anchored_urbs(&dev->tx_submitted);
 558        atomic_set(&dev->active_tx_urbs, 0);
 559}
 560
 561/*
 562 * called by netdev to close the corresponding CAN interface.
 563 */
 564static int peak_usb_ndo_stop(struct net_device *netdev)
 565{
 566        struct peak_usb_device *dev = netdev_priv(netdev);
 567
 568        dev->state &= ~PCAN_USB_STATE_STARTED;
 569        netif_stop_queue(netdev);
 570
 571        close_candev(netdev);
 572
 573        dev->can.state = CAN_STATE_STOPPED;
 574
 575        /* unlink all pending urbs and free used memory */
 576        peak_usb_unlink_all_urbs(dev);
 577
 578        if (dev->adapter->dev_stop)
 579                dev->adapter->dev_stop(dev);
 580
 581        /* can set bus off now */
 582        if (dev->adapter->dev_set_bus) {
 583                int err = dev->adapter->dev_set_bus(dev, 0);
 584                if (err)
 585                        return err;
 586        }
 587
 588        return 0;
 589}
 590
 591/*
 592 * handle end of waiting for the device to reset
 593 */
 594void peak_usb_restart_complete(struct peak_usb_device *dev)
 595{
 596        /* finally MUST update can state */
 597        dev->can.state = CAN_STATE_ERROR_ACTIVE;
 598
 599        /* netdev queue can be awaken now */
 600        netif_wake_queue(dev->netdev);
 601}
 602
 603void peak_usb_async_complete(struct urb *urb)
 604{
 605        kfree(urb->transfer_buffer);
 606        usb_free_urb(urb);
 607}
 608
 609/*
 610 * device (auto-)restart mechanism runs in a timer context =>
 611 * MUST handle restart with asynchronous usb transfers
 612 */
 613static int peak_usb_restart(struct peak_usb_device *dev)
 614{
 615        struct urb *urb;
 616        int err;
 617        u8 *buf;
 618
 619        /*
 620         * if device doesn't define any asynchronous restart handler, simply
 621         * wake the netdev queue up
 622         */
 623        if (!dev->adapter->dev_restart_async) {
 624                peak_usb_restart_complete(dev);
 625                return 0;
 626        }
 627
 628        /* first allocate a urb to handle the asynchronous steps */
 629        urb = usb_alloc_urb(0, GFP_ATOMIC);
 630        if (!urb)
 631                return -ENOMEM;
 632
 633        /* also allocate enough space for the commands to send */
 634        buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_ATOMIC);
 635        if (!buf) {
 636                usb_free_urb(urb);
 637                return -ENOMEM;
 638        }
 639
 640        /* call the device specific handler for the restart */
 641        err = dev->adapter->dev_restart_async(dev, urb, buf);
 642        if (!err)
 643                return 0;
 644
 645        kfree(buf);
 646        usb_free_urb(urb);
 647
 648        return err;
 649}
 650
 651/*
 652 * candev callback used to change CAN mode.
 653 * Warning: this is called from a timer context!
 654 */
 655static int peak_usb_set_mode(struct net_device *netdev, enum can_mode mode)
 656{
 657        struct peak_usb_device *dev = netdev_priv(netdev);
 658        int err = 0;
 659
 660        switch (mode) {
 661        case CAN_MODE_START:
 662                err = peak_usb_restart(dev);
 663                if (err)
 664                        netdev_err(netdev, "couldn't start device (err %d)\n",
 665                                   err);
 666                break;
 667
 668        default:
 669                return -EOPNOTSUPP;
 670        }
 671
 672        return err;
 673}
 674
 675/*
 676 * candev callback used to set device nominal/arbitration bitrate.
 677 */
 678static int peak_usb_set_bittiming(struct net_device *netdev)
 679{
 680        struct peak_usb_device *dev = netdev_priv(netdev);
 681        const struct peak_usb_adapter *pa = dev->adapter;
 682
 683        if (pa->dev_set_bittiming) {
 684                struct can_bittiming *bt = &dev->can.bittiming;
 685                int err = pa->dev_set_bittiming(dev, bt);
 686
 687                if (err)
 688                        netdev_info(netdev, "couldn't set bitrate (err %d)\n",
 689                                    err);
 690                return err;
 691        }
 692
 693        return 0;
 694}
 695
 696/*
 697 * candev callback used to set device data bitrate.
 698 */
 699static int peak_usb_set_data_bittiming(struct net_device *netdev)
 700{
 701        struct peak_usb_device *dev = netdev_priv(netdev);
 702        const struct peak_usb_adapter *pa = dev->adapter;
 703
 704        if (pa->dev_set_data_bittiming) {
 705                struct can_bittiming *bt = &dev->can.data_bittiming;
 706                int err = pa->dev_set_data_bittiming(dev, bt);
 707
 708                if (err)
 709                        netdev_info(netdev,
 710                                    "couldn't set data bitrate (err %d)\n",
 711                                    err);
 712
 713                return err;
 714        }
 715
 716        return 0;
 717}
 718
 719static const struct net_device_ops peak_usb_netdev_ops = {
 720        .ndo_open = peak_usb_ndo_open,
 721        .ndo_stop = peak_usb_ndo_stop,
 722        .ndo_start_xmit = peak_usb_ndo_start_xmit,
 723        .ndo_change_mtu = can_change_mtu,
 724};
 725
 726/*
 727 * create one device which is attached to CAN controller #ctrl_idx of the
 728 * usb adapter.
 729 */
 730static int peak_usb_create_dev(const struct peak_usb_adapter *peak_usb_adapter,
 731                               struct usb_interface *intf, int ctrl_idx)
 732{
 733        struct usb_device *usb_dev = interface_to_usbdev(intf);
 734        int sizeof_candev = peak_usb_adapter->sizeof_dev_private;
 735        struct peak_usb_device *dev;
 736        struct net_device *netdev;
 737        int i, err;
 738        u16 tmp16;
 739
 740        if (sizeof_candev < sizeof(struct peak_usb_device))
 741                sizeof_candev = sizeof(struct peak_usb_device);
 742
 743        netdev = alloc_candev(sizeof_candev, PCAN_USB_MAX_TX_URBS);
 744        if (!netdev) {
 745                dev_err(&intf->dev, "%s: couldn't alloc candev\n",
 746                        PCAN_USB_DRIVER_NAME);
 747                return -ENOMEM;
 748        }
 749
 750        dev = netdev_priv(netdev);
 751
 752        /* allocate a buffer large enough to send commands */
 753        dev->cmd_buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_KERNEL);
 754        if (!dev->cmd_buf) {
 755                err = -ENOMEM;
 756                goto lbl_free_candev;
 757        }
 758
 759        dev->udev = usb_dev;
 760        dev->netdev = netdev;
 761        dev->adapter = peak_usb_adapter;
 762        dev->ctrl_idx = ctrl_idx;
 763        dev->state = PCAN_USB_STATE_CONNECTED;
 764
 765        dev->ep_msg_in = peak_usb_adapter->ep_msg_in;
 766        dev->ep_msg_out = peak_usb_adapter->ep_msg_out[ctrl_idx];
 767
 768        dev->can.clock = peak_usb_adapter->clock;
 769        dev->can.bittiming_const = peak_usb_adapter->bittiming_const;
 770        dev->can.do_set_bittiming = peak_usb_set_bittiming;
 771        dev->can.data_bittiming_const = peak_usb_adapter->data_bittiming_const;
 772        dev->can.do_set_data_bittiming = peak_usb_set_data_bittiming;
 773        dev->can.do_set_mode = peak_usb_set_mode;
 774        dev->can.do_get_berr_counter = peak_usb_adapter->do_get_berr_counter;
 775        dev->can.ctrlmode_supported = peak_usb_adapter->ctrlmode_supported;
 776
 777        netdev->netdev_ops = &peak_usb_netdev_ops;
 778
 779        netdev->flags |= IFF_ECHO; /* we support local echo */
 780
 781        init_usb_anchor(&dev->rx_submitted);
 782
 783        init_usb_anchor(&dev->tx_submitted);
 784        atomic_set(&dev->active_tx_urbs, 0);
 785
 786        for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++)
 787                dev->tx_contexts[i].echo_index = PCAN_USB_MAX_TX_URBS;
 788
 789        dev->prev_siblings = usb_get_intfdata(intf);
 790        usb_set_intfdata(intf, dev);
 791
 792        SET_NETDEV_DEV(netdev, &intf->dev);
 793        netdev->dev_id = ctrl_idx;
 794
 795        err = register_candev(netdev);
 796        if (err) {
 797                dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
 798                goto lbl_restore_intf_data;
 799        }
 800
 801        if (dev->prev_siblings)
 802                (dev->prev_siblings)->next_siblings = dev;
 803
 804        /* keep hw revision into the netdevice */
 805        tmp16 = le16_to_cpu(usb_dev->descriptor.bcdDevice);
 806        dev->device_rev = tmp16 >> 8;
 807
 808        if (dev->adapter->dev_init) {
 809                err = dev->adapter->dev_init(dev);
 810                if (err)
 811                        goto lbl_unregister_candev;
 812        }
 813
 814        /* set bus off */
 815        if (dev->adapter->dev_set_bus) {
 816                err = dev->adapter->dev_set_bus(dev, 0);
 817                if (err)
 818                        goto lbl_unregister_candev;
 819        }
 820
 821        /* get device number early */
 822        if (dev->adapter->dev_get_device_id)
 823                dev->adapter->dev_get_device_id(dev, &dev->device_number);
 824
 825        netdev_info(netdev, "attached to %s channel %u (device %u)\n",
 826                        peak_usb_adapter->name, ctrl_idx, dev->device_number);
 827
 828        return 0;
 829
 830lbl_unregister_candev:
 831        unregister_candev(netdev);
 832
 833lbl_restore_intf_data:
 834        usb_set_intfdata(intf, dev->prev_siblings);
 835        kfree(dev->cmd_buf);
 836
 837lbl_free_candev:
 838        free_candev(netdev);
 839
 840        return err;
 841}
 842
 843/*
 844 * called by the usb core when the device is unplugged from the system
 845 */
 846static void peak_usb_disconnect(struct usb_interface *intf)
 847{
 848        struct peak_usb_device *dev;
 849        struct peak_usb_device *dev_prev_siblings;
 850
 851        /* unregister as many netdev devices as siblings */
 852        for (dev = usb_get_intfdata(intf); dev; dev = dev_prev_siblings) {
 853                struct net_device *netdev = dev->netdev;
 854                char name[IFNAMSIZ];
 855
 856                dev_prev_siblings = dev->prev_siblings;
 857                dev->state &= ~PCAN_USB_STATE_CONNECTED;
 858                strlcpy(name, netdev->name, IFNAMSIZ);
 859
 860                unregister_netdev(netdev);
 861
 862                kfree(dev->cmd_buf);
 863                dev->next_siblings = NULL;
 864                if (dev->adapter->dev_free)
 865                        dev->adapter->dev_free(dev);
 866
 867                free_candev(netdev);
 868                dev_info(&intf->dev, "%s removed\n", name);
 869        }
 870
 871        usb_set_intfdata(intf, NULL);
 872}
 873
 874/*
 875 * probe function for new PEAK-System devices
 876 */
 877static int peak_usb_probe(struct usb_interface *intf,
 878                          const struct usb_device_id *id)
 879{
 880        struct usb_device *usb_dev = interface_to_usbdev(intf);
 881        const u16 usb_id_product = le16_to_cpu(usb_dev->descriptor.idProduct);
 882        const struct peak_usb_adapter *peak_usb_adapter = NULL;
 883        int i, err = -ENOMEM;
 884
 885        /* get corresponding PCAN-USB adapter */
 886        for (i = 0; i < ARRAY_SIZE(peak_usb_adapters_list); i++)
 887                if (peak_usb_adapters_list[i]->device_id == usb_id_product) {
 888                        peak_usb_adapter = peak_usb_adapters_list[i];
 889                        break;
 890                }
 891
 892        if (!peak_usb_adapter) {
 893                /* should never come except device_id bad usage in this file */
 894                pr_err("%s: didn't find device id. 0x%x in devices list\n",
 895                        PCAN_USB_DRIVER_NAME, usb_id_product);
 896                return -ENODEV;
 897        }
 898
 899        /* got corresponding adapter: check if it handles current interface */
 900        if (peak_usb_adapter->intf_probe) {
 901                err = peak_usb_adapter->intf_probe(intf);
 902                if (err)
 903                        return err;
 904        }
 905
 906        for (i = 0; i < peak_usb_adapter->ctrl_count; i++) {
 907                err = peak_usb_create_dev(peak_usb_adapter, intf, i);
 908                if (err) {
 909                        /* deregister already created devices */
 910                        peak_usb_disconnect(intf);
 911                        break;
 912                }
 913        }
 914
 915        return err;
 916}
 917
 918/* usb specific object needed to register this driver with the usb subsystem */
 919static struct usb_driver peak_usb_driver = {
 920        .name = PCAN_USB_DRIVER_NAME,
 921        .disconnect = peak_usb_disconnect,
 922        .probe = peak_usb_probe,
 923        .id_table = peak_usb_table,
 924};
 925
 926static int __init peak_usb_init(void)
 927{
 928        int err;
 929
 930        /* register this driver with the USB subsystem */
 931        err = usb_register(&peak_usb_driver);
 932        if (err)
 933                pr_err("%s: usb_register failed (err %d)\n",
 934                        PCAN_USB_DRIVER_NAME, err);
 935
 936        return err;
 937}
 938
 939static int peak_usb_do_device_exit(struct device *d, void *arg)
 940{
 941        struct usb_interface *intf = to_usb_interface(d);
 942        struct peak_usb_device *dev;
 943
 944        /* stop as many netdev devices as siblings */
 945        for (dev = usb_get_intfdata(intf); dev; dev = dev->prev_siblings) {
 946                struct net_device *netdev = dev->netdev;
 947
 948                if (netif_device_present(netdev))
 949                        if (dev->adapter->dev_exit)
 950                                dev->adapter->dev_exit(dev);
 951        }
 952
 953        return 0;
 954}
 955
 956static void __exit peak_usb_exit(void)
 957{
 958        int err;
 959
 960        /* last chance do send any synchronous commands here */
 961        err = driver_for_each_device(&peak_usb_driver.drvwrap.driver, NULL,
 962                                     NULL, peak_usb_do_device_exit);
 963        if (err)
 964                pr_err("%s: failed to stop all can devices (err %d)\n",
 965                        PCAN_USB_DRIVER_NAME, err);
 966
 967        /* deregister this driver with the USB subsystem */
 968        usb_deregister(&peak_usb_driver);
 969
 970        pr_info("%s: PCAN-USB interfaces driver unloaded\n",
 971                PCAN_USB_DRIVER_NAME);
 972}
 973
 974module_init(peak_usb_init);
 975module_exit(peak_usb_exit);
 976