linux/drivers/net/can/usb/peak_usb/pcan_usb.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * CAN driver for PEAK System PCAN-USB adapter
   4 * Derived from the PCAN project file driver/src/pcan_usb.c
   5 *
   6 * Copyright (C) 2003-2010 PEAK System-Technik GmbH
   7 * Copyright (C) 2011-2012 Stephane Grosjean <s.grosjean@peak-system.com>
   8 *
   9 * Many thanks to Klaus Hitschler <klaus.hitschler@gmx.de>
  10 */
  11#include <linux/netdevice.h>
  12#include <linux/usb.h>
  13#include <linux/module.h>
  14
  15#include <linux/can.h>
  16#include <linux/can/dev.h>
  17#include <linux/can/error.h>
  18
  19#include "pcan_usb_core.h"
  20
  21MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB adapter");
  22
  23/* PCAN-USB Endpoints */
  24#define PCAN_USB_EP_CMDOUT              1
  25#define PCAN_USB_EP_CMDIN               (PCAN_USB_EP_CMDOUT | USB_DIR_IN)
  26#define PCAN_USB_EP_MSGOUT              2
  27#define PCAN_USB_EP_MSGIN               (PCAN_USB_EP_MSGOUT | USB_DIR_IN)
  28
  29/* PCAN-USB command struct */
  30#define PCAN_USB_CMD_FUNC               0
  31#define PCAN_USB_CMD_NUM                1
  32#define PCAN_USB_CMD_ARGS               2
  33#define PCAN_USB_CMD_ARGS_LEN           14
  34#define PCAN_USB_CMD_LEN                (PCAN_USB_CMD_ARGS + \
  35                                         PCAN_USB_CMD_ARGS_LEN)
  36
  37/* PCAN-USB command timeout (ms.) */
  38#define PCAN_USB_COMMAND_TIMEOUT        1000
  39
  40/* PCAN-USB startup timeout (ms.) */
  41#define PCAN_USB_STARTUP_TIMEOUT        10
  42
  43/* PCAN-USB rx/tx buffers size */
  44#define PCAN_USB_RX_BUFFER_SIZE         64
  45#define PCAN_USB_TX_BUFFER_SIZE         64
  46
  47#define PCAN_USB_MSG_HEADER_LEN         2
  48
  49/* PCAN-USB adapter internal clock (MHz) */
  50#define PCAN_USB_CRYSTAL_HZ             16000000
  51
  52/* PCAN-USB USB message record status/len field */
  53#define PCAN_USB_STATUSLEN_TIMESTAMP    (1 << 7)
  54#define PCAN_USB_STATUSLEN_INTERNAL     (1 << 6)
  55#define PCAN_USB_STATUSLEN_EXT_ID       (1 << 5)
  56#define PCAN_USB_STATUSLEN_RTR          (1 << 4)
  57#define PCAN_USB_STATUSLEN_DLC          (0xf)
  58
  59/* PCAN-USB error flags */
  60#define PCAN_USB_ERROR_TXFULL           0x01
  61#define PCAN_USB_ERROR_RXQOVR           0x02
  62#define PCAN_USB_ERROR_BUS_LIGHT        0x04
  63#define PCAN_USB_ERROR_BUS_HEAVY        0x08
  64#define PCAN_USB_ERROR_BUS_OFF          0x10
  65#define PCAN_USB_ERROR_RXQEMPTY         0x20
  66#define PCAN_USB_ERROR_QOVR             0x40
  67#define PCAN_USB_ERROR_TXQFULL          0x80
  68
  69/* SJA1000 modes */
  70#define SJA1000_MODE_NORMAL             0x00
  71#define SJA1000_MODE_INIT               0x01
  72
  73/*
  74 * tick duration = 42.666 us =>
  75 * (tick_number * 44739243) >> 20 ~ (tick_number * 42666) / 1000
  76 * accuracy = 10^-7
  77 */
  78#define PCAN_USB_TS_DIV_SHIFTER         20
  79#define PCAN_USB_TS_US_PER_TICK         44739243
  80
  81/* PCAN-USB messages record types */
  82#define PCAN_USB_REC_ERROR              1
  83#define PCAN_USB_REC_ANALOG             2
  84#define PCAN_USB_REC_BUSLOAD            3
  85#define PCAN_USB_REC_TS                 4
  86#define PCAN_USB_REC_BUSEVT             5
  87
  88/* private to PCAN-USB adapter */
  89struct pcan_usb {
  90        struct peak_usb_device dev;
  91        struct peak_time_ref time_ref;
  92        struct timer_list restart_timer;
  93};
  94
  95/* incoming message context for decoding */
  96struct pcan_usb_msg_context {
  97        u16 ts16;
  98        u8 prev_ts8;
  99        u8 *ptr;
 100        u8 *end;
 101        u8 rec_cnt;
 102        u8 rec_idx;
 103        u8 rec_data_idx;
 104        struct net_device *netdev;
 105        struct pcan_usb *pdev;
 106};
 107
 108/*
 109 * send a command
 110 */
 111static int pcan_usb_send_cmd(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
 112{
 113        int err;
 114        int actual_length;
 115
 116        /* usb device unregistered? */
 117        if (!(dev->state & PCAN_USB_STATE_CONNECTED))
 118                return 0;
 119
 120        dev->cmd_buf[PCAN_USB_CMD_FUNC] = f;
 121        dev->cmd_buf[PCAN_USB_CMD_NUM] = n;
 122
 123        if (p)
 124                memcpy(dev->cmd_buf + PCAN_USB_CMD_ARGS,
 125                        p, PCAN_USB_CMD_ARGS_LEN);
 126
 127        err = usb_bulk_msg(dev->udev,
 128                        usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
 129                        dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
 130                        PCAN_USB_COMMAND_TIMEOUT);
 131        if (err)
 132                netdev_err(dev->netdev,
 133                        "sending cmd f=0x%x n=0x%x failure: %d\n",
 134                        f, n, err);
 135        return err;
 136}
 137
 138/*
 139 * send a command then wait for its response
 140 */
 141static int pcan_usb_wait_rsp(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
 142{
 143        int err;
 144        int actual_length;
 145
 146        /* usb device unregistered? */
 147        if (!(dev->state & PCAN_USB_STATE_CONNECTED))
 148                return 0;
 149
 150        /* first, send command */
 151        err = pcan_usb_send_cmd(dev, f, n, NULL);
 152        if (err)
 153                return err;
 154
 155        err = usb_bulk_msg(dev->udev,
 156                usb_rcvbulkpipe(dev->udev, PCAN_USB_EP_CMDIN),
 157                dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
 158                PCAN_USB_COMMAND_TIMEOUT);
 159        if (err)
 160                netdev_err(dev->netdev,
 161                        "waiting rsp f=0x%x n=0x%x failure: %d\n", f, n, err);
 162        else if (p)
 163                memcpy(p, dev->cmd_buf + PCAN_USB_CMD_ARGS,
 164                        PCAN_USB_CMD_ARGS_LEN);
 165
 166        return err;
 167}
 168
 169static int pcan_usb_set_sja1000(struct peak_usb_device *dev, u8 mode)
 170{
 171        u8 args[PCAN_USB_CMD_ARGS_LEN] = {
 172                [1] = mode,
 173        };
 174
 175        return pcan_usb_send_cmd(dev, 9, 2, args);
 176}
 177
 178static int pcan_usb_set_bus(struct peak_usb_device *dev, u8 onoff)
 179{
 180        u8 args[PCAN_USB_CMD_ARGS_LEN] = {
 181                [0] = !!onoff,
 182        };
 183
 184        return pcan_usb_send_cmd(dev, 3, 2, args);
 185}
 186
 187static int pcan_usb_set_silent(struct peak_usb_device *dev, u8 onoff)
 188{
 189        u8 args[PCAN_USB_CMD_ARGS_LEN] = {
 190                [0] = !!onoff,
 191        };
 192
 193        return pcan_usb_send_cmd(dev, 3, 3, args);
 194}
 195
 196static int pcan_usb_set_ext_vcc(struct peak_usb_device *dev, u8 onoff)
 197{
 198        u8 args[PCAN_USB_CMD_ARGS_LEN] = {
 199                [0] = !!onoff,
 200        };
 201
 202        return pcan_usb_send_cmd(dev, 10, 2, args);
 203}
 204
 205/*
 206 * set bittiming value to can
 207 */
 208static int pcan_usb_set_bittiming(struct peak_usb_device *dev,
 209                                  struct can_bittiming *bt)
 210{
 211        u8 args[PCAN_USB_CMD_ARGS_LEN];
 212        u8 btr0, btr1;
 213
 214        btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
 215        btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
 216                (((bt->phase_seg2 - 1) & 0x7) << 4);
 217        if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
 218                btr1 |= 0x80;
 219
 220        netdev_info(dev->netdev, "setting BTR0=0x%02x BTR1=0x%02x\n",
 221                btr0, btr1);
 222
 223        args[0] = btr1;
 224        args[1] = btr0;
 225
 226        return pcan_usb_send_cmd(dev, 1, 2, args);
 227}
 228
 229/*
 230 * init/reset can
 231 */
 232static int pcan_usb_write_mode(struct peak_usb_device *dev, u8 onoff)
 233{
 234        int err;
 235
 236        err = pcan_usb_set_bus(dev, onoff);
 237        if (err)
 238                return err;
 239
 240        if (!onoff) {
 241                err = pcan_usb_set_sja1000(dev, SJA1000_MODE_INIT);
 242        } else {
 243                /* the PCAN-USB needs time to init */
 244                set_current_state(TASK_INTERRUPTIBLE);
 245                schedule_timeout(msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
 246        }
 247
 248        return err;
 249}
 250
 251/*
 252 * handle end of waiting for the device to reset
 253 */
 254static void pcan_usb_restart(struct timer_list *t)
 255{
 256        struct pcan_usb *pdev = from_timer(pdev, t, restart_timer);
 257        struct peak_usb_device *dev = &pdev->dev;
 258
 259        /* notify candev and netdev */
 260        peak_usb_restart_complete(dev);
 261}
 262
 263/*
 264 * handle the submission of the restart urb
 265 */
 266static void pcan_usb_restart_pending(struct urb *urb)
 267{
 268        struct pcan_usb *pdev = urb->context;
 269
 270        /* the PCAN-USB needs time to restart */
 271        mod_timer(&pdev->restart_timer,
 272                        jiffies + msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
 273
 274        /* can delete usb resources */
 275        peak_usb_async_complete(urb);
 276}
 277
 278/*
 279 * handle asynchronous restart
 280 */
 281static int pcan_usb_restart_async(struct peak_usb_device *dev, struct urb *urb,
 282                                  u8 *buf)
 283{
 284        struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
 285
 286        if (timer_pending(&pdev->restart_timer))
 287                return -EBUSY;
 288
 289        /* set bus on */
 290        buf[PCAN_USB_CMD_FUNC] = 3;
 291        buf[PCAN_USB_CMD_NUM] = 2;
 292        buf[PCAN_USB_CMD_ARGS] = 1;
 293
 294        usb_fill_bulk_urb(urb, dev->udev,
 295                        usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
 296                        buf, PCAN_USB_CMD_LEN,
 297                        pcan_usb_restart_pending, pdev);
 298
 299        return usb_submit_urb(urb, GFP_ATOMIC);
 300}
 301
 302/*
 303 * read serial number from device
 304 */
 305static int pcan_usb_get_serial(struct peak_usb_device *dev, u32 *serial_number)
 306{
 307        u8 args[PCAN_USB_CMD_ARGS_LEN];
 308        int err;
 309
 310        err = pcan_usb_wait_rsp(dev, 6, 1, args);
 311        if (err) {
 312                netdev_err(dev->netdev, "getting serial failure: %d\n", err);
 313        } else if (serial_number) {
 314                __le32 tmp32;
 315
 316                memcpy(&tmp32, args, 4);
 317                *serial_number = le32_to_cpu(tmp32);
 318        }
 319
 320        return err;
 321}
 322
 323/*
 324 * read device id from device
 325 */
 326static int pcan_usb_get_device_id(struct peak_usb_device *dev, u32 *device_id)
 327{
 328        u8 args[PCAN_USB_CMD_ARGS_LEN];
 329        int err;
 330
 331        err = pcan_usb_wait_rsp(dev, 4, 1, args);
 332        if (err)
 333                netdev_err(dev->netdev, "getting device id failure: %d\n", err);
 334        else if (device_id)
 335                *device_id = args[0];
 336
 337        return err;
 338}
 339
 340/*
 341 * update current time ref with received timestamp
 342 */
 343static int pcan_usb_update_ts(struct pcan_usb_msg_context *mc)
 344{
 345        __le16 tmp16;
 346
 347        if ((mc->ptr+2) > mc->end)
 348                return -EINVAL;
 349
 350        memcpy(&tmp16, mc->ptr, 2);
 351
 352        mc->ts16 = le16_to_cpu(tmp16);
 353
 354        if (mc->rec_idx > 0)
 355                peak_usb_update_ts_now(&mc->pdev->time_ref, mc->ts16);
 356        else
 357                peak_usb_set_ts_now(&mc->pdev->time_ref, mc->ts16);
 358
 359        return 0;
 360}
 361
 362/*
 363 * decode received timestamp
 364 */
 365static int pcan_usb_decode_ts(struct pcan_usb_msg_context *mc, u8 first_packet)
 366{
 367        /* only 1st packet supplies a word timestamp */
 368        if (first_packet) {
 369                __le16 tmp16;
 370
 371                if ((mc->ptr + 2) > mc->end)
 372                        return -EINVAL;
 373
 374                memcpy(&tmp16, mc->ptr, 2);
 375                mc->ptr += 2;
 376
 377                mc->ts16 = le16_to_cpu(tmp16);
 378                mc->prev_ts8 = mc->ts16 & 0x00ff;
 379        } else {
 380                u8 ts8;
 381
 382                if ((mc->ptr + 1) > mc->end)
 383                        return -EINVAL;
 384
 385                ts8 = *mc->ptr++;
 386
 387                if (ts8 < mc->prev_ts8)
 388                        mc->ts16 += 0x100;
 389
 390                mc->ts16 &= 0xff00;
 391                mc->ts16 |= ts8;
 392                mc->prev_ts8 = ts8;
 393        }
 394
 395        return 0;
 396}
 397
 398static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
 399                                 u8 status_len)
 400{
 401        struct sk_buff *skb;
 402        struct can_frame *cf;
 403        enum can_state new_state;
 404
 405        /* ignore this error until 1st ts received */
 406        if (n == PCAN_USB_ERROR_QOVR)
 407                if (!mc->pdev->time_ref.tick_count)
 408                        return 0;
 409
 410        new_state = mc->pdev->dev.can.state;
 411
 412        switch (mc->pdev->dev.can.state) {
 413        case CAN_STATE_ERROR_ACTIVE:
 414                if (n & PCAN_USB_ERROR_BUS_LIGHT) {
 415                        new_state = CAN_STATE_ERROR_WARNING;
 416                        break;
 417                }
 418                /* fall through */
 419
 420        case CAN_STATE_ERROR_WARNING:
 421                if (n & PCAN_USB_ERROR_BUS_HEAVY) {
 422                        new_state = CAN_STATE_ERROR_PASSIVE;
 423                        break;
 424                }
 425                if (n & PCAN_USB_ERROR_BUS_OFF) {
 426                        new_state = CAN_STATE_BUS_OFF;
 427                        break;
 428                }
 429                if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
 430                        /*
 431                         * trick to bypass next comparison and process other
 432                         * errors
 433                         */
 434                        new_state = CAN_STATE_MAX;
 435                        break;
 436                }
 437                if ((n & PCAN_USB_ERROR_BUS_LIGHT) == 0) {
 438                        /* no error (back to active state) */
 439                        mc->pdev->dev.can.state = CAN_STATE_ERROR_ACTIVE;
 440                        return 0;
 441                }
 442                break;
 443
 444        case CAN_STATE_ERROR_PASSIVE:
 445                if (n & PCAN_USB_ERROR_BUS_OFF) {
 446                        new_state = CAN_STATE_BUS_OFF;
 447                        break;
 448                }
 449                if (n & PCAN_USB_ERROR_BUS_LIGHT) {
 450                        new_state = CAN_STATE_ERROR_WARNING;
 451                        break;
 452                }
 453                if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
 454                        /*
 455                         * trick to bypass next comparison and process other
 456                         * errors
 457                         */
 458                        new_state = CAN_STATE_MAX;
 459                        break;
 460                }
 461
 462                if ((n & PCAN_USB_ERROR_BUS_HEAVY) == 0) {
 463                        /* no error (back to active state) */
 464                        mc->pdev->dev.can.state = CAN_STATE_ERROR_ACTIVE;
 465                        return 0;
 466                }
 467                break;
 468
 469        default:
 470                /* do nothing waiting for restart */
 471                return 0;
 472        }
 473
 474        /* donot post any error if current state didn't change */
 475        if (mc->pdev->dev.can.state == new_state)
 476                return 0;
 477
 478        /* allocate an skb to store the error frame */
 479        skb = alloc_can_err_skb(mc->netdev, &cf);
 480        if (!skb)
 481                return -ENOMEM;
 482
 483        switch (new_state) {
 484        case CAN_STATE_BUS_OFF:
 485                cf->can_id |= CAN_ERR_BUSOFF;
 486                mc->pdev->dev.can.can_stats.bus_off++;
 487                can_bus_off(mc->netdev);
 488                break;
 489
 490        case CAN_STATE_ERROR_PASSIVE:
 491                cf->can_id |= CAN_ERR_CRTL;
 492                cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE |
 493                               CAN_ERR_CRTL_RX_PASSIVE;
 494                mc->pdev->dev.can.can_stats.error_passive++;
 495                break;
 496
 497        case CAN_STATE_ERROR_WARNING:
 498                cf->can_id |= CAN_ERR_CRTL;
 499                cf->data[1] |= CAN_ERR_CRTL_TX_WARNING |
 500                               CAN_ERR_CRTL_RX_WARNING;
 501                mc->pdev->dev.can.can_stats.error_warning++;
 502                break;
 503
 504        default:
 505                /* CAN_STATE_MAX (trick to handle other errors) */
 506                cf->can_id |= CAN_ERR_CRTL;
 507                cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
 508                mc->netdev->stats.rx_over_errors++;
 509                mc->netdev->stats.rx_errors++;
 510
 511                new_state = mc->pdev->dev.can.state;
 512                break;
 513        }
 514
 515        mc->pdev->dev.can.state = new_state;
 516
 517        if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
 518                struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
 519
 520                peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16,
 521                                     &hwts->hwtstamp);
 522        }
 523
 524        mc->netdev->stats.rx_packets++;
 525        mc->netdev->stats.rx_bytes += cf->can_dlc;
 526        netif_rx(skb);
 527
 528        return 0;
 529}
 530
 531/*
 532 * decode non-data usb message
 533 */
 534static int pcan_usb_decode_status(struct pcan_usb_msg_context *mc,
 535                                  u8 status_len)
 536{
 537        u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
 538        u8 f, n;
 539        int err;
 540
 541        /* check whether function and number can be read */
 542        if ((mc->ptr + 2) > mc->end)
 543                return -EINVAL;
 544
 545        f = mc->ptr[PCAN_USB_CMD_FUNC];
 546        n = mc->ptr[PCAN_USB_CMD_NUM];
 547        mc->ptr += PCAN_USB_CMD_ARGS;
 548
 549        if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
 550                int err = pcan_usb_decode_ts(mc, !mc->rec_idx);
 551
 552                if (err)
 553                        return err;
 554        }
 555
 556        switch (f) {
 557        case PCAN_USB_REC_ERROR:
 558                err = pcan_usb_decode_error(mc, n, status_len);
 559                if (err)
 560                        return err;
 561                break;
 562
 563        case PCAN_USB_REC_ANALOG:
 564                /* analog values (ignored) */
 565                rec_len = 2;
 566                break;
 567
 568        case PCAN_USB_REC_BUSLOAD:
 569                /* bus load (ignored) */
 570                rec_len = 1;
 571                break;
 572
 573        case PCAN_USB_REC_TS:
 574                /* only timestamp */
 575                if (pcan_usb_update_ts(mc))
 576                        return -EINVAL;
 577                break;
 578
 579        case PCAN_USB_REC_BUSEVT:
 580                /* error frame/bus event */
 581                if (n & PCAN_USB_ERROR_TXQFULL)
 582                        netdev_dbg(mc->netdev, "device Tx queue full)\n");
 583                break;
 584        default:
 585                netdev_err(mc->netdev, "unexpected function %u\n", f);
 586                break;
 587        }
 588
 589        if ((mc->ptr + rec_len) > mc->end)
 590                return -EINVAL;
 591
 592        mc->ptr += rec_len;
 593
 594        return 0;
 595}
 596
 597/*
 598 * decode data usb message
 599 */
 600static int pcan_usb_decode_data(struct pcan_usb_msg_context *mc, u8 status_len)
 601{
 602        u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
 603        struct sk_buff *skb;
 604        struct can_frame *cf;
 605        struct skb_shared_hwtstamps *hwts;
 606
 607        skb = alloc_can_skb(mc->netdev, &cf);
 608        if (!skb)
 609                return -ENOMEM;
 610
 611        if (status_len & PCAN_USB_STATUSLEN_EXT_ID) {
 612                __le32 tmp32;
 613
 614                if ((mc->ptr + 4) > mc->end)
 615                        goto decode_failed;
 616
 617                memcpy(&tmp32, mc->ptr, 4);
 618                mc->ptr += 4;
 619
 620                cf->can_id = (le32_to_cpu(tmp32) >> 3) | CAN_EFF_FLAG;
 621        } else {
 622                __le16 tmp16;
 623
 624                if ((mc->ptr + 2) > mc->end)
 625                        goto decode_failed;
 626
 627                memcpy(&tmp16, mc->ptr, 2);
 628                mc->ptr += 2;
 629
 630                cf->can_id = le16_to_cpu(tmp16) >> 5;
 631        }
 632
 633        cf->can_dlc = get_can_dlc(rec_len);
 634
 635        /* first data packet timestamp is a word */
 636        if (pcan_usb_decode_ts(mc, !mc->rec_data_idx))
 637                goto decode_failed;
 638
 639        /* read data */
 640        memset(cf->data, 0x0, sizeof(cf->data));
 641        if (status_len & PCAN_USB_STATUSLEN_RTR) {
 642                cf->can_id |= CAN_RTR_FLAG;
 643        } else {
 644                if ((mc->ptr + rec_len) > mc->end)
 645                        goto decode_failed;
 646
 647                memcpy(cf->data, mc->ptr, cf->can_dlc);
 648                mc->ptr += rec_len;
 649        }
 650
 651        /* convert timestamp into kernel time */
 652        hwts = skb_hwtstamps(skb);
 653        peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16, &hwts->hwtstamp);
 654
 655        /* update statistics */
 656        mc->netdev->stats.rx_packets++;
 657        mc->netdev->stats.rx_bytes += cf->can_dlc;
 658        /* push the skb */
 659        netif_rx(skb);
 660
 661        return 0;
 662
 663decode_failed:
 664        dev_kfree_skb(skb);
 665        return -EINVAL;
 666}
 667
 668/*
 669 * process incoming message
 670 */
 671static int pcan_usb_decode_msg(struct peak_usb_device *dev, u8 *ibuf, u32 lbuf)
 672{
 673        struct pcan_usb_msg_context mc = {
 674                .rec_cnt = ibuf[1],
 675                .ptr = ibuf + PCAN_USB_MSG_HEADER_LEN,
 676                .end = ibuf + lbuf,
 677                .netdev = dev->netdev,
 678                .pdev = container_of(dev, struct pcan_usb, dev),
 679        };
 680        int err;
 681
 682        for (err = 0; mc.rec_idx < mc.rec_cnt && !err; mc.rec_idx++) {
 683                u8 sl = *mc.ptr++;
 684
 685                /* handle status and error frames here */
 686                if (sl & PCAN_USB_STATUSLEN_INTERNAL) {
 687                        err = pcan_usb_decode_status(&mc, sl);
 688                /* handle normal can frames here */
 689                } else {
 690                        err = pcan_usb_decode_data(&mc, sl);
 691                        mc.rec_data_idx++;
 692                }
 693        }
 694
 695        return err;
 696}
 697
 698/*
 699 * process any incoming buffer
 700 */
 701static int pcan_usb_decode_buf(struct peak_usb_device *dev, struct urb *urb)
 702{
 703        int err = 0;
 704
 705        if (urb->actual_length > PCAN_USB_MSG_HEADER_LEN) {
 706                err = pcan_usb_decode_msg(dev, urb->transfer_buffer,
 707                        urb->actual_length);
 708
 709        } else if (urb->actual_length > 0) {
 710                netdev_err(dev->netdev, "usb message length error (%u)\n",
 711                        urb->actual_length);
 712                err = -EINVAL;
 713        }
 714
 715        return err;
 716}
 717
 718/*
 719 * process outgoing packet
 720 */
 721static int pcan_usb_encode_msg(struct peak_usb_device *dev, struct sk_buff *skb,
 722                               u8 *obuf, size_t *size)
 723{
 724        struct net_device *netdev = dev->netdev;
 725        struct net_device_stats *stats = &netdev->stats;
 726        struct can_frame *cf = (struct can_frame *)skb->data;
 727        u8 *pc;
 728
 729        obuf[0] = 2;
 730        obuf[1] = 1;
 731
 732        pc = obuf + PCAN_USB_MSG_HEADER_LEN;
 733
 734        /* status/len byte */
 735        *pc = cf->can_dlc;
 736        if (cf->can_id & CAN_RTR_FLAG)
 737                *pc |= PCAN_USB_STATUSLEN_RTR;
 738
 739        /* can id */
 740        if (cf->can_id & CAN_EFF_FLAG) {
 741                __le32 tmp32 = cpu_to_le32((cf->can_id & CAN_ERR_MASK) << 3);
 742
 743                *pc |= PCAN_USB_STATUSLEN_EXT_ID;
 744                memcpy(++pc, &tmp32, 4);
 745                pc += 4;
 746        } else {
 747                __le16 tmp16 = cpu_to_le16((cf->can_id & CAN_ERR_MASK) << 5);
 748
 749                memcpy(++pc, &tmp16, 2);
 750                pc += 2;
 751        }
 752
 753        /* can data */
 754        if (!(cf->can_id & CAN_RTR_FLAG)) {
 755                memcpy(pc, cf->data, cf->can_dlc);
 756                pc += cf->can_dlc;
 757        }
 758
 759        obuf[(*size)-1] = (u8)(stats->tx_packets & 0xff);
 760
 761        return 0;
 762}
 763
 764/*
 765 * start interface
 766 */
 767static int pcan_usb_start(struct peak_usb_device *dev)
 768{
 769        struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
 770
 771        /* number of bits used in timestamps read from adapter struct */
 772        peak_usb_init_time_ref(&pdev->time_ref, &pcan_usb);
 773
 774        /* if revision greater than 3, can put silent mode on/off */
 775        if (dev->device_rev > 3) {
 776                int err;
 777
 778                err = pcan_usb_set_silent(dev,
 779                                dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
 780                if (err)
 781                        return err;
 782        }
 783
 784        return pcan_usb_set_ext_vcc(dev, 0);
 785}
 786
 787static int pcan_usb_init(struct peak_usb_device *dev)
 788{
 789        struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
 790        u32 serial_number;
 791        int err;
 792
 793        /* initialize a timer needed to wait for hardware restart */
 794        timer_setup(&pdev->restart_timer, pcan_usb_restart, 0);
 795
 796        /*
 797         * explicit use of dev_xxx() instead of netdev_xxx() here:
 798         * information displayed are related to the device itself, not
 799         * to the canx netdevice.
 800         */
 801        err = pcan_usb_get_serial(dev, &serial_number);
 802        if (err) {
 803                dev_err(dev->netdev->dev.parent,
 804                        "unable to read %s serial number (err %d)\n",
 805                        pcan_usb.name, err);
 806                return err;
 807        }
 808
 809        dev_info(dev->netdev->dev.parent,
 810                 "PEAK-System %s adapter hwrev %u serial %08X (%u channel)\n",
 811                 pcan_usb.name, dev->device_rev, serial_number,
 812                 pcan_usb.ctrl_count);
 813
 814        return 0;
 815}
 816
 817/*
 818 * probe function for new PCAN-USB usb interface
 819 */
 820static int pcan_usb_probe(struct usb_interface *intf)
 821{
 822        struct usb_host_interface *if_desc;
 823        int i;
 824
 825        if_desc = intf->altsetting;
 826
 827        /* check interface endpoint addresses */
 828        for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
 829                struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
 830
 831                switch (ep->bEndpointAddress) {
 832                case PCAN_USB_EP_CMDOUT:
 833                case PCAN_USB_EP_CMDIN:
 834                case PCAN_USB_EP_MSGOUT:
 835                case PCAN_USB_EP_MSGIN:
 836                        break;
 837                default:
 838                        return -ENODEV;
 839                }
 840        }
 841
 842        return 0;
 843}
 844
 845/*
 846 * describe the PCAN-USB adapter
 847 */
 848static const struct can_bittiming_const pcan_usb_const = {
 849        .name = "pcan_usb",
 850        .tseg1_min = 1,
 851        .tseg1_max = 16,
 852        .tseg2_min = 1,
 853        .tseg2_max = 8,
 854        .sjw_max = 4,
 855        .brp_min = 1,
 856        .brp_max = 64,
 857        .brp_inc = 1,
 858};
 859
 860const struct peak_usb_adapter pcan_usb = {
 861        .name = "PCAN-USB",
 862        .device_id = PCAN_USB_PRODUCT_ID,
 863        .ctrl_count = 1,
 864        .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
 865        .clock = {
 866                .freq = PCAN_USB_CRYSTAL_HZ / 2 ,
 867        },
 868        .bittiming_const = &pcan_usb_const,
 869
 870        /* size of device private data */
 871        .sizeof_dev_private = sizeof(struct pcan_usb),
 872
 873        /* timestamps usage */
 874        .ts_used_bits = 16,
 875        .ts_period = 24575, /* calibration period in ts. */
 876        .us_per_ts_scale = PCAN_USB_TS_US_PER_TICK, /* us=(ts*scale) */
 877        .us_per_ts_shift = PCAN_USB_TS_DIV_SHIFTER, /*  >> shift     */
 878
 879        /* give here messages in/out endpoints */
 880        .ep_msg_in = PCAN_USB_EP_MSGIN,
 881        .ep_msg_out = {PCAN_USB_EP_MSGOUT},
 882
 883        /* size of rx/tx usb buffers */
 884        .rx_buffer_size = PCAN_USB_RX_BUFFER_SIZE,
 885        .tx_buffer_size = PCAN_USB_TX_BUFFER_SIZE,
 886
 887        /* device callbacks */
 888        .intf_probe = pcan_usb_probe,
 889        .dev_init = pcan_usb_init,
 890        .dev_set_bus = pcan_usb_write_mode,
 891        .dev_set_bittiming = pcan_usb_set_bittiming,
 892        .dev_get_device_id = pcan_usb_get_device_id,
 893        .dev_decode_buf = pcan_usb_decode_buf,
 894        .dev_encode_msg = pcan_usb_encode_msg,
 895        .dev_start = pcan_usb_start,
 896        .dev_restart_async = pcan_usb_restart_async,
 897};
 898