Skip to contentSkip to conversation

dummyxl: update config possibilities and rename dummyxl_priv

PATCH In review Oliver Hartkopp 3 participants 5 messages Jul 6, 2025 – Jul 15, 2025 CAN network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 dummyxl: update config possibilities and rename dummyxl_priv

---
 drivers/net/can/dummyxl.c | 55 +++++++++++++++++++++++++--------------
 1 file changed, 35 insertions(+), 20 deletions(-)
drivers/net/can/dummyxl.c +35 -20
index ac1b579b4c1f..c61f27b93e9f 100644
@@ -10,16 +10,17 @@
 #include <linux/units.h>
 #include <linux/can.h>
 #include <linux/can/dev.h>
 #include <linux/can/skb.h>
 
-struct dummyxl {
+struct dummyxl_priv {
 	struct can_priv can;
 	struct net_device *dev;
 };
 
-static struct dummyxl *dummyxl;
+/* pointer to our single dummy device (for removal) */
+static struct net_device *dummyxl_dev = NULL;
 
 static const struct can_bittiming_const dummyxl_bittiming_const = {
 	.name = "dummyxl nominal",
 	.tseg1_min = 2,
 	.tseg1_max = 256,
@@ -71,11 +72,12 @@ static const struct can_tdc_const dummyxl_xl_tdc_const = {
 	.tdco_max = 127,
 	.tdcf_min = 0,
 	.tdcf_max = 127
 };
 
-static void dummyxl_print_bittiming(struct net_device *dev, struct can_bittiming *bt)
+static void dummyxl_print_bittiming(struct net_device *dev,
+				    struct can_bittiming *bt)
 {
 	netdev_info(dev, "\tbitrate: %u\n", bt->bitrate);
 	netdev_info(dev, "\tsample_point: %u\n", bt->sample_point);
 	netdev_info(dev, "\ttq: %u\n", bt->tq);
 	netdev_info(dev, "\tprop_seg: %u\n", bt->prop_seg);
@@ -90,15 +92,14 @@ static void dummyxl_print_tdc(struct net_device *dev, struct can_tdc *tdc)
 	netdev_info(dev, "\t\ttdcv: %u\n", tdc->tdcv);
 	netdev_info(dev, "\t\ttdco: %u\n", tdc->tdco);
 	netdev_info(dev, "\t\ttdcf: %u\n", tdc->tdcf);
 }
 
-static int dummyxl_netdev_open(struct net_device *dev)
+static void dummyxl_print_config(struct net_device *dev)
 {
-	struct dummyxl *priv = netdev_priv(dev);
+	struct dummyxl_priv *priv = netdev_priv(dev);
 	struct can_priv *can_priv = &priv->can;
-	int ret;
 
 	netdev_info(dev, "CAN control mode/supported : %08X/%08X\n",
 		    can_priv->ctrlmode, can_priv->ctrlmode_supported);
 	netdev_info(dev, "CAN CC nominal bittiming:\n");
 	dummyxl_print_bittiming(dev, &can_priv->bittiming);
@@ -129,10 +130,17 @@ static int dummyxl_netdev_open(struct net_device *dev)
 		}
 	} else {
 		netdev_info(dev, "CAN XL is off\n");
 	}
 	netdev_info(dev, "\n");
+}
+
+static int dummyxl_netdev_open(struct net_device *dev)
+{
+	int ret;
+
+	dummyxl_print_config(dev);
 
 	ret = open_candev(dev);
 	if (ret)
 		return ret;
 	netif_start_queue(dev);
@@ -174,50 +182,57 @@ static const struct net_device_ops dummyxl_netdev_ops = {
 };
 
 static int __init dummyxl_init(void)
 {
 	struct net_device *dev;
-	struct dummyxl *priv;
+	struct dummyxl_priv *priv;
 	int ret;
 
-	dev = alloc_candev(sizeof(struct dummyxl), 0);
+	dev = alloc_candev(sizeof(struct dummyxl_priv), 0);
 	if (!dev)
 		return -ENOMEM;
 
 	dev->netdev_ops = &dummyxl_netdev_ops;
 	priv = netdev_priv(dev);
 	priv->can.bittiming_const = &dummyxl_bittiming_const;
-	priv->can.bitrate_max = 8 * MEGA /* BPS */;
-	priv->can.clock.freq = 80 * MEGA /* Hz */;
+	priv->can.bitrate_max = 20 * MEGA /* BPS */;
+	priv->can.clock.freq = 160 * MEGA /* Hz */;
 	priv->can.fd.data_bittiming_const = &dummyxl_fd_databittiming_const;
 	priv->can.fd.tdc_const = &dummyxl_fd_tdc_const;
 	priv->can.xl.data_bittiming_const = &dummyxl_xl_databittiming_const;
 	priv->can.xl.tdc_const = &dummyxl_xl_tdc_const;
-	priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY |
-		CAN_CTRLMODE_FD | CAN_CTRLMODE_TDC_AUTO |
-		CAN_CTRLMODE_XL | CAN_CTRLMODE_XL_TDC_AUTO;
+	priv->can.ctrlmode_supported =
+		CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_CC_LEN8_DLC |
+		CAN_CTRLMODE_FD |
+		CAN_CTRLMODE_TDC_AUTO | CAN_CTRLMODE_TDC_MANUAL |
+		CAN_CTRLMODE_XL |
+		CAN_CTRLMODE_XL_TDC_AUTO | CAN_CTRLMODE_XL_TDC_MANUAL |
+		CAN_CTRLMODE_XL_RRS | CAN_CTRLMODE_XL_TRX |
+		CAN_CTRLMODE_XL_ERR_SIGNAL;
 	priv->dev = dev;
 
-	ret = register_candev(priv->dev);
+	ret = register_candev(dev);
 	if (ret) {
-		free_candev(priv->dev);
+		free_candev(dev);
 		return ret;
 	}
 
-	dummyxl = priv;
+	dummyxl_dev = dev;
 	netdev_info(dev, "dummyxl OK\n");
 
 	return 0;
 }
 
 static void __exit dummyxl_exit(void)
 {
-	struct net_device *dev = dummyxl->dev;
+	struct net_device *dev = dummyxl_dev;
 
-	netdev_info(dev, "dummyxl bye bye\n");
-	unregister_candev(dev);
-	free_candev(dev);
+	if (dev) {
+		netdev_info(dev, "dummyxl bye bye\n");
+		unregister_candev(dev);
+		free_candev(dev);
+	}
 }
 
 module_init(dummyxl_init);
 module_exit(dummyxl_exit);
 
Signature · 1 line
2.47.2
#1 lore raw

PATCH 2/2 dummyxl: print ctrlmode and PWM values

---
 drivers/net/can/dummyxl.c | 92 +++++++++++++++++++++++++++++++++++----
 1 file changed, 83 insertions(+), 9 deletions(-)
drivers/net/can/dummyxl.c +83 -9
index c61f27b93e9f..5178fb7c10c4 100644
@@ -76,11 +76,11 @@ static const struct can_tdc_const dummyxl_xl_tdc_const = {
 
 static void dummyxl_print_bittiming(struct net_device *dev,
 				    struct can_bittiming *bt)
 {
 	netdev_info(dev, "\tbitrate: %u\n", bt->bitrate);
-	netdev_info(dev, "\tsample_point: %u\n", bt->sample_point);
+	netdev_info(dev, "\tsample_point: 0.%u\n", bt->sample_point);
 	netdev_info(dev, "\ttq: %u\n", bt->tq);
 	netdev_info(dev, "\tprop_seg: %u\n", bt->prop_seg);
 	netdev_info(dev, "\tphase_seg1: %u\n", bt->phase_seg1);
 	netdev_info(dev, "\tphase_seg2: %u\n", bt->phase_seg2);
 	netdev_info(dev, "\tsjw: %u\n", bt->sjw);
@@ -92,48 +92,122 @@ static void dummyxl_print_tdc(struct net_device *dev, struct can_tdc *tdc)
 	netdev_info(dev, "\t\ttdcv: %u\n", tdc->tdcv);
 	netdev_info(dev, "\t\ttdco: %u\n", tdc->tdco);
 	netdev_info(dev, "\t\ttdcf: %u\n", tdc->tdcf);
 }
 
-static void dummyxl_print_config(struct net_device *dev)
+static void dummyxl_print_ctrlmode(struct net_device *dev)
 {
 	struct dummyxl_priv *priv = netdev_priv(dev);
 	struct can_priv *can_priv = &priv->can;
+	char opts[300] = { 0 };
 
 	netdev_info(dev, "CAN control mode/supported : %08X/%08X\n",
 		    can_priv->ctrlmode, can_priv->ctrlmode_supported);
+
+	if (can_priv->ctrlmode & CAN_CTRLMODE_LOOPBACK)
+		strcat(opts, " loopback");
+
+	if (can_priv->ctrlmode & CAN_CTRLMODE_LISTENONLY)
+		strcat(opts, " listen-only");
+
+	if (can_priv->ctrlmode & CAN_CTRLMODE_3_SAMPLES)
+		strcat(opts, " triple-sampling");
+
+	if (can_priv->ctrlmode & CAN_CTRLMODE_ONE_SHOT)
+		strcat(opts, " one-shot");
+
+	if (can_priv->ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
+		strcat(opts, " berr-reporting");
+
+	if (can_priv->ctrlmode & CAN_CTRLMODE_FD)
+		strcat(opts, " fd");
+
+	if (can_priv->ctrlmode & CAN_CTRLMODE_PRESUME_ACK)
+		strcat(opts, " presume-ack");
+
+	if (can_priv->ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
+		strcat(opts, " fd-non-iso");
+
+	if (can_priv->ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC)
+		strcat(opts, " cc-len8-dlc");
+
+	if (can_priv->ctrlmode & CAN_CTRLMODE_TDC_AUTO)
+		strcat(opts, " tdc-mode-auto");
+	else if (can_priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL)
+		strcat(opts, " tdc-mode-manual");
+	else
+		strcat(opts, " tdc-mode-off");
+
+	if (can_priv->ctrlmode & CAN_CTRLMODE_XL)
+		strcat(opts, " xl");
+
+	if (can_priv->ctrlmode & CAN_CTRLMODE_XL_TDC_AUTO)
+		strcat(opts, " xltdc-mode-auto");
+	else if (can_priv->ctrlmode & CAN_CTRLMODE_XL_TDC_MANUAL)
+		strcat(opts, " xltdc-mode-manual");
+	else
+		strcat(opts, " xltdc-mode-off");
+
+	if (can_priv->ctrlmode & CAN_CTRLMODE_XL_RRS)
+		strcat(opts, " xlrrs");
+
+	if (can_priv->ctrlmode & CAN_CTRLMODE_XL_TRX)
+		strcat(opts, " xltrx");
+
+	if (can_priv->ctrlmode & CAN_CTRLMODE_XL_ERR_SIGNAL)
+		strcat(opts, " xlerrsig");
+
+	netdev_info(dev, "Enabled modes:%s\n", opts);
+	netdev_info(dev, " \n");
+}
+
+static void dummyxl_print_config(struct net_device *dev)
+{
+	struct dummyxl_priv *priv = netdev_priv(dev);
+	struct can_priv *can_priv = &priv->can;
+
+	netdev_info(dev, " \n");
+	dummyxl_print_ctrlmode(dev);
+
+	netdev_info(dev, "CAN clock:       %9u Hz\n", priv->can.clock.freq);
+	netdev_info(dev, "CAN max bitrate: %9u BPS\n", priv->can.bitrate_max);
+	netdev_info(dev, " \n");
+
 	netdev_info(dev, "CAN CC nominal bittiming:\n");
 	dummyxl_print_bittiming(dev, &can_priv->bittiming);
-	netdev_info(dev, "\n");
+	netdev_info(dev, " \n");
 
 	if (can_priv->ctrlmode & CAN_CTRLMODE_FD) {
 		netdev_info(dev, "CAN FD databittiming:\n");
 		dummyxl_print_bittiming(dev, &can_priv->fd.data_bittiming);
 		if (can_fd_tdc_is_enabled(can_priv)) {
 			netdev_info(dev, "\tCAN FD TDC:\n");
 			dummyxl_print_tdc(dev, &can_priv->fd.tdc);
 		} else {
 			netdev_info(dev, "\tCAN FD TDC is off\n");
 		}
-	} else {
-		netdev_info(dev, "CAN FD is off\n");
+		netdev_info(dev, " \n");
 	}
-	netdev_info(dev, "\n");
 
 	if (can_priv->ctrlmode & CAN_CTRLMODE_XL) {
 		netdev_info(dev, "CAN XL databittiming:\n");
 		dummyxl_print_bittiming(dev, &can_priv->xl.data_bittiming);
 		if (can_xl_tdc_is_enabled(can_priv)) {
 			netdev_info(dev, "\tCAN XL TDC:\n");
 			dummyxl_print_tdc(dev, &can_priv->xl.tdc);
 		} else {
 			netdev_info(dev, "\tCAN XL TDC is off\n");
 		}
-	} else {
-		netdev_info(dev, "CAN XL is off\n");
+		if (can_priv->ctrlmode & CAN_CTRLMODE_XL_TRX) {
+			struct can_pwm *pwm = &can_priv->pwm;
+			netdev_info(dev, "\tCAN XL PWM:\n");
+			netdev_info(dev, "\t\tpwmo: %u\n", pwm->pwm_offset);
+			netdev_info(dev, "\t\tpwml: %u\n", pwm->pwm_phase_long);
+			netdev_info(dev, "\t\tpwms: %u\n", pwm->pwm_phase_short);
+		}
+		netdev_info(dev, " \n");
 	}
-	netdev_info(dev, "\n");
 }
 
 static int dummyxl_netdev_open(struct net_device *dev)
 {
 	int ret;
Signature · 1 line
2.47.2
#2 lore raw

Re: [PATCH can-next/b4 2/2] dummyxl: print ctrlmode and PWM values

Quoted 1 line · Oliver Hartkopp

(...)

Quoted 52 lines
drivers/net/can/dummyxl.c +45 -1
-static void dummyxl_print_config(struct net_device *dev)
+static void dummyxl_print_ctrlmode(struct net_device *dev)
 {
        struct dummyxl_priv *priv = netdev_priv(dev);
        struct can_priv *can_priv = &priv->can;
+       char opts[300] = { 0 };
 
        netdev_info(dev, "CAN control mode/supported : %08X/%08X\n",
                    can_priv->ctrlmode, can_priv->ctrlmode_supported);
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_LOOPBACK)
+               strcat(opts, " loopback");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_LISTENONLY)
+               strcat(opts, " listen-only");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_3_SAMPLES)
+               strcat(opts, " triple-sampling");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_ONE_SHOT)
+               strcat(opts, " one-shot");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
+               strcat(opts, " berr-reporting");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_FD)
+               strcat(opts, " fd");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_PRESUME_ACK)
+               strcat(opts, " presume-ack");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
+               strcat(opts, " fd-non-iso");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC)
+               strcat(opts, " cc-len8-dlc");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_TDC_AUTO)
+               strcat(opts, " tdc-mode-auto");
+       else if (can_priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL)
+               strcat(opts, " tdc-mode-manual");
+       else
+               strcat(opts, " tdc-mode-off");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_XL)
+               strcat(opts, " xl");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_XL_TDC_AUTO)
+               strcat(opts, " xltdc-mode-auto");
+       else if (can_priv->ctrlmode & CAN_CTRLMODE_XL_TDC_MANUAL)
+               strcat(opts, " xltdc-mode-manual");
+       else

drivers/net/can/dummyxl.c static void dummyxl_print_tdc(struct net_device *dev, struct can_tdc *tdc) · line 157

Quoted 10 lines
drivers/net/can/dummyxl.c +10 -0
+               strcat(opts, " xltdc-mode-off");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_XL_RRS)
+               strcat(opts, " xlrrs");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_XL_TRX)
+               strcat(opts, " xltrx");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_XL_ERR_SIGNAL)
+               strcat(opts, " xlerrsig");

I am not a big fan of having a fixed length buffer and then doing several strcat() on it. I rewrote it as below:

  static void dummyxl_print_ctrlmode(struct net_device *dev)
  {
          static const char *names[] = {
                  [ilog2(CAN_CTRLMODE_LOOPBACK)] = "loopback",
                  [ilog2(CAN_CTRLMODE_LISTENONLY)] = "listen-only",
                  [ilog2(CAN_CTRLMODE_3_SAMPLES)] = "triple-sampling",
                  [ilog2(CAN_CTRLMODE_ONE_SHOT)] = "one-shot",
                  [ilog2(CAN_CTRLMODE_BERR_REPORTING)] = "berr-reporting",
                  [ilog2(CAN_CTRLMODE_FD)] = "fd",
                  [ilog2(CAN_CTRLMODE_PRESUME_ACK)] = "presume-ack",
                  [ilog2(CAN_CTRLMODE_FD_NON_ISO)] = "fd-non-iso",
                  [ilog2(CAN_CTRLMODE_CC_LEN8_DLC)] = "cc-len8-dlc",
                  [ilog2(CAN_CTRLMODE_TDC_AUTO)] = "fd-tdc-auto",
                  [ilog2(CAN_CTRLMODE_TDC_MANUAL)] = "fd-tdc-manual",
                  [ilog2(CAN_CTRLMODE_XL)] = "xl",
                  [ilog2(CAN_CTRLMODE_XL_TDC_AUTO)] = "xl-tdc-auto",
                  [ilog2(CAN_CTRLMODE_XL_TDC_MANUAL)] = "xl-tdc-manual",
                  [ilog2(CAN_CTRLMODE_XL_RRS)] = "xl-rrs",
                  [ilog2(CAN_CTRLMODE_XL_TRX)] = "xl-trx",
                  [ilog2(CAN_CTRLMODE_XL_ERR_SIGNAL)] = "xl-err-signal",
                  [ilog2(CAN_CTRLMODE_XL_PWM)] = "pmw",
          };
          struct dummyxl *priv = netdev_priv(dev);
          unsigned long ctrlmode = priv->can.ctrlmode;
          netdev_info(dev, "Control modes: 0x%08lx\n", ctrlmode);

          if (ctrlmode) {
                  u32 idx;

                  netdev_info(dev, "\tlist:");
                  for_each_set_bit(idx, &ctrlmode, ARRAY_SIZE(names))
                          netdev_info(dev, "\t\t%s\n", names[idx]);

                  if (ctrlmode >= BIT(ARRAY_SIZE(names)))
                          netdev_info(dev, "\tunknown: 0x%08lx\n",
                                      ctrlmode &
                                      ~(BIT(ARRAY_SIZE(names)) - 1));
          }

}

It is a bit more concise and more scalable (if you forgot to update the table, you still get the hexadecimal value listed under "unknown").

drivers/net/can/dummyxl.c static void dummyxl_print_tdc(struct net_device *dev, struct can_tdc *tdc) · line 161

Quoted 3 lines
drivers/net/can/dummyxl.c +3 -0
+       netdev_info(dev, "Enabled modes:%s\n", opts);
+       netdev_info(dev, " \n");
+}

I am moving forward with the PWM. Contrary to the last month which was extra busy, I finally managed to liberate some time. I still need some debugging here and there, I will keep you posted :)

Yours sincerely,
Vincent Mailhol

#3 lore raw

Re: [PATCH can-next/b4 2/2] dummyxl: print ctrlmode and PWM values

Quoted 62 lines · Vincent Mailhol
Quoted 1 line · Oliver Hartkopp

(...)

Quoted 57 lines
drivers/net/can/dummyxl.c +50 -1
-static void dummyxl_print_config(struct net_device *dev)
+static void dummyxl_print_ctrlmode(struct net_device *dev)
 {
        struct dummyxl_priv *priv = netdev_priv(dev);
        struct can_priv *can_priv = &priv->can;
+       char opts[300] = { 0 };
 
        netdev_info(dev, "CAN control mode/supported : %08X/%08X\n",
                    can_priv->ctrlmode, can_priv->ctrlmode_supported);
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_LOOPBACK)
+               strcat(opts, " loopback");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_LISTENONLY)
+               strcat(opts, " listen-only");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_3_SAMPLES)
+               strcat(opts, " triple-sampling");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_ONE_SHOT)
+               strcat(opts, " one-shot");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
+               strcat(opts, " berr-reporting");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_FD)
+               strcat(opts, " fd");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_PRESUME_ACK)
+               strcat(opts, " presume-ack");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
+               strcat(opts, " fd-non-iso");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC)
+               strcat(opts, " cc-len8-dlc");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_TDC_AUTO)
+               strcat(opts, " tdc-mode-auto");
+       else if (can_priv->ctrlmode & CAN_CTRLMODE_TDC_MANUAL)
+               strcat(opts, " tdc-mode-manual");
+       else
+               strcat(opts, " tdc-mode-off");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_XL)
+               strcat(opts, " xl");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_XL_TDC_AUTO)
+               strcat(opts, " xltdc-mode-auto");
+       else if (can_priv->ctrlmode & CAN_CTRLMODE_XL_TDC_MANUAL)
+               strcat(opts, " xltdc-mode-manual");
+       else
+               strcat(opts, " xltdc-mode-off");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_XL_RRS)
+               strcat(opts, " xlrrs");
+
Quoted 8 lines

drivers/net/can/dummyxl.c static void dummyxl_print_tdc(struct net_device *dev, struct can_tdc *tdc) · line 157

Quoted 5 lines
drivers/net/can/dummyxl.c +5 -0
+       if (can_priv->ctrlmode & CAN_CTRLMODE_XL_TRX)
+               strcat(opts, " xltrx");
+
+       if (can_priv->ctrlmode & CAN_CTRLMODE_XL_ERR_SIGNAL)
+               strcat(opts, " xlerrsig");

I am not a big fan of having a fixed length buffer and then doing several strcat() on it.

+1

Quoted 16 lines

I rewrote it as below:

  static void dummyxl_print_ctrlmode(struct net_device *dev)
  {
          static const char *names[] = {
                  [ilog2(CAN_CTRLMODE_LOOPBACK)] = "loopback",
                  [ilog2(CAN_CTRLMODE_LISTENONLY)] = "listen-only",
                  [ilog2(CAN_CTRLMODE_3_SAMPLES)] = "triple-sampling",
                  [ilog2(CAN_CTRLMODE_ONE_SHOT)] = "one-shot",
                  [ilog2(CAN_CTRLMODE_BERR_REPORTING)] = "berr-reporting",
                  [ilog2(CAN_CTRLMODE_FD)] = "fd",
                  [ilog2(CAN_CTRLMODE_PRESUME_ACK)] = "presume-ack",
                  [ilog2(CAN_CTRLMODE_FD_NON_ISO)] = "fd-non-iso",
                  [ilog2(CAN_CTRLMODE_CC_LEN8_DLC)] = "cc-len8-dlc",
                  [ilog2(CAN_CTRLMODE_TDC_AUTO)] = "fd-tdc-auto",
                  [ilog2(CAN_CTRLMODE_TDC_MANUAL)] = "fd-tdc-manual",
Quoted 8 lines
                  [ilog2(CAN_CTRLMODE_XL)] = "xl",
                  [ilog2(CAN_CTRLMODE_XL_TDC_AUTO)] = "xl-tdc-auto",
                  [ilog2(CAN_CTRLMODE_XL_TDC_MANUAL)] = "xl-tdc-manual",
                  [ilog2(CAN_CTRLMODE_XL_RRS)] = "xl-rrs",
                  [ilog2(CAN_CTRLMODE_XL_TRX)] = "xl-trx",
                  [ilog2(CAN_CTRLMODE_XL_ERR_SIGNAL)] = "xl-err-signal",
                  [ilog2(CAN_CTRLMODE_XL_PWM)] = "pmw",
          };

Is the compiler clever enough, or do you have to use const_ilog2()?

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
#4 lore raw

Re: [PATCH can-next/b4 2/2] dummyxl: print ctrlmode and PWM values

Quoted 2 lines · Marc Kleine-Budde
Quoted 1 line · Vincent Mailhol

On Sun. 6 juil. 2025 at 22:36, Oliver Hartkopp <[email protected]> wrote:

(...)

Quoted 25 lines
Quoted 16 lines

I rewrote it as below:

  static void dummyxl_print_ctrlmode(struct net_device *dev)
  {
          static const char *names[] = {
                  [ilog2(CAN_CTRLMODE_LOOPBACK)] = "loopback",
                  [ilog2(CAN_CTRLMODE_LISTENONLY)] = "listen-only",
                  [ilog2(CAN_CTRLMODE_3_SAMPLES)] = "triple-sampling",
                  [ilog2(CAN_CTRLMODE_ONE_SHOT)] = "one-shot",
                  [ilog2(CAN_CTRLMODE_BERR_REPORTING)] = "berr-reporting",
                  [ilog2(CAN_CTRLMODE_FD)] = "fd",
                  [ilog2(CAN_CTRLMODE_PRESUME_ACK)] = "presume-ack",
                  [ilog2(CAN_CTRLMODE_FD_NON_ISO)] = "fd-non-iso",
                  [ilog2(CAN_CTRLMODE_CC_LEN8_DLC)] = "cc-len8-dlc",
                  [ilog2(CAN_CTRLMODE_TDC_AUTO)] = "fd-tdc-auto",
                  [ilog2(CAN_CTRLMODE_TDC_MANUAL)] = "fd-tdc-manual",
Quoted 8 lines
                  [ilog2(CAN_CTRLMODE_XL)] = "xl",
                  [ilog2(CAN_CTRLMODE_XL_TDC_AUTO)] = "xl-tdc-auto",
                  [ilog2(CAN_CTRLMODE_XL_TDC_MANUAL)] = "xl-tdc-manual",
                  [ilog2(CAN_CTRLMODE_XL_RRS)] = "xl-rrs",
                  [ilog2(CAN_CTRLMODE_XL_TRX)] = "xl-trx",
                  [ilog2(CAN_CTRLMODE_XL_ERR_SIGNAL)] = "xl-err-signal",
                  [ilog2(CAN_CTRLMODE_XL_PWM)] = "pmw",
          };
Quoted 1 line

Is the compiler clever enough, or do you have to use const_ilog2()?

Yes, the code above compiles and runs without issue. ilog2() expands into const_ilog2() if its argument is an integer constant expression (which is the case here).

Also, note that the [ilog2(CAN_CTRLMODE_XXXXX)] thing is optional. Doing:

  static const char *names[] = {
  	"loopback",
  	"listen-only",
  	"triple-sampling",
  	"one-shot",
  	"berr-reporting",
  	"fd",
  	"presume-ack",
  	"fd-non-iso",
  	"cc-len8-dlc",
  	"fd-tdc-auto",
  	"fd-tdc-manual",
  	"xl",
  	"xl-tdc-auto",
  	"xl-tdc-manual",
  	"xl-rrs",
  	"xl-trx",
  	"xl-err-signal",
  	"pmw",
  };

also works. But the above is more prone to silly mistakes. The ilog2() trick prevents any index mismatch.

Yours sincerely,
Vincent Mailhol