linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Marc Kleine-Budde <mkl@pengutronix.de>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, kuba@kernel.org, linux-can@vger.kernel.org,
	kernel@pengutronix.de, Oliver Hartkopp <socketcan@hartkopp.net>,
	Marc Kleine-Budde <mkl@pengutronix.de>
Subject: [net-next 08/25] can: drivers: introduce helpers to access Classical CAN DLC values
Date: Fri, 20 Nov 2020 14:33:01 +0100	[thread overview]
Message-ID: <20201120133318.3428231-9-mkl@pengutronix.de> (raw)
In-Reply-To: <20201120133318.3428231-1-mkl@pengutronix.de>

From: Oliver Hartkopp <socketcan@hartkopp.net>

This patch adds the following helper to functions to access Classical CAN DLC
values.

can_get_cc_dlc(): get the data length code for Classical CAN raw DLC access
can_frame_set_cc_len(): set len and len8_dlc value for Classical CAN raw DLC access

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Link: https://lore.kernel.org/r/20201110154913.1404582-2-mkl@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 include/linux/can/dev.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/include/linux/can/dev.h b/include/linux/can/dev.h
index e767a96ae075..197a79535cc2 100644
--- a/include/linux/can/dev.h
+++ b/include/linux/can/dev.h
@@ -170,6 +170,31 @@ static inline bool can_is_canfd_skb(const struct sk_buff *skb)
 	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 struct can_frame *cf, const u32 ctrlmode)
+{
+	/* 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 set len and len8_dlc value for Classical CAN raw DLC access */
+static inline void can_frame_set_cc_len(struct can_frame *cf, const u8 dlc,
+					const u32 ctrlmode)
+{
+	/* 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 */
+	cf->len = can_cc_dlc2len(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)
-- 
2.29.2


  parent reply	other threads:[~2020-11-20 13:34 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-20 13:32 pull-request: can-next 2020-11-20 Marc Kleine-Budde
2020-11-20 13:32 ` [net-next 01/25] can: j1939: add tables for the CAN identifier and its fields Marc Kleine-Budde
2020-11-20 13:32 ` [net-next 02/25] can: add optional DLC element to Classical CAN frame structure Marc Kleine-Budde
2020-11-20 13:32 ` [net-next 03/25] can: rename get_can_dlc() macro with can_cc_dlc2len() Marc Kleine-Budde
2020-11-20 13:32 ` [net-next 04/25] can: remove obsolete get_canfd_dlc() macro Marc Kleine-Budde
2020-11-20 13:32 ` [net-next 05/25] can: replace can_dlc as variable/element for payload length Marc Kleine-Budde
2020-11-20 13:32 ` [net-next 06/25] can: rename CAN FD related can_len2dlc and can_dlc2len helpers Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 07/25] can: update documentation for DLC usage in Classical CAN Marc Kleine-Budde
2020-11-20 13:33 ` Marc Kleine-Budde [this message]
2020-11-20 13:33 ` [net-next 09/25] can: drivers: add len8_dlc support for various CAN adapters Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 10/25] can: drivers: add len8_dlc support for esd_usb2 CAN adapter Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 11/25] can: gw: support modification of Classical CAN DLCs Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 12/25] dt-bindings: can: fsl,flexcan: add uint32 reference to clock-frequency property Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 13/25] dt-bindings: can: fsl,flexcan: fix fsl,clk-source property Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 14/25] dt-bindings: firmware: add IMX_SC_R_CAN(x) macro for CAN Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 15/25] can: flexcan: rename macro FLEXCAN_QUIRK_SETUP_STOP_MODE -> FLEXCAN_QUIRK_SETUP_STOP_MODE_GPR Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 16/25] can: flexcan: factor out enabling and disabling of interrupts into separate function Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 17/25] can: flexcan: move enabling/disabling of interrupts from flexcan_chip_{start,stop}() to callers Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 18/25] can: flexcan: flexcan_rx_offload_setup(): factor out mailbox and rx-offload setup into separate function Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 19/25] can: flexcan: flexcan_open(): completely initialize controller before requesting IRQ Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 20/25] can: flexcan: flexcan_close(): change order if commands to properly shut down the controller Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 21/25] can: kvaser_usb: Add USB_{LEAF,HYDRA}_PRODUCT_ID_END defines Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 22/25] can: kvaser_usb: Add new Kvaser Leaf v2 devices Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 23/25] can: kvaser_usb: kvaser_usb_hydra: Add support for new device variant Marc Kleine-Budde
2020-11-20 13:33 ` [net-next 24/25] can: kvaser_usb: Add new Kvaser hydra devices Marc Kleine-Budde
2020-11-20 13:44 ` [PATCH 25/25] can: mcp251xfd: remove useless code in mcp251xfd_chip_softreset Marc Kleine-Budde
2020-11-21 23:09 ` pull-request: can-next 2020-11-20 Jakub Kicinski

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=20201120133318.3428231-9-mkl@pengutronix.de \
    --to=mkl@pengutronix.de \
    --cc=davem@davemloft.net \
    --cc=kernel@pengutronix.de \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=socketcan@hartkopp.net \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).