can: kvaser_usb: CAN clock frequency regression
Subsystems (2)
v2 cover can: kvaser_usb: CAN clock frequency regression
When fixing the CAN clock frequency,fb12797ab1fe ("can: kvaser_usb: get CAN clock frequency from device"), I introduced a regression.
For Leaf devices based on M32C, the firmware expects bittiming parameters calculated for 16MHz clock. Regardless of the actual clock frequency.
This regression affects M32C based Leaf devices with non-16MHz clock.
Also correct the bittiming constants in kvaser_usb_leaf.c, where the limits are different depending on which firmware/device being used.
Once merged to mainline, I'll backport these fixes for the stable kernels.
Changes in v2:
- Add struct kvaser_usb_driver_info as suggested by Marc Kleine-Budde [1],
instead of adding dev->card_data.quirks.
[1] https://lore.kernel.org/linux-can/[email protected]/T/#maf9cd66c368ce43eb509a27c69d0323cfc776809
Jimmy Assarsson (3):
can: kvaser_usb: Replace run-time checks with struct
kvaser_usb_driver_info
can: kvaser_usb: kvaser_usb_leaf: Fix CAN clock frequency regression
can: kvaser_usb: kvaser_usb_leaf: Fix bittiming limits
drivers/net/can/usb/kvaser_usb/kvaser_usb.h | 73 ++++- .../net/can/usb/kvaser_usb/kvaser_usb_core.c | 253 +++++++++--------- .../net/can/usb/kvaser_usb/kvaser_usb_hydra.c | 14 +- .../net/can/usb/kvaser_usb/kvaser_usb_leaf.c | 119 ++++---- 4 files changed, 255 insertions(+), 204 deletions(-)
Signature · 1 line
2.36.1
PATCH v2 1/3 can: kvaser_usb: Replace run-time checks with struct kvaser_usb_driver_info
Unify and move compile-time known information into new struct kvaser_usb_driver_info, in favour of run-time checks.
All Kvaser USBcanII supports listen-only mode and error counter reporting.
---
Changes in v2:
- New patch
Note: I got a patch for reading capabilities from Leaf devices,
that will remove kvaser_usb_driver_info_leaf_{err,err_listen}.
drivers/net/can/usb/kvaser_usb/kvaser_usb.h | 52 +++- .../net/can/usb/kvaser_usb/kvaser_usb_core.c | 249 +++++++++--------- .../net/can/usb/kvaser_usb/kvaser_usb_leaf.c | 24 +- 3 files changed, 174 insertions(+), 151 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb.h +42 -10
@@ -35,9 +35,9 @@#define KVASER_USB_RX_BUFFER_SIZE 3072#define KVASER_USB_MAX_NET_DEVICES 5 -/* USB devices features */ -#define KVASER_USB_HAS_SILENT_MODE BIT(0) -#define KVASER_USB_HAS_TXRX_ERRORS BIT(1) +/* Kvaser USB device quirks */ +#define KVASER_USB_QUIRK_HAS_SILENT_MODE BIT(0) +#define KVASER_USB_QUIRK_HAS_TXRX_ERRORS BIT(1)/* Device capabilities */ #define KVASER_USB_CAP_BERR_CAP 0x01
@@ -65,12 +65,7 @@ struct kvaser_usb_dev_card_data_hydra {struct kvaser_usb_dev_card_data {u32 ctrlmode_supported;u32 capabilities; -union { -struct { -enum kvaser_usb_leaf_family family; - } leaf; -struct kvaser_usb_dev_card_data_hydra hydra; - }; +struct kvaser_usb_dev_card_data_hydra hydra; };/* Context for an outstanding, not yet ACKed, transmission */
@@ -83,7 +78,7 @@ struct kvaser_usb {struct usb_device *udev;struct usb_interface *intf;struct kvaser_usb_net_priv *nets[KVASER_USB_MAX_NET_DEVICES ]; -const struct kvaser_usb_dev_ops *ops; +const struct kvaser_usb_driver_info *driver_info;const struct kvaser_usb_dev_cfg *cfg;struct usb_endpoint_descriptor *bulk_in, *bulk_out;
@@ -165,6 +160,12 @@ struct kvaser_usb_dev_ops { u16 transid); }; +struct kvaser_usb_driver_info { +u32 quirks; +enum kvaser_usb_leaf_family family; +const struct kvaser_usb_dev_ops *ops; +}; +struct kvaser_usb_dev_cfg {const struct can_clock clock;const unsigned int timestamp_freq;
@@ -184,4 +185,35 @@ int kvaser_usb_send_cmd_async(struct kvaser_usb_net_priv *priv, void *cmd, int len);int kvaser_usb_can_rx_over_error (struct net_device *netdev); + +static const struct kvaser_usb_driver_info kvaser_usb_driver_info_hydra = { + .quirks =0 , + .ops = &kvaser_usb_hydra_dev_ops, +}; + +static const struct kvaser_usb_driver_info kvaser_usb_driver_info_usbcan = { + .quirks =KVASER_USB_QUIRK_HAS_TXRX_ERRORS | +KVASER_USB_QUIRK_HAS_SILENT_MODE , + .family =KVASER_USBCAN , + .ops = &kvaser_usb_leaf_dev_ops, +}; + +static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf = { + .quirks =0 , + .family =KVASER_LEAF , + .ops = &kvaser_usb_leaf_dev_ops, +}; + +static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf_err = { + .quirks =KVASER_USB_QUIRK_HAS_TXRX_ERRORS , + .family =KVASER_LEAF , + .ops = &kvaser_usb_leaf_dev_ops, +}; + +static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf_err_listen = { + .quirks =KVASER_USB_QUIRK_HAS_TXRX_ERRORS | +KVASER_USB_QUIRK_HAS_SILENT_MODE , + .family =KVASER_LEAF , + .ops = &kvaser_usb_leaf_dev_ops, +};#endif /* KVASER_USB_H */
drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c +120 -129
418 lines not on this page · raw message
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +12 -12
@@ -404,7 +404,7 @@ kvaser_usb_leaf_frame_to_cmd(const struct kvaser_usb_net_priv *priv,sizeof (struct kvaser_cmd_tx_can ); cmd->u.tx_can.channel = priv->channel; -switch (dev->card_data.leaf.family) { +switch (dev->driver_info->family) {case KVASER_LEAF : cmd_tx_can_flags = &cmd->u.tx_can.leaf.flags;break ;
@@ -550,7 +550,7 @@ static int kvaser_usb_leaf_get_software_info_inner(struct kvaser_usb *dev)if (err)return err; -switch (dev->card_data.leaf.family) { +switch (dev->driver_info->family) {case KVASER_LEAF :kvaser_usb_leaf_get_software_info_leaf (dev, &cmd.u.leaf.softinfo);break ;
@@ -597,7 +597,7 @@ static int kvaser_usb_leaf_get_card_info(struct kvaser_usb *dev) dev->nchannels = cmd.u.cardinfo.nchannels;if (dev->nchannels >KVASER_USB_MAX_NET_DEVICES || - (dev->card_data.leaf.family ==KVASER_USBCAN && + (dev->driver_info->family ==KVASER_USBCAN && dev->nchannels >MAX_USBCAN_NET_DEVICES ))return -EINVAL ;
@@ -730,7 +730,7 @@ kvaser_usb_leaf_rx_error_update_can_state(struct kvaser_usb_net_priv *priv, new_state < CAN_STATE_BUS_OFF) priv->can.can_stats.restarts++; -switch (dev->card_data.leaf.family) { +switch (dev->driver_info->family) {case KVASER_LEAF :if (es->leaf.error_factor) { priv->can.can_stats.bus_error++;
@@ -809,7 +809,7 @@ static void kvaser_usb_leaf_rx_error(const struct kvaser_usb *dev, } } -switch (dev->card_data.leaf.family) { +switch (dev->driver_info->family) {case KVASER_LEAF :if (es->leaf.error_factor) { cf->can_id |=CAN_ERR_BUSERROR |CAN_ERR_PROT ;
@@ -999,7 +999,7 @@ static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev, stats = &priv->netdev->stats;if ((cmd->u.rx_can_header.flag &MSG_FLAG_ERROR_FRAME ) && - (dev->card_data.leaf.family ==KVASER_LEAF && + (dev->driver_info->family ==KVASER_LEAF && cmd->id ==CMD_LEAF_LOG_MESSAGE )) {kvaser_usb_leaf_leaf_rx_error (dev, cmd);return ;
@@ -1015,7 +1015,7 @@ static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev,return ; } -switch (dev->card_data.leaf.family) { +switch (dev->driver_info->family) {case KVASER_LEAF : rx_data = cmd->u.leaf.rx_can.data;break ;
@@ -1030,7 +1030,7 @@ static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev,return ; } -if (dev->card_data.leaf.family ==KVASER_LEAF && cmd->id == +if (dev->driver_info->family ==KVASER_LEAF && cmd->id ==CMD_LEAF_LOG_MESSAGE ) { cf->can_id =le32_to_cpu (cmd->u.leaf.log_message.id);if (cf->can_id &KVASER_EXTENDED_FRAME )
@@ -1128,14 +1128,14 @@ static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev,break ;case CMD_LEAF_LOG_MESSAGE : -if (dev->card_data.leaf.family !=KVASER_LEAF ) +if (dev->driver_info->family !=KVASER_LEAF ) goto warn;kvaser_usb_leaf_rx_can_msg (dev, cmd);break ;case CMD_CHIP_STATE_EVENT :case CMD_CAN_ERROR_EVENT : -if (dev->card_data.leaf.family ==KVASER_LEAF ) +if (dev->driver_info->family ==KVASER_LEAF )kvaser_usb_leaf_leaf_rx_error (dev, cmd);else kvaser_usb_leaf_usbcan_rx_error (dev, cmd);
@@ -1147,12 +1147,12 @@ static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev,/* Ignored commands */ case CMD_USBCAN_CLOCK_OVERFLOW_EVENT : -if (dev->card_data.leaf.family !=KVASER_USBCAN ) +if (dev->driver_info->family !=KVASER_USBCAN ) goto warn;break ;case CMD_FLUSH_QUEUE_REPLY : -if (dev->card_data.leaf.family !=KVASER_LEAF ) +if (dev->driver_info->family !=KVASER_LEAF ) goto warn;break ;
Signature · 1 line
2.36.1
PATCH v2 2/3 can: kvaser_usb: kvaser_usb_leaf: Fix CAN clock frequency regression
In reply to Jimmy AssarssonThe firmware of M32C based Leaf devices expects bittiming parameters calculated for 16MHz clock.
Since we use the actual clock frequency of the device, the device may end up with wrong bittiming parameters, depending on user requested parameters.
This regression affects M32C based Leaf devices with non-16MHz clock.
---
Changes in v2:
- Remove quriks member in struct kvaser_usb_dev_card_data, and use quirks in struct kvaser_usb_driver_info instead.
drivers/net/can/usb/kvaser_usb/kvaser_usb.h | 14 ++++++++--- .../net/can/usb/kvaser_usb/kvaser_usb_core.c | 20 ++++++++------- .../net/can/usb/kvaser_usb/kvaser_usb_leaf.c | 25 ++++++++++++------- 3 files changed, 38 insertions(+), 21 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb.h +11 -3
@@ -38,6 +38,7 @@/* Kvaser USB device quirks */ #define KVASER_USB_QUIRK_HAS_SILENT_MODE BIT(0)#define KVASER_USB_QUIRK_HAS_TXRX_ERRORS BIT(1) +#define KVASER_USB_QUIRK_IGNORE_CLK_FREQ BIT(2)/* Device capabilities */ #define KVASER_USB_CAP_BERR_CAP 0x01
@@ -199,21 +200,28 @@ static const struct kvaser_usb_driver_info kvaser_usb_driver_info_usbcan = { };static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf = { - .quirks =0 , + .quirks =KVASER_USB_QUIRK_IGNORE_CLK_FREQ , .family =KVASER_LEAF , .ops = &kvaser_usb_leaf_dev_ops, };static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf_err = { - .quirks =KVASER_USB_QUIRK_HAS_TXRX_ERRORS , + .quirks =KVASER_USB_QUIRK_HAS_TXRX_ERRORS | +KVASER_USB_QUIRK_IGNORE_CLK_FREQ , .family =KVASER_LEAF , .ops = &kvaser_usb_leaf_dev_ops, };static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf_err_listen = { .quirks =KVASER_USB_QUIRK_HAS_TXRX_ERRORS | -KVASER_USB_QUIRK_HAS_SILENT_MODE , +KVASER_USB_QUIRK_HAS_SILENT_MODE | +KVASER_USB_QUIRK_IGNORE_CLK_FREQ , .family =KVASER_LEAF , .ops = &kvaser_usb_leaf_dev_ops, }; + +static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leafimx = { + .quirks =0 , + .ops = &kvaser_usb_leaf_dev_ops, +};#endif /* KVASER_USB_H */
drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c +11 -9
@@ -89,7 +89,7 @@#define USB_HYBRID_PRO_CANLIN_PRODUCT_ID 278static const struct usb_device_id kvaser_usb_table[] = { -/* Leaf USB product IDs */ +/* Leaf M32C USB product IDs */ {USB_DEVICE (KVASER_VENDOR_ID ,USB_LEAF_DEVEL_PRODUCT_ID ), .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf }, {USB_DEVICE (KVASER_VENDOR_ID ,USB_LEAF_LITE_PRODUCT_ID ),
@@ -130,22 +130,24 @@ static const struct usb_device_id kvaser_usb_table[] = { .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err }, { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID), .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err }, + +/* Leaf i.MX28 USB product IDs */ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID), - .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx }, { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID), - .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx }, { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID), - .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx }, { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_2HS_PRODUCT_ID), - .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx }, { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_2HS_PRODUCT_ID), - .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx }, { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_R_V2_PRODUCT_ID), - .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx }, { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_R_V2_PRODUCT_ID), - .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx }, { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_HS_V2_OEM2_PRODUCT_ID), - .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leafimx },/* USBCANII USB product IDs */ { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID),
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +16 -9
@@ -524,16 +524,23 @@ static void kvaser_usb_leaf_get_software_info_leaf(struct kvaser_usb *dev, dev->fw_version =le32_to_cpu (softinfo->fw_version); dev->max_tx_urbs =le16_to_cpu (softinfo->max_outstanding_tx); -switch (sw_options &KVASER_USB_LEAF_SWOPTION_FREQ_MASK ) { -case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK : +if (dev->driver_info->quirks &KVASER_USB_QUIRK_IGNORE_CLK_FREQ ) { +/* Firmware expects bittiming parameters calculated for 16MHz +* clock, regardless of the actual clock +*/ dev->cfg = &kvaser_usb_leaf_dev_cfg_16mhz; -break ; -case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK : - dev->cfg = &kvaser_usb_leaf_dev_cfg_24mhz; -break ; -case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK : - dev->cfg = &kvaser_usb_leaf_dev_cfg_32mhz; -break ; + }else { +switch (sw_options &KVASER_USB_LEAF_SWOPTION_FREQ_MASK ) { +case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK : + dev->cfg = &kvaser_usb_leaf_dev_cfg_16mhz; +break ; +case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK : + dev->cfg = &kvaser_usb_leaf_dev_cfg_24mhz; +break ; +case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK : + dev->cfg = &kvaser_usb_leaf_dev_cfg_32mhz; +break ; + } } }
Signature · 1 line
2.36.1
PATCH v2 3/3 can: kvaser_usb: kvaser_usb_leaf: Fix bittiming limits
In reply to Jimmy AssarssonUse correct bittiming limits depending on device. For devices based on USBcanII, Leaf M32C or Leaf i.MX28.
---
Changes in v2:
- Rebased patch
drivers/net/can/usb/kvaser_usb/kvaser_usb.h | 13 ++++ .../net/can/usb/kvaser_usb/kvaser_usb_hydra.c | 14 +--- .../net/can/usb/kvaser_usb/kvaser_usb_leaf.c | 76 +++++++++++-------- 3 files changed, 57 insertions(+), 46 deletions(-)
drivers/net/can/usb/kvaser_usb/kvaser_usb.h +13 -0
@@ -28,6 +28,7 @@#include <linux/can.h> #include <linux/can/dev.h> +#include <linux/can/netlink.h> #define KVASER_USB_MAX_RX_URBS 4#define KVASER_USB_MAX_TX_URBS 128
@@ -224,4 +225,16 @@ static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leafimx = { .quirks = 0, .ops = &kvaser_usb_leaf_dev_ops, }; + +static const struct can_bittiming_const kvaser_usb_flexc_bittiming_const = { + .name ="kvaser_usb_flex" , + .tseg1_min =4 , + .tseg1_max =16 , + .tseg2_min =2 , + .tseg2_max =8 , + .sjw_max =4 , + .brp_min =1 , + .brp_max =256 , + .brp_inc =1 , +};#endif /* KVASER_USB_H */
drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c +1 -13
@@ -375,18 +375,6 @@ static const struct can_bittiming_const kvaser_usb_hydra_kcan_bittiming_c = { .brp_inc = 1, }; -static const struct can_bittiming_const kvaser_usb_hydra_flexc_bittiming_c = { - .name ="kvaser_usb_flex" , - .tseg1_min =4 , - .tseg1_max =16 , - .tseg2_min =2 , - .tseg2_max =8 , - .sjw_max =4 , - .brp_min =1 , - .brp_max =256 , - .brp_inc =1 , -}; -static const struct can_bittiming_const kvaser_usb_hydra_rt_bittiming_c = { .name ="kvaser_usb_rt" , .tseg1_min =2 ,
@@ -2052,7 +2040,7 @@ static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_flexc = { .freq = 24 * MEGA /* Hz */, }, .timestamp_freq = 1, - .bittiming_const = &kvaser_usb_hydra_flexc_bittiming_c, + .bittiming_const = &kvaser_usb_flexc_bittiming_const, }; static const struct kvaser_usb_dev_cfg kvaser_usb_hydra_dev_cfg_rt = {
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c +43 -33
131 lines not on this page · raw message
Signature · 1 line
2.36.1
Marc Kleine-Budde
maintainerQuoted 7 lines · Jimmy Assarsson
When fixing the CAN clock frequency,
fb12797ab1fe("can:kvaser_usb: get CAN clock frequency from device"), I introduced a regression.For Leaf devices based on M32C, the firmware expects bittiming parameters calculated for 16MHz clock. Regardless of the actual clock frequency.
Quoted 6 lines
This regression affects M32C based Leaf devices with non-16MHz clock.
Also correct the bittiming constants in
kvaser_usb_leaf.c, where the limits are different depending on whichfirmware/devicebeing used.Once merged to mainline, I'll backport these fixes for the stable kernels.
Added to linux-can/testing. I had to move the kvaser_usb_driver_info into kvaser_usb_core.c and the keep struct can_bittiming_const kvaser_usb_flexc_bittiming_const in kvaser_usb_hydra.c as structs in header files cause defined but not used warnings on some platforms.
regards,
Marc
Signature · 4 lines
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 |
Quoted 13 lines · Marc Kleine-Budde
Quoted 12 lines · Jimmy Assarsson
When fixing the CAN clock frequency,
fb12797ab1fe("can:kvaser_usb: get CAN clock frequency from device"), I introduced a regression.For Leaf devices based on M32C, the firmware expects bittiming parameters calculated for 16MHz clock. Regardless of the actual clock frequency.
This regression affects M32C based Leaf devices with non-16MHz clock.
Also correct the bittiming constants in
kvaser_usb_leaf.c, where the limits are different depending on whichfirmware/devicebeing used.
Quoted 6 lines
Quoted 1 line
Once merged to mainline, I'll backport these fixes for the stable kernels.
Added to linux-can/testing. I had to move the
kvaser_usb_driver_infointokvaser_usb_core.cand the keep structcan_bittiming_constkvaser_usb_flexc_bittiming_constinkvaser_usb_hydra.cas structs in header files cause defined but not used warnings on some platforms.
Ok. Thanks Marc!