forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-13 9d77db3c730780c8ef5ccd4b66403ff5675cfe4e
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.
....@@ -387,6 +381,9 @@
387381 }
388382
389383 if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
384
+ stats->rx_over_errors++;
385
+ stats->rx_errors++;
386
+
390387 skb = alloc_can_err_skb(netdev, &cf);
391388 if (!skb)
392389 goto resubmit_urb;
....@@ -394,8 +391,6 @@
394391 cf->can_id |= CAN_ERR_CRTL;
395392 cf->can_dlc = CAN_ERR_DLC;
396393 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
397
- stats->rx_over_errors++;
398
- stats->rx_errors++;
399394 netif_rx(skb);
400395 }
401396
....@@ -596,10 +591,11 @@
596591 if (rc)
597592 return rc;
598593
599
- if (atomic_add_return(1, &parent->active_channels) == 1) {
594
+ if (!parent->active_channels) {
600595 for (i = 0; i < GS_MAX_RX_URBS; i++) {
601596 struct urb *urb;
602597 u8 *buf;
598
+ dma_addr_t buf_dma;
603599
604600 /* alloc rx urb */
605601 urb = usb_alloc_urb(0, GFP_KERNEL);
....@@ -610,13 +606,15 @@
610606 buf = usb_alloc_coherent(dev->udev,
611607 sizeof(struct gs_host_frame),
612608 GFP_KERNEL,
613
- &urb->transfer_dma);
609
+ &buf_dma);
614610 if (!buf) {
615611 netdev_err(netdev,
616612 "No memory left for USB buffer\n");
617613 usb_free_urb(urb);
618614 return -ENOMEM;
619615 }
616
+
617
+ urb->transfer_dma = buf_dma;
620618
621619 /* fill, anchor, and submit rx urb */
622620 usb_fill_bulk_urb(urb,
....@@ -641,9 +639,16 @@
641639 rc);
642640
643641 usb_unanchor_urb(urb);
642
+ usb_free_coherent(dev->udev,
643
+ sizeof(struct gs_host_frame),
644
+ buf,
645
+ buf_dma);
644646 usb_free_urb(urb);
645647 break;
646648 }
649
+
650
+ dev->rxbuf[i] = buf;
651
+ dev->rxbuf_dma[i] = buf_dma;
647652
648653 /* Drop reference,
649654 * USB core will take care of freeing it
....@@ -674,6 +679,7 @@
674679 flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
675680
676681 /* finally start device */
682
+ dev->can.state = CAN_STATE_ERROR_ACTIVE;
677683 dm->mode = cpu_to_le32(GS_CAN_MODE_START);
678684 dm->flags = cpu_to_le32(flags);
679685 rc = usb_control_msg(interface_to_usbdev(dev->iface),
....@@ -690,13 +696,13 @@
690696 if (rc < 0) {
691697 netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
692698 kfree(dm);
699
+ dev->can.state = CAN_STATE_STOPPED;
693700 return rc;
694701 }
695702
696703 kfree(dm);
697704
698
- dev->can.state = CAN_STATE_ERROR_ACTIVE;
699
-
705
+ parent->active_channels++;
700706 if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
701707 netif_start_queue(netdev);
702708
....@@ -708,16 +714,26 @@
708714 int rc;
709715 struct gs_can *dev = netdev_priv(netdev);
710716 struct gs_usb *parent = dev->parent;
717
+ unsigned int i;
711718
712719 netif_stop_queue(netdev);
713720
714721 /* Stop polling */
715
- if (atomic_dec_and_test(&parent->active_channels))
722
+ parent->active_channels--;
723
+ if (!parent->active_channels) {
716724 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
+ }
717731
718732 /* Stop sending URBs */
719733 usb_kill_anchored_urbs(&dev->tx_submitted);
720734 atomic_set(&dev->active_tx_urbs, 0);
735
+
736
+ dev->can.state = CAN_STATE_STOPPED;
721737
722738 /* reset the device */
723739 rc = gs_cmd_reset(dev);
....@@ -847,7 +863,7 @@
847863
848864 netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
849865
850
- /* dev settup */
866
+ /* dev setup */
851867 strcpy(dev->bt_const.name, "gs_usb");
852868 dev->bt_const.tseg1_min = le32_to_cpu(bt_const->tseg1_min);
853869 dev->bt_const.tseg1_max = le32_to_cpu(bt_const->tseg1_max);
....@@ -871,7 +887,7 @@
871887 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
872888 }
873889
874
- /* can settup */
890
+ /* can setup */
875891 dev->can.state = CAN_STATE_STOPPED;
876892 dev->can.clock.freq = le32_to_cpu(bt_const->fclk_can);
877893 dev->can.bittiming_const = &dev->bt_const;
....@@ -990,8 +1006,6 @@
9901006 }
9911007
9921008 init_usb_anchor(&dev->rx_submitted);
993
-
994
- atomic_set(&dev->active_channels, 0);
9951009
9961010 usb_set_intfdata(intf, dev);
9971011 dev->udev = interface_to_usbdev(intf);