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