1 // SPDX-License-Identifier: GPL-2.0-only 2 /* CAN driver for Geschwister Schneider USB/CAN devices 3 * and bytewerk.org candleLight USB CAN interfaces. 4 * 5 * Copyright (C) 2013-2016 Geschwister Schneider Technologie-, 6 * Entwicklungs- und Vertriebs UG (Haftungsbeschränkt). 7 * Copyright (C) 2016 Hubert Denkmair 8 * 9 * Many thanks to all socketcan devs! 10 */ 11 12 #include <linux/init.h> 13 #include <linux/signal.h> 14 #include <linux/module.h> 15 #include <linux/netdevice.h> 16 #include <linux/usb.h> 17 18 #include <linux/can.h> 19 #include <linux/can/dev.h> 20 #include <linux/can/error.h> 21 22 /* Device specific constants */ 23 #define USB_GSUSB_1_VENDOR_ID 0x1d50 24 #define USB_GSUSB_1_PRODUCT_ID 0x606f 25 26 #define USB_CANDLELIGHT_VENDOR_ID 0x1209 27 #define USB_CANDLELIGHT_PRODUCT_ID 0x2323 28 29 #define GSUSB_ENDPOINT_IN 1 30 #define GSUSB_ENDPOINT_OUT 2 31 32 /* Device specific constants */ 33 enum gs_usb_breq { 34 GS_USB_BREQ_HOST_FORMAT = 0, 35 GS_USB_BREQ_BITTIMING, 36 GS_USB_BREQ_MODE, 37 GS_USB_BREQ_BERR, 38 GS_USB_BREQ_BT_CONST, 39 GS_USB_BREQ_DEVICE_CONFIG, 40 GS_USB_BREQ_TIMESTAMP, 41 GS_USB_BREQ_IDENTIFY, 42 }; 43 44 enum gs_can_mode { 45 /* reset a channel. turns it off */ 46 GS_CAN_MODE_RESET = 0, 47 /* starts a channel */ 48 GS_CAN_MODE_START 49 }; 50 51 enum gs_can_state { 52 GS_CAN_STATE_ERROR_ACTIVE = 0, 53 GS_CAN_STATE_ERROR_WARNING, 54 GS_CAN_STATE_ERROR_PASSIVE, 55 GS_CAN_STATE_BUS_OFF, 56 GS_CAN_STATE_STOPPED, 57 GS_CAN_STATE_SLEEPING 58 }; 59 60 enum gs_can_identify_mode { 61 GS_CAN_IDENTIFY_OFF = 0, 62 GS_CAN_IDENTIFY_ON 63 }; 64 65 /* data types passed between host and device */ 66 67 /* The firmware on the original USB2CAN by Geschwister Schneider 68 * Technologie Entwicklungs- und Vertriebs UG exchanges all data 69 * between the host and the device in host byte order. This is done 70 * with the struct gs_host_config::byte_order member, which is sent 71 * first to indicate the desired byte order. 72 * 73 * The widely used open source firmware candleLight doesn't support 74 * this feature and exchanges the data in little endian byte order. 75 */ 76 struct gs_host_config { 77 __le32 byte_order; 78 } __packed; 79 80 struct gs_device_config { 81 u8 reserved1; 82 u8 reserved2; 83 u8 reserved3; 84 u8 icount; 85 __le32 sw_version; 86 __le32 hw_version; 87 } __packed; 88 89 #define GS_CAN_MODE_NORMAL 0 90 #define GS_CAN_MODE_LISTEN_ONLY BIT(0) 91 #define GS_CAN_MODE_LOOP_BACK BIT(1) 92 #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2) 93 #define GS_CAN_MODE_ONE_SHOT BIT(3) 94 95 struct gs_device_mode { 96 __le32 mode; 97 __le32 flags; 98 } __packed; 99 100 struct gs_device_state { 101 __le32 state; 102 __le32 rxerr; 103 __le32 txerr; 104 } __packed; 105 106 struct gs_device_bittiming { 107 __le32 prop_seg; 108 __le32 phase_seg1; 109 __le32 phase_seg2; 110 __le32 sjw; 111 __le32 brp; 112 } __packed; 113 114 struct gs_identify_mode { 115 __le32 mode; 116 } __packed; 117 118 #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0) 119 #define GS_CAN_FEATURE_LOOP_BACK BIT(1) 120 #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2) 121 #define GS_CAN_FEATURE_ONE_SHOT BIT(3) 122 #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4) 123 #define GS_CAN_FEATURE_IDENTIFY BIT(5) 124 125 struct gs_device_bt_const { 126 __le32 feature; 127 __le32 fclk_can; 128 __le32 tseg1_min; 129 __le32 tseg1_max; 130 __le32 tseg2_min; 131 __le32 tseg2_max; 132 __le32 sjw_max; 133 __le32 brp_min; 134 __le32 brp_max; 135 __le32 brp_inc; 136 } __packed; 137 138 #define GS_CAN_FLAG_OVERFLOW 1 139 140 struct gs_host_frame { 141 u32 echo_id; 142 __le32 can_id; 143 144 u8 can_dlc; 145 u8 channel; 146 u8 flags; 147 u8 reserved; 148 149 u8 data[8]; 150 } __packed; 151 /* The GS USB devices make use of the same flags and masks as in 152 * linux/can.h and linux/can/error.h, and no additional mapping is necessary. 153 */ 154 155 /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */ 156 #define GS_MAX_TX_URBS 10 157 /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */ 158 #define GS_MAX_RX_URBS 30 159 /* Maximum number of interfaces the driver supports per device. 160 * Current hardware only supports 2 interfaces. The future may vary. 161 */ 162 #define GS_MAX_INTF 2 163 164 struct gs_tx_context { 165 struct gs_can *dev; 166 unsigned int echo_id; 167 }; 168 169 struct gs_can { 170 struct can_priv can; /* must be the first member */ 171 172 struct gs_usb *parent; 173 174 struct net_device *netdev; 175 struct usb_device *udev; 176 struct usb_interface *iface; 177 178 struct can_bittiming_const bt_const; 179 unsigned int channel; /* channel number */ 180 181 /* This lock prevents a race condition between xmit and receive. */ 182 spinlock_t tx_ctx_lock; 183 struct gs_tx_context tx_context[GS_MAX_TX_URBS]; 184 185 struct usb_anchor tx_submitted; 186 atomic_t active_tx_urbs; 187 }; 188 189 /* usb interface struct */ 190 struct gs_usb { 191 struct gs_can *canch[GS_MAX_INTF]; 192 struct usb_anchor rx_submitted; 193 atomic_t active_channels; 194 struct usb_device *udev; 195 }; 196 197 /* 'allocate' a tx context. 198 * returns a valid tx context or NULL if there is no space. 199 */ 200 static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev) 201 { 202 int i = 0; 203 unsigned long flags; 204 205 spin_lock_irqsave(&dev->tx_ctx_lock, flags); 206 207 for (; i < GS_MAX_TX_URBS; i++) { 208 if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) { 209 dev->tx_context[i].echo_id = i; 210 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); 211 return &dev->tx_context[i]; 212 } 213 } 214 215 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); 216 return NULL; 217 } 218 219 /* releases a tx context 220 */ 221 static void gs_free_tx_context(struct gs_tx_context *txc) 222 { 223 txc->echo_id = GS_MAX_TX_URBS; 224 } 225 226 /* Get a tx context by id. 227 */ 228 static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev, 229 unsigned int id) 230 { 231 unsigned long flags; 232 233 if (id < GS_MAX_TX_URBS) { 234 spin_lock_irqsave(&dev->tx_ctx_lock, flags); 235 if (dev->tx_context[id].echo_id == id) { 236 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); 237 return &dev->tx_context[id]; 238 } 239 spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); 240 } 241 return NULL; 242 } 243 244 static int gs_cmd_reset(struct gs_can *gsdev) 245 { 246 struct gs_device_mode *dm; 247 struct usb_interface *intf = gsdev->iface; 248 int rc; 249 250 dm = kzalloc(sizeof(*dm), GFP_KERNEL); 251 if (!dm) 252 return -ENOMEM; 253 254 dm->mode = GS_CAN_MODE_RESET; 255 256 rc = usb_control_msg(interface_to_usbdev(intf), 257 usb_sndctrlpipe(interface_to_usbdev(intf), 0), 258 GS_USB_BREQ_MODE, 259 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 260 gsdev->channel, 261 0, 262 dm, 263 sizeof(*dm), 264 1000); 265 266 kfree(dm); 267 268 return rc; 269 } 270 271 static void gs_update_state(struct gs_can *dev, struct can_frame *cf) 272 { 273 struct can_device_stats *can_stats = &dev->can.can_stats; 274 275 if (cf->can_id & CAN_ERR_RESTARTED) { 276 dev->can.state = CAN_STATE_ERROR_ACTIVE; 277 can_stats->restarts++; 278 } else if (cf->can_id & CAN_ERR_BUSOFF) { 279 dev->can.state = CAN_STATE_BUS_OFF; 280 can_stats->bus_off++; 281 } else if (cf->can_id & CAN_ERR_CRTL) { 282 if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) || 283 (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) { 284 dev->can.state = CAN_STATE_ERROR_WARNING; 285 can_stats->error_warning++; 286 } else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) || 287 (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) { 288 dev->can.state = CAN_STATE_ERROR_PASSIVE; 289 can_stats->error_passive++; 290 } else { 291 dev->can.state = CAN_STATE_ERROR_ACTIVE; 292 } 293 } 294 } 295 296 static void gs_usb_receive_bulk_callback(struct urb *urb) 297 { 298 struct gs_usb *usbcan = urb->context; 299 struct gs_can *dev; 300 struct net_device *netdev; 301 int rc; 302 struct net_device_stats *stats; 303 struct gs_host_frame *hf = urb->transfer_buffer; 304 struct gs_tx_context *txc; 305 struct can_frame *cf; 306 struct sk_buff *skb; 307 308 BUG_ON(!usbcan); 309 310 switch (urb->status) { 311 case 0: /* success */ 312 break; 313 case -ENOENT: 314 case -ESHUTDOWN: 315 return; 316 default: 317 /* do not resubmit aborted urbs. eg: when device goes down */ 318 return; 319 } 320 321 /* device reports out of range channel id */ 322 if (hf->channel >= GS_MAX_INTF) 323 goto resubmit_urb; 324 325 dev = usbcan->canch[hf->channel]; 326 327 netdev = dev->netdev; 328 stats = &netdev->stats; 329 330 if (!netif_device_present(netdev)) 331 return; 332 333 if (hf->echo_id == -1) { /* normal rx */ 334 skb = alloc_can_skb(dev->netdev, &cf); 335 if (!skb) 336 return; 337 338 cf->can_id = le32_to_cpu(hf->can_id); 339 340 cf->can_dlc = get_can_dlc(hf->can_dlc); 341 memcpy(cf->data, hf->data, 8); 342 343 /* ERROR frames tell us information about the controller */ 344 if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG) 345 gs_update_state(dev, cf); 346 347 netdev->stats.rx_packets++; 348 netdev->stats.rx_bytes += hf->can_dlc; 349 350 netif_rx(skb); 351 } else { /* echo_id == hf->echo_id */ 352 if (hf->echo_id >= GS_MAX_TX_URBS) { 353 netdev_err(netdev, 354 "Unexpected out of range echo id %d\n", 355 hf->echo_id); 356 goto resubmit_urb; 357 } 358 359 netdev->stats.tx_packets++; 360 netdev->stats.tx_bytes += hf->can_dlc; 361 362 txc = gs_get_tx_context(dev, hf->echo_id); 363 364 /* bad devices send bad echo_ids. */ 365 if (!txc) { 366 netdev_err(netdev, 367 "Unexpected unused echo id %d\n", 368 hf->echo_id); 369 goto resubmit_urb; 370 } 371 372 can_get_echo_skb(netdev, hf->echo_id); 373 374 gs_free_tx_context(txc); 375 376 atomic_dec(&dev->active_tx_urbs); 377 378 netif_wake_queue(netdev); 379 } 380 381 if (hf->flags & GS_CAN_FLAG_OVERFLOW) { 382 skb = alloc_can_err_skb(netdev, &cf); 383 if (!skb) 384 goto resubmit_urb; 385 386 cf->can_id |= CAN_ERR_CRTL; 387 cf->can_dlc = CAN_ERR_DLC; 388 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; 389 stats->rx_over_errors++; 390 stats->rx_errors++; 391 netif_rx(skb); 392 } 393 394 resubmit_urb: 395 usb_fill_bulk_urb(urb, 396 usbcan->udev, 397 usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN), 398 hf, 399 sizeof(struct gs_host_frame), 400 gs_usb_receive_bulk_callback, 401 usbcan 402 ); 403 404 rc = usb_submit_urb(urb, GFP_ATOMIC); 405 406 /* USB failure take down all interfaces */ 407 if (rc == -ENODEV) { 408 for (rc = 0; rc < GS_MAX_INTF; rc++) { 409 if (usbcan->canch[rc]) 410 netif_device_detach(usbcan->canch[rc]->netdev); 411 } 412 } 413 } 414 415 static int gs_usb_set_bittiming(struct net_device *netdev) 416 { 417 struct gs_can *dev = netdev_priv(netdev); 418 struct can_bittiming *bt = &dev->can.bittiming; 419 struct usb_interface *intf = dev->iface; 420 int rc; 421 struct gs_device_bittiming *dbt; 422 423 dbt = kmalloc(sizeof(*dbt), GFP_KERNEL); 424 if (!dbt) 425 return -ENOMEM; 426 427 dbt->prop_seg = cpu_to_le32(bt->prop_seg); 428 dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1); 429 dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2); 430 dbt->sjw = cpu_to_le32(bt->sjw); 431 dbt->brp = cpu_to_le32(bt->brp); 432 433 /* request bit timings */ 434 rc = usb_control_msg(interface_to_usbdev(intf), 435 usb_sndctrlpipe(interface_to_usbdev(intf), 0), 436 GS_USB_BREQ_BITTIMING, 437 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 438 dev->channel, 439 0, 440 dbt, 441 sizeof(*dbt), 442 1000); 443 444 kfree(dbt); 445 446 if (rc < 0) 447 dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)", 448 rc); 449 450 return (rc > 0) ? 0 : rc; 451 } 452 453 static void gs_usb_xmit_callback(struct urb *urb) 454 { 455 struct gs_tx_context *txc = urb->context; 456 struct gs_can *dev = txc->dev; 457 struct net_device *netdev = dev->netdev; 458 459 if (urb->status) 460 netdev_info(netdev, "usb xmit fail %d\n", txc->echo_id); 461 462 usb_free_coherent(urb->dev, 463 urb->transfer_buffer_length, 464 urb->transfer_buffer, 465 urb->transfer_dma); 466 } 467 468 static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb, 469 struct net_device *netdev) 470 { 471 struct gs_can *dev = netdev_priv(netdev); 472 struct net_device_stats *stats = &dev->netdev->stats; 473 struct urb *urb; 474 struct gs_host_frame *hf; 475 struct can_frame *cf; 476 int rc; 477 unsigned int idx; 478 struct gs_tx_context *txc; 479 480 if (can_dropped_invalid_skb(netdev, skb)) 481 return NETDEV_TX_OK; 482 483 /* find an empty context to keep track of transmission */ 484 txc = gs_alloc_tx_context(dev); 485 if (!txc) 486 return NETDEV_TX_BUSY; 487 488 /* create a URB, and a buffer for it */ 489 urb = usb_alloc_urb(0, GFP_ATOMIC); 490 if (!urb) 491 goto nomem_urb; 492 493 hf = usb_alloc_coherent(dev->udev, sizeof(*hf), GFP_ATOMIC, 494 &urb->transfer_dma); 495 if (!hf) { 496 netdev_err(netdev, "No memory left for USB buffer\n"); 497 goto nomem_hf; 498 } 499 500 idx = txc->echo_id; 501 502 if (idx >= GS_MAX_TX_URBS) { 503 netdev_err(netdev, "Invalid tx context %d\n", idx); 504 goto badidx; 505 } 506 507 hf->echo_id = idx; 508 hf->channel = dev->channel; 509 510 cf = (struct can_frame *)skb->data; 511 512 hf->can_id = cpu_to_le32(cf->can_id); 513 hf->can_dlc = cf->can_dlc; 514 memcpy(hf->data, cf->data, cf->can_dlc); 515 516 usb_fill_bulk_urb(urb, dev->udev, 517 usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT), 518 hf, 519 sizeof(*hf), 520 gs_usb_xmit_callback, 521 txc); 522 523 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 524 usb_anchor_urb(urb, &dev->tx_submitted); 525 526 can_put_echo_skb(skb, netdev, idx); 527 528 atomic_inc(&dev->active_tx_urbs); 529 530 rc = usb_submit_urb(urb, GFP_ATOMIC); 531 if (unlikely(rc)) { /* usb send failed */ 532 atomic_dec(&dev->active_tx_urbs); 533 534 can_free_echo_skb(netdev, idx); 535 gs_free_tx_context(txc); 536 537 usb_unanchor_urb(urb); 538 usb_free_coherent(dev->udev, 539 sizeof(*hf), 540 hf, 541 urb->transfer_dma); 542 543 if (rc == -ENODEV) { 544 netif_device_detach(netdev); 545 } else { 546 netdev_err(netdev, "usb_submit failed (err=%d)\n", rc); 547 stats->tx_dropped++; 548 } 549 } else { 550 /* Slow down tx path */ 551 if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS) 552 netif_stop_queue(netdev); 553 } 554 555 /* let usb core take care of this urb */ 556 usb_free_urb(urb); 557 558 return NETDEV_TX_OK; 559 560 badidx: 561 usb_free_coherent(dev->udev, 562 sizeof(*hf), 563 hf, 564 urb->transfer_dma); 565 nomem_hf: 566 usb_free_urb(urb); 567 568 nomem_urb: 569 gs_free_tx_context(txc); 570 dev_kfree_skb(skb); 571 stats->tx_dropped++; 572 return NETDEV_TX_OK; 573 } 574 575 static int gs_can_open(struct net_device *netdev) 576 { 577 struct gs_can *dev = netdev_priv(netdev); 578 struct gs_usb *parent = dev->parent; 579 int rc, i; 580 struct gs_device_mode *dm; 581 u32 ctrlmode; 582 u32 flags = 0; 583 584 rc = open_candev(netdev); 585 if (rc) 586 return rc; 587 588 if (atomic_add_return(1, &parent->active_channels) == 1) { 589 for (i = 0; i < GS_MAX_RX_URBS; i++) { 590 struct urb *urb; 591 u8 *buf; 592 593 /* alloc rx urb */ 594 urb = usb_alloc_urb(0, GFP_KERNEL); 595 if (!urb) 596 return -ENOMEM; 597 598 /* alloc rx buffer */ 599 buf = usb_alloc_coherent(dev->udev, 600 sizeof(struct gs_host_frame), 601 GFP_KERNEL, 602 &urb->transfer_dma); 603 if (!buf) { 604 netdev_err(netdev, 605 "No memory left for USB buffer\n"); 606 usb_free_urb(urb); 607 return -ENOMEM; 608 } 609 610 /* fill, anchor, and submit rx urb */ 611 usb_fill_bulk_urb(urb, 612 dev->udev, 613 usb_rcvbulkpipe(dev->udev, 614 GSUSB_ENDPOINT_IN), 615 buf, 616 sizeof(struct gs_host_frame), 617 gs_usb_receive_bulk_callback, 618 parent); 619 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 620 621 usb_anchor_urb(urb, &parent->rx_submitted); 622 623 rc = usb_submit_urb(urb, GFP_KERNEL); 624 if (rc) { 625 if (rc == -ENODEV) 626 netif_device_detach(dev->netdev); 627 628 netdev_err(netdev, 629 "usb_submit failed (err=%d)\n", 630 rc); 631 632 usb_unanchor_urb(urb); 633 usb_free_urb(urb); 634 break; 635 } 636 637 /* Drop reference, 638 * USB core will take care of freeing it 639 */ 640 usb_free_urb(urb); 641 } 642 } 643 644 dm = kmalloc(sizeof(*dm), GFP_KERNEL); 645 if (!dm) 646 return -ENOMEM; 647 648 /* flags */ 649 ctrlmode = dev->can.ctrlmode; 650 651 if (ctrlmode & CAN_CTRLMODE_LOOPBACK) 652 flags |= GS_CAN_MODE_LOOP_BACK; 653 else if (ctrlmode & CAN_CTRLMODE_LISTENONLY) 654 flags |= GS_CAN_MODE_LISTEN_ONLY; 655 656 /* Controller is not allowed to retry TX 657 * this mode is unavailable on atmels uc3c hardware 658 */ 659 if (ctrlmode & CAN_CTRLMODE_ONE_SHOT) 660 flags |= GS_CAN_MODE_ONE_SHOT; 661 662 if (ctrlmode & CAN_CTRLMODE_3_SAMPLES) 663 flags |= GS_CAN_MODE_TRIPLE_SAMPLE; 664 665 /* finally start device */ 666 dm->mode = cpu_to_le32(GS_CAN_MODE_START); 667 dm->flags = cpu_to_le32(flags); 668 rc = usb_control_msg(interface_to_usbdev(dev->iface), 669 usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0), 670 GS_USB_BREQ_MODE, 671 USB_DIR_OUT | USB_TYPE_VENDOR | 672 USB_RECIP_INTERFACE, 673 dev->channel, 674 0, 675 dm, 676 sizeof(*dm), 677 1000); 678 679 if (rc < 0) { 680 netdev_err(netdev, "Couldn't start device (err=%d)\n", rc); 681 kfree(dm); 682 return rc; 683 } 684 685 kfree(dm); 686 687 dev->can.state = CAN_STATE_ERROR_ACTIVE; 688 689 if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)) 690 netif_start_queue(netdev); 691 692 return 0; 693 } 694 695 static int gs_can_close(struct net_device *netdev) 696 { 697 int rc; 698 struct gs_can *dev = netdev_priv(netdev); 699 struct gs_usb *parent = dev->parent; 700 701 netif_stop_queue(netdev); 702 703 /* Stop polling */ 704 if (atomic_dec_and_test(&parent->active_channels)) 705 usb_kill_anchored_urbs(&parent->rx_submitted); 706 707 /* Stop sending URBs */ 708 usb_kill_anchored_urbs(&dev->tx_submitted); 709 atomic_set(&dev->active_tx_urbs, 0); 710 711 /* reset the device */ 712 rc = gs_cmd_reset(dev); 713 if (rc < 0) 714 netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc); 715 716 /* reset tx contexts */ 717 for (rc = 0; rc < GS_MAX_TX_URBS; rc++) { 718 dev->tx_context[rc].dev = dev; 719 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS; 720 } 721 722 /* close the netdev */ 723 close_candev(netdev); 724 725 return 0; 726 } 727 728 static const struct net_device_ops gs_usb_netdev_ops = { 729 .ndo_open = gs_can_open, 730 .ndo_stop = gs_can_close, 731 .ndo_start_xmit = gs_can_start_xmit, 732 .ndo_change_mtu = can_change_mtu, 733 }; 734 735 static int gs_usb_set_identify(struct net_device *netdev, bool do_identify) 736 { 737 struct gs_can *dev = netdev_priv(netdev); 738 struct gs_identify_mode *imode; 739 int rc; 740 741 imode = kmalloc(sizeof(*imode), GFP_KERNEL); 742 743 if (!imode) 744 return -ENOMEM; 745 746 if (do_identify) 747 imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_ON); 748 else 749 imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF); 750 751 rc = usb_control_msg(interface_to_usbdev(dev->iface), 752 usb_sndctrlpipe(interface_to_usbdev(dev->iface), 753 0), 754 GS_USB_BREQ_IDENTIFY, 755 USB_DIR_OUT | USB_TYPE_VENDOR | 756 USB_RECIP_INTERFACE, 757 dev->channel, 758 0, 759 imode, 760 sizeof(*imode), 761 100); 762 763 kfree(imode); 764 765 return (rc > 0) ? 0 : rc; 766 } 767 768 /* blink LED's for finding the this interface */ 769 static int gs_usb_set_phys_id(struct net_device *dev, 770 enum ethtool_phys_id_state state) 771 { 772 int rc = 0; 773 774 switch (state) { 775 case ETHTOOL_ID_ACTIVE: 776 rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON); 777 break; 778 case ETHTOOL_ID_INACTIVE: 779 rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF); 780 break; 781 default: 782 break; 783 } 784 785 return rc; 786 } 787 788 static const struct ethtool_ops gs_usb_ethtool_ops = { 789 .set_phys_id = gs_usb_set_phys_id, 790 }; 791 792 static struct gs_can *gs_make_candev(unsigned int channel, 793 struct usb_interface *intf, 794 struct gs_device_config *dconf) 795 { 796 struct gs_can *dev; 797 struct net_device *netdev; 798 int rc; 799 struct gs_device_bt_const *bt_const; 800 u32 feature; 801 802 bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL); 803 if (!bt_const) 804 return ERR_PTR(-ENOMEM); 805 806 /* fetch bit timing constants */ 807 rc = usb_control_msg(interface_to_usbdev(intf), 808 usb_rcvctrlpipe(interface_to_usbdev(intf), 0), 809 GS_USB_BREQ_BT_CONST, 810 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 811 channel, 812 0, 813 bt_const, 814 sizeof(*bt_const), 815 1000); 816 817 if (rc < 0) { 818 dev_err(&intf->dev, 819 "Couldn't get bit timing const for channel (err=%d)\n", 820 rc); 821 kfree(bt_const); 822 return ERR_PTR(rc); 823 } 824 825 /* create netdev */ 826 netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS); 827 if (!netdev) { 828 dev_err(&intf->dev, "Couldn't allocate candev\n"); 829 kfree(bt_const); 830 return ERR_PTR(-ENOMEM); 831 } 832 833 dev = netdev_priv(netdev); 834 835 netdev->netdev_ops = &gs_usb_netdev_ops; 836 837 netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */ 838 839 /* dev setup */ 840 strcpy(dev->bt_const.name, "gs_usb"); 841 dev->bt_const.tseg1_min = le32_to_cpu(bt_const->tseg1_min); 842 dev->bt_const.tseg1_max = le32_to_cpu(bt_const->tseg1_max); 843 dev->bt_const.tseg2_min = le32_to_cpu(bt_const->tseg2_min); 844 dev->bt_const.tseg2_max = le32_to_cpu(bt_const->tseg2_max); 845 dev->bt_const.sjw_max = le32_to_cpu(bt_const->sjw_max); 846 dev->bt_const.brp_min = le32_to_cpu(bt_const->brp_min); 847 dev->bt_const.brp_max = le32_to_cpu(bt_const->brp_max); 848 dev->bt_const.brp_inc = le32_to_cpu(bt_const->brp_inc); 849 850 dev->udev = interface_to_usbdev(intf); 851 dev->iface = intf; 852 dev->netdev = netdev; 853 dev->channel = channel; 854 855 init_usb_anchor(&dev->tx_submitted); 856 atomic_set(&dev->active_tx_urbs, 0); 857 spin_lock_init(&dev->tx_ctx_lock); 858 for (rc = 0; rc < GS_MAX_TX_URBS; rc++) { 859 dev->tx_context[rc].dev = dev; 860 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS; 861 } 862 863 /* can setup */ 864 dev->can.state = CAN_STATE_STOPPED; 865 dev->can.clock.freq = le32_to_cpu(bt_const->fclk_can); 866 dev->can.bittiming_const = &dev->bt_const; 867 dev->can.do_set_bittiming = gs_usb_set_bittiming; 868 869 dev->can.ctrlmode_supported = 0; 870 871 feature = le32_to_cpu(bt_const->feature); 872 if (feature & GS_CAN_FEATURE_LISTEN_ONLY) 873 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY; 874 875 if (feature & GS_CAN_FEATURE_LOOP_BACK) 876 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK; 877 878 if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE) 879 dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES; 880 881 if (feature & GS_CAN_FEATURE_ONE_SHOT) 882 dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT; 883 884 SET_NETDEV_DEV(netdev, &intf->dev); 885 886 if (le32_to_cpu(dconf->sw_version) > 1) 887 if (feature & GS_CAN_FEATURE_IDENTIFY) 888 netdev->ethtool_ops = &gs_usb_ethtool_ops; 889 890 kfree(bt_const); 891 892 rc = register_candev(dev->netdev); 893 if (rc) { 894 free_candev(dev->netdev); 895 dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc); 896 return ERR_PTR(rc); 897 } 898 899 return dev; 900 } 901 902 static void gs_destroy_candev(struct gs_can *dev) 903 { 904 unregister_candev(dev->netdev); 905 usb_kill_anchored_urbs(&dev->tx_submitted); 906 free_candev(dev->netdev); 907 } 908 909 static int gs_usb_probe(struct usb_interface *intf, 910 const struct usb_device_id *id) 911 { 912 struct gs_usb *dev; 913 int rc = -ENOMEM; 914 unsigned int icount, i; 915 struct gs_host_config *hconf; 916 struct gs_device_config *dconf; 917 918 hconf = kmalloc(sizeof(*hconf), GFP_KERNEL); 919 if (!hconf) 920 return -ENOMEM; 921 922 hconf->byte_order = cpu_to_le32(0x0000beef); 923 924 /* send host config */ 925 rc = usb_control_msg(interface_to_usbdev(intf), 926 usb_sndctrlpipe(interface_to_usbdev(intf), 0), 927 GS_USB_BREQ_HOST_FORMAT, 928 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 929 1, 930 intf->cur_altsetting->desc.bInterfaceNumber, 931 hconf, 932 sizeof(*hconf), 933 1000); 934 935 kfree(hconf); 936 937 if (rc < 0) { 938 dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", 939 rc); 940 return rc; 941 } 942 943 dconf = kmalloc(sizeof(*dconf), GFP_KERNEL); 944 if (!dconf) 945 return -ENOMEM; 946 947 /* read device config */ 948 rc = usb_control_msg(interface_to_usbdev(intf), 949 usb_rcvctrlpipe(interface_to_usbdev(intf), 0), 950 GS_USB_BREQ_DEVICE_CONFIG, 951 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 952 1, 953 intf->cur_altsetting->desc.bInterfaceNumber, 954 dconf, 955 sizeof(*dconf), 956 1000); 957 if (rc < 0) { 958 dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n", 959 rc); 960 kfree(dconf); 961 return rc; 962 } 963 964 icount = dconf->icount + 1; 965 dev_info(&intf->dev, "Configuring for %d interfaces\n", icount); 966 967 if (icount > GS_MAX_INTF) { 968 dev_err(&intf->dev, 969 "Driver cannot handle more that %d CAN interfaces\n", 970 GS_MAX_INTF); 971 kfree(dconf); 972 return -EINVAL; 973 } 974 975 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 976 if (!dev) { 977 kfree(dconf); 978 return -ENOMEM; 979 } 980 981 init_usb_anchor(&dev->rx_submitted); 982 983 atomic_set(&dev->active_channels, 0); 984 985 usb_set_intfdata(intf, dev); 986 dev->udev = interface_to_usbdev(intf); 987 988 for (i = 0; i < icount; i++) { 989 dev->canch[i] = gs_make_candev(i, intf, dconf); 990 if (IS_ERR_OR_NULL(dev->canch[i])) { 991 /* save error code to return later */ 992 rc = PTR_ERR(dev->canch[i]); 993 994 /* on failure destroy previously created candevs */ 995 icount = i; 996 for (i = 0; i < icount; i++) 997 gs_destroy_candev(dev->canch[i]); 998 999 usb_kill_anchored_urbs(&dev->rx_submitted); 1000 kfree(dconf); 1001 kfree(dev); 1002 return rc; 1003 } 1004 dev->canch[i]->parent = dev; 1005 } 1006 1007 kfree(dconf); 1008 1009 return 0; 1010 } 1011 1012 static void gs_usb_disconnect(struct usb_interface *intf) 1013 { 1014 unsigned i; 1015 struct gs_usb *dev = usb_get_intfdata(intf); 1016 usb_set_intfdata(intf, NULL); 1017 1018 if (!dev) { 1019 dev_err(&intf->dev, "Disconnect (nodata)\n"); 1020 return; 1021 } 1022 1023 for (i = 0; i < GS_MAX_INTF; i++) 1024 if (dev->canch[i]) 1025 gs_destroy_candev(dev->canch[i]); 1026 1027 usb_kill_anchored_urbs(&dev->rx_submitted); 1028 kfree(dev); 1029 } 1030 1031 static const struct usb_device_id gs_usb_table[] = { 1032 { USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID, 1033 USB_GSUSB_1_PRODUCT_ID, 0) }, 1034 { USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID, 1035 USB_CANDLELIGHT_PRODUCT_ID, 0) }, 1036 {} /* Terminating entry */ 1037 }; 1038 1039 MODULE_DEVICE_TABLE(usb, gs_usb_table); 1040 1041 static struct usb_driver gs_usb_driver = { 1042 .name = "gs_usb", 1043 .probe = gs_usb_probe, 1044 .disconnect = gs_usb_disconnect, 1045 .id_table = gs_usb_table, 1046 }; 1047 1048 module_usb_driver(gs_usb_driver); 1049 1050 MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>"); 1051 MODULE_DESCRIPTION( 1052 "Socket CAN device driver for Geschwister Schneider Technologie-, " 1053 "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n" 1054 "and bytewerk.org candleLight USB CAN interfaces."); 1055 MODULE_LICENSE("GPL v2"); 1056