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