public inbox for linux-can@vger.kernel.org
 help / color / mirror / Atom feed
From: Jimmy Assarsson <jimmyassarsson@gmail.com>
To: Jimmy Assarsson <extja@kvaser.com>,
	linux-can@vger.kernel.org, stable@vger.kernel.org,
	Marc Kleine-Budde <mkl@pengutronix.de>
Subject: Re: [PATCH] can: kvaser_usb: get CAN clock frequency from device
Date: Wed, 22 Dec 2021 01:37:24 +0100	[thread overview]
Message-ID: <eaefb340-6677-1fcc-83c2-dacc17360b92@gmail.com> (raw)
In-Reply-To: <20211222002826.1398761-1-extja@kvaser.com>

Hi,

I'm not familiar with the tag conventions used in the stable tree.
This is a backport of upstream commit fb12797ab1fef480ad8a32a30984844444eeb00d,
for stable 4.4, 4.9 and 4.14.
How to properly specify this?

Best regards,
jimmy

On 2021-12-22 01:28, Jimmy Assarsson wrote:
> commit fb12797ab1fef480ad8a32a30984844444eeb00d upstream.
> 
> The CAN clock frequency is used when calculating the CAN bittiming
> parameters. When wrong clock frequency is used, the device may end up
> with wrong bittiming parameters, depending on user requested bittiming
> parameters.
> 
> To avoid this, get the CAN clock frequency from the device. Various
> existing Kvaser Leaf products use different CAN clocks.
> 
> Fixes: 080f40a6fa28 ("can: kvaser_usb: Add support for Kvaser CAN/USB devices")
> Link: https://lore.kernel.org/all/20211208152122.250852-2-extja@kvaser.com
> Cc: stable@vger.kernel.org
> Signed-off-by: Jimmy Assarsson <extja@kvaser.com>
> [backport note: kvaser_usb was split into multiple files in 4.19]
> ---
>   drivers/net/can/usb/kvaser_usb.c | 41 ++++++++++++++++++++++++++++----
>   1 file changed, 36 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/net/can/usb/kvaser_usb.c b/drivers/net/can/usb/kvaser_usb.c
> index 9991ee93735a..81abb30d9ec0 100644
> --- a/drivers/net/can/usb/kvaser_usb.c
> +++ b/drivers/net/can/usb/kvaser_usb.c
> @@ -31,7 +31,10 @@
>   #define USB_SEND_TIMEOUT		1000 /* msecs */
>   #define USB_RECV_TIMEOUT		1000 /* msecs */
>   #define RX_BUFFER_SIZE			3072
> -#define CAN_USB_CLOCK			8000000
> +#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
>   
> @@ -139,6 +142,12 @@ static inline bool kvaser_is_usbcan(const struct usb_device_id *id)
>   #define CMD_LEAF_USB_THROTTLE		77
>   #define CMD_LEAF_LOG_MESSAGE		106
>   
> +/* Leaf frequency options */
> +#define KVASER_USB_LEAF_SWOPTION_FREQ_MASK 0x60
> +#define KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK 0
> +#define KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK BIT(5)
> +#define KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK BIT(6)
> +
>   /* error factors */
>   #define M16C_EF_ACKE			BIT(0)
>   #define M16C_EF_CRCE			BIT(1)
> @@ -469,6 +478,8 @@ 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 {
> @@ -646,6 +657,27 @@ static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
>   	return rc;
>   }
>   
> +static void kvaser_usb_get_software_info_leaf(struct kvaser_usb *dev,
> +					      const struct leaf_msg_softinfo *softinfo)
> +{
> +	u32 sw_options = le32_to_cpu(softinfo->sw_options);
> +
> +	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:
> +		dev->clock.freq = KVASER_USB_CAN_CLOCK_16MHZ;
> +		break;
> +	case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK:
> +		dev->clock.freq = KVASER_USB_CAN_CLOCK_24MHZ;
> +		break;
> +	case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK:
> +		dev->clock.freq = KVASER_USB_CAN_CLOCK_32MHZ;
> +		break;
> +	}
> +}
> +
>   static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
>   {
>   	struct kvaser_msg msg;
> @@ -661,14 +693,13 @@ static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
>   
>   	switch (dev->family) {
>   	case KVASER_LEAF:
> -		dev->fw_version = le32_to_cpu(msg.u.leaf.softinfo.fw_version);
> -		dev->max_tx_urbs =
> -			le16_to_cpu(msg.u.leaf.softinfo.max_outstanding_tx);
> +		kvaser_usb_get_software_info_leaf(dev, &msg.u.leaf.softinfo);
>   		break;
>   	case KVASER_USBCAN:
>   		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;
>   		break;
>   	}
>   
> @@ -1925,7 +1956,7 @@ 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 = CAN_USB_CLOCK;
> +	priv->can.clock.freq = dev->clock.freq;
>   	priv->can.bittiming_const = &kvaser_usb_bittiming_const;
>   	priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
>   	priv->can.do_set_mode = kvaser_usb_set_mode;
> 

  reply	other threads:[~2021-12-22  0:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-22  0:28 [PATCH] can: kvaser_usb: get CAN clock frequency from device Jimmy Assarsson
2021-12-22  0:37 ` Jimmy Assarsson [this message]
2021-12-22 12:00   ` Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=eaefb340-6677-1fcc-83c2-dacc17360b92@gmail.com \
    --to=jimmyassarsson@gmail.com \
    --cc=extja@kvaser.com \
    --cc=linux-can@vger.kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox