forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/net/can/usb/gs_usb.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* CAN driver for Geschwister Schneider USB/CAN devices
23 * and bytewerk.org candleLight USB CAN interfaces.
34 *
....@@ -6,15 +7,6 @@
67 * Copyright (C) 2016 Hubert Denkmair
78 *
89 * 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.
1810 */
1911
2012 #include <linux/init.h>
....@@ -192,14 +184,16 @@
192184
193185 struct usb_anchor tx_submitted;
194186 atomic_t active_tx_urbs;
187
+ void *rxbuf[GS_MAX_RX_URBS];
188
+ dma_addr_t rxbuf_dma[GS_MAX_RX_URBS];
195189 };
196190
197191 /* usb interface struct */
198192 struct gs_usb {
199193 struct gs_can *canch[GS_MAX_INTF];
200194 struct usb_anchor rx_submitted;
201
- atomic_t active_channels;
202195 struct usb_device *udev;
196
+ u8 active_channels;
203197 };
204198
205199 /* 'allocate' a tx context.
....@@ -596,10 +590,11 @@
596590 if (rc)
597591 return rc;
598592
599
- if (atomic_add_return(1, &parent->active_channels) == 1) {
593
+ if (!parent->active_channels) {
600594 for (i = 0; i < GS_MAX_RX_URBS; i++) {
601595 struct urb *urb;
602596 u8 *buf;
597
+ dma_addr_t buf_dma;
603598
604599 /* alloc rx urb */
605600 urb = usb_alloc_urb(0, GFP_KERNEL);
....@@ -610,13 +605,15 @@
610605 buf = usb_alloc_coherent(dev->udev,
611606 sizeof(struct gs_host_frame),
612607 GFP_KERNEL,
613
- &urb->transfer_dma);
608
+ &buf_dma);
614609 if (!buf) {
615610 netdev_err(netdev,
616611 "No memory left for USB buffer\n");
617612 usb_free_urb(urb);
618613 return -ENOMEM;
619614 }
615
+
616
+ urb->transfer_dma = buf_dma;
620617
621618 /* fill, anchor, and submit rx urb */
622619 usb_fill_bulk_urb(urb,
....@@ -641,9 +638,16 @@
641638 rc);
642639
643640 usb_unanchor_urb(urb);
641
+ usb_free_coherent(dev->udev,
642
+ sizeof(struct gs_host_frame),
643
+ buf,
644
+ buf_dma);
644645 usb_free_urb(urb);
645646 break;
646647 }
648
+
649
+ dev->rxbuf[i] = buf;
650
+ dev->rxbuf_dma[i] = buf_dma;
647651
648652 /* Drop reference,
649653 * USB core will take care of freeing it
....@@ -674,6 +678,7 @@
674678 flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
675679
676680 /* finally start device */
681
+ dev->can.state = CAN_STATE_ERROR_ACTIVE;
677682 dm->mode = cpu_to_le32(GS_CAN_MODE_START);
678683 dm->flags = cpu_to_le32(flags);
679684 rc = usb_control_msg(interface_to_usbdev(dev->iface),
....@@ -690,13 +695,13 @@
690695 if (rc < 0) {
691696 netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
692697 kfree(dm);
698
+ dev->can.state = CAN_STATE_STOPPED;
693699 return rc;
694700 }
695701
696702 kfree(dm);
697703
698
- dev->can.state = CAN_STATE_ERROR_ACTIVE;
699
-
704
+ parent->active_channels++;
700705 if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
701706 netif_start_queue(netdev);
702707
....@@ -708,12 +713,20 @@
708713 int rc;
709714 struct gs_can *dev = netdev_priv(netdev);
710715 struct gs_usb *parent = dev->parent;
716
+ unsigned int i;
711717
712718 netif_stop_queue(netdev);
713719
714720 /* Stop polling */
715
- if (atomic_dec_and_test(&parent->active_channels))
721
+ parent->active_channels--;
722
+ if (!parent->active_channels) {
716723 usb_kill_anchored_urbs(&parent->rx_submitted);
724
+ for (i = 0; i < GS_MAX_RX_URBS; i++)
725
+ usb_free_coherent(dev->udev,
726
+ sizeof(struct gs_host_frame),
727
+ dev->rxbuf[i],
728
+ dev->rxbuf_dma[i]);
729
+ }
717730
718731 /* Stop sending URBs */
719732 usb_kill_anchored_urbs(&dev->tx_submitted);
....@@ -847,7 +860,7 @@
847860
848861 netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
849862
850
- /* dev settup */
863
+ /* dev setup */
851864 strcpy(dev->bt_const.name, "gs_usb");
852865 dev->bt_const.tseg1_min = le32_to_cpu(bt_const->tseg1_min);
853866 dev->bt_const.tseg1_max = le32_to_cpu(bt_const->tseg1_max);
....@@ -871,7 +884,7 @@
871884 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
872885 }
873886
874
- /* can settup */
887
+ /* can setup */
875888 dev->can.state = CAN_STATE_STOPPED;
876889 dev->can.clock.freq = le32_to_cpu(bt_const->fclk_can);
877890 dev->can.bittiming_const = &dev->bt_const;
....@@ -990,8 +1003,6 @@
9901003 }
9911004
9921005 init_usb_anchor(&dev->rx_submitted);
993
-
994
- atomic_set(&dev->active_channels, 0);
9951006
9961007 usb_set_intfdata(intf, dev);
9971008 dev->udev = interface_to_usbdev(intf);