Backport of upstream patch series [1].
Note: The patch "[PATCH 4.14 1/4] can: kvaser_usb: Add struct kvaser_usb_dev_cfg" is added for this backport, and was not part of the original series.
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.
[1] https://lore.kernel.org/linux-can/20220603083820.800246-1-extja@kvaser.com/
Jimmy Assarsson (4): can: kvaser_usb: Add struct kvaser_usb_dev_cfg can: kvaser_usb: replace run-time checks with struct kvaser_usb_driver_info can: kvaser_usb: fix CAN clock frequency regression can: kvaser_usb: fix bittiming limits
drivers/net/can/usb/kvaser_usb.c | 325 +++++++++++++++++++------------ 1 file changed, 201 insertions(+), 124 deletions(-)
Add struct kvaser_usb_dev_cfg to ease backporting of upstream commits: 49f274c72357 (can: kvaser_usb: replace run-time checks with struct kvaser_usb_driver_info) e6c80e601053 (can: kvaser_usb: kvaser_usb_leaf: fix CAN clock frequency regression) b3b6df2c56d8 (can: kvaser_usb: kvaser_usb_leaf: fix bittiming limits)
Cc: stable@vger.kernel.org Signed-off-by: Jimmy Assarsson extja@kvaser.com --- drivers/net/can/usb/kvaser_usb.c | 76 ++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 24 deletions(-)
diff --git a/drivers/net/can/usb/kvaser_usb.c b/drivers/net/can/usb/kvaser_usb.c index 9742e32d5cd5..6759868924b2 100644 --- a/drivers/net/can/usb/kvaser_usb.c +++ b/drivers/net/can/usb/kvaser_usb.c @@ -31,10 +31,6 @@ #define USB_SEND_TIMEOUT 1000 /* msecs */ #define USB_RECV_TIMEOUT 1000 /* msecs */ #define RX_BUFFER_SIZE 3072 -#define KVASER_USB_CAN_CLOCK_8MHZ 8000000 -#define KVASER_USB_CAN_CLOCK_16MHZ 16000000 -#define KVASER_USB_CAN_CLOCK_24MHZ 24000000 -#define KVASER_USB_CAN_CLOCK_32MHZ 32000000 #define MAX_NET_DEVICES 3 #define MAX_USBCAN_NET_DEVICES 2
@@ -465,6 +461,7 @@ struct kvaser_usb_tx_urb_context { struct kvaser_usb { struct usb_device *udev; struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES]; + const struct kvaser_usb_dev_cfg *cfg;
struct usb_endpoint_descriptor *bulk_in, *bulk_out; struct usb_anchor rx_submitted; @@ -481,8 +478,6 @@ struct kvaser_usb { bool rxinitdone; void *rxbuf[MAX_RX_URBS]; dma_addr_t rxbuf_dma[MAX_RX_URBS]; - - struct can_clock clock; };
struct kvaser_usb_net_priv { @@ -501,6 +496,51 @@ struct kvaser_usb_net_priv { struct kvaser_usb_tx_urb_context tx_contexts[]; };
+struct kvaser_usb_dev_cfg { + const struct can_clock clock; + const struct can_bittiming_const * const bittiming_const; +}; + +static const struct can_bittiming_const kvaser_usb_bittiming_const = { + .name = "kvaser_usb", + .tseg1_min = KVASER_USB_TSEG1_MIN, + .tseg1_max = KVASER_USB_TSEG1_MAX, + .tseg2_min = KVASER_USB_TSEG2_MIN, + .tseg2_max = KVASER_USB_TSEG2_MAX, + .sjw_max = KVASER_USB_SJW_MAX, + .brp_min = KVASER_USB_BRP_MIN, + .brp_max = KVASER_USB_BRP_MAX, + .brp_inc = KVASER_USB_BRP_INC, +}; + +static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_8mhz = { + .clock = { + .freq = 8000000, + }, + .bittiming_const = &kvaser_usb_bittiming_const, +}; + +static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_16mhz = { + .clock = { + .freq = 16000000, + }, + .bittiming_const = &kvaser_usb_bittiming_const, +}; + +static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_24mhz = { + .clock = { + .freq = 24000000, + }, + .bittiming_const = &kvaser_usb_bittiming_const, +}; + +static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_32mhz = { + .clock = { + .freq = 32000000, + }, + .bittiming_const = &kvaser_usb_bittiming_const, +}; + static const struct usb_device_id kvaser_usb_table[] = { /* Leaf family IDs */ { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) }, @@ -673,13 +713,13 @@ static void kvaser_usb_get_software_info_leaf(struct kvaser_usb *dev,
switch (sw_options & KVASER_USB_LEAF_SWOPTION_FREQ_MASK) { case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK: - dev->clock.freq = KVASER_USB_CAN_CLOCK_16MHZ; + dev->cfg = &kvaser_usb_leaf_dev_cfg_16mhz; break; case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK: - dev->clock.freq = KVASER_USB_CAN_CLOCK_24MHZ; + dev->cfg = &kvaser_usb_leaf_dev_cfg_24mhz; break; case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK: - dev->clock.freq = KVASER_USB_CAN_CLOCK_32MHZ; + dev->cfg = &kvaser_usb_leaf_dev_cfg_32mhz; break; } } @@ -705,7 +745,7 @@ static int kvaser_usb_get_software_info(struct kvaser_usb *dev) dev->fw_version = le32_to_cpu(msg.u.usbcan.softinfo.fw_version); dev->max_tx_urbs = le16_to_cpu(msg.u.usbcan.softinfo.max_outstanding_tx); - dev->clock.freq = KVASER_USB_CAN_CLOCK_8MHZ; + dev->cfg = &kvaser_usb_leaf_dev_cfg_8mhz; break; }
@@ -1829,18 +1869,6 @@ static const struct net_device_ops kvaser_usb_netdev_ops = { .ndo_change_mtu = can_change_mtu, };
-static const struct can_bittiming_const kvaser_usb_bittiming_const = { - .name = "kvaser_usb", - .tseg1_min = KVASER_USB_TSEG1_MIN, - .tseg1_max = KVASER_USB_TSEG1_MAX, - .tseg2_min = KVASER_USB_TSEG2_MIN, - .tseg2_max = KVASER_USB_TSEG2_MAX, - .sjw_max = KVASER_USB_SJW_MAX, - .brp_min = KVASER_USB_BRP_MIN, - .brp_max = KVASER_USB_BRP_MAX, - .brp_inc = KVASER_USB_BRP_INC, -}; - static int kvaser_usb_set_bittiming(struct net_device *netdev) { struct kvaser_usb_net_priv *priv = netdev_priv(netdev); @@ -1957,8 +1985,8 @@ static int kvaser_usb_init_one(struct usb_interface *intf, kvaser_usb_reset_tx_urb_contexts(priv);
priv->can.state = CAN_STATE_STOPPED; - priv->can.clock.freq = dev->clock.freq; - priv->can.bittiming_const = &kvaser_usb_bittiming_const; + priv->can.clock.freq = dev->cfg->clock.freq; + priv->can.bittiming_const = dev->cfg->bittiming_const; priv->can.do_set_bittiming = kvaser_usb_set_bittiming; priv->can.do_set_mode = kvaser_usb_set_mode; if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
On Fri, Jul 08, 2022 at 08:46:50PM +0200, Jimmy Assarsson wrote:
Add struct kvaser_usb_dev_cfg to ease backporting of upstream commits: 49f274c72357 (can: kvaser_usb: replace run-time checks with struct kvaser_usb_driver_info) e6c80e601053 (can: kvaser_usb: kvaser_usb_leaf: fix CAN clock frequency regression) b3b6df2c56d8 (can: kvaser_usb: kvaser_usb_leaf: fix bittiming limits)
What upstream commit is this from?
Cc: stable@vger.kernel.org Signed-off-by: Jimmy Assarsson extja@kvaser.com
drivers/net/can/usb/kvaser_usb.c | 76 ++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 24 deletions(-)
diff --git a/drivers/net/can/usb/kvaser_usb.c b/drivers/net/can/usb/kvaser_usb.c index 9742e32d5cd5..6759868924b2 100644 --- a/drivers/net/can/usb/kvaser_usb.c +++ b/drivers/net/can/usb/kvaser_usb.c @@ -31,10 +31,6 @@ #define USB_SEND_TIMEOUT 1000 /* msecs */ #define USB_RECV_TIMEOUT 1000 /* msecs */ #define RX_BUFFER_SIZE 3072 -#define KVASER_USB_CAN_CLOCK_8MHZ 8000000 -#define KVASER_USB_CAN_CLOCK_16MHZ 16000000 -#define KVASER_USB_CAN_CLOCK_24MHZ 24000000 -#define KVASER_USB_CAN_CLOCK_32MHZ 32000000
You also deleted these, you didn't really describe any of this in the changelog text at all :(
Why not just backport the needed commit instead of this unknown one?
thanks,
greg k-h
On 7/10/22 16:21, Greg KH wrote:
On Fri, Jul 08, 2022 at 08:46:50PM +0200, Jimmy Assarsson wrote:
Add struct kvaser_usb_dev_cfg to ease backporting of upstream commits: 49f274c72357 (can: kvaser_usb: replace run-time checks with struct kvaser_usb_driver_info) e6c80e601053 (can: kvaser_usb: kvaser_usb_leaf: fix CAN clock frequency regression) b3b6df2c56d8 (can: kvaser_usb: kvaser_usb_leaf: fix bittiming limits)
What upstream commit is this from?
Hi Greg,
The original upstream commit introducing struct kvaser_usb_dev_cfg is 7259124eac7d1b76b41c7a9cb2511a30556deebe can: kvaser_usb: Split driver into kvaser_usb_core.c and kvaser_usb_leaf.c And was first merged to 4.19.
This was part of a major restructure of the driver to add support for new type of devices.
And upstream commit commit fb12797ab1fef480ad8a32a30984844444eeb00d can: kvaser_usb: get CAN clock frequency from device introduced kvaser_usb_leaf_dev_cfg_{8,16,24,32}mhz
Cc: stable@vger.kernel.org Signed-off-by: Jimmy Assarsson extja@kvaser.com
drivers/net/can/usb/kvaser_usb.c | 76 ++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 24 deletions(-)
diff --git a/drivers/net/can/usb/kvaser_usb.c b/drivers/net/can/usb/kvaser_usb.c index 9742e32d5cd5..6759868924b2 100644 --- a/drivers/net/can/usb/kvaser_usb.c +++ b/drivers/net/can/usb/kvaser_usb.c @@ -31,10 +31,6 @@ #define USB_SEND_TIMEOUT 1000 /* msecs */ #define USB_RECV_TIMEOUT 1000 /* msecs */ #define RX_BUFFER_SIZE 3072 -#define KVASER_USB_CAN_CLOCK_8MHZ 8000000 -#define KVASER_USB_CAN_CLOCK_16MHZ 16000000 -#define KVASER_USB_CAN_CLOCK_24MHZ 24000000 -#define KVASER_USB_CAN_CLOCK_32MHZ 32000000
You also deleted these, you didn't really describe any of this in the changelog text at all :(
They where replaced by const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_{8,16,24,32}mhz. Sorry for not mentioning this.
Why not just backport the needed commit instead of this unknown one?
Sure, if you prefer it :) In that case I would also like to backport the rest of the patches related to the adding of kvaser_usb/kvaser_usb_hydra.c, if you don't mind?
Best regards, jimmy
thanks,
greg k-h
On Sun, Jul 10, 2022 at 08:40:30PM +0200, Jimmy Assarsson wrote:
On 7/10/22 16:21, Greg KH wrote:
On Fri, Jul 08, 2022 at 08:46:50PM +0200, Jimmy Assarsson wrote:
Add struct kvaser_usb_dev_cfg to ease backporting of upstream commits: 49f274c72357 (can: kvaser_usb: replace run-time checks with struct kvaser_usb_driver_info) e6c80e601053 (can: kvaser_usb: kvaser_usb_leaf: fix CAN clock frequency regression) b3b6df2c56d8 (can: kvaser_usb: kvaser_usb_leaf: fix bittiming limits)
What upstream commit is this from?
Hi Greg,
The original upstream commit introducing struct kvaser_usb_dev_cfg is 7259124eac7d1b76b41c7a9cb2511a30556deebe can: kvaser_usb: Split driver into kvaser_usb_core.c and kvaser_usb_leaf.c And was first merged to 4.19.
This was part of a major restructure of the driver to add support for new type of devices.
Then why not just take thos commits, _OR_ rework the bugfixes to handle the old style of drivers?
Doing this type of major change that is not directly upstream is almost always a big mistake as it's not tested well and diverges from what is in Linus's tree.
Please try to rework these two series and resubmit.
thanks,
greg k-h
commit 49f274c72357d2d74cba70b172cf369768909707 upstream.
Unify and move compile-time known information into new struct kvaser_usb_driver_info, in favor of run-time checks.
All Kvaser USBcanII supports listen-only mode and error counter reporting.
Link: https://lore.kernel.org/all/20220603083820.800246-2-extja@kvaser.com Suggested-by: Marc Kleine-Budde mkl@pengutronix.de Cc: stable@vger.kernel.org Signed-off-by: Jimmy Assarsson extja@kvaser.com --- drivers/net/can/usb/kvaser_usb.c | 175 ++++++++++++++++--------------- 1 file changed, 93 insertions(+), 82 deletions(-)
diff --git a/drivers/net/can/usb/kvaser_usb.c b/drivers/net/can/usb/kvaser_usb.c index 6759868924b2..7564194f4c87 100644 --- a/drivers/net/can/usb/kvaser_usb.c +++ b/drivers/net/can/usb/kvaser_usb.c @@ -62,27 +62,15 @@ #define USB_USBCAN_LIGHT_2HS_PRODUCT_ID 291 #define USB_MINI_PCIE_2HS_PRODUCT_ID 292
-static inline bool kvaser_is_leaf(const struct usb_device_id *id) -{ - return id->idProduct >= USB_LEAF_DEVEL_PRODUCT_ID && - id->idProduct <= USB_MINI_PCIE_2HS_PRODUCT_ID; -} - /* Kvaser USBCan-II devices */ #define USB_USBCAN_REVB_PRODUCT_ID 2 #define USB_VCI2_PRODUCT_ID 3 #define USB_USBCAN2_PRODUCT_ID 4 #define USB_MEMORATOR_PRODUCT_ID 5
-static inline bool kvaser_is_usbcan(const struct usb_device_id *id) -{ - return id->idProduct >= USB_USBCAN_REVB_PRODUCT_ID && - id->idProduct <= USB_MEMORATOR_PRODUCT_ID; -} - -/* USB devices features */ -#define KVASER_HAS_SILENT_MODE BIT(0) -#define KVASER_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)
/* Message header size */ #define MSG_HEADER_LEN 2 @@ -461,6 +449,7 @@ struct kvaser_usb_tx_urb_context { struct kvaser_usb { struct usb_device *udev; struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES]; + const struct kvaser_usb_driver_info *driver_info; const struct kvaser_usb_dev_cfg *cfg;
struct usb_endpoint_descriptor *bulk_in, *bulk_out; @@ -473,7 +462,6 @@ struct kvaser_usb { u32 fw_version; unsigned int nchannels; unsigned int max_tx_urbs; - enum kvaser_usb_family family;
bool rxinitdone; void *rxbuf[MAX_RX_URBS]; @@ -496,6 +484,11 @@ struct kvaser_usb_net_priv { struct kvaser_usb_tx_urb_context tx_contexts[]; };
+struct kvaser_usb_driver_info { + u32 quirks; + enum kvaser_usb_family family; +}; + struct kvaser_usb_dev_cfg { const struct can_clock clock; const struct can_bittiming_const * const bittiming_const; @@ -541,70 +534,90 @@ static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_32mhz = { .bittiming_const = &kvaser_usb_bittiming_const, };
+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, +}; + +static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf = { + .quirks = 0, + .family = KVASER_LEAF, +}; + +static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leaf_err = { + .quirks = KVASER_USB_QUIRK_HAS_TXRX_ERRORS, + .family = KVASER_LEAF, +}; + +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, +}; + static const struct usb_device_id kvaser_usb_table[] = { /* Leaf family IDs */ - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) }, - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) }, + { 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), + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf }, { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS | - KVASER_HAS_SILENT_MODE }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen }, { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS | - KVASER_HAS_SILENT_MODE }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen }, { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS | - KVASER_HAS_SILENT_MODE }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen }, { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS | - KVASER_HAS_SILENT_MODE }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen }, { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS | - KVASER_HAS_SILENT_MODE }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen }, { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS | - KVASER_HAS_SILENT_MODE }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen }, { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS | - KVASER_HAS_SILENT_MODE }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen }, { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS | - KVASER_HAS_SILENT_MODE }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen }, { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS | - KVASER_HAS_SILENT_MODE }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen }, { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS }, - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err }, + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID), + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf }, { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS | - KVASER_HAS_SILENT_MODE }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err_listen }, { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err }, { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err }, { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err }, { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err }, { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err }, { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS }, - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) }, - { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) }, - { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID) }, - { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_2HS_PRODUCT_ID) }, - { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_2HS_PRODUCT_ID) }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf_err }, + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID), + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf }, + { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID), + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf }, + { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LIGHT_HS_V2_OEM_PRODUCT_ID), + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf }, + { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_LIGHT_2HS_PRODUCT_ID), + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf }, + { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_2HS_PRODUCT_ID), + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_leaf },
/* USBCANII family IDs */ { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_usbcan }, { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_usbcan }, { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_usbcan }, { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID), - .driver_info = KVASER_HAS_TXRX_ERRORS }, + .driver_info = (kernel_ulong_t)&kvaser_usb_driver_info_usbcan },
{ } }; @@ -737,7 +750,7 @@ static int kvaser_usb_get_software_info(struct kvaser_usb *dev) if (err) return err;
- switch (dev->family) { + switch (dev->driver_info->family) { case KVASER_LEAF: kvaser_usb_get_software_info_leaf(dev, &msg.u.leaf.softinfo); break; @@ -767,7 +780,7 @@ static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
dev->nchannels = msg.u.cardinfo.nchannels; if ((dev->nchannels > MAX_NET_DEVICES) || - (dev->family == KVASER_USBCAN && + (dev->driver_info->family == KVASER_USBCAN && dev->nchannels > MAX_USBCAN_NET_DEVICES)) return -EINVAL;
@@ -938,7 +951,7 @@ static void kvaser_usb_rx_error_update_can_state(struct kvaser_usb_net_priv *pri priv->can.can_stats.restarts++; }
- switch (dev->family) { + switch (dev->driver_info->family) { case KVASER_LEAF: if (es->leaf.error_factor) { priv->can.can_stats.bus_error++; @@ -1015,7 +1028,7 @@ static void kvaser_usb_rx_error(const struct kvaser_usb *dev, } }
- switch (dev->family) { + switch (dev->driver_info->family) { case KVASER_LEAF: if (es->leaf.error_factor) { cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT; @@ -1224,7 +1237,8 @@ static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev, stats = &priv->netdev->stats;
if ((msg->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) && - (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE)) { + (dev->driver_info->family == KVASER_LEAF && + msg->id == CMD_LEAF_LOG_MESSAGE)) { kvaser_leaf_rx_error(dev, msg); return; } else if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME | @@ -1239,7 +1253,7 @@ static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev, return; }
- switch (dev->family) { + switch (dev->driver_info->family) { case KVASER_LEAF: rx_msg = msg->u.leaf.rx_can.msg; break; @@ -1254,7 +1268,8 @@ static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev, return; }
- if (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE) { + if (dev->driver_info->family == KVASER_LEAF && msg->id == + CMD_LEAF_LOG_MESSAGE) { cf->can_id = le32_to_cpu(msg->u.leaf.log_message.id); if (cf->can_id & KVASER_EXTENDED_FRAME) cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG; @@ -1351,14 +1366,14 @@ static void kvaser_usb_handle_message(const struct kvaser_usb *dev, break;
case CMD_LEAF_LOG_MESSAGE: - if (dev->family != KVASER_LEAF) + if (dev->driver_info->family != KVASER_LEAF) goto warn; kvaser_usb_rx_can_msg(dev, msg); break;
case CMD_CHIP_STATE_EVENT: case CMD_CAN_ERROR_EVENT: - if (dev->family == KVASER_LEAF) + if (dev->driver_info->family == KVASER_LEAF) kvaser_leaf_rx_error(dev, msg); else kvaser_usbcan_rx_error(dev, msg); @@ -1370,12 +1385,12 @@ static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
/* Ignored messages */ case CMD_USBCAN_CLOCK_OVERFLOW_EVENT: - if (dev->family != KVASER_USBCAN) + if (dev->driver_info->family != KVASER_USBCAN) goto warn; break;
case CMD_FLUSH_QUEUE_REPLY: - if (dev->family != KVASER_LEAF) + if (dev->driver_info->family != KVASER_LEAF) goto warn; break;
@@ -1764,7 +1779,7 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb, msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can); msg->u.tx_can.channel = priv->channel;
- switch (dev->family) { + switch (dev->driver_info->family) { case KVASER_LEAF: msg_tx_can_flags = &msg->u.tx_can.leaf.flags; break; @@ -1951,12 +1966,12 @@ static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev) } }
-static int kvaser_usb_init_one(struct usb_interface *intf, - const struct usb_device_id *id, int channel) +static int kvaser_usb_init_one(struct usb_interface *intf, int channel) { struct kvaser_usb *dev = usb_get_intfdata(intf); struct net_device *netdev; struct kvaser_usb_net_priv *priv; + const struct kvaser_usb_driver_info *driver_info = dev->driver_info; int err;
err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel); @@ -1989,10 +2004,10 @@ static int kvaser_usb_init_one(struct usb_interface *intf, priv->can.bittiming_const = dev->cfg->bittiming_const; priv->can.do_set_bittiming = kvaser_usb_set_bittiming; priv->can.do_set_mode = kvaser_usb_set_mode; - if (id->driver_info & KVASER_HAS_TXRX_ERRORS) + if (driver_info->quirks & KVASER_USB_QUIRK_HAS_TXRX_ERRORS) priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter; priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES; - if (id->driver_info & KVASER_HAS_SILENT_MODE) + if (driver_info->quirks & KVASER_USB_QUIRK_HAS_SILENT_MODE) priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
netdev->flags |= IFF_ECHO; @@ -2050,21 +2065,17 @@ static int kvaser_usb_probe(struct usb_interface *intf, struct kvaser_usb *dev; int err = -ENOMEM; int i, retry = 3; + const struct kvaser_usb_driver_info *driver_info; + + driver_info = (const struct kvaser_usb_driver_info *)id->driver_info; + if (!driver_info) + return -ENODEV;
dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL); if (!dev) return -ENOMEM;
- if (kvaser_is_leaf(id)) { - dev->family = KVASER_LEAF; - } else if (kvaser_is_usbcan(id)) { - dev->family = KVASER_USBCAN; - } else { - dev_err(&intf->dev, - "Product ID (%d) does not belong to any known Kvaser USB family", - id->idProduct); - return -ENODEV; - } + dev->driver_info = driver_info;
err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out); if (err) { @@ -2108,7 +2119,7 @@ static int kvaser_usb_probe(struct usb_interface *intf, }
for (i = 0; i < dev->nchannels; i++) { - err = kvaser_usb_init_one(intf, id, i); + err = kvaser_usb_init_one(intf, i); if (err) { kvaser_usb_remove_interfaces(dev); return err;
commit e6c80e601053ffdac5709f11ff3ec1e19ed05f7b upstream.
The 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.
Fixes: 5967fc906ec0 ("can: kvaser_usb: get CAN clock frequency from device") Link: https://lore.kernel.org/all/20220603083820.800246-3-extja@kvaser.com Cc: stable@vger.kernel.org Signed-off-by: Jimmy Assarsson extja@kvaser.com Signed-off-by: Marc Kleine-Budde mkl@pengutronix.de --- drivers/net/can/usb/kvaser_usb.c | 53 +++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 18 deletions(-)
diff --git a/drivers/net/can/usb/kvaser_usb.c b/drivers/net/can/usb/kvaser_usb.c index 7564194f4c87..5190897ec6c9 100644 --- a/drivers/net/can/usb/kvaser_usb.c +++ b/drivers/net/can/usb/kvaser_usb.c @@ -71,6 +71,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)
/* Message header size */ #define MSG_HEADER_LEN 2 @@ -541,23 +542,30 @@ 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, };
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, };
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, +}; + +static const struct kvaser_usb_driver_info kvaser_usb_driver_info_leafimx = { + .quirks = 0, .family = KVASER_LEAF, };
static const struct usb_device_id kvaser_usb_table[] = { - /* Leaf family 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), @@ -598,16 +606,18 @@ 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 },
/* USBCANII family IDs */ { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID), @@ -724,16 +734,23 @@ static void kvaser_usb_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; + } } }
commit b3b6df2c56d80b8c6740433cff5f016668b8de70 upstream.
Use correct bittiming limits depending on device. For devices based on USBcanII, Leaf M32C or Leaf i.MX28.
Fixes: 080f40a6fa28 ("can: kvaser_usb: Add support for Kvaser CAN/USB devices") Fixes: b4f20130af23 ("can: kvaser_usb: add support for Kvaser Leaf v2 and usb mini PCIe") Fixes: f5d4abea3ce0 ("can: kvaser_usb: Add support for the USBcan-II family") Link: https://lore.kernel.org/all/20220603083820.800246-4-extja@kvaser.com Cc: stable@vger.kernel.org Signed-off-by: Jimmy Assarsson extja@kvaser.com --- drivers/net/can/usb/kvaser_usb.c | 87 ++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 33 deletions(-)
diff --git a/drivers/net/can/usb/kvaser_usb.c b/drivers/net/can/usb/kvaser_usb.c index 5190897ec6c9..b1a140a39773 100644 --- a/drivers/net/can/usb/kvaser_usb.c +++ b/drivers/net/can/usb/kvaser_usb.c @@ -154,16 +154,6 @@ #define USBCAN_ERROR_STATE_RX_ERROR BIT(1) #define USBCAN_ERROR_STATE_BUSERROR BIT(2)
-/* bittiming parameters */ -#define KVASER_USB_TSEG1_MIN 1 -#define KVASER_USB_TSEG1_MAX 16 -#define KVASER_USB_TSEG2_MIN 1 -#define KVASER_USB_TSEG2_MAX 8 -#define KVASER_USB_SJW_MAX 4 -#define KVASER_USB_BRP_MIN 1 -#define KVASER_USB_BRP_MAX 64 -#define KVASER_USB_BRP_INC 1 - /* ctrl modes */ #define KVASER_CTRL_MODE_NORMAL 1 #define KVASER_CTRL_MODE_SILENT 2 @@ -495,44 +485,75 @@ struct kvaser_usb_dev_cfg { const struct can_bittiming_const * const bittiming_const; };
-static const struct can_bittiming_const kvaser_usb_bittiming_const = { - .name = "kvaser_usb", - .tseg1_min = KVASER_USB_TSEG1_MIN, - .tseg1_max = KVASER_USB_TSEG1_MAX, - .tseg2_min = KVASER_USB_TSEG2_MIN, - .tseg2_max = KVASER_USB_TSEG2_MAX, - .sjw_max = KVASER_USB_SJW_MAX, - .brp_min = KVASER_USB_BRP_MIN, - .brp_max = KVASER_USB_BRP_MAX, - .brp_inc = KVASER_USB_BRP_INC, +static const struct can_bittiming_const kvaser_usb_m16c_bittiming_const = { + .name = "kvaser_usb_ucii", + .tseg1_min = 4, + .tseg1_max = 16, + .tseg2_min = 2, + .tseg2_max = 8, + .sjw_max = 4, + .brp_min = 1, + .brp_max = 16, + .brp_inc = 1, +}; + +static const struct can_bittiming_const kvaser_usb_m32c_bittiming_const = { + .name = "kvaser_usb_leaf", + .tseg1_min = 3, + .tseg1_max = 16, + .tseg2_min = 2, + .tseg2_max = 8, + .sjw_max = 4, + .brp_min = 2, + .brp_max = 128, + .brp_inc = 2, +}; + +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, };
-static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_8mhz = { +static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_usbcan_dev_cfg = { .clock = { .freq = 8000000, }, - .bittiming_const = &kvaser_usb_bittiming_const, + .bittiming_const = &kvaser_usb_m16c_bittiming_const, +}; + +static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_m32c_dev_cfg = { + .clock = { + .freq = 16000000, + }, + .bittiming_const = &kvaser_usb_m32c_bittiming_const, };
-static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_16mhz = { +static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_16mhz = { .clock = { .freq = 16000000, }, - .bittiming_const = &kvaser_usb_bittiming_const, + .bittiming_const = &kvaser_usb_flexc_bittiming_const, };
-static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_24mhz = { +static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_24mhz = { .clock = { .freq = 24000000, }, - .bittiming_const = &kvaser_usb_bittiming_const, + .bittiming_const = &kvaser_usb_flexc_bittiming_const, };
-static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_32mhz = { +static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_32mhz = { .clock = { .freq = 32000000, }, - .bittiming_const = &kvaser_usb_bittiming_const, + .bittiming_const = &kvaser_usb_flexc_bittiming_const, };
static const struct kvaser_usb_driver_info kvaser_usb_driver_info_usbcan = { @@ -738,17 +759,17 @@ static void kvaser_usb_get_software_info_leaf(struct kvaser_usb *dev, /* Firmware expects bittiming parameters calculated for 16MHz * clock, regardless of the actual clock */ - dev->cfg = &kvaser_usb_leaf_dev_cfg_16mhz; + dev->cfg = &kvaser_usb_leaf_m32c_dev_cfg; } 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; + dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_16mhz; break; case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK: - dev->cfg = &kvaser_usb_leaf_dev_cfg_24mhz; + dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_24mhz; break; case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK: - dev->cfg = &kvaser_usb_leaf_dev_cfg_32mhz; + dev->cfg = &kvaser_usb_leaf_imx_dev_cfg_32mhz; break; } } @@ -775,7 +796,7 @@ static int kvaser_usb_get_software_info(struct kvaser_usb *dev) dev->fw_version = le32_to_cpu(msg.u.usbcan.softinfo.fw_version); dev->max_tx_urbs = le16_to_cpu(msg.u.usbcan.softinfo.max_outstanding_tx); - dev->cfg = &kvaser_usb_leaf_dev_cfg_8mhz; + dev->cfg = &kvaser_usb_leaf_usbcan_dev_cfg; break; }
linux-stable-mirror@lists.linaro.org