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