hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/drivers/net/can/usb/mcba_usb.c
....@@ -1,18 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* SocketCAN driver for Microchip CAN BUS Analyzer Tool
23 *
34 * Copyright (C) 2017 Mobica Limited
4
- *
5
- * This program is free software; you can redistribute it and/or modify it
6
- * under the terms of the GNU General Public License as published
7
- * by the Free Software Foundation; version 2 of the License.
8
- *
9
- * This program is distributed in the hope that it will be useful, but
10
- * WITHOUT ANY WARRANTY; without even the implied warranty of
11
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
- * General Public License for more details.
13
- *
14
- * You should have received a copy of the GNU General Public License along
15
- * with this program.
165 *
176 * This driver is inspired by the 4.6.2 version of net/can/usb/usb_8dev.c
187 */
....@@ -39,14 +28,10 @@
3928 #define MCBA_CTX_FREE MCBA_MAX_TX_URBS
4029
4130 /* RX buffer must be bigger than msg size since at the
42
- * beggining USB messages are stacked.
31
+ * beginning USB messages are stacked.
4332 */
4433 #define MCBA_USB_RX_BUFF_SIZE 64
4534 #define MCBA_USB_TX_BUFF_SIZE (sizeof(struct mcba_usb_msg))
46
-
47
-/* MCBA endpoint numbers */
48
-#define MCBA_USB_EP_IN 1
49
-#define MCBA_USB_EP_OUT 1
5035
5136 /* Microchip command id */
5237 #define MBCA_CMD_RECEIVE_MESSAGE 0xE3
....@@ -61,6 +46,10 @@
6146
6247 #define MCBA_VER_REQ_USB 1
6348 #define MCBA_VER_REQ_CAN 2
49
+
50
+/* Drive the CAN_RES signal LOW "0" to activate R24 and R25 */
51
+#define MCBA_VER_TERMINATION_ON 0
52
+#define MCBA_VER_TERMINATION_OFF 1
6453
6554 #define MCBA_SIDL_EXID_MASK 0x8
6655 #define MCBA_DLC_MASK 0xf
....@@ -95,6 +84,8 @@
9584 atomic_t free_ctx_cnt;
9685 void *rxbuf[MCBA_MAX_RX_URBS];
9786 dma_addr_t rxbuf_dma[MCBA_MAX_RX_URBS];
87
+ int rx_pipe;
88
+ int tx_pipe;
9889 };
9990
10091 /* CAN frame */
....@@ -283,10 +274,8 @@
283274
284275 memcpy(buf, usb_msg, MCBA_USB_TX_BUFF_SIZE);
285276
286
- usb_fill_bulk_urb(urb, priv->udev,
287
- usb_sndbulkpipe(priv->udev, MCBA_USB_EP_OUT), buf,
288
- MCBA_USB_TX_BUFF_SIZE, mcba_usb_write_bulk_callback,
289
- ctx);
277
+ usb_fill_bulk_urb(urb, priv->udev, priv->tx_pipe, buf, MCBA_USB_TX_BUFF_SIZE,
278
+ mcba_usb_write_bulk_callback, ctx);
290279
291280 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
292281 usb_anchor_urb(urb, &priv->tx_submitted);
....@@ -379,7 +368,6 @@
379368 xmit_failed:
380369 can_free_echo_skb(priv->netdev, ctx->ndx);
381370 mcba_usb_free_ctx(ctx);
382
- dev_kfree_skb(skb);
383371 stats->tx_dropped++;
384372
385373 return NETDEV_TX_OK;
....@@ -485,7 +473,7 @@
485473 priv->usb_ka_first_pass = false;
486474 }
487475
488
- if (msg->termination_state)
476
+ if (msg->termination_state == MCBA_VER_TERMINATION_ON)
489477 priv->can.termination = MCBA_TERMINATION_ENABLED;
490478 else
491479 priv->can.termination = MCBA_TERMINATION_DISABLED;
....@@ -622,7 +610,7 @@
622610 resubmit_urb:
623611
624612 usb_fill_bulk_urb(urb, priv->udev,
625
- usb_rcvbulkpipe(priv->udev, MCBA_USB_EP_OUT),
613
+ priv->rx_pipe,
626614 urb->transfer_buffer, MCBA_USB_RX_BUFF_SIZE,
627615 mcba_usb_read_bulk_callback, priv);
628616
....@@ -667,7 +655,7 @@
667655 urb->transfer_dma = buf_dma;
668656
669657 usb_fill_bulk_urb(urb, priv->udev,
670
- usb_rcvbulkpipe(priv->udev, MCBA_USB_EP_IN),
658
+ priv->rx_pipe,
671659 buf, MCBA_USB_RX_BUFF_SIZE,
672660 mcba_usb_read_bulk_callback, priv);
673661 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
....@@ -805,9 +793,9 @@
805793 };
806794
807795 if (term == MCBA_TERMINATION_ENABLED)
808
- usb_msg.termination = 1;
796
+ usb_msg.termination = MCBA_VER_TERMINATION_ON;
809797 else
810
- usb_msg.termination = 0;
798
+ usb_msg.termination = MCBA_VER_TERMINATION_OFF;
811799
812800 mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
813801
....@@ -819,8 +807,15 @@
819807 {
820808 struct net_device *netdev;
821809 struct mcba_priv *priv;
822
- int err = -ENOMEM;
810
+ int err;
823811 struct usb_device *usbdev = interface_to_usbdev(intf);
812
+ struct usb_endpoint_descriptor *in, *out;
813
+
814
+ err = usb_find_common_endpoints(intf->cur_altsetting, &in, &out, NULL, NULL);
815
+ if (err) {
816
+ dev_err(&intf->dev, "Can't find endpoints\n");
817
+ return err;
818
+ }
824819
825820 netdev = alloc_candev(sizeof(struct mcba_priv), MCBA_MAX_TX_URBS);
826821 if (!netdev) {
....@@ -866,6 +861,9 @@
866861 goto cleanup_free_candev;
867862 }
868863
864
+ priv->rx_pipe = usb_rcvbulkpipe(priv->udev, in->bEndpointAddress);
865
+ priv->tx_pipe = usb_sndbulkpipe(priv->udev, out->bEndpointAddress);
866
+
869867 devm_can_led_init(netdev);
870868
871869 /* Start USB dev only if we have successfully registered CAN device */