drivers/net/can/dev/bittiming.c

Source file repositories/reference/linux-study-clean/drivers/net/can/dev/bittiming.c

File Facts

System
Linux kernel
Corpus path
drivers/net/can/dev/bittiming.c
Extension
.c
Size
6541 bytes
Lines
217
Domain
Driver Families
Bucket
drivers/net
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
 * Copyright (c) 2025 Vincent Mailhol <mailhol@kernel.org>
 */

#include <linux/can/dev.h>

void can_sjw_set_default(struct can_bittiming *bt)
{
	if (bt->sjw)
		return;

	/* If user space provides no sjw, use sane default of phase_seg2 / 2 */
	bt->sjw = max(1U, min(bt->phase_seg1, bt->phase_seg2 / 2));
}

int can_sjw_check(const struct net_device *dev, const struct can_bittiming *bt,
		  const struct can_bittiming_const *btc, struct netlink_ext_ack *extack)
{
	if (bt->sjw > btc->sjw_max) {
		NL_SET_ERR_MSG_FMT(extack, "sjw: %u greater than max sjw: %u",
				   bt->sjw, btc->sjw_max);
		return -EINVAL;
	}

	if (bt->sjw > bt->phase_seg1) {
		NL_SET_ERR_MSG_FMT(extack,
				   "sjw: %u greater than phase-seg1: %u",
				   bt->sjw, bt->phase_seg1);
		return -EINVAL;
	}

	if (bt->sjw > bt->phase_seg2) {
		NL_SET_ERR_MSG_FMT(extack,
				   "sjw: %u greater than phase-seg2: %u",
				   bt->sjw, bt->phase_seg2);
		return -EINVAL;
	}

	return 0;
}

/* Checks the validity of the specified bit-timing parameters prop_seg,
 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
 * prescaler value brp. You can find more information in the header
 * file linux/can/netlink.h.
 */
static int can_fixup_bittiming(const struct net_device *dev, struct can_bittiming *bt,
			       const struct can_bittiming_const *btc,
			       struct netlink_ext_ack *extack)
{
	const unsigned int tseg1 = bt->prop_seg + bt->phase_seg1;
	const struct can_priv *priv = netdev_priv(dev);
	u64 brp64;
	int err;

	if (tseg1 < btc->tseg1_min) {
		NL_SET_ERR_MSG_FMT(extack, "prop-seg + phase-seg1: %u less than tseg1-min: %u",
				   tseg1, btc->tseg1_min);
		return -EINVAL;
	}
	if (tseg1 > btc->tseg1_max) {
		NL_SET_ERR_MSG_FMT(extack, "prop-seg + phase-seg1: %u greater than tseg1-max: %u",
				   tseg1, btc->tseg1_max);
		return -EINVAL;
	}
	if (bt->phase_seg2 < btc->tseg2_min) {
		NL_SET_ERR_MSG_FMT(extack, "phase-seg2: %u less than tseg2-min: %u",
				   bt->phase_seg2, btc->tseg2_min);
		return -EINVAL;
	}
	if (bt->phase_seg2 > btc->tseg2_max) {
		NL_SET_ERR_MSG_FMT(extack, "phase-seg2: %u greater than tseg2-max: %u",
				   bt->phase_seg2, btc->tseg2_max);
		return -EINVAL;
	}

	can_sjw_set_default(bt);

	err = can_sjw_check(dev, bt, btc, extack);
	if (err)
		return err;

	brp64 = (u64)priv->clock.freq * (u64)bt->tq;
	if (btc->brp_inc > 1)
		do_div(brp64, btc->brp_inc);
	brp64 += 500000000UL - 1;
	do_div(brp64, 1000000000UL); /* the practicable BRP */

Annotation

Implementation Notes