linux/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
<<
>>
Prefs
   1/*
   2 * CAN driver for PEAK System PCAN-USB Pro adapter
   3 * Derived from the PCAN project file driver/src/pcan_usbpro.c
   4 *
   5 * Copyright (C) 2003-2011 PEAK System-Technik GmbH
   6 * Copyright (C) 2011-2012 Stephane Grosjean <s.grosjean@peak-system.com>
   7 *
   8 * This program is free software; you can redistribute it and/or modify it
   9 * under the terms of the GNU General Public License as published
  10 * by the Free Software Foundation; version 2 of the License.
  11 *
  12 * This program is distributed in the hope that it will be useful, but
  13 * WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15 * General Public License for more details.
  16 */
  17#include <linux/netdevice.h>
  18#include <linux/usb.h>
  19#include <linux/module.h>
  20
  21#include <linux/can.h>
  22#include <linux/can/dev.h>
  23#include <linux/can/error.h>
  24
  25#include "pcan_usb_core.h"
  26#include "pcan_usb_pro.h"
  27
  28MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB Pro adapter");
  29
  30#define PCAN_USBPRO_CHANNEL_COUNT       2
  31
  32/* PCAN-USB Pro adapter internal clock (MHz) */
  33#define PCAN_USBPRO_CRYSTAL_HZ          56000000
  34
  35/* PCAN-USB Pro command timeout (ms.) */
  36#define PCAN_USBPRO_COMMAND_TIMEOUT     1000
  37
  38/* PCAN-USB Pro rx/tx buffers size */
  39#define PCAN_USBPRO_RX_BUFFER_SIZE      1024
  40#define PCAN_USBPRO_TX_BUFFER_SIZE      64
  41
  42#define PCAN_USBPRO_MSG_HEADER_LEN      4
  43
  44/* some commands responses need to be re-submitted */
  45#define PCAN_USBPRO_RSP_SUBMIT_MAX      2
  46
  47#define PCAN_USBPRO_RTR                 0x01
  48#define PCAN_USBPRO_EXT                 0x02
  49
  50#define PCAN_USBPRO_CMD_BUFFER_SIZE     512
  51
  52/* handle device specific info used by the netdevices */
  53struct pcan_usb_pro_interface {
  54        struct peak_usb_device *dev[PCAN_USBPRO_CHANNEL_COUNT];
  55        struct peak_time_ref time_ref;
  56        int cm_ignore_count;
  57        int dev_opened_count;
  58};
  59
  60/* device information */
  61struct pcan_usb_pro_device {
  62        struct peak_usb_device dev;
  63        struct pcan_usb_pro_interface *usb_if;
  64        u32 cached_ccbt;
  65};
  66
  67/* internal structure used to handle messages sent to bulk urb */
  68struct pcan_usb_pro_msg {
  69        u8 *rec_ptr;
  70        int rec_buffer_size;
  71        int rec_buffer_len;
  72        union {
  73                __le16 *rec_cnt_rd;
  74                __le32 *rec_cnt;
  75                u8 *rec_buffer;
  76        } u;
  77};
  78
  79/* records sizes table indexed on message id. (8-bits value) */
  80static u16 pcan_usb_pro_sizeof_rec[256] = {
  81        [PCAN_USBPRO_SETBTR] = sizeof(struct pcan_usb_pro_btr),
  82        [PCAN_USBPRO_SETBUSACT] = sizeof(struct pcan_usb_pro_busact),
  83        [PCAN_USBPRO_SETSILENT] = sizeof(struct pcan_usb_pro_silent),
  84        [PCAN_USBPRO_SETFILTR] = sizeof(struct pcan_usb_pro_filter),
  85        [PCAN_USBPRO_SETTS] = sizeof(struct pcan_usb_pro_setts),
  86        [PCAN_USBPRO_GETDEVID] = sizeof(struct pcan_usb_pro_devid),
  87        [PCAN_USBPRO_SETLED] = sizeof(struct pcan_usb_pro_setled),
  88        [PCAN_USBPRO_RXMSG8] = sizeof(struct pcan_usb_pro_rxmsg),
  89        [PCAN_USBPRO_RXMSG4] = sizeof(struct pcan_usb_pro_rxmsg) - 4,
  90        [PCAN_USBPRO_RXMSG0] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
  91        [PCAN_USBPRO_RXRTR] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
  92        [PCAN_USBPRO_RXSTATUS] = sizeof(struct pcan_usb_pro_rxstatus),
  93        [PCAN_USBPRO_RXTS] = sizeof(struct pcan_usb_pro_rxts),
  94        [PCAN_USBPRO_TXMSG8] = sizeof(struct pcan_usb_pro_txmsg),
  95        [PCAN_USBPRO_TXMSG4] = sizeof(struct pcan_usb_pro_txmsg) - 4,
  96        [PCAN_USBPRO_TXMSG0] = sizeof(struct pcan_usb_pro_txmsg) - 8,
  97};
  98
  99/*
 100 * initialize PCAN-USB Pro message data structure
 101 */
 102static u8 *pcan_msg_init(struct pcan_usb_pro_msg *pm, void *buffer_addr,
 103                         int buffer_size)
 104{
 105        if (buffer_size < PCAN_USBPRO_MSG_HEADER_LEN)
 106                return NULL;
 107
 108        pm->u.rec_buffer = (u8 *)buffer_addr;
 109        pm->rec_buffer_size = pm->rec_buffer_len = buffer_size;
 110        pm->rec_ptr = pm->u.rec_buffer + PCAN_USBPRO_MSG_HEADER_LEN;
 111
 112        return pm->rec_ptr;
 113}
 114
 115static u8 *pcan_msg_init_empty(struct pcan_usb_pro_msg *pm,
 116                               void *buffer_addr, int buffer_size)
 117{
 118        u8 *pr = pcan_msg_init(pm, buffer_addr, buffer_size);
 119
 120        if (pr) {
 121                pm->rec_buffer_len = PCAN_USBPRO_MSG_HEADER_LEN;
 122                *pm->u.rec_cnt = 0;
 123        }
 124        return pr;
 125}
 126
 127/*
 128 * add one record to a message being built
 129 */
 130static int pcan_msg_add_rec(struct pcan_usb_pro_msg *pm, int id, ...)
 131{
 132        int len, i;
 133        u8 *pc;
 134        va_list ap;
 135
 136        va_start(ap, id);
 137
 138        pc = pm->rec_ptr + 1;
 139
 140        i = 0;
 141        switch (id) {
 142        case PCAN_USBPRO_TXMSG8:
 143                i += 4;
 144                /* fall through */
 145        case PCAN_USBPRO_TXMSG4:
 146                i += 4;
 147                /* fall through */
 148        case PCAN_USBPRO_TXMSG0:
 149                *pc++ = va_arg(ap, int);
 150                *pc++ = va_arg(ap, int);
 151                *pc++ = va_arg(ap, int);
 152                *(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
 153                pc += 4;
 154                memcpy(pc, va_arg(ap, int *), i);
 155                pc += i;
 156                break;
 157
 158        case PCAN_USBPRO_SETBTR:
 159        case PCAN_USBPRO_GETDEVID:
 160                *pc++ = va_arg(ap, int);
 161                pc += 2;
 162                *(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
 163                pc += 4;
 164                break;
 165
 166        case PCAN_USBPRO_SETFILTR:
 167        case PCAN_USBPRO_SETBUSACT:
 168        case PCAN_USBPRO_SETSILENT:
 169                *pc++ = va_arg(ap, int);
 170                *(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
 171                pc += 2;
 172                break;
 173
 174        case PCAN_USBPRO_SETLED:
 175                *pc++ = va_arg(ap, int);
 176                *(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
 177                pc += 2;
 178                *(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
 179                pc += 4;
 180                break;
 181
 182        case PCAN_USBPRO_SETTS:
 183                pc++;
 184                *(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
 185                pc += 2;
 186                break;
 187
 188        default:
 189                pr_err("%s: %s(): unknown data type %02Xh (%d)\n",
 190                        PCAN_USB_DRIVER_NAME, __func__, id, id);
 191                pc--;
 192                break;
 193        }
 194
 195        len = pc - pm->rec_ptr;
 196        if (len > 0) {
 197                *pm->u.rec_cnt = cpu_to_le32(le32_to_cpu(*pm->u.rec_cnt) + 1);
 198                *pm->rec_ptr = id;
 199
 200                pm->rec_ptr = pc;
 201                pm->rec_buffer_len += len;
 202        }
 203
 204        va_end(ap);
 205
 206        return len;
 207}
 208
 209/*
 210 * send PCAN-USB Pro command synchronously
 211 */
 212static int pcan_usb_pro_send_cmd(struct peak_usb_device *dev,
 213                                 struct pcan_usb_pro_msg *pum)
 214{
 215        int actual_length;
 216        int err;
 217
 218        /* usb device unregistered? */
 219        if (!(dev->state & PCAN_USB_STATE_CONNECTED))
 220                return 0;
 221
 222        err = usb_bulk_msg(dev->udev,
 223                usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
 224                pum->u.rec_buffer, pum->rec_buffer_len,
 225                &actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
 226        if (err)
 227                netdev_err(dev->netdev, "sending command failure: %d\n", err);
 228
 229        return err;
 230}
 231
 232/*
 233 * wait for PCAN-USB Pro command response
 234 */
 235static int pcan_usb_pro_wait_rsp(struct peak_usb_device *dev,
 236                                 struct pcan_usb_pro_msg *pum)
 237{
 238        u8 req_data_type, req_channel;
 239        int actual_length;
 240        int i, err = 0;
 241
 242        /* usb device unregistered? */
 243        if (!(dev->state & PCAN_USB_STATE_CONNECTED))
 244                return 0;
 245
 246        req_data_type = pum->u.rec_buffer[4];
 247        req_channel = pum->u.rec_buffer[5];
 248
 249        *pum->u.rec_cnt = 0;
 250        for (i = 0; !err && i < PCAN_USBPRO_RSP_SUBMIT_MAX; i++) {
 251                struct pcan_usb_pro_msg rsp;
 252                union pcan_usb_pro_rec *pr;
 253                u32 r, rec_cnt;
 254                u16 rec_len;
 255                u8 *pc;
 256
 257                err = usb_bulk_msg(dev->udev,
 258                        usb_rcvbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDIN),
 259                        pum->u.rec_buffer, pum->rec_buffer_len,
 260                        &actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
 261                if (err) {
 262                        netdev_err(dev->netdev, "waiting rsp error %d\n", err);
 263                        break;
 264                }
 265
 266                if (actual_length == 0)
 267                        continue;
 268
 269                err = -EBADMSG;
 270                if (actual_length < PCAN_USBPRO_MSG_HEADER_LEN) {
 271                        netdev_err(dev->netdev,
 272                                   "got abnormal too small rsp (len=%d)\n",
 273                                   actual_length);
 274                        break;
 275                }
 276
 277                pc = pcan_msg_init(&rsp, pum->u.rec_buffer,
 278                        actual_length);
 279
 280                rec_cnt = le32_to_cpu(*rsp.u.rec_cnt);
 281
 282                /* loop on records stored into message */
 283                for (r = 0; r < rec_cnt; r++) {
 284                        pr = (union pcan_usb_pro_rec *)pc;
 285                        rec_len = pcan_usb_pro_sizeof_rec[pr->data_type];
 286                        if (!rec_len) {
 287                                netdev_err(dev->netdev,
 288                                           "got unprocessed record in msg\n");
 289                                pcan_dump_mem("rcvd rsp msg", pum->u.rec_buffer,
 290                                              actual_length);
 291                                break;
 292                        }
 293
 294                        /* check if response corresponds to request */
 295                        if (pr->data_type != req_data_type)
 296                                netdev_err(dev->netdev,
 297                                           "got unwanted rsp %xh: ignored\n",
 298                                           pr->data_type);
 299
 300                        /* check if channel in response corresponds too */
 301                        else if ((req_channel != 0xff) && \
 302                                (pr->bus_act.channel != req_channel))
 303                                netdev_err(dev->netdev,
 304                                        "got rsp %xh but on chan%u: ignored\n",
 305                                        req_data_type, pr->bus_act.channel);
 306
 307                        /* got the response */
 308                        else
 309                                return 0;
 310
 311                        /* otherwise, go on with next record in message */
 312                        pc += rec_len;
 313                }
 314        }
 315
 316        return (i >= PCAN_USBPRO_RSP_SUBMIT_MAX) ? -ERANGE : err;
 317}
 318
 319int pcan_usb_pro_send_req(struct peak_usb_device *dev, int req_id,
 320                          int req_value, void *req_addr, int req_size)
 321{
 322        int err;
 323        u8 req_type;
 324        unsigned int p;
 325
 326        /* usb device unregistered? */
 327        if (!(dev->state & PCAN_USB_STATE_CONNECTED))
 328                return 0;
 329
 330        req_type = USB_TYPE_VENDOR | USB_RECIP_OTHER;
 331
 332        switch (req_id) {
 333        case PCAN_USBPRO_REQ_FCT:
 334                p = usb_sndctrlpipe(dev->udev, 0);
 335                break;
 336
 337        default:
 338                p = usb_rcvctrlpipe(dev->udev, 0);
 339                req_type |= USB_DIR_IN;
 340                memset(req_addr, '\0', req_size);
 341                break;
 342        }
 343
 344        err = usb_control_msg(dev->udev, p, req_id, req_type, req_value, 0,
 345                              req_addr, req_size, 2 * USB_CTRL_GET_TIMEOUT);
 346        if (err < 0) {
 347                netdev_info(dev->netdev,
 348                            "unable to request usb[type=%d value=%d] err=%d\n",
 349                            req_id, req_value, err);
 350                return err;
 351        }
 352
 353        return 0;
 354}
 355
 356static int pcan_usb_pro_set_ts(struct peak_usb_device *dev, u16 onoff)
 357{
 358        struct pcan_usb_pro_msg um;
 359
 360        pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 361        pcan_msg_add_rec(&um, PCAN_USBPRO_SETTS, onoff);
 362
 363        return pcan_usb_pro_send_cmd(dev, &um);
 364}
 365
 366static int pcan_usb_pro_set_bitrate(struct peak_usb_device *dev, u32 ccbt)
 367{
 368        struct pcan_usb_pro_device *pdev =
 369                        container_of(dev, struct pcan_usb_pro_device, dev);
 370        struct pcan_usb_pro_msg um;
 371
 372        pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 373        pcan_msg_add_rec(&um, PCAN_USBPRO_SETBTR, dev->ctrl_idx, ccbt);
 374
 375        /* cache the CCBT value to reuse it before next buson */
 376        pdev->cached_ccbt = ccbt;
 377
 378        return pcan_usb_pro_send_cmd(dev, &um);
 379}
 380
 381static int pcan_usb_pro_set_bus(struct peak_usb_device *dev, u8 onoff)
 382{
 383        struct pcan_usb_pro_msg um;
 384
 385        /* if bus=on, be sure the bitrate being set before! */
 386        if (onoff) {
 387                struct pcan_usb_pro_device *pdev =
 388                             container_of(dev, struct pcan_usb_pro_device, dev);
 389
 390                pcan_usb_pro_set_bitrate(dev, pdev->cached_ccbt);
 391        }
 392
 393        pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 394        pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, onoff);
 395
 396        return pcan_usb_pro_send_cmd(dev, &um);
 397}
 398
 399static int pcan_usb_pro_set_silent(struct peak_usb_device *dev, u8 onoff)
 400{
 401        struct pcan_usb_pro_msg um;
 402
 403        pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 404        pcan_msg_add_rec(&um, PCAN_USBPRO_SETSILENT, dev->ctrl_idx, onoff);
 405
 406        return pcan_usb_pro_send_cmd(dev, &um);
 407}
 408
 409static int pcan_usb_pro_set_filter(struct peak_usb_device *dev, u16 filter_mode)
 410{
 411        struct pcan_usb_pro_msg um;
 412
 413        pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 414        pcan_msg_add_rec(&um, PCAN_USBPRO_SETFILTR, dev->ctrl_idx, filter_mode);
 415
 416        return pcan_usb_pro_send_cmd(dev, &um);
 417}
 418
 419static int pcan_usb_pro_set_led(struct peak_usb_device *dev, u8 mode,
 420                                u32 timeout)
 421{
 422        struct pcan_usb_pro_msg um;
 423
 424        pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 425        pcan_msg_add_rec(&um, PCAN_USBPRO_SETLED, dev->ctrl_idx, mode, timeout);
 426
 427        return pcan_usb_pro_send_cmd(dev, &um);
 428}
 429
 430static int pcan_usb_pro_get_device_id(struct peak_usb_device *dev,
 431                                      u32 *device_id)
 432{
 433        struct pcan_usb_pro_devid *pdn;
 434        struct pcan_usb_pro_msg um;
 435        int err;
 436        u8 *pc;
 437
 438        pc = pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
 439        pcan_msg_add_rec(&um, PCAN_USBPRO_GETDEVID, dev->ctrl_idx);
 440
 441        err =  pcan_usb_pro_send_cmd(dev, &um);
 442        if (err)
 443                return err;
 444
 445        err = pcan_usb_pro_wait_rsp(dev, &um);
 446        if (err)
 447                return err;
 448
 449        pdn = (struct pcan_usb_pro_devid *)pc;
 450        if (device_id)
 451                *device_id = le32_to_cpu(pdn->serial_num);
 452
 453        return err;
 454}
 455
 456static int pcan_usb_pro_set_bittiming(struct peak_usb_device *dev,
 457                                      struct can_bittiming *bt)
 458{
 459        u32 ccbt;
 460
 461        ccbt = (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 0x00800000 : 0;
 462        ccbt |= (bt->sjw - 1) << 24;
 463        ccbt |= (bt->phase_seg2 - 1) << 20;
 464        ccbt |= (bt->prop_seg + bt->phase_seg1 - 1) << 16; /* = tseg1 */
 465        ccbt |= bt->brp - 1;
 466
 467        netdev_info(dev->netdev, "setting ccbt=0x%08x\n", ccbt);
 468
 469        return pcan_usb_pro_set_bitrate(dev, ccbt);
 470}
 471
 472void pcan_usb_pro_restart_complete(struct urb *urb)
 473{
 474        /* can delete usb resources */
 475        peak_usb_async_complete(urb);
 476
 477        /* notify candev and netdev */
 478        peak_usb_restart_complete(urb->context);
 479}
 480
 481/*
 482 * handle restart but in asynchronously way
 483 */
 484static int pcan_usb_pro_restart_async(struct peak_usb_device *dev,
 485                                      struct urb *urb, u8 *buf)
 486{
 487        struct pcan_usb_pro_msg um;
 488
 489        pcan_msg_init_empty(&um, buf, PCAN_USB_MAX_CMD_LEN);
 490        pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, 1);
 491
 492        usb_fill_bulk_urb(urb, dev->udev,
 493                        usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
 494                        buf, PCAN_USB_MAX_CMD_LEN,
 495                        pcan_usb_pro_restart_complete, dev);
 496
 497        return usb_submit_urb(urb, GFP_ATOMIC);
 498}
 499
 500static int pcan_usb_pro_drv_loaded(struct peak_usb_device *dev, int loaded)
 501{
 502        u8 *buffer;
 503        int err;
 504
 505        buffer = kmalloc(PCAN_USBPRO_FCT_DRVLD_REQ_LEN, GFP_KERNEL);
 506        if (!buffer)
 507                return -ENOMEM;
 508
 509        buffer[0] = 0;
 510        buffer[1] = !!loaded;
 511
 512        err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_FCT,
 513                                    PCAN_USBPRO_FCT_DRVLD, buffer,
 514                                    PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
 515        kfree(buffer);
 516
 517        return err;
 518}
 519
 520static inline
 521struct pcan_usb_pro_interface *pcan_usb_pro_dev_if(struct peak_usb_device *dev)
 522{
 523        struct pcan_usb_pro_device *pdev =
 524                        container_of(dev, struct pcan_usb_pro_device, dev);
 525        return pdev->usb_if;
 526}
 527
 528static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if,
 529                                      struct pcan_usb_pro_rxmsg *rx)
 530{
 531        const unsigned int ctrl_idx = (rx->len >> 4) & 0x0f;
 532        struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
 533        struct net_device *netdev = dev->netdev;
 534        struct can_frame *can_frame;
 535        struct sk_buff *skb;
 536        struct skb_shared_hwtstamps *hwts;
 537
 538        skb = alloc_can_skb(netdev, &can_frame);
 539        if (!skb)
 540                return -ENOMEM;
 541
 542        can_frame->can_id = le32_to_cpu(rx->id);
 543        can_frame->can_dlc = rx->len & 0x0f;
 544
 545        if (rx->flags & PCAN_USBPRO_EXT)
 546                can_frame->can_id |= CAN_EFF_FLAG;
 547
 548        if (rx->flags & PCAN_USBPRO_RTR)
 549                can_frame->can_id |= CAN_RTR_FLAG;
 550        else
 551                memcpy(can_frame->data, rx->data, can_frame->can_dlc);
 552
 553        hwts = skb_hwtstamps(skb);
 554        peak_usb_get_ts_time(&usb_if->time_ref, le32_to_cpu(rx->ts32),
 555                             &hwts->hwtstamp);
 556
 557        netdev->stats.rx_packets++;
 558        netdev->stats.rx_bytes += can_frame->can_dlc;
 559        netif_rx(skb);
 560
 561        return 0;
 562}
 563
 564static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if,
 565                                     struct pcan_usb_pro_rxstatus *er)
 566{
 567        const u16 raw_status = le16_to_cpu(er->status);
 568        const unsigned int ctrl_idx = (er->channel >> 4) & 0x0f;
 569        struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
 570        struct net_device *netdev = dev->netdev;
 571        struct can_frame *can_frame;
 572        enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
 573        u8 err_mask = 0;
 574        struct sk_buff *skb;
 575        struct skb_shared_hwtstamps *hwts;
 576
 577        /* nothing should be sent while in BUS_OFF state */
 578        if (dev->can.state == CAN_STATE_BUS_OFF)
 579                return 0;
 580
 581        if (!raw_status) {
 582                /* no error bit (back to active state) */
 583                dev->can.state = CAN_STATE_ERROR_ACTIVE;
 584                return 0;
 585        }
 586
 587        if (raw_status & (PCAN_USBPRO_STATUS_OVERRUN |
 588                          PCAN_USBPRO_STATUS_QOVERRUN)) {
 589                /* trick to bypass next comparison and process other errors */
 590                new_state = CAN_STATE_MAX;
 591        }
 592
 593        if (raw_status & PCAN_USBPRO_STATUS_BUS) {
 594                new_state = CAN_STATE_BUS_OFF;
 595        } else if (raw_status & PCAN_USBPRO_STATUS_ERROR) {
 596                u32 rx_err_cnt = (le32_to_cpu(er->err_frm) & 0x00ff0000) >> 16;
 597                u32 tx_err_cnt = (le32_to_cpu(er->err_frm) & 0xff000000) >> 24;
 598
 599                if (rx_err_cnt > 127)
 600                        err_mask |= CAN_ERR_CRTL_RX_PASSIVE;
 601                else if (rx_err_cnt > 96)
 602                        err_mask |= CAN_ERR_CRTL_RX_WARNING;
 603
 604                if (tx_err_cnt > 127)
 605                        err_mask |= CAN_ERR_CRTL_TX_PASSIVE;
 606                else if (tx_err_cnt > 96)
 607                        err_mask |= CAN_ERR_CRTL_TX_WARNING;
 608
 609                if (err_mask & (CAN_ERR_CRTL_RX_WARNING |
 610                                CAN_ERR_CRTL_TX_WARNING))
 611                        new_state = CAN_STATE_ERROR_WARNING;
 612                else if (err_mask & (CAN_ERR_CRTL_RX_PASSIVE |
 613                                     CAN_ERR_CRTL_TX_PASSIVE))
 614                        new_state = CAN_STATE_ERROR_PASSIVE;
 615        }
 616
 617        /* donot post any error if current state didn't change */
 618        if (dev->can.state == new_state)
 619                return 0;
 620
 621        /* allocate an skb to store the error frame */
 622        skb = alloc_can_err_skb(netdev, &can_frame);
 623        if (!skb)
 624                return -ENOMEM;
 625
 626        switch (new_state) {
 627        case CAN_STATE_BUS_OFF:
 628                can_frame->can_id |= CAN_ERR_BUSOFF;
 629                dev->can.can_stats.bus_off++;
 630                can_bus_off(netdev);
 631                break;
 632
 633        case CAN_STATE_ERROR_PASSIVE:
 634                can_frame->can_id |= CAN_ERR_CRTL;
 635                can_frame->data[1] |= err_mask;
 636                dev->can.can_stats.error_passive++;
 637                break;
 638
 639        case CAN_STATE_ERROR_WARNING:
 640                can_frame->can_id |= CAN_ERR_CRTL;
 641                can_frame->data[1] |= err_mask;
 642                dev->can.can_stats.error_warning++;
 643                break;
 644
 645        case CAN_STATE_ERROR_ACTIVE:
 646                break;
 647
 648        default:
 649                /* CAN_STATE_MAX (trick to handle other errors) */
 650                if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) {
 651                        can_frame->can_id |= CAN_ERR_PROT;
 652                        can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD;
 653                        netdev->stats.rx_over_errors++;
 654                        netdev->stats.rx_errors++;
 655                }
 656
 657                if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) {
 658                        can_frame->can_id |= CAN_ERR_CRTL;
 659                        can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
 660                        netdev->stats.rx_over_errors++;
 661                        netdev->stats.rx_errors++;
 662                }
 663
 664                new_state = CAN_STATE_ERROR_ACTIVE;
 665                break;
 666        }
 667
 668        dev->can.state = new_state;
 669
 670        hwts = skb_hwtstamps(skb);
 671        peak_usb_get_ts_time(&usb_if->time_ref, le32_to_cpu(er->ts32), &hwts->hwtstamp);
 672        netdev->stats.rx_packets++;
 673        netdev->stats.rx_bytes += can_frame->can_dlc;
 674        netif_rx(skb);
 675
 676        return 0;
 677}
 678
 679static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if,
 680                                   struct pcan_usb_pro_rxts *ts)
 681{
 682        /* should wait until clock is stabilized */
 683        if (usb_if->cm_ignore_count > 0)
 684                usb_if->cm_ignore_count--;
 685        else
 686                peak_usb_set_ts_now(&usb_if->time_ref,
 687                                    le32_to_cpu(ts->ts64[1]));
 688}
 689
 690/*
 691 * callback for bulk IN urb
 692 */
 693static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb)
 694{
 695        struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev);
 696        struct net_device *netdev = dev->netdev;
 697        struct pcan_usb_pro_msg usb_msg;
 698        u8 *rec_ptr, *msg_end;
 699        u16 rec_cnt;
 700        int err = 0;
 701
 702        rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
 703                                        urb->actual_length);
 704        if (!rec_ptr) {
 705                netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length);
 706                return -EINVAL;
 707        }
 708
 709        /* loop reading all the records from the incoming message */
 710        msg_end = urb->transfer_buffer + urb->actual_length;
 711        rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd);
 712        for (; rec_cnt > 0; rec_cnt--) {
 713                union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr;
 714                u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type];
 715
 716                if (!sizeof_rec) {
 717                        netdev_err(netdev,
 718                                   "got unsupported rec in usb msg:\n");
 719                        err = -ENOTSUPP;
 720                        break;
 721                }
 722
 723                /* check if the record goes out of current packet */
 724                if (rec_ptr + sizeof_rec > msg_end) {
 725                        netdev_err(netdev,
 726                                "got frag rec: should inc usb rx buf size\n");
 727                        err = -EBADMSG;
 728                        break;
 729                }
 730
 731                switch (pr->data_type) {
 732                case PCAN_USBPRO_RXMSG8:
 733                case PCAN_USBPRO_RXMSG4:
 734                case PCAN_USBPRO_RXMSG0:
 735                case PCAN_USBPRO_RXRTR:
 736                        err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg);
 737                        if (err < 0)
 738                                goto fail;
 739                        break;
 740
 741                case PCAN_USBPRO_RXSTATUS:
 742                        err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status);
 743                        if (err < 0)
 744                                goto fail;
 745                        break;
 746
 747                case PCAN_USBPRO_RXTS:
 748                        pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts);
 749                        break;
 750
 751                default:
 752                        netdev_err(netdev,
 753                                   "unhandled rec type 0x%02x (%d): ignored\n",
 754                                   pr->data_type, pr->data_type);
 755                        break;
 756                }
 757
 758                rec_ptr += sizeof_rec;
 759        }
 760
 761fail:
 762        if (err)
 763                pcan_dump_mem("received msg",
 764                              urb->transfer_buffer, urb->actual_length);
 765
 766        return err;
 767}
 768
 769static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev,
 770                                   struct sk_buff *skb, u8 *obuf, size_t *size)
 771{
 772        struct can_frame *cf = (struct can_frame *)skb->data;
 773        u8 data_type, len, flags;
 774        struct pcan_usb_pro_msg usb_msg;
 775
 776        pcan_msg_init_empty(&usb_msg, obuf, *size);
 777
 778        if ((cf->can_id & CAN_RTR_FLAG) || (cf->can_dlc == 0))
 779                data_type = PCAN_USBPRO_TXMSG0;
 780        else if (cf->can_dlc <= 4)
 781                data_type = PCAN_USBPRO_TXMSG4;
 782        else
 783                data_type = PCAN_USBPRO_TXMSG8;
 784
 785        len = (dev->ctrl_idx << 4) | (cf->can_dlc & 0x0f);
 786
 787        flags = 0;
 788        if (cf->can_id & CAN_EFF_FLAG)
 789                flags |= 0x02;
 790        if (cf->can_id & CAN_RTR_FLAG)
 791                flags |= 0x01;
 792
 793        pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id,
 794                         cf->data);
 795
 796        *size = usb_msg.rec_buffer_len;
 797
 798        return 0;
 799}
 800
 801static int pcan_usb_pro_start(struct peak_usb_device *dev)
 802{
 803        struct pcan_usb_pro_device *pdev =
 804                        container_of(dev, struct pcan_usb_pro_device, dev);
 805        int err;
 806
 807        err = pcan_usb_pro_set_silent(dev,
 808                                dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
 809        if (err)
 810                return err;
 811
 812        /* filter mode: 0-> All OFF; 1->bypass */
 813        err = pcan_usb_pro_set_filter(dev, 1);
 814        if (err)
 815                return err;
 816
 817        /* opening first device: */
 818        if (pdev->usb_if->dev_opened_count == 0) {
 819                /* reset time_ref */
 820                peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro);
 821
 822                /* ask device to send ts messages */
 823                err = pcan_usb_pro_set_ts(dev, 1);
 824        }
 825
 826        pdev->usb_if->dev_opened_count++;
 827
 828        return err;
 829}
 830
 831/*
 832 * stop interface
 833 * (last chance before set bus off)
 834 */
 835static int pcan_usb_pro_stop(struct peak_usb_device *dev)
 836{
 837        struct pcan_usb_pro_device *pdev =
 838                        container_of(dev, struct pcan_usb_pro_device, dev);
 839
 840        /* turn off ts msgs for that interface if no other dev opened */
 841        if (pdev->usb_if->dev_opened_count == 1)
 842                pcan_usb_pro_set_ts(dev, 0);
 843
 844        pdev->usb_if->dev_opened_count--;
 845
 846        return 0;
 847}
 848
 849/*
 850 * called when probing to initialize a device object.
 851 */
 852static int pcan_usb_pro_init(struct peak_usb_device *dev)
 853{
 854        struct pcan_usb_pro_device *pdev =
 855                        container_of(dev, struct pcan_usb_pro_device, dev);
 856        struct pcan_usb_pro_interface *usb_if = NULL;
 857        struct pcan_usb_pro_fwinfo *fi = NULL;
 858        struct pcan_usb_pro_blinfo *bi = NULL;
 859        int err;
 860
 861        /* do this for 1st channel only */
 862        if (!dev->prev_siblings) {
 863                /* allocate netdevices common structure attached to first one */
 864                usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
 865                                 GFP_KERNEL);
 866                fi = kmalloc(sizeof(struct pcan_usb_pro_fwinfo), GFP_KERNEL);
 867                bi = kmalloc(sizeof(struct pcan_usb_pro_blinfo), GFP_KERNEL);
 868                if (!usb_if || !fi || !bi) {
 869                        err = -ENOMEM;
 870                        goto err_out;
 871                }
 872
 873                /* number of ts msgs to ignore before taking one into account */
 874                usb_if->cm_ignore_count = 5;
 875
 876                /*
 877                 * explicit use of dev_xxx() instead of netdev_xxx() here:
 878                 * information displayed are related to the device itself, not
 879                 * to the canx netdevices.
 880                 */
 881                err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
 882                                            PCAN_USBPRO_INFO_FW,
 883                                            fi, sizeof(*fi));
 884                if (err) {
 885                        dev_err(dev->netdev->dev.parent,
 886                                "unable to read %s firmware info (err %d)\n",
 887                                pcan_usb_pro.name, err);
 888                        goto err_out;
 889                }
 890
 891                err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
 892                                            PCAN_USBPRO_INFO_BL,
 893                                            bi, sizeof(*bi));
 894                if (err) {
 895                        dev_err(dev->netdev->dev.parent,
 896                                "unable to read %s bootloader info (err %d)\n",
 897                                pcan_usb_pro.name, err);
 898                        goto err_out;
 899                }
 900
 901                /* tell the device the can driver is running */
 902                err = pcan_usb_pro_drv_loaded(dev, 1);
 903                if (err)
 904                        goto err_out;
 905
 906                dev_info(dev->netdev->dev.parent,
 907                     "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
 908                     pcan_usb_pro.name,
 909                     bi->hw_rev, bi->serial_num_hi, bi->serial_num_lo,
 910                     pcan_usb_pro.ctrl_count);
 911        } else {
 912                usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
 913        }
 914
 915        pdev->usb_if = usb_if;
 916        usb_if->dev[dev->ctrl_idx] = dev;
 917
 918        /* set LED in default state (end of init phase) */
 919        pcan_usb_pro_set_led(dev, 0, 1);
 920
 921        kfree(bi);
 922        kfree(fi);
 923
 924        return 0;
 925
 926 err_out:
 927        kfree(bi);
 928        kfree(fi);
 929        kfree(usb_if);
 930
 931        return err;
 932}
 933
 934static void pcan_usb_pro_exit(struct peak_usb_device *dev)
 935{
 936        struct pcan_usb_pro_device *pdev =
 937                        container_of(dev, struct pcan_usb_pro_device, dev);
 938
 939        /*
 940         * when rmmod called before unplug and if down, should reset things
 941         * before leaving
 942         */
 943        if (dev->can.state != CAN_STATE_STOPPED) {
 944                /* set bus off on the corresponding channel */
 945                pcan_usb_pro_set_bus(dev, 0);
 946        }
 947
 948        /* if channel #0 (only) */
 949        if (dev->ctrl_idx == 0) {
 950                /* turn off calibration message if any device were opened */
 951                if (pdev->usb_if->dev_opened_count > 0)
 952                        pcan_usb_pro_set_ts(dev, 0);
 953
 954                /* tell the PCAN-USB Pro device the driver is being unloaded */
 955                pcan_usb_pro_drv_loaded(dev, 0);
 956        }
 957}
 958
 959/*
 960 * called when PCAN-USB Pro adapter is unplugged
 961 */
 962static void pcan_usb_pro_free(struct peak_usb_device *dev)
 963{
 964        /* last device: can free pcan_usb_pro_interface object now */
 965        if (!dev->prev_siblings && !dev->next_siblings)
 966                kfree(pcan_usb_pro_dev_if(dev));
 967}
 968
 969/*
 970 * probe function for new PCAN-USB Pro usb interface
 971 */
 972int pcan_usb_pro_probe(struct usb_interface *intf)
 973{
 974        struct usb_host_interface *if_desc;
 975        int i;
 976
 977        if_desc = intf->altsetting;
 978
 979        /* check interface endpoint addresses */
 980        for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
 981                struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
 982
 983                /*
 984                 * below is the list of valid ep addreses. Any other ep address
 985                 * is considered as not-CAN interface address => no dev created
 986                 */
 987                switch (ep->bEndpointAddress) {
 988                case PCAN_USBPRO_EP_CMDOUT:
 989                case PCAN_USBPRO_EP_CMDIN:
 990                case PCAN_USBPRO_EP_MSGOUT_0:
 991                case PCAN_USBPRO_EP_MSGOUT_1:
 992                case PCAN_USBPRO_EP_MSGIN:
 993                case PCAN_USBPRO_EP_UNUSED:
 994                        break;
 995                default:
 996                        return -ENODEV;
 997                }
 998        }
 999
1000        return 0;
1001}
1002
1003/*
1004 * describe the PCAN-USB Pro adapter
1005 */
1006static const struct can_bittiming_const pcan_usb_pro_const = {
1007        .name = "pcan_usb_pro",
1008        .tseg1_min = 1,
1009        .tseg1_max = 16,
1010        .tseg2_min = 1,
1011        .tseg2_max = 8,
1012        .sjw_max = 4,
1013        .brp_min = 1,
1014        .brp_max = 1024,
1015        .brp_inc = 1,
1016};
1017
1018const struct peak_usb_adapter pcan_usb_pro = {
1019        .name = "PCAN-USB Pro",
1020        .device_id = PCAN_USBPRO_PRODUCT_ID,
1021        .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
1022        .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
1023        .clock = {
1024                .freq = PCAN_USBPRO_CRYSTAL_HZ,
1025        },
1026        .bittiming_const = &pcan_usb_pro_const,
1027
1028        /* size of device private data */
1029        .sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
1030
1031        /* timestamps usage */
1032        .ts_used_bits = 32,
1033        .ts_period = 1000000, /* calibration period in ts. */
1034        .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1035        .us_per_ts_shift = 0,
1036
1037        /* give here messages in/out endpoints */
1038        .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1039        .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1040
1041        /* size of rx/tx usb buffers */
1042        .rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
1043        .tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
1044
1045        /* device callbacks */
1046        .intf_probe = pcan_usb_pro_probe,
1047        .dev_init = pcan_usb_pro_init,
1048        .dev_exit = pcan_usb_pro_exit,
1049        .dev_free = pcan_usb_pro_free,
1050        .dev_set_bus = pcan_usb_pro_set_bus,
1051        .dev_set_bittiming = pcan_usb_pro_set_bittiming,
1052        .dev_get_device_id = pcan_usb_pro_get_device_id,
1053        .dev_decode_buf = pcan_usb_pro_decode_buf,
1054        .dev_encode_msg = pcan_usb_pro_encode_msg,
1055        .dev_start = pcan_usb_pro_start,
1056        .dev_stop = pcan_usb_pro_stop,
1057        .dev_restart_async = pcan_usb_pro_restart_async,
1058};
1059