.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* CAN driver for Geschwister Schneider USB/CAN devices |
---|
2 | 3 | * and bytewerk.org candleLight USB CAN interfaces. |
---|
3 | 4 | * |
---|
.. | .. |
---|
6 | 7 | * Copyright (C) 2016 Hubert Denkmair |
---|
7 | 8 | * |
---|
8 | 9 | * Many thanks to all socketcan devs! |
---|
9 | | - * |
---|
10 | | - * This program is free software; you can redistribute it and/or modify it |
---|
11 | | - * under the terms of the GNU General Public License as published |
---|
12 | | - * by the Free Software Foundation; version 2 of the License. |
---|
13 | | - * |
---|
14 | | - * This program is distributed in the hope that it will be useful, but |
---|
15 | | - * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | | - * General Public License for more details. |
---|
18 | 10 | */ |
---|
19 | 11 | |
---|
20 | 12 | #include <linux/init.h> |
---|
.. | .. |
---|
192 | 184 | |
---|
193 | 185 | struct usb_anchor tx_submitted; |
---|
194 | 186 | atomic_t active_tx_urbs; |
---|
| 187 | + void *rxbuf[GS_MAX_RX_URBS]; |
---|
| 188 | + dma_addr_t rxbuf_dma[GS_MAX_RX_URBS]; |
---|
195 | 189 | }; |
---|
196 | 190 | |
---|
197 | 191 | /* usb interface struct */ |
---|
198 | 192 | struct gs_usb { |
---|
199 | 193 | struct gs_can *canch[GS_MAX_INTF]; |
---|
200 | 194 | struct usb_anchor rx_submitted; |
---|
201 | | - atomic_t active_channels; |
---|
202 | 195 | struct usb_device *udev; |
---|
| 196 | + u8 active_channels; |
---|
203 | 197 | }; |
---|
204 | 198 | |
---|
205 | 199 | /* 'allocate' a tx context. |
---|
.. | .. |
---|
387 | 381 | } |
---|
388 | 382 | |
---|
389 | 383 | if (hf->flags & GS_CAN_FLAG_OVERFLOW) { |
---|
| 384 | + stats->rx_over_errors++; |
---|
| 385 | + stats->rx_errors++; |
---|
| 386 | + |
---|
390 | 387 | skb = alloc_can_err_skb(netdev, &cf); |
---|
391 | 388 | if (!skb) |
---|
392 | 389 | goto resubmit_urb; |
---|
.. | .. |
---|
394 | 391 | cf->can_id |= CAN_ERR_CRTL; |
---|
395 | 392 | cf->can_dlc = CAN_ERR_DLC; |
---|
396 | 393 | cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; |
---|
397 | | - stats->rx_over_errors++; |
---|
398 | | - stats->rx_errors++; |
---|
399 | 394 | netif_rx(skb); |
---|
400 | 395 | } |
---|
401 | 396 | |
---|
.. | .. |
---|
596 | 591 | if (rc) |
---|
597 | 592 | return rc; |
---|
598 | 593 | |
---|
599 | | - if (atomic_add_return(1, &parent->active_channels) == 1) { |
---|
| 594 | + if (!parent->active_channels) { |
---|
600 | 595 | for (i = 0; i < GS_MAX_RX_URBS; i++) { |
---|
601 | 596 | struct urb *urb; |
---|
602 | 597 | u8 *buf; |
---|
| 598 | + dma_addr_t buf_dma; |
---|
603 | 599 | |
---|
604 | 600 | /* alloc rx urb */ |
---|
605 | 601 | urb = usb_alloc_urb(0, GFP_KERNEL); |
---|
.. | .. |
---|
610 | 606 | buf = usb_alloc_coherent(dev->udev, |
---|
611 | 607 | sizeof(struct gs_host_frame), |
---|
612 | 608 | GFP_KERNEL, |
---|
613 | | - &urb->transfer_dma); |
---|
| 609 | + &buf_dma); |
---|
614 | 610 | if (!buf) { |
---|
615 | 611 | netdev_err(netdev, |
---|
616 | 612 | "No memory left for USB buffer\n"); |
---|
617 | 613 | usb_free_urb(urb); |
---|
618 | 614 | return -ENOMEM; |
---|
619 | 615 | } |
---|
| 616 | + |
---|
| 617 | + urb->transfer_dma = buf_dma; |
---|
620 | 618 | |
---|
621 | 619 | /* fill, anchor, and submit rx urb */ |
---|
622 | 620 | usb_fill_bulk_urb(urb, |
---|
.. | .. |
---|
641 | 639 | rc); |
---|
642 | 640 | |
---|
643 | 641 | usb_unanchor_urb(urb); |
---|
| 642 | + usb_free_coherent(dev->udev, |
---|
| 643 | + sizeof(struct gs_host_frame), |
---|
| 644 | + buf, |
---|
| 645 | + buf_dma); |
---|
644 | 646 | usb_free_urb(urb); |
---|
645 | 647 | break; |
---|
646 | 648 | } |
---|
| 649 | + |
---|
| 650 | + dev->rxbuf[i] = buf; |
---|
| 651 | + dev->rxbuf_dma[i] = buf_dma; |
---|
647 | 652 | |
---|
648 | 653 | /* Drop reference, |
---|
649 | 654 | * USB core will take care of freeing it |
---|
.. | .. |
---|
674 | 679 | flags |= GS_CAN_MODE_TRIPLE_SAMPLE; |
---|
675 | 680 | |
---|
676 | 681 | /* finally start device */ |
---|
| 682 | + dev->can.state = CAN_STATE_ERROR_ACTIVE; |
---|
677 | 683 | dm->mode = cpu_to_le32(GS_CAN_MODE_START); |
---|
678 | 684 | dm->flags = cpu_to_le32(flags); |
---|
679 | 685 | rc = usb_control_msg(interface_to_usbdev(dev->iface), |
---|
.. | .. |
---|
690 | 696 | if (rc < 0) { |
---|
691 | 697 | netdev_err(netdev, "Couldn't start device (err=%d)\n", rc); |
---|
692 | 698 | kfree(dm); |
---|
| 699 | + dev->can.state = CAN_STATE_STOPPED; |
---|
693 | 700 | return rc; |
---|
694 | 701 | } |
---|
695 | 702 | |
---|
696 | 703 | kfree(dm); |
---|
697 | 704 | |
---|
698 | | - dev->can.state = CAN_STATE_ERROR_ACTIVE; |
---|
699 | | - |
---|
| 705 | + parent->active_channels++; |
---|
700 | 706 | if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)) |
---|
701 | 707 | netif_start_queue(netdev); |
---|
702 | 708 | |
---|
.. | .. |
---|
708 | 714 | int rc; |
---|
709 | 715 | struct gs_can *dev = netdev_priv(netdev); |
---|
710 | 716 | struct gs_usb *parent = dev->parent; |
---|
| 717 | + unsigned int i; |
---|
711 | 718 | |
---|
712 | 719 | netif_stop_queue(netdev); |
---|
713 | 720 | |
---|
714 | 721 | /* Stop polling */ |
---|
715 | | - if (atomic_dec_and_test(&parent->active_channels)) |
---|
| 722 | + parent->active_channels--; |
---|
| 723 | + if (!parent->active_channels) { |
---|
716 | 724 | usb_kill_anchored_urbs(&parent->rx_submitted); |
---|
| 725 | + for (i = 0; i < GS_MAX_RX_URBS; i++) |
---|
| 726 | + usb_free_coherent(dev->udev, |
---|
| 727 | + sizeof(struct gs_host_frame), |
---|
| 728 | + dev->rxbuf[i], |
---|
| 729 | + dev->rxbuf_dma[i]); |
---|
| 730 | + } |
---|
717 | 731 | |
---|
718 | 732 | /* Stop sending URBs */ |
---|
719 | 733 | usb_kill_anchored_urbs(&dev->tx_submitted); |
---|
720 | 734 | atomic_set(&dev->active_tx_urbs, 0); |
---|
| 735 | + |
---|
| 736 | + dev->can.state = CAN_STATE_STOPPED; |
---|
721 | 737 | |
---|
722 | 738 | /* reset the device */ |
---|
723 | 739 | rc = gs_cmd_reset(dev); |
---|
.. | .. |
---|
847 | 863 | |
---|
848 | 864 | netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */ |
---|
849 | 865 | |
---|
850 | | - /* dev settup */ |
---|
| 866 | + /* dev setup */ |
---|
851 | 867 | strcpy(dev->bt_const.name, "gs_usb"); |
---|
852 | 868 | dev->bt_const.tseg1_min = le32_to_cpu(bt_const->tseg1_min); |
---|
853 | 869 | dev->bt_const.tseg1_max = le32_to_cpu(bt_const->tseg1_max); |
---|
.. | .. |
---|
871 | 887 | dev->tx_context[rc].echo_id = GS_MAX_TX_URBS; |
---|
872 | 888 | } |
---|
873 | 889 | |
---|
874 | | - /* can settup */ |
---|
| 890 | + /* can setup */ |
---|
875 | 891 | dev->can.state = CAN_STATE_STOPPED; |
---|
876 | 892 | dev->can.clock.freq = le32_to_cpu(bt_const->fclk_can); |
---|
877 | 893 | dev->can.bittiming_const = &dev->bt_const; |
---|
.. | .. |
---|
990 | 1006 | } |
---|
991 | 1007 | |
---|
992 | 1008 | init_usb_anchor(&dev->rx_submitted); |
---|
993 | | - |
---|
994 | | - atomic_set(&dev->active_channels, 0); |
---|
995 | 1009 | |
---|
996 | 1010 | usb_set_intfdata(intf, dev); |
---|
997 | 1011 | dev->udev = interface_to_usbdev(intf); |
---|