All of lore.kernel.org
 help / color / mirror / Atom feed
From: Oliver Hartkopp <socketcan@hartkopp.net>
To: linux-can@vger.kernel.org, mkl@pengutronix.de,
	mailhol.vincent@wanadoo.fr
Cc: netdev@vger.kernel.org, Oliver Hartkopp <socketcan@hartkopp.net>
Subject: [PATCH v6 7/8] can-dev: introduce helpers to access Classical CAN DLC values
Date: Tue, 10 Nov 2020 11:18:51 +0100	[thread overview]
Message-ID: <20201110101852.1973-8-socketcan@hartkopp.net> (raw)
In-Reply-To: <20201110101852.1973-1-socketcan@hartkopp.net>

can_get_cc_dlc: get the data length code for Classical CAN raw DLC access
can_get_cc_len: get len and len8_dlc value for Classical CAN raw DLC access

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 include/linux/can/dev.h | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/include/linux/can/dev.h b/include/linux/can/dev.h
index e767a96ae075..9a787f73399e 100644
--- a/include/linux/can/dev.h
+++ b/include/linux/can/dev.h
@@ -168,10 +168,38 @@ static inline bool can_is_canfd_skb(const struct sk_buff *skb)
 {
 	/* the CAN specific type of skb is identified by its data length */
 	return skb->len == CANFD_MTU;
 }
 
+/* helper to get the data length code (DLC) for Classical CAN raw DLC access */
+static inline u8 can_get_cc_dlc(const u32 ctrlmode, const struct can_frame *cf)
+{
+	/* return len8_dlc as dlc value only if all conditions apply */
+	if ((ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC) &&
+	    (cf->len == CAN_MAX_DLEN) &&
+	    (cf->len8_dlc > CAN_MAX_DLEN && cf->len8_dlc <= CAN_MAX_RAW_DLC))
+		return cf->len8_dlc;
+
+	/* return the payload length as dlc value */
+	return cf->len;
+}
+
+/* helper to get len and len8_dlc value for Classical CAN raw DLC access */
+static inline u8 can_get_cc_len(const u32 ctrlmode, struct can_frame *cf,
+				u8 dlc)
+{
+	/* the caller already ensured that dlc is a value from 0 .. 15 */
+	if ((ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC) && (dlc > CAN_MAX_DLEN))
+		cf->len8_dlc = dlc;
+
+	/* limit the payload length 'len' to CAN_MAX_DLEN */
+	if (dlc > CAN_MAX_DLEN)
+		return CAN_MAX_DLEN;
+
+	return dlc;
+}
+
 /* helper to define static CAN controller features at device creation time */
 static inline void can_set_static_ctrlmode(struct net_device *dev,
 					   u32 static_mode)
 {
 	struct can_priv *priv = netdev_priv(dev);
-- 
2.28.0


  parent reply	other threads:[~2020-11-10 10:19 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-10 10:18 [PATCH v6 0/8] Introduce optional DLC element for Classic CAN Oliver Hartkopp
2020-11-10 10:18 ` [PATCH v6 1/8] can: add optional DLC element to Classical CAN frame structure Oliver Hartkopp
2020-11-10 10:18 ` [PATCH v6 2/8] can: rename get_can_dlc() macro with can_cc_dlc2len() Oliver Hartkopp
2020-11-10 10:18 ` [PATCH v6 3/8] can: remove obsolete get_canfd_dlc() macro Oliver Hartkopp
2020-11-10 10:18 ` [PATCH v6 4/8] can: replace can_dlc as variable/element for payload length Oliver Hartkopp
2020-11-20  9:49   ` broken on next-next - " Oliver Hartkopp
2020-11-10 10:18 ` [PATCH v6 5/8] can: rename CAN FD related can_len2dlc and can_dlc2len helpers Oliver Hartkopp
2020-11-10 10:18 ` [PATCH v6 6/8] can: update documentation for DLC usage in Classical CAN Oliver Hartkopp
2020-11-10 10:18 ` Oliver Hartkopp [this message]
2020-11-10 15:50   ` [PATCH v6 7/8] can-dev: introduce helpers to access Classical CAN DLC values Marc Kleine-Budde
2020-11-10 17:00     ` Oliver Hartkopp
2020-11-10 10:18 ` [PATCH v6 8/8] can-dev: add len8_dlc support for various CAN USB adapters Oliver Hartkopp

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=20201110101852.1973-8-socketcan@hartkopp.net \
    --to=socketcan@hartkopp.net \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol.vincent@wanadoo.fr \
    --cc=mkl@pengutronix.de \
    --cc=netdev@vger.kernel.org \
    /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.