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