root/drivers/net/can/usb/esd_usb2.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. esd_usb2_rx_event
  2. esd_usb2_rx_can_msg
  3. esd_usb2_tx_done_msg
  4. esd_usb2_read_bulk_callback
  5. esd_usb2_write_bulk_callback
  6. show_firmware
  7. show_hardware
  8. show_nets
  9. esd_usb2_send_msg
  10. esd_usb2_wait_msg
  11. esd_usb2_setup_rx_urbs
  12. esd_usb2_start
  13. unlink_all_urbs
  14. esd_usb2_open
  15. esd_usb2_start_xmit
  16. esd_usb2_close
  17. esd_usb2_set_bittiming
  18. esd_usb2_get_berr_counter
  19. esd_usb2_set_mode
  20. esd_usb2_probe_one_net
  21. esd_usb2_probe
  22. esd_usb2_disconnect

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * CAN driver for esd CAN-USB/2 and CAN-USB/Micro
   4  *
   5  * Copyright (C) 2010-2012 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
   6  */
   7 #include <linux/signal.h>
   8 #include <linux/slab.h>
   9 #include <linux/module.h>
  10 #include <linux/netdevice.h>
  11 #include <linux/usb.h>
  12 
  13 #include <linux/can.h>
  14 #include <linux/can/dev.h>
  15 #include <linux/can/error.h>
  16 
  17 MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd.eu>");
  18 MODULE_DESCRIPTION("CAN driver for esd CAN-USB/2 and CAN-USB/Micro interfaces");
  19 MODULE_LICENSE("GPL v2");
  20 
  21 /* Define these values to match your devices */
  22 #define USB_ESDGMBH_VENDOR_ID   0x0ab4
  23 #define USB_CANUSB2_PRODUCT_ID  0x0010
  24 #define USB_CANUSBM_PRODUCT_ID  0x0011
  25 
  26 #define ESD_USB2_CAN_CLOCK      60000000
  27 #define ESD_USBM_CAN_CLOCK      36000000
  28 #define ESD_USB2_MAX_NETS       2
  29 
  30 /* USB2 commands */
  31 #define CMD_VERSION             1 /* also used for VERSION_REPLY */
  32 #define CMD_CAN_RX              2 /* device to host only */
  33 #define CMD_CAN_TX              3 /* also used for TX_DONE */
  34 #define CMD_SETBAUD             4 /* also used for SETBAUD_REPLY */
  35 #define CMD_TS                  5 /* also used for TS_REPLY */
  36 #define CMD_IDADD               6 /* also used for IDADD_REPLY */
  37 
  38 /* esd CAN message flags - dlc field */
  39 #define ESD_RTR                 0x10
  40 
  41 /* esd CAN message flags - id field */
  42 #define ESD_EXTID               0x20000000
  43 #define ESD_EVENT               0x40000000
  44 #define ESD_IDMASK              0x1fffffff
  45 
  46 /* esd CAN event ids used by this driver */
  47 #define ESD_EV_CAN_ERROR_EXT    2
  48 
  49 /* baudrate message flags */
  50 #define ESD_USB2_UBR            0x80000000
  51 #define ESD_USB2_LOM            0x40000000
  52 #define ESD_USB2_NO_BAUDRATE    0x7fffffff
  53 #define ESD_USB2_TSEG1_MIN      1
  54 #define ESD_USB2_TSEG1_MAX      16
  55 #define ESD_USB2_TSEG1_SHIFT    16
  56 #define ESD_USB2_TSEG2_MIN      1
  57 #define ESD_USB2_TSEG2_MAX      8
  58 #define ESD_USB2_TSEG2_SHIFT    20
  59 #define ESD_USB2_SJW_MAX        4
  60 #define ESD_USB2_SJW_SHIFT      14
  61 #define ESD_USBM_SJW_SHIFT      24
  62 #define ESD_USB2_BRP_MIN        1
  63 #define ESD_USB2_BRP_MAX        1024
  64 #define ESD_USB2_BRP_INC        1
  65 #define ESD_USB2_3_SAMPLES      0x00800000
  66 
  67 /* esd IDADD message */
  68 #define ESD_ID_ENABLE           0x80
  69 #define ESD_MAX_ID_SEGMENT      64
  70 
  71 /* SJA1000 ECC register (emulated by usb2 firmware) */
  72 #define SJA1000_ECC_SEG         0x1F
  73 #define SJA1000_ECC_DIR         0x20
  74 #define SJA1000_ECC_ERR         0x06
  75 #define SJA1000_ECC_BIT         0x00
  76 #define SJA1000_ECC_FORM        0x40
  77 #define SJA1000_ECC_STUFF       0x80
  78 #define SJA1000_ECC_MASK        0xc0
  79 
  80 /* esd bus state event codes */
  81 #define ESD_BUSSTATE_MASK       0xc0
  82 #define ESD_BUSSTATE_WARN       0x40
  83 #define ESD_BUSSTATE_ERRPASSIVE 0x80
  84 #define ESD_BUSSTATE_BUSOFF     0xc0
  85 
  86 #define RX_BUFFER_SIZE          1024
  87 #define MAX_RX_URBS             4
  88 #define MAX_TX_URBS             16 /* must be power of 2 */
  89 
  90 struct header_msg {
  91         u8 len; /* len is always the total message length in 32bit words */
  92         u8 cmd;
  93         u8 rsvd[2];
  94 };
  95 
  96 struct version_msg {
  97         u8 len;
  98         u8 cmd;
  99         u8 rsvd;
 100         u8 flags;
 101         __le32 drv_version;
 102 };
 103 
 104 struct version_reply_msg {
 105         u8 len;
 106         u8 cmd;
 107         u8 nets;
 108         u8 features;
 109         __le32 version;
 110         u8 name[16];
 111         __le32 rsvd;
 112         __le32 ts;
 113 };
 114 
 115 struct rx_msg {
 116         u8 len;
 117         u8 cmd;
 118         u8 net;
 119         u8 dlc;
 120         __le32 ts;
 121         __le32 id; /* upper 3 bits contain flags */
 122         u8 data[8];
 123 };
 124 
 125 struct tx_msg {
 126         u8 len;
 127         u8 cmd;
 128         u8 net;
 129         u8 dlc;
 130         u32 hnd;        /* opaque handle, not used by device */
 131         __le32 id; /* upper 3 bits contain flags */
 132         u8 data[8];
 133 };
 134 
 135 struct tx_done_msg {
 136         u8 len;
 137         u8 cmd;
 138         u8 net;
 139         u8 status;
 140         u32 hnd;        /* opaque handle, not used by device */
 141         __le32 ts;
 142 };
 143 
 144 struct id_filter_msg {
 145         u8 len;
 146         u8 cmd;
 147         u8 net;
 148         u8 option;
 149         __le32 mask[ESD_MAX_ID_SEGMENT + 1];
 150 };
 151 
 152 struct set_baudrate_msg {
 153         u8 len;
 154         u8 cmd;
 155         u8 net;
 156         u8 rsvd;
 157         __le32 baud;
 158 };
 159 
 160 /* Main message type used between library and application */
 161 struct __attribute__ ((packed)) esd_usb2_msg {
 162         union {
 163                 struct header_msg hdr;
 164                 struct version_msg version;
 165                 struct version_reply_msg version_reply;
 166                 struct rx_msg rx;
 167                 struct tx_msg tx;
 168                 struct tx_done_msg txdone;
 169                 struct set_baudrate_msg setbaud;
 170                 struct id_filter_msg filter;
 171         } msg;
 172 };
 173 
 174 static struct usb_device_id esd_usb2_table[] = {
 175         {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)},
 176         {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSBM_PRODUCT_ID)},
 177         {}
 178 };
 179 MODULE_DEVICE_TABLE(usb, esd_usb2_table);
 180 
 181 struct esd_usb2_net_priv;
 182 
 183 struct esd_tx_urb_context {
 184         struct esd_usb2_net_priv *priv;
 185         u32 echo_index;
 186         int dlc;
 187 };
 188 
 189 struct esd_usb2 {
 190         struct usb_device *udev;
 191         struct esd_usb2_net_priv *nets[ESD_USB2_MAX_NETS];
 192 
 193         struct usb_anchor rx_submitted;
 194 
 195         int net_count;
 196         u32 version;
 197         int rxinitdone;
 198 };
 199 
 200 struct esd_usb2_net_priv {
 201         struct can_priv can; /* must be the first member */
 202 
 203         atomic_t active_tx_jobs;
 204         struct usb_anchor tx_submitted;
 205         struct esd_tx_urb_context tx_contexts[MAX_TX_URBS];
 206 
 207         struct esd_usb2 *usb2;
 208         struct net_device *netdev;
 209         int index;
 210         u8 old_state;
 211         struct can_berr_counter bec;
 212 };
 213 
 214 static void esd_usb2_rx_event(struct esd_usb2_net_priv *priv,
 215                               struct esd_usb2_msg *msg)
 216 {
 217         struct net_device_stats *stats = &priv->netdev->stats;
 218         struct can_frame *cf;
 219         struct sk_buff *skb;
 220         u32 id = le32_to_cpu(msg->msg.rx.id) & ESD_IDMASK;
 221 
 222         if (id == ESD_EV_CAN_ERROR_EXT) {
 223                 u8 state = msg->msg.rx.data[0];
 224                 u8 ecc = msg->msg.rx.data[1];
 225                 u8 txerr = msg->msg.rx.data[2];
 226                 u8 rxerr = msg->msg.rx.data[3];
 227 
 228                 skb = alloc_can_err_skb(priv->netdev, &cf);
 229                 if (skb == NULL) {
 230                         stats->rx_dropped++;
 231                         return;
 232                 }
 233 
 234                 if (state != priv->old_state) {
 235                         priv->old_state = state;
 236 
 237                         switch (state & ESD_BUSSTATE_MASK) {
 238                         case ESD_BUSSTATE_BUSOFF:
 239                                 priv->can.state = CAN_STATE_BUS_OFF;
 240                                 cf->can_id |= CAN_ERR_BUSOFF;
 241                                 priv->can.can_stats.bus_off++;
 242                                 can_bus_off(priv->netdev);
 243                                 break;
 244                         case ESD_BUSSTATE_WARN:
 245                                 priv->can.state = CAN_STATE_ERROR_WARNING;
 246                                 priv->can.can_stats.error_warning++;
 247                                 break;
 248                         case ESD_BUSSTATE_ERRPASSIVE:
 249                                 priv->can.state = CAN_STATE_ERROR_PASSIVE;
 250                                 priv->can.can_stats.error_passive++;
 251                                 break;
 252                         default:
 253                                 priv->can.state = CAN_STATE_ERROR_ACTIVE;
 254                                 break;
 255                         }
 256                 } else {
 257                         priv->can.can_stats.bus_error++;
 258                         stats->rx_errors++;
 259 
 260                         cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
 261 
 262                         switch (ecc & SJA1000_ECC_MASK) {
 263                         case SJA1000_ECC_BIT:
 264                                 cf->data[2] |= CAN_ERR_PROT_BIT;
 265                                 break;
 266                         case SJA1000_ECC_FORM:
 267                                 cf->data[2] |= CAN_ERR_PROT_FORM;
 268                                 break;
 269                         case SJA1000_ECC_STUFF:
 270                                 cf->data[2] |= CAN_ERR_PROT_STUFF;
 271                                 break;
 272                         default:
 273                                 cf->data[3] = ecc & SJA1000_ECC_SEG;
 274                                 break;
 275                         }
 276 
 277                         /* Error occurred during transmission? */
 278                         if (!(ecc & SJA1000_ECC_DIR))
 279                                 cf->data[2] |= CAN_ERR_PROT_TX;
 280 
 281                         if (priv->can.state == CAN_STATE_ERROR_WARNING ||
 282                             priv->can.state == CAN_STATE_ERROR_PASSIVE) {
 283                                 cf->data[1] = (txerr > rxerr) ?
 284                                         CAN_ERR_CRTL_TX_PASSIVE :
 285                                         CAN_ERR_CRTL_RX_PASSIVE;
 286                         }
 287                         cf->data[6] = txerr;
 288                         cf->data[7] = rxerr;
 289                 }
 290 
 291                 priv->bec.txerr = txerr;
 292                 priv->bec.rxerr = rxerr;
 293 
 294                 stats->rx_packets++;
 295                 stats->rx_bytes += cf->can_dlc;
 296                 netif_rx(skb);
 297         }
 298 }
 299 
 300 static void esd_usb2_rx_can_msg(struct esd_usb2_net_priv *priv,
 301                                 struct esd_usb2_msg *msg)
 302 {
 303         struct net_device_stats *stats = &priv->netdev->stats;
 304         struct can_frame *cf;
 305         struct sk_buff *skb;
 306         int i;
 307         u32 id;
 308 
 309         if (!netif_device_present(priv->netdev))
 310                 return;
 311 
 312         id = le32_to_cpu(msg->msg.rx.id);
 313 
 314         if (id & ESD_EVENT) {
 315                 esd_usb2_rx_event(priv, msg);
 316         } else {
 317                 skb = alloc_can_skb(priv->netdev, &cf);
 318                 if (skb == NULL) {
 319                         stats->rx_dropped++;
 320                         return;
 321                 }
 322 
 323                 cf->can_id = id & ESD_IDMASK;
 324                 cf->can_dlc = get_can_dlc(msg->msg.rx.dlc & ~ESD_RTR);
 325 
 326                 if (id & ESD_EXTID)
 327                         cf->can_id |= CAN_EFF_FLAG;
 328 
 329                 if (msg->msg.rx.dlc & ESD_RTR) {
 330                         cf->can_id |= CAN_RTR_FLAG;
 331                 } else {
 332                         for (i = 0; i < cf->can_dlc; i++)
 333                                 cf->data[i] = msg->msg.rx.data[i];
 334                 }
 335 
 336                 stats->rx_packets++;
 337                 stats->rx_bytes += cf->can_dlc;
 338                 netif_rx(skb);
 339         }
 340 
 341         return;
 342 }
 343 
 344 static void esd_usb2_tx_done_msg(struct esd_usb2_net_priv *priv,
 345                                  struct esd_usb2_msg *msg)
 346 {
 347         struct net_device_stats *stats = &priv->netdev->stats;
 348         struct net_device *netdev = priv->netdev;
 349         struct esd_tx_urb_context *context;
 350 
 351         if (!netif_device_present(netdev))
 352                 return;
 353 
 354         context = &priv->tx_contexts[msg->msg.txdone.hnd & (MAX_TX_URBS - 1)];
 355 
 356         if (!msg->msg.txdone.status) {
 357                 stats->tx_packets++;
 358                 stats->tx_bytes += context->dlc;
 359                 can_get_echo_skb(netdev, context->echo_index);
 360         } else {
 361                 stats->tx_errors++;
 362                 can_free_echo_skb(netdev, context->echo_index);
 363         }
 364 
 365         /* Release context */
 366         context->echo_index = MAX_TX_URBS;
 367         atomic_dec(&priv->active_tx_jobs);
 368 
 369         netif_wake_queue(netdev);
 370 }
 371 
 372 static void esd_usb2_read_bulk_callback(struct urb *urb)
 373 {
 374         struct esd_usb2 *dev = urb->context;
 375         int retval;
 376         int pos = 0;
 377         int i;
 378 
 379         switch (urb->status) {
 380         case 0: /* success */
 381                 break;
 382 
 383         case -ENOENT:
 384         case -EPIPE:
 385         case -EPROTO:
 386         case -ESHUTDOWN:
 387                 return;
 388 
 389         default:
 390                 dev_info(dev->udev->dev.parent,
 391                          "Rx URB aborted (%d)\n", urb->status);
 392                 goto resubmit_urb;
 393         }
 394 
 395         while (pos < urb->actual_length) {
 396                 struct esd_usb2_msg *msg;
 397 
 398                 msg = (struct esd_usb2_msg *)(urb->transfer_buffer + pos);
 399 
 400                 switch (msg->msg.hdr.cmd) {
 401                 case CMD_CAN_RX:
 402                         if (msg->msg.rx.net >= dev->net_count) {
 403                                 dev_err(dev->udev->dev.parent, "format error\n");
 404                                 break;
 405                         }
 406 
 407                         esd_usb2_rx_can_msg(dev->nets[msg->msg.rx.net], msg);
 408                         break;
 409 
 410                 case CMD_CAN_TX:
 411                         if (msg->msg.txdone.net >= dev->net_count) {
 412                                 dev_err(dev->udev->dev.parent, "format error\n");
 413                                 break;
 414                         }
 415 
 416                         esd_usb2_tx_done_msg(dev->nets[msg->msg.txdone.net],
 417                                              msg);
 418                         break;
 419                 }
 420 
 421                 pos += msg->msg.hdr.len << 2;
 422 
 423                 if (pos > urb->actual_length) {
 424                         dev_err(dev->udev->dev.parent, "format error\n");
 425                         break;
 426                 }
 427         }
 428 
 429 resubmit_urb:
 430         usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
 431                           urb->transfer_buffer, RX_BUFFER_SIZE,
 432                           esd_usb2_read_bulk_callback, dev);
 433 
 434         retval = usb_submit_urb(urb, GFP_ATOMIC);
 435         if (retval == -ENODEV) {
 436                 for (i = 0; i < dev->net_count; i++) {
 437                         if (dev->nets[i])
 438                                 netif_device_detach(dev->nets[i]->netdev);
 439                 }
 440         } else if (retval) {
 441                 dev_err(dev->udev->dev.parent,
 442                         "failed resubmitting read bulk urb: %d\n", retval);
 443         }
 444 
 445         return;
 446 }
 447 
 448 /*
 449  * callback for bulk IN urb
 450  */
 451 static void esd_usb2_write_bulk_callback(struct urb *urb)
 452 {
 453         struct esd_tx_urb_context *context = urb->context;
 454         struct esd_usb2_net_priv *priv;
 455         struct net_device *netdev;
 456         size_t size = sizeof(struct esd_usb2_msg);
 457 
 458         WARN_ON(!context);
 459 
 460         priv = context->priv;
 461         netdev = priv->netdev;
 462 
 463         /* free up our allocated buffer */
 464         usb_free_coherent(urb->dev, size,
 465                           urb->transfer_buffer, urb->transfer_dma);
 466 
 467         if (!netif_device_present(netdev))
 468                 return;
 469 
 470         if (urb->status)
 471                 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
 472 
 473         netif_trans_update(netdev);
 474 }
 475 
 476 static ssize_t show_firmware(struct device *d,
 477                              struct device_attribute *attr, char *buf)
 478 {
 479         struct usb_interface *intf = to_usb_interface(d);
 480         struct esd_usb2 *dev = usb_get_intfdata(intf);
 481 
 482         return sprintf(buf, "%d.%d.%d\n",
 483                        (dev->version >> 12) & 0xf,
 484                        (dev->version >> 8) & 0xf,
 485                        dev->version & 0xff);
 486 }
 487 static DEVICE_ATTR(firmware, 0444, show_firmware, NULL);
 488 
 489 static ssize_t show_hardware(struct device *d,
 490                              struct device_attribute *attr, char *buf)
 491 {
 492         struct usb_interface *intf = to_usb_interface(d);
 493         struct esd_usb2 *dev = usb_get_intfdata(intf);
 494 
 495         return sprintf(buf, "%d.%d.%d\n",
 496                        (dev->version >> 28) & 0xf,
 497                        (dev->version >> 24) & 0xf,
 498                        (dev->version >> 16) & 0xff);
 499 }
 500 static DEVICE_ATTR(hardware, 0444, show_hardware, NULL);
 501 
 502 static ssize_t show_nets(struct device *d,
 503                          struct device_attribute *attr, char *buf)
 504 {
 505         struct usb_interface *intf = to_usb_interface(d);
 506         struct esd_usb2 *dev = usb_get_intfdata(intf);
 507 
 508         return sprintf(buf, "%d", dev->net_count);
 509 }
 510 static DEVICE_ATTR(nets, 0444, show_nets, NULL);
 511 
 512 static int esd_usb2_send_msg(struct esd_usb2 *dev, struct esd_usb2_msg *msg)
 513 {
 514         int actual_length;
 515 
 516         return usb_bulk_msg(dev->udev,
 517                             usb_sndbulkpipe(dev->udev, 2),
 518                             msg,
 519                             msg->msg.hdr.len << 2,
 520                             &actual_length,
 521                             1000);
 522 }
 523 
 524 static int esd_usb2_wait_msg(struct esd_usb2 *dev,
 525                              struct esd_usb2_msg *msg)
 526 {
 527         int actual_length;
 528 
 529         return usb_bulk_msg(dev->udev,
 530                             usb_rcvbulkpipe(dev->udev, 1),
 531                             msg,
 532                             sizeof(*msg),
 533                             &actual_length,
 534                             1000);
 535 }
 536 
 537 static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev)
 538 {
 539         int i, err = 0;
 540 
 541         if (dev->rxinitdone)
 542                 return 0;
 543 
 544         for (i = 0; i < MAX_RX_URBS; i++) {
 545                 struct urb *urb = NULL;
 546                 u8 *buf = NULL;
 547 
 548                 /* create a URB, and a buffer for it */
 549                 urb = usb_alloc_urb(0, GFP_KERNEL);
 550                 if (!urb) {
 551                         err = -ENOMEM;
 552                         break;
 553                 }
 554 
 555                 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
 556                                          &urb->transfer_dma);
 557                 if (!buf) {
 558                         dev_warn(dev->udev->dev.parent,
 559                                  "No memory left for USB buffer\n");
 560                         err = -ENOMEM;
 561                         goto freeurb;
 562                 }
 563 
 564                 usb_fill_bulk_urb(urb, dev->udev,
 565                                   usb_rcvbulkpipe(dev->udev, 1),
 566                                   buf, RX_BUFFER_SIZE,
 567                                   esd_usb2_read_bulk_callback, dev);
 568                 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
 569                 usb_anchor_urb(urb, &dev->rx_submitted);
 570 
 571                 err = usb_submit_urb(urb, GFP_KERNEL);
 572                 if (err) {
 573                         usb_unanchor_urb(urb);
 574                         usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
 575                                           urb->transfer_dma);
 576                 }
 577 
 578 freeurb:
 579                 /* Drop reference, USB core will take care of freeing it */
 580                 usb_free_urb(urb);
 581                 if (err)
 582                         break;
 583         }
 584 
 585         /* Did we submit any URBs */
 586         if (i == 0) {
 587                 dev_err(dev->udev->dev.parent, "couldn't setup read URBs\n");
 588                 return err;
 589         }
 590 
 591         /* Warn if we've couldn't transmit all the URBs */
 592         if (i < MAX_RX_URBS) {
 593                 dev_warn(dev->udev->dev.parent,
 594                          "rx performance may be slow\n");
 595         }
 596 
 597         dev->rxinitdone = 1;
 598         return 0;
 599 }
 600 
 601 /*
 602  * Start interface
 603  */
 604 static int esd_usb2_start(struct esd_usb2_net_priv *priv)
 605 {
 606         struct esd_usb2 *dev = priv->usb2;
 607         struct net_device *netdev = priv->netdev;
 608         struct esd_usb2_msg *msg;
 609         int err, i;
 610 
 611         msg = kmalloc(sizeof(*msg), GFP_KERNEL);
 612         if (!msg) {
 613                 err = -ENOMEM;
 614                 goto out;
 615         }
 616 
 617         /*
 618          * Enable all IDs
 619          * The IDADD message takes up to 64 32 bit bitmasks (2048 bits).
 620          * Each bit represents one 11 bit CAN identifier. A set bit
 621          * enables reception of the corresponding CAN identifier. A cleared
 622          * bit disabled this identifier. An additional bitmask value
 623          * following the CAN 2.0A bits is used to enable reception of
 624          * extended CAN frames. Only the LSB of this final mask is checked
 625          * for the complete 29 bit ID range. The IDADD message also allows
 626          * filter configuration for an ID subset. In this case you can add
 627          * the number of the starting bitmask (0..64) to the filter.option
 628          * field followed by only some bitmasks.
 629          */
 630         msg->msg.hdr.cmd = CMD_IDADD;
 631         msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
 632         msg->msg.filter.net = priv->index;
 633         msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
 634         for (i = 0; i < ESD_MAX_ID_SEGMENT; i++)
 635                 msg->msg.filter.mask[i] = cpu_to_le32(0xffffffff);
 636         /* enable 29bit extended IDs */
 637         msg->msg.filter.mask[ESD_MAX_ID_SEGMENT] = cpu_to_le32(0x00000001);
 638 
 639         err = esd_usb2_send_msg(dev, msg);
 640         if (err)
 641                 goto out;
 642 
 643         err = esd_usb2_setup_rx_urbs(dev);
 644         if (err)
 645                 goto out;
 646 
 647         priv->can.state = CAN_STATE_ERROR_ACTIVE;
 648 
 649 out:
 650         if (err == -ENODEV)
 651                 netif_device_detach(netdev);
 652         if (err)
 653                 netdev_err(netdev, "couldn't start device: %d\n", err);
 654 
 655         kfree(msg);
 656         return err;
 657 }
 658 
 659 static void unlink_all_urbs(struct esd_usb2 *dev)
 660 {
 661         struct esd_usb2_net_priv *priv;
 662         int i, j;
 663 
 664         usb_kill_anchored_urbs(&dev->rx_submitted);
 665         for (i = 0; i < dev->net_count; i++) {
 666                 priv = dev->nets[i];
 667                 if (priv) {
 668                         usb_kill_anchored_urbs(&priv->tx_submitted);
 669                         atomic_set(&priv->active_tx_jobs, 0);
 670 
 671                         for (j = 0; j < MAX_TX_URBS; j++)
 672                                 priv->tx_contexts[j].echo_index = MAX_TX_URBS;
 673                 }
 674         }
 675 }
 676 
 677 static int esd_usb2_open(struct net_device *netdev)
 678 {
 679         struct esd_usb2_net_priv *priv = netdev_priv(netdev);
 680         int err;
 681 
 682         /* common open */
 683         err = open_candev(netdev);
 684         if (err)
 685                 return err;
 686 
 687         /* finally start device */
 688         err = esd_usb2_start(priv);
 689         if (err) {
 690                 netdev_warn(netdev, "couldn't start device: %d\n", err);
 691                 close_candev(netdev);
 692                 return err;
 693         }
 694 
 695         netif_start_queue(netdev);
 696 
 697         return 0;
 698 }
 699 
 700 static netdev_tx_t esd_usb2_start_xmit(struct sk_buff *skb,
 701                                       struct net_device *netdev)
 702 {
 703         struct esd_usb2_net_priv *priv = netdev_priv(netdev);
 704         struct esd_usb2 *dev = priv->usb2;
 705         struct esd_tx_urb_context *context = NULL;
 706         struct net_device_stats *stats = &netdev->stats;
 707         struct can_frame *cf = (struct can_frame *)skb->data;
 708         struct esd_usb2_msg *msg;
 709         struct urb *urb;
 710         u8 *buf;
 711         int i, err;
 712         int ret = NETDEV_TX_OK;
 713         size_t size = sizeof(struct esd_usb2_msg);
 714 
 715         if (can_dropped_invalid_skb(netdev, skb))
 716                 return NETDEV_TX_OK;
 717 
 718         /* create a URB, and a buffer for it, and copy the data to the URB */
 719         urb = usb_alloc_urb(0, GFP_ATOMIC);
 720         if (!urb) {
 721                 stats->tx_dropped++;
 722                 dev_kfree_skb(skb);
 723                 goto nourbmem;
 724         }
 725 
 726         buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC,
 727                                  &urb->transfer_dma);
 728         if (!buf) {
 729                 netdev_err(netdev, "No memory left for USB buffer\n");
 730                 stats->tx_dropped++;
 731                 dev_kfree_skb(skb);
 732                 goto nobufmem;
 733         }
 734 
 735         msg = (struct esd_usb2_msg *)buf;
 736 
 737         msg->msg.hdr.len = 3; /* minimal length */
 738         msg->msg.hdr.cmd = CMD_CAN_TX;
 739         msg->msg.tx.net = priv->index;
 740         msg->msg.tx.dlc = cf->can_dlc;
 741         msg->msg.tx.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
 742 
 743         if (cf->can_id & CAN_RTR_FLAG)
 744                 msg->msg.tx.dlc |= ESD_RTR;
 745 
 746         if (cf->can_id & CAN_EFF_FLAG)
 747                 msg->msg.tx.id |= cpu_to_le32(ESD_EXTID);
 748 
 749         for (i = 0; i < cf->can_dlc; i++)
 750                 msg->msg.tx.data[i] = cf->data[i];
 751 
 752         msg->msg.hdr.len += (cf->can_dlc + 3) >> 2;
 753 
 754         for (i = 0; i < MAX_TX_URBS; i++) {
 755                 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
 756                         context = &priv->tx_contexts[i];
 757                         break;
 758                 }
 759         }
 760 
 761         /*
 762          * This may never happen.
 763          */
 764         if (!context) {
 765                 netdev_warn(netdev, "couldn't find free context\n");
 766                 ret = NETDEV_TX_BUSY;
 767                 goto releasebuf;
 768         }
 769 
 770         context->priv = priv;
 771         context->echo_index = i;
 772         context->dlc = cf->can_dlc;
 773 
 774         /* hnd must not be 0 - MSB is stripped in txdone handling */
 775         msg->msg.tx.hnd = 0x80000000 | i; /* returned in TX done message */
 776 
 777         usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
 778                           msg->msg.hdr.len << 2,
 779                           esd_usb2_write_bulk_callback, context);
 780 
 781         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
 782 
 783         usb_anchor_urb(urb, &priv->tx_submitted);
 784 
 785         can_put_echo_skb(skb, netdev, context->echo_index);
 786 
 787         atomic_inc(&priv->active_tx_jobs);
 788 
 789         /* Slow down tx path */
 790         if (atomic_read(&priv->active_tx_jobs) >= MAX_TX_URBS)
 791                 netif_stop_queue(netdev);
 792 
 793         err = usb_submit_urb(urb, GFP_ATOMIC);
 794         if (err) {
 795                 can_free_echo_skb(netdev, context->echo_index);
 796 
 797                 atomic_dec(&priv->active_tx_jobs);
 798                 usb_unanchor_urb(urb);
 799 
 800                 stats->tx_dropped++;
 801 
 802                 if (err == -ENODEV)
 803                         netif_device_detach(netdev);
 804                 else
 805                         netdev_warn(netdev, "failed tx_urb %d\n", err);
 806 
 807                 goto releasebuf;
 808         }
 809 
 810         netif_trans_update(netdev);
 811 
 812         /*
 813          * Release our reference to this URB, the USB core will eventually free
 814          * it entirely.
 815          */
 816         usb_free_urb(urb);
 817 
 818         return NETDEV_TX_OK;
 819 
 820 releasebuf:
 821         usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
 822 
 823 nobufmem:
 824         usb_free_urb(urb);
 825 
 826 nourbmem:
 827         return ret;
 828 }
 829 
 830 static int esd_usb2_close(struct net_device *netdev)
 831 {
 832         struct esd_usb2_net_priv *priv = netdev_priv(netdev);
 833         struct esd_usb2_msg *msg;
 834         int i;
 835 
 836         msg = kmalloc(sizeof(*msg), GFP_KERNEL);
 837         if (!msg)
 838                 return -ENOMEM;
 839 
 840         /* Disable all IDs (see esd_usb2_start()) */
 841         msg->msg.hdr.cmd = CMD_IDADD;
 842         msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
 843         msg->msg.filter.net = priv->index;
 844         msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
 845         for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++)
 846                 msg->msg.filter.mask[i] = 0;
 847         if (esd_usb2_send_msg(priv->usb2, msg) < 0)
 848                 netdev_err(netdev, "sending idadd message failed\n");
 849 
 850         /* set CAN controller to reset mode */
 851         msg->msg.hdr.len = 2;
 852         msg->msg.hdr.cmd = CMD_SETBAUD;
 853         msg->msg.setbaud.net = priv->index;
 854         msg->msg.setbaud.rsvd = 0;
 855         msg->msg.setbaud.baud = cpu_to_le32(ESD_USB2_NO_BAUDRATE);
 856         if (esd_usb2_send_msg(priv->usb2, msg) < 0)
 857                 netdev_err(netdev, "sending setbaud message failed\n");
 858 
 859         priv->can.state = CAN_STATE_STOPPED;
 860 
 861         netif_stop_queue(netdev);
 862 
 863         close_candev(netdev);
 864 
 865         kfree(msg);
 866 
 867         return 0;
 868 }
 869 
 870 static const struct net_device_ops esd_usb2_netdev_ops = {
 871         .ndo_open = esd_usb2_open,
 872         .ndo_stop = esd_usb2_close,
 873         .ndo_start_xmit = esd_usb2_start_xmit,
 874         .ndo_change_mtu = can_change_mtu,
 875 };
 876 
 877 static const struct can_bittiming_const esd_usb2_bittiming_const = {
 878         .name = "esd_usb2",
 879         .tseg1_min = ESD_USB2_TSEG1_MIN,
 880         .tseg1_max = ESD_USB2_TSEG1_MAX,
 881         .tseg2_min = ESD_USB2_TSEG2_MIN,
 882         .tseg2_max = ESD_USB2_TSEG2_MAX,
 883         .sjw_max = ESD_USB2_SJW_MAX,
 884         .brp_min = ESD_USB2_BRP_MIN,
 885         .brp_max = ESD_USB2_BRP_MAX,
 886         .brp_inc = ESD_USB2_BRP_INC,
 887 };
 888 
 889 static int esd_usb2_set_bittiming(struct net_device *netdev)
 890 {
 891         struct esd_usb2_net_priv *priv = netdev_priv(netdev);
 892         struct can_bittiming *bt = &priv->can.bittiming;
 893         struct esd_usb2_msg *msg;
 894         int err;
 895         u32 canbtr;
 896         int sjw_shift;
 897 
 898         canbtr = ESD_USB2_UBR;
 899         if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
 900                 canbtr |= ESD_USB2_LOM;
 901 
 902         canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1);
 903 
 904         if (le16_to_cpu(priv->usb2->udev->descriptor.idProduct) ==
 905             USB_CANUSBM_PRODUCT_ID)
 906                 sjw_shift = ESD_USBM_SJW_SHIFT;
 907         else
 908                 sjw_shift = ESD_USB2_SJW_SHIFT;
 909 
 910         canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1))
 911                 << sjw_shift;
 912         canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1)
 913                    & (ESD_USB2_TSEG1_MAX - 1))
 914                 << ESD_USB2_TSEG1_SHIFT;
 915         canbtr |= ((bt->phase_seg2 - 1) & (ESD_USB2_TSEG2_MAX - 1))
 916                 << ESD_USB2_TSEG2_SHIFT;
 917         if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
 918                 canbtr |= ESD_USB2_3_SAMPLES;
 919 
 920         msg = kmalloc(sizeof(*msg), GFP_KERNEL);
 921         if (!msg)
 922                 return -ENOMEM;
 923 
 924         msg->msg.hdr.len = 2;
 925         msg->msg.hdr.cmd = CMD_SETBAUD;
 926         msg->msg.setbaud.net = priv->index;
 927         msg->msg.setbaud.rsvd = 0;
 928         msg->msg.setbaud.baud = cpu_to_le32(canbtr);
 929 
 930         netdev_info(netdev, "setting BTR=%#x\n", canbtr);
 931 
 932         err = esd_usb2_send_msg(priv->usb2, msg);
 933 
 934         kfree(msg);
 935         return err;
 936 }
 937 
 938 static int esd_usb2_get_berr_counter(const struct net_device *netdev,
 939                                      struct can_berr_counter *bec)
 940 {
 941         struct esd_usb2_net_priv *priv = netdev_priv(netdev);
 942 
 943         bec->txerr = priv->bec.txerr;
 944         bec->rxerr = priv->bec.rxerr;
 945 
 946         return 0;
 947 }
 948 
 949 static int esd_usb2_set_mode(struct net_device *netdev, enum can_mode mode)
 950 {
 951         switch (mode) {
 952         case CAN_MODE_START:
 953                 netif_wake_queue(netdev);
 954                 break;
 955 
 956         default:
 957                 return -EOPNOTSUPP;
 958         }
 959 
 960         return 0;
 961 }
 962 
 963 static int esd_usb2_probe_one_net(struct usb_interface *intf, int index)
 964 {
 965         struct esd_usb2 *dev = usb_get_intfdata(intf);
 966         struct net_device *netdev;
 967         struct esd_usb2_net_priv *priv;
 968         int err = 0;
 969         int i;
 970 
 971         netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
 972         if (!netdev) {
 973                 dev_err(&intf->dev, "couldn't alloc candev\n");
 974                 err = -ENOMEM;
 975                 goto done;
 976         }
 977 
 978         priv = netdev_priv(netdev);
 979 
 980         init_usb_anchor(&priv->tx_submitted);
 981         atomic_set(&priv->active_tx_jobs, 0);
 982 
 983         for (i = 0; i < MAX_TX_URBS; i++)
 984                 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
 985 
 986         priv->usb2 = dev;
 987         priv->netdev = netdev;
 988         priv->index = index;
 989 
 990         priv->can.state = CAN_STATE_STOPPED;
 991         priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY;
 992 
 993         if (le16_to_cpu(dev->udev->descriptor.idProduct) ==
 994             USB_CANUSBM_PRODUCT_ID)
 995                 priv->can.clock.freq = ESD_USBM_CAN_CLOCK;
 996         else {
 997                 priv->can.clock.freq = ESD_USB2_CAN_CLOCK;
 998                 priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
 999         }
1000 
1001         priv->can.bittiming_const = &esd_usb2_bittiming_const;
1002         priv->can.do_set_bittiming = esd_usb2_set_bittiming;
1003         priv->can.do_set_mode = esd_usb2_set_mode;
1004         priv->can.do_get_berr_counter = esd_usb2_get_berr_counter;
1005 
1006         netdev->flags |= IFF_ECHO; /* we support local echo */
1007 
1008         netdev->netdev_ops = &esd_usb2_netdev_ops;
1009 
1010         SET_NETDEV_DEV(netdev, &intf->dev);
1011         netdev->dev_id = index;
1012 
1013         err = register_candev(netdev);
1014         if (err) {
1015                 dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
1016                 free_candev(netdev);
1017                 err = -ENOMEM;
1018                 goto done;
1019         }
1020 
1021         dev->nets[index] = priv;
1022         netdev_info(netdev, "device %s registered\n", netdev->name);
1023 
1024 done:
1025         return err;
1026 }
1027 
1028 /*
1029  * probe function for new USB2 devices
1030  *
1031  * check version information and number of available
1032  * CAN interfaces
1033  */
1034 static int esd_usb2_probe(struct usb_interface *intf,
1035                          const struct usb_device_id *id)
1036 {
1037         struct esd_usb2 *dev;
1038         struct esd_usb2_msg *msg;
1039         int i, err;
1040 
1041         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1042         if (!dev) {
1043                 err = -ENOMEM;
1044                 goto done;
1045         }
1046 
1047         dev->udev = interface_to_usbdev(intf);
1048 
1049         init_usb_anchor(&dev->rx_submitted);
1050 
1051         usb_set_intfdata(intf, dev);
1052 
1053         msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1054         if (!msg) {
1055                 err = -ENOMEM;
1056                 goto free_msg;
1057         }
1058 
1059         /* query number of CAN interfaces (nets) */
1060         msg->msg.hdr.cmd = CMD_VERSION;
1061         msg->msg.hdr.len = 2;
1062         msg->msg.version.rsvd = 0;
1063         msg->msg.version.flags = 0;
1064         msg->msg.version.drv_version = 0;
1065 
1066         err = esd_usb2_send_msg(dev, msg);
1067         if (err < 0) {
1068                 dev_err(&intf->dev, "sending version message failed\n");
1069                 goto free_msg;
1070         }
1071 
1072         err = esd_usb2_wait_msg(dev, msg);
1073         if (err < 0) {
1074                 dev_err(&intf->dev, "no version message answer\n");
1075                 goto free_msg;
1076         }
1077 
1078         dev->net_count = (int)msg->msg.version_reply.nets;
1079         dev->version = le32_to_cpu(msg->msg.version_reply.version);
1080 
1081         if (device_create_file(&intf->dev, &dev_attr_firmware))
1082                 dev_err(&intf->dev,
1083                         "Couldn't create device file for firmware\n");
1084 
1085         if (device_create_file(&intf->dev, &dev_attr_hardware))
1086                 dev_err(&intf->dev,
1087                         "Couldn't create device file for hardware\n");
1088 
1089         if (device_create_file(&intf->dev, &dev_attr_nets))
1090                 dev_err(&intf->dev,
1091                         "Couldn't create device file for nets\n");
1092 
1093         /* do per device probing */
1094         for (i = 0; i < dev->net_count; i++)
1095                 esd_usb2_probe_one_net(intf, i);
1096 
1097 free_msg:
1098         kfree(msg);
1099         if (err)
1100                 kfree(dev);
1101 done:
1102         return err;
1103 }
1104 
1105 /*
1106  * called by the usb core when the device is removed from the system
1107  */
1108 static void esd_usb2_disconnect(struct usb_interface *intf)
1109 {
1110         struct esd_usb2 *dev = usb_get_intfdata(intf);
1111         struct net_device *netdev;
1112         int i;
1113 
1114         device_remove_file(&intf->dev, &dev_attr_firmware);
1115         device_remove_file(&intf->dev, &dev_attr_hardware);
1116         device_remove_file(&intf->dev, &dev_attr_nets);
1117 
1118         usb_set_intfdata(intf, NULL);
1119 
1120         if (dev) {
1121                 for (i = 0; i < dev->net_count; i++) {
1122                         if (dev->nets[i]) {
1123                                 netdev = dev->nets[i]->netdev;
1124                                 unregister_netdev(netdev);
1125                                 free_candev(netdev);
1126                         }
1127                 }
1128                 unlink_all_urbs(dev);
1129                 kfree(dev);
1130         }
1131 }
1132 
1133 /* usb specific object needed to register this driver with the usb subsystem */
1134 static struct usb_driver esd_usb2_driver = {
1135         .name = "esd_usb2",
1136         .probe = esd_usb2_probe,
1137         .disconnect = esd_usb2_disconnect,
1138         .id_table = esd_usb2_table,
1139 };
1140 
1141 module_usb_driver(esd_usb2_driver);

/* [<][>][^][v][top][bottom][index][help] */