All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gerhard Uttenthaler <uttenthaler@ems-wuensche.com>
To: linux-can@vger.kernel.org
Cc: wg@grandegger.com, mkl@pengutronix.de,
	Gerhard Uttenthaler <uttenthaler@ems-wuensche.com>
Subject: [PATCH v2 08/16] can: ems_usb: Modified ems_usb_read_bulk_callback to handle also CPC-USB/FD
Date: Thu, 12 Nov 2020 19:03:38 +0100	[thread overview]
Message-ID: <20201112180346.29070-9-uttenthaler@ems-wuensche.com> (raw)
In-Reply-To: <20201112180346.29070-1-uttenthaler@ems-wuensche.com>

The ems_usb_read_bulk_callback of the drivers present version uses a
byte in the beginning of the received bulk buffer to check how many 
messages come from the interface. This is not possible with CPC-USB/FD
and hence changed in a way to satisfy both types of interface.

Signed-off-by: Gerhard Uttenthaler <uttenthaler@ems-wuensche.com>
---
 drivers/net/can/usb/ems_usb.c | 91 +++++++++++++++++++----------------
 1 file changed, 50 insertions(+), 41 deletions(-)

diff --git a/drivers/net/can/usb/ems_usb.c b/drivers/net/can/usb/ems_usb.c
index 0e8d1b75ef8d..41f9fb126e0a 100644
--- a/drivers/net/can/usb/ems_usb.c
+++ b/drivers/net/can/usb/ems_usb.c
@@ -456,6 +456,7 @@ static void ems_usb_read_bulk_callback(struct urb *urb)
 	struct ems_usb *dev = urb->context;
 	struct net_device *netdev;
 	int retval;
+	u32 length, start;
 
 	netdev = dev->netdev;
 
@@ -474,50 +475,58 @@ static void ems_usb_read_bulk_callback(struct urb *urb)
 		goto resubmit_urb;
 	}
 
-	if (urb->actual_length > CPC_HEADER_SIZE) {
+	length = urb->actual_length;
+	start = CPC_HEADER_SIZE;
+
+	while (length >= CPC_MSG_HEADER_LEN) {
 		struct ems_cpc_msg *msg;
 		u8 *ibuf = urb->transfer_buffer;
-		u8 msg_count, start;
-
-		msg_count = ibuf[0] & ~0x80;
-
-		start = CPC_HEADER_SIZE;
-
-		while (msg_count) {
-			msg = (struct ems_cpc_msg *)&ibuf[start];
-
-			switch (msg->type) {
-			case CPC_MSG_TYPE_CAN_STATE:
-				/* Process CAN state changes */
-				ems_usb_rx_err(dev, msg);
-				break;
-
-			case CPC_MSG_TYPE_CAN_FRAME:
-			case CPC_MSG_TYPE_EXT_CAN_FRAME:
-			case CPC_MSG_TYPE_RTR_FRAME:
-			case CPC_MSG_TYPE_EXT_RTR_FRAME:
-				ems_usb_rx_can_msg(dev, msg);
-				break;
-
-			case CPC_MSG_TYPE_CAN_FRAME_ERROR:
-				/* Process errorframe */
-				ems_usb_rx_err(dev, msg);
-				break;
-
-			case CPC_MSG_TYPE_OVERRUN:
-				/* Message lost while receiving */
-				ems_usb_rx_err(dev, msg);
-				break;
-			}
-
-			start += CPC_MSG_HEADER_LEN + msg->length;
-			msg_count--;
-
-			if (start > urb->transfer_buffer_length) {
-				netdev_err(netdev, "format error\n");
-				break;
-			}
+		u32 read_count;
+
+		msg = (struct ems_cpc_msg *)&ibuf[start];
+
+		/* We should leave if length is smaller than
+		 * length indicated by payload
+		 */
+		if (length < CPC_MSG_HEADER_LEN + msg->length) {
+			netdev_err(netdev, "format error\n");
+			break;
+		}
+
+		switch (msg->type) {
+		case CPC_MSG_TYPE_CAN_STATE:
+			/* Process CAN state changes */
+			ems_usb_rx_err(dev, msg);
+			break;
+
+		case CPC_MSG_TYPE_CAN_FRAME:
+		case CPC_MSG_TYPE_EXT_CAN_FRAME:
+		case CPC_MSG_TYPE_RTR_FRAME:
+		case CPC_MSG_TYPE_EXT_RTR_FRAME:
+			ems_usb_rx_can_msg(dev, msg);
+			break;
+
+		case CPC_MSG_TYPE_CAN_FRAME_ERROR:
+			/* Process errorframe */
+			ems_usb_rx_err(dev, msg);
+			break;
+
+		case CPC_MSG_TYPE_OVERRUN:
+			/* Message lost while receiving */
+			ems_usb_rx_err(dev, msg);
+			break;
 		}
+
+		read_count = CPC_MSG_HEADER_LEN + msg->length;
+		start += read_count;
+
+		if (start > urb->transfer_buffer_length) {
+			netdev_err(netdev, "format error\n");
+			break;
+		}
+
+		if (read_count <= length)
+			length -= read_count;
 	}
 
 resubmit_urb:
-- 
2.26.2

-- 
EMS Dr. Thomas Wuensche e.K.
Sonnenhang 3
85304 Ilmmuenster
HR Ingolstadt, HRA 170106

Phone: +49-8441-490260
Fax  : +49-8441-81860
http://www.ems-wuensche.com

  parent reply	other threads:[~2020-11-12 18:04 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-12 18:03 [PATCH v2 00/16] Add support for CPC-USB/FD CAN FD interface Gerhard Uttenthaler
2020-11-12 18:03 ` [PATCH v2 01/16] can: ems_usb: Fixed warnings and comments Gerhard Uttenthaler
2020-11-12 18:03 ` [PATCH v2 02/16] can: ems_usb: Added CPC_ClearCmdQueue routine Gerhard Uttenthaler
2020-11-12 18:03 ` [PATCH v2 03/16] can: ems_usb: Fixed non C99 style initialization Gerhard Uttenthaler
2020-11-12 18:03 ` [PATCH v2 04/16] can: ems_usb: Added CAN FD message representation Gerhard Uttenthaler
2020-11-12 18:03 ` [PATCH v2 05/16] can: ems_usb: Added CAN FD initialization struct Gerhard Uttenthaler
2020-11-12 18:03 ` [PATCH v2 06/16] can: ems_usb: Made RX_BUFFER_SIZE, ems_usb_write_mode and ems_usb_probe device dependent Gerhard Uttenthaler
2020-11-12 18:03 ` [PATCH v2 07/16] can: ems_usb: Added listen only mode for CPC-USB/ARM7 Gerhard Uttenthaler
2020-11-12 18:03 ` Gerhard Uttenthaler [this message]
2020-11-12 18:03 ` [PATCH v2 09/16] can: ems_usb: Added CAN controller initialization for CAN FD Gerhard Uttenthaler
2020-11-12 18:03 ` [PATCH v2 10/16] can: ems_usb: Added receive routine for CAN FD messages Gerhard Uttenthaler
2020-11-12 18:03 ` [PATCH v2 11/16] can: ems_usb: Added ems_usb_write_mode_fd Gerhard Uttenthaler
2020-11-12 18:03 ` [PATCH v2 12/16] can: ems_usb: Fixed ems_usb_start_xmit for CAN FD Gerhard Uttenthaler
2020-11-12 18:03 ` [PATCH v2 13/16] can: ems_usb: Made CAN error reporting CAN controller dependent Gerhard Uttenthaler
2020-11-12 18:03 ` [PATCH v2 14/16] can: ems_usb: Made structs packed Gerhard Uttenthaler
2020-11-12 18:03 ` [PATCH v2 15/16] can: ems_usb: Added error reporting for CPC-USB/FD Gerhard Uttenthaler
2020-11-12 18:03 ` [PATCH v2 16/16] can: ems_usb: Enable CPC-USB/FD support Gerhard Uttenthaler
2020-11-26 17:49 ` [PATCH v2 00/16] Add support for CPC-USB/FD CAN FD interface Gerhard Uttenthaler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201112180346.29070-9-uttenthaler@ems-wuensche.com \
    --to=uttenthaler@ems-wuensche.com \
    --cc=linux-can@vger.kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=wg@grandegger.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.