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        {} /* Terminating entry */
  43};
  44
  45MODULE_DEVICE_TABLE(usb, peak_usb_table);
  46
  47/* List of supported PCAN-USB adapters (NULL terminated list) */
  48static const struct peak_usb_adapter *const peak_usb_adapters_list[] = {
  49        &pcan_usb,
  50        &pcan_usb_pro,
  51        &pcan_usb_fd,
  52        &pcan_usb_pro_fd,
  53};
  54
  55/*
  56 * dump memory
  57 */
  58#define DUMP_WIDTH      16
  59void pcan_dump_mem(char *prompt, void *p, int l)
  60{
  61        pr_info("%s dumping %s (%d bytes):\n",
  62                PCAN_USB_DRIVER_NAME, prompt ? prompt : "memory", l);
  63        print_hex_dump(KERN_INFO, PCAN_USB_DRIVER_NAME " ", DUMP_PREFIX_NONE,
  64                       DUMP_WIDTH, 1, p, l, false);
  65}
  66
  67/*
  68 * initialize a time_ref object with usb adapter own settings
  69 */
  70void peak_usb_init_time_ref(struct peak_time_ref *time_ref,
  71                            const struct peak_usb_adapter *adapter)
  72{
  73        if (time_ref) {
  74                memset(time_ref, 0, sizeof(struct peak_time_ref));
  75                time_ref->adapter = adapter;
  76        }
  77}
  78
  79static void peak_usb_add_us(struct timeval *tv, u32 delta_us)
  80{
  81        /* number of s. to add to final time */
  82        u32 delta_s = delta_us / 1000000;
  83
  84        delta_us -= delta_s * 1000000;
  85
  86        tv->tv_usec += delta_us;
  87        if (tv->tv_usec >= 1000000) {
  88                tv->tv_usec -= 1000000;
  89                delta_s++;
  90        }
  91        tv->tv_sec += delta_s;
  92}
  93
  94/*
  95 * sometimes, another now may be  more recent than current one...
  96 */
  97void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
  98{
  99        time_ref->ts_dev_2 = ts_now;
 100
 101        /* should wait at least two passes before computing */
 102        if (time_ref->tv_host.tv_sec > 0) {
 103                u32 delta_ts = time_ref->ts_dev_2 - time_ref->ts_dev_1;
 104
 105                if (time_ref->ts_dev_2 < time_ref->ts_dev_1)
 106                        delta_ts &= (1 << time_ref->adapter->ts_used_bits) - 1;
 107
 108                time_ref->ts_total += delta_ts;
 109        }
 110}
 111
 112/*
 113 * register device timestamp as now
 114 */
 115void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
 116{
 117        if (time_ref->tv_host_0.tv_sec == 0) {
 118                /* use monotonic clock to correctly compute further deltas */
 119                time_ref->tv_host_0 = ktime_to_timeval(ktime_get());
 120                time_ref->tv_host.tv_sec = 0;
 121        } else {
 122                /*
 123                 * delta_us should not be >= 2^32 => delta_s should be < 4294
 124                 * handle 32-bits wrapping here: if count of s. reaches 4200,
 125                 * reset counters and change time base
 126                 */
 127                if (time_ref->tv_host.tv_sec != 0) {
 128                        u32 delta_s = time_ref->tv_host.tv_sec
 129                                                - time_ref->tv_host_0.tv_sec;
 130                        if (delta_s > 4200) {
 131                                time_ref->tv_host_0 = time_ref->tv_host;
 132                                time_ref->ts_total = 0;
 133                        }
 134                }
 135
 136                time_ref->tv_host = ktime_to_timeval(ktime_get());
 137                time_ref->tick_count++;
 138        }
 139
 140        time_ref->ts_dev_1 = time_ref->ts_dev_2;
 141        peak_usb_update_ts_now(time_ref, ts_now);
 142}
 143
 144/*
 145 * compute timeval according to current ts and time_ref data
 146 */
 147void peak_usb_get_ts_tv(struct peak_time_ref *time_ref, u32 ts,
 148                        struct timeval *tv)
 149{
 150        /* protect from getting timeval before setting now */
 151        if (time_ref->tv_host.tv_sec > 0) {
 152                u64 delta_us;
 153
 154                delta_us = ts - time_ref->ts_dev_2;
 155                if (ts < time_ref->ts_dev_2)
 156                        delta_us &= (1 << time_ref->adapter->ts_used_bits) - 1;
 157
 158                delta_us += time_ref->ts_total;
 159
 160                delta_us *= time_ref->adapter->us_per_ts_scale;
 161                delta_us >>= time_ref->adapter->us_per_ts_shift;
 162
 163                *tv = time_ref->tv_host_0;
 164                peak_usb_add_us(tv, (u32)delta_us);
 165        } else {
 166                *tv = ktime_to_timeval(ktime_get());
 167        }
 168}
 169
 170/*
 171 * post received skb after having set any hw timestamp
 172 */
 173int peak_usb_netif_rx(struct sk_buff *skb,
 174                      struct peak_time_ref *time_ref, u32 ts_low, u32 ts_high)
 175{
 176        struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
 177        struct timeval tv;
 178
 179        peak_usb_get_ts_tv(time_ref, ts_low, &tv);
 180        hwts->hwtstamp = timeval_to_ktime(tv);
 181
 182        return netif_rx(skb);
 183}
 184
 185/*
 186 * callback for bulk Rx urb
 187 */
 188static void peak_usb_read_bulk_callback(struct urb *urb)
 189{
 190        struct peak_usb_device *dev = urb->context;
 191        struct net_device *netdev;
 192        int err;
 193
 194        netdev = dev->netdev;
 195
 196        if (!netif_device_present(netdev))
 197                return;
 198
 199        /* check reception status */
 200        switch (urb->status) {
 201        case 0:
 202                /* success */
 203                break;
 204
 205        case -EILSEQ:
 206        case -ENOENT:
 207        case -ECONNRESET:
 208        case -ESHUTDOWN:
 209                return;
 210
 211        default:
 212                if (net_ratelimit())
 213                        netdev_err(netdev,
 214                                   "Rx urb aborted (%d)\n", urb->status);
 215                goto resubmit_urb;
 216        }
 217
 218        /* protect from any incoming empty msgs */
 219        if ((urb->actual_length > 0) && (dev->adapter->dev_decode_buf)) {
 220                /* handle these kinds of msgs only if _start callback called */
 221                if (dev->state & PCAN_USB_STATE_STARTED) {
 222                        err = dev->adapter->dev_decode_buf(dev, urb);
 223                        if (err)
 224                                pcan_dump_mem("received usb message",
 225                                              urb->transfer_buffer,
 226                                              urb->transfer_buffer_length);
 227                }
 228        }
 229
 230resubmit_urb:
 231        usb_fill_bulk_urb(urb, dev->udev,
 232                usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
 233                urb->transfer_buffer, dev->adapter->rx_buffer_size,
 234                peak_usb_read_bulk_callback, dev);
 235
 236        usb_anchor_urb(urb, &dev->rx_submitted);
 237        err = usb_submit_urb(urb, GFP_ATOMIC);
 238        if (!err)
 239                return;
 240
 241        usb_unanchor_urb(urb);
 242
 243        if (err == -ENODEV)
 244                netif_device_detach(netdev);
 245        else
 246                netdev_err(netdev, "failed resubmitting read bulk urb: %d\n",
 247                           err);
 248}
 249
 250/*
 251 * callback for bulk Tx urb
 252 */
 253static void peak_usb_write_bulk_callback(struct urb *urb)
 254{
 255        struct peak_tx_urb_context *context = urb->context;
 256        struct peak_usb_device *dev;
 257        struct net_device *netdev;
 258
 259        BUG_ON(!context);
 260
 261        dev = context->dev;
 262        netdev = dev->netdev;
 263
 264        atomic_dec(&dev->active_tx_urbs);
 265
 266        if (!netif_device_present(netdev))
 267                return;
 268
 269        /* check tx status */
 270        switch (urb->status) {
 271        case 0:
 272                /* transmission complete */
 273                netdev->stats.tx_packets++;
 274                netdev->stats.tx_bytes += context->data_len;
 275
 276                /* prevent tx timeout */
 277                netif_trans_update(netdev);
 278                break;
 279
 280        default:
 281                if (net_ratelimit())
 282                        netdev_err(netdev, "Tx urb aborted (%d)\n",
 283                                   urb->status);
 284        case -EPROTO:
 285        case -ENOENT:
 286        case -ECONNRESET:
 287        case -ESHUTDOWN:
 288
 289                break;
 290        }
 291
 292        /* should always release echo skb and corresponding context */
 293        can_get_echo_skb(netdev, context->echo_index);
 294        context->echo_index = PCAN_USB_MAX_TX_URBS;
 295
 296        /* do wakeup tx queue in case of success only */
 297        if (!urb->status)
 298                netif_wake_queue(netdev);
 299}
 300
 301/*
 302 * called by netdev to send one skb on the CAN interface.
 303 */
 304static netdev_tx_t peak_usb_ndo_start_xmit(struct sk_buff *skb,
 305                                           struct net_device *netdev)
 306{
 307        struct peak_usb_device *dev = netdev_priv(netdev);
 308        struct peak_tx_urb_context *context = NULL;
 309        struct net_device_stats *stats = &netdev->stats;
 310        struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
 311        struct urb *urb;
 312        u8 *obuf;
 313        int i, err;
 314        size_t size = dev->adapter->tx_buffer_size;
 315
 316        if (can_dropped_invalid_skb(netdev, skb))
 317                return NETDEV_TX_OK;
 318
 319        for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++)
 320                if (dev->tx_contexts[i].echo_index == PCAN_USB_MAX_TX_URBS) {
 321                        context = dev->tx_contexts + i;
 322                        break;
 323                }
 324
 325        if (!context) {
 326                /* should not occur except during restart */
 327                return NETDEV_TX_BUSY;
 328        }
 329
 330        urb = context->urb;
 331        obuf = urb->transfer_buffer;
 332
 333        err = dev->adapter->dev_encode_msg(dev, skb, obuf, &size);
 334        if (err) {
 335                if (net_ratelimit())
 336                        netdev_err(netdev, "packet dropped\n");
 337                dev_kfree_skb(skb);
 338                stats->tx_dropped++;
 339                return NETDEV_TX_OK;
 340        }
 341
 342        context->echo_index = i;
 343
 344        /* Note: this works with CANFD frames too */
 345        context->data_len = cfd->len;
 346
 347        usb_anchor_urb(urb, &dev->tx_submitted);
 348
 349        can_put_echo_skb(skb, netdev, context->echo_index);
 350
 351        atomic_inc(&dev->active_tx_urbs);
 352
 353        err = usb_submit_urb(urb, GFP_ATOMIC);
 354        if (err) {
 355                can_free_echo_skb(netdev, context->echo_index);
 356
 357                usb_unanchor_urb(urb);
 358
 359                /* this context is not used in fact */
 360                context->echo_index = PCAN_USB_MAX_TX_URBS;
 361
 362                atomic_dec(&dev->active_tx_urbs);
 363
 364                switch (err) {
 365                case -ENODEV:
 366                        netif_device_detach(netdev);
 367                        break;
 368                default:
 369                        netdev_warn(netdev, "tx urb submitting failed err=%d\n",
 370                                    err);
 371                case -ENOENT:
 372                        /* cable unplugged */
 373                        stats->tx_dropped++;
 374                }
 375        } else {
 376                netif_trans_update(netdev);
 377
 378                /* slow down tx path */
 379                if (atomic_read(&dev->active_tx_urbs) >= PCAN_USB_MAX_TX_URBS)
 380                        netif_stop_queue(netdev);
 381        }
 382
 383        return NETDEV_TX_OK;
 384}
 385
 386/*
 387 * start the CAN interface.
 388 * Rx and Tx urbs are allocated here. Rx urbs are submitted here.
 389 */
 390static int peak_usb_start(struct peak_usb_device *dev)
 391{
 392        struct net_device *netdev = dev->netdev;
 393        int err, i;
 394
 395        for (i = 0; i < PCAN_USB_MAX_RX_URBS; i++) {
 396                struct urb *urb;
 397                u8 *buf;
 398
 399                /* create a URB, and a buffer for it, to receive usb messages */
 400                urb = usb_alloc_urb(0, GFP_KERNEL);
 401                if (!urb) {
 402                        netdev_err(netdev, "No memory left for URBs\n");
 403                        err = -ENOMEM;
 404                        break;
 405                }
 406
 407                buf = kmalloc(dev->adapter->rx_buffer_size, GFP_KERNEL);
 408                if (!buf) {
 409                        usb_free_urb(urb);
 410                        err = -ENOMEM;
 411                        break;
 412                }
 413
 414                usb_fill_bulk_urb(urb, dev->udev,
 415                        usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
 416                        buf, dev->adapter->rx_buffer_size,
 417                        peak_usb_read_bulk_callback, dev);
 418
 419                /* ask last usb_free_urb() to also kfree() transfer_buffer */
 420                urb->transfer_flags |= URB_FREE_BUFFER;
 421                usb_anchor_urb(urb, &dev->rx_submitted);
 422
 423                err = usb_submit_urb(urb, GFP_KERNEL);
 424                if (err) {
 425                        if (err == -ENODEV)
 426                                netif_device_detach(dev->netdev);
 427
 428                        usb_unanchor_urb(urb);
 429                        kfree(buf);
 430                        usb_free_urb(urb);
 431                        break;
 432                }
 433
 434                /* drop reference, USB core will take care of freeing it */
 435                usb_free_urb(urb);
 436        }
 437
 438        /* did we submit any URBs? Warn if we was not able to submit all urbs */
 439        if (i < PCAN_USB_MAX_RX_URBS) {
 440                if (i == 0) {
 441                        netdev_err(netdev, "couldn't setup any rx URB\n");
 442                        return err;
 443                }
 444
 445                netdev_warn(netdev, "rx performance may be slow\n");
 446        }
 447
 448        /* pre-alloc tx buffers and corresponding urbs */
 449        for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
 450                struct peak_tx_urb_context *context;
 451                struct urb *urb;
 452                u8 *buf;
 453
 454                /* create a URB and a buffer for it, to transmit usb messages */
 455                urb = usb_alloc_urb(0, GFP_KERNEL);
 456                if (!urb) {
 457                        netdev_err(netdev, "No memory left for URBs\n");
 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                netdev_err(dev->netdev, "no memory left for urb\n");
 656                return -ENOMEM;
 657        }
 658
 659        /* also allocate enough space for the commands to send */
 660        buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_ATOMIC);
 661        if (!buf) {
 662                usb_free_urb(urb);
 663                return -ENOMEM;
 664        }
 665
 666        /* call the device specific handler for the restart */
 667        err = dev->adapter->dev_restart_async(dev, urb, buf);
 668        if (!err)
 669                return 0;
 670
 671        kfree(buf);
 672        usb_free_urb(urb);
 673
 674        return err;
 675}
 676
 677/*
 678 * candev callback used to change CAN mode.
 679 * Warning: this is called from a timer context!
 680 */
 681static int peak_usb_set_mode(struct net_device *netdev, enum can_mode mode)
 682{
 683        struct peak_usb_device *dev = netdev_priv(netdev);
 684        int err = 0;
 685
 686        switch (mode) {
 687        case CAN_MODE_START:
 688                err = peak_usb_restart(dev);
 689                if (err)
 690                        netdev_err(netdev, "couldn't start device (err %d)\n",
 691                                   err);
 692                break;
 693
 694        default:
 695                return -EOPNOTSUPP;
 696        }
 697
 698        return err;
 699}
 700
 701/*
 702 * candev callback used to set device nominal/arbitration bitrate.
 703 */
 704static int peak_usb_set_bittiming(struct net_device *netdev)
 705{
 706        struct peak_usb_device *dev = netdev_priv(netdev);
 707        const struct peak_usb_adapter *pa = dev->adapter;
 708
 709        if (pa->dev_set_bittiming) {
 710                struct can_bittiming *bt = &dev->can.bittiming;
 711                int err = pa->dev_set_bittiming(dev, bt);
 712
 713                if (err)
 714                        netdev_info(netdev, "couldn't set bitrate (err %d)\n",
 715                                    err);
 716                return err;
 717        }
 718
 719        return 0;
 720}
 721
 722/*
 723 * candev callback used to set device data bitrate.
 724 */
 725static int peak_usb_set_data_bittiming(struct net_device *netdev)
 726{
 727        struct peak_usb_device *dev = netdev_priv(netdev);
 728        const struct peak_usb_adapter *pa = dev->adapter;
 729
 730        if (pa->dev_set_data_bittiming) {
 731                struct can_bittiming *bt = &dev->can.data_bittiming;
 732                int err = pa->dev_set_data_bittiming(dev, bt);
 733
 734                if (err)
 735                        netdev_info(netdev,
 736                                    "couldn't set data bitrate (err %d)\n",
 737                                    err);
 738
 739                return err;
 740        }
 741
 742        return 0;
 743}
 744
 745static const struct net_device_ops peak_usb_netdev_ops = {
 746        .ndo_open = peak_usb_ndo_open,
 747        .ndo_stop = peak_usb_ndo_stop,
 748        .ndo_start_xmit = peak_usb_ndo_start_xmit,
 749        .ndo_change_mtu = can_change_mtu,
 750};
 751
 752/*
 753 * create one device which is attached to CAN controller #ctrl_idx of the
 754 * usb adapter.
 755 */
 756static int peak_usb_create_dev(const struct peak_usb_adapter *peak_usb_adapter,
 757                               struct usb_interface *intf, int ctrl_idx)
 758{
 759        struct usb_device *usb_dev = interface_to_usbdev(intf);
 760        int sizeof_candev = peak_usb_adapter->sizeof_dev_private;
 761        struct peak_usb_device *dev;
 762        struct net_device *netdev;
 763        int i, err;
 764        u16 tmp16;
 765
 766        if (sizeof_candev < sizeof(struct peak_usb_device))
 767                sizeof_candev = sizeof(struct peak_usb_device);
 768
 769        netdev = alloc_candev(sizeof_candev, PCAN_USB_MAX_TX_URBS);
 770        if (!netdev) {
 771                dev_err(&intf->dev, "%s: couldn't alloc candev\n",
 772                        PCAN_USB_DRIVER_NAME);
 773                return -ENOMEM;
 774        }
 775
 776        dev = netdev_priv(netdev);
 777
 778        /* allocate a buffer large enough to send commands */
 779        dev->cmd_buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_KERNEL);
 780        if (!dev->cmd_buf) {
 781                err = -ENOMEM;
 782                goto lbl_free_candev;
 783        }
 784
 785        dev->udev = usb_dev;
 786        dev->netdev = netdev;
 787        dev->adapter = peak_usb_adapter;
 788        dev->ctrl_idx = ctrl_idx;
 789        dev->state = PCAN_USB_STATE_CONNECTED;
 790
 791        dev->ep_msg_in = peak_usb_adapter->ep_msg_in;
 792        dev->ep_msg_out = peak_usb_adapter->ep_msg_out[ctrl_idx];
 793
 794        dev->can.clock = peak_usb_adapter->clock;
 795        dev->can.bittiming_const = peak_usb_adapter->bittiming_const;
 796        dev->can.do_set_bittiming = peak_usb_set_bittiming;
 797        dev->can.data_bittiming_const = peak_usb_adapter->data_bittiming_const;
 798        dev->can.do_set_data_bittiming = peak_usb_set_data_bittiming;
 799        dev->can.do_set_mode = peak_usb_set_mode;
 800        dev->can.do_get_berr_counter = peak_usb_adapter->do_get_berr_counter;
 801        dev->can.ctrlmode_supported = peak_usb_adapter->ctrlmode_supported;
 802
 803        netdev->netdev_ops = &peak_usb_netdev_ops;
 804
 805        netdev->flags |= IFF_ECHO; /* we support local echo */
 806
 807        init_usb_anchor(&dev->rx_submitted);
 808
 809        init_usb_anchor(&dev->tx_submitted);
 810        atomic_set(&dev->active_tx_urbs, 0);
 811
 812        for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++)
 813                dev->tx_contexts[i].echo_index = PCAN_USB_MAX_TX_URBS;
 814
 815        dev->prev_siblings = usb_get_intfdata(intf);
 816        usb_set_intfdata(intf, dev);
 817
 818        SET_NETDEV_DEV(netdev, &intf->dev);
 819        netdev->dev_id = ctrl_idx;
 820
 821        err = register_candev(netdev);
 822        if (err) {
 823                dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
 824                goto lbl_restore_intf_data;
 825        }
 826
 827        if (dev->prev_siblings)
 828                (dev->prev_siblings)->next_siblings = dev;
 829
 830        /* keep hw revision into the netdevice */
 831        tmp16 = le16_to_cpu(usb_dev->descriptor.bcdDevice);
 832        dev->device_rev = tmp16 >> 8;
 833
 834        if (dev->adapter->dev_init) {
 835                err = dev->adapter->dev_init(dev);
 836                if (err)
 837                        goto lbl_unregister_candev;
 838        }
 839
 840        /* set bus off */
 841        if (dev->adapter->dev_set_bus) {
 842                err = dev->adapter->dev_set_bus(dev, 0);
 843                if (err)
 844                        goto lbl_unregister_candev;
 845        }
 846
 847        /* get device number early */
 848        if (dev->adapter->dev_get_device_id)
 849                dev->adapter->dev_get_device_id(dev, &dev->device_number);
 850
 851        netdev_info(netdev, "attached to %s channel %u (device %u)\n",
 852                        peak_usb_adapter->name, ctrl_idx, dev->device_number);
 853
 854        return 0;
 855
 856lbl_unregister_candev:
 857        unregister_candev(netdev);
 858
 859lbl_restore_intf_data:
 860        usb_set_intfdata(intf, dev->prev_siblings);
 861        kfree(dev->cmd_buf);
 862
 863lbl_free_candev:
 864        free_candev(netdev);
 865
 866        return err;
 867}
 868
 869/*
 870 * called by the usb core when the device is unplugged from the system
 871 */
 872static void peak_usb_disconnect(struct usb_interface *intf)
 873{
 874        struct peak_usb_device *dev;
 875
 876        /* unregister as many netdev devices as siblings */
 877        for (dev = usb_get_intfdata(intf); dev; dev = dev->prev_siblings) {
 878                struct net_device *netdev = dev->netdev;
 879                char name[IFNAMSIZ];
 880
 881                dev->state &= ~PCAN_USB_STATE_CONNECTED;
 882                strncpy(name, netdev->name, IFNAMSIZ);
 883
 884                unregister_netdev(netdev);
 885                free_candev(netdev);
 886
 887                kfree(dev->cmd_buf);
 888                dev->next_siblings = NULL;
 889                if (dev->adapter->dev_free)
 890                        dev->adapter->dev_free(dev);
 891
 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.