On 16.06.2021 22:53:02, Vincent MAILHOL wrote: > On Wed. 16 Jun 2021 at 18:46, Marc Kleine-Budde wrote: > > On 04.06.2021 00:15:50, Vincent Mailhol wrote: > > [...] > > > > > +static size_t can_tdc_get_size(const struct net_device *dev) > > > +{ > > > + struct can_priv *priv = netdev_priv(dev); > > > + size_t size; > > > + > > > + if (!priv->tdc_const) > > > + return 0; > > > + > > > + size = nla_total_size(0); /* nest IFLA_CAN_TDC */ > > > + size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCV_MAX */ > > > + size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCO_MAX */ > > > + size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCF_MAX */ > > > + > > > + if (priv->tdc.tdco) { > > > > Naively I'd say, iff the device has tdc_const give the user space the > > tdc parameters, regardless if some value is 0 or not. > > > > What do you think? > > I thought about that. > The first important remark is that if tdc.tdco is zero, then TDC > is off (c.f. documentation of struct can_tdc::tdco). > > Let me illustrate my vision through examples. [...] examples makes sense \o/ [...] > Finally, I have one side comment. It seems to me that you did not > understand that the intent of > | if (priv->tdc.tdco) > was to actually check whether TDC was on or off. In other words, my > code was unclear. > > I am now thinking to introduce an helper macro: > static bool can_tdc_is_enabled(const struct can_priv *priv) > |{ > | return !!priv->tdc.tdco; > |} > > The code would look more clear like that. > - if (priv->tdc.tdco) { > + if (can_tdc_is_enabled(priv) { Sounds good, I'm squashing this patch: | diff --git a/drivers/net/can/dev/netlink.c b/drivers/net/can/dev/netlink.c | index 6134bbf69c10..d48be574eae7 100644 | --- a/drivers/net/can/dev/netlink.c | +++ b/drivers/net/can/dev/netlink.c | @@ -311,7 +311,7 @@ static size_t can_tdc_get_size(const struct net_device *dev) | size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCO_MAX */ | size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCF_MAX */ | | - if (priv->tdc.tdco) { | + if (can_tdc_is_enabled(priv)) { | size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCV */ | size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCO */ | size += nla_total_size(sizeof(u32)); /* IFLA_CAN_TDCF */ | @@ -352,6 +352,7 @@ static size_t can_get_size(const struct net_device *dev) | priv->data_bitrate_const_cnt); | size += sizeof(priv->bitrate_max); /* IFLA_CAN_BITRATE_MAX */ | size += can_tdc_get_size(dev); /* IFLA_CAN_TDC */ | + | return size; | } | | @@ -374,7 +375,7 @@ static int can_tdc_fill_info(struct sk_buff *skb, const struct net_device *dev) | nla_put_u32(skb, IFLA_CAN_TDC_TDCF_MAX, tdc_const->tdcf_max)) | goto err_cancel; | | - if (priv->tdc.tdco) | + if (can_tdc_is_enabled(priv)) { | if (nla_put_u32(skb, IFLA_CAN_TDC_TDCV, tdc->tdcv) || | nla_put_u32(skb, IFLA_CAN_TDC_TDCO, tdc->tdco) || | nla_put_u32(skb, IFLA_CAN_TDC_TDCF, tdc->tdcf)) | diff --git a/include/linux/can/bittiming.h b/include/linux/can/bittiming.h | index 9de6e9053e34..b6d1db1e7258 100644 | --- a/include/linux/can/bittiming.h | +++ b/include/linux/can/bittiming.h | @@ -83,6 +83,11 @@ struct can_tdc_const { | u32 tdcf_max; | }; | | +static inline bool can_tdc_is_enabled(const struct can_priv *priv) | +{ | + return !!priv->tdc.tdco; | +} | + | #ifdef CONFIG_CAN_CALC_BITTIMING | int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, | const struct can_bittiming_const *btc); regards, Marc -- Pengutronix e.K. | Marc Kleine-Budde | Embedded Linux | https://www.pengutronix.de | Vertretung West/Dortmund | Phone: +49-231-2826-924 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |