2019-05-29 07:17:59 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2016-06-08 18:00:26 +00:00
|
|
|
/* CAN driver for Geschwister Schneider USB/CAN devices
|
|
|
|
* and bytewerk.org candleLight USB CAN interfaces.
|
2013-12-24 19:29:45 +00:00
|
|
|
*
|
2016-06-08 18:00:26 +00:00
|
|
|
* Copyright (C) 2013-2016 Geschwister Schneider Technologie-,
|
2013-12-24 19:29:45 +00:00
|
|
|
* Entwicklungs- und Vertriebs UG (Haftungsbeschränkt).
|
2016-06-08 18:00:26 +00:00
|
|
|
* Copyright (C) 2016 Hubert Denkmair
|
2023-06-02 09:29:07 +02:00
|
|
|
* Copyright (c) 2023 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
|
2013-12-24 19:29:45 +00:00
|
|
|
*
|
|
|
|
* Many thanks to all socketcan devs!
|
|
|
|
*/
|
|
|
|
|
2021-12-07 19:43:24 +01:00
|
|
|
#include <linux/bitfield.h>
|
2022-08-22 12:01:10 +02:00
|
|
|
#include <linux/clocksource.h>
|
2020-11-20 14:50:52 -08:00
|
|
|
#include <linux/ethtool.h>
|
2013-12-24 19:29:45 +00:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/netdevice.h>
|
2021-12-31 20:12:51 +01:00
|
|
|
#include <linux/signal.h>
|
2022-08-22 12:01:10 +02:00
|
|
|
#include <linux/timecounter.h>
|
|
|
|
#include <linux/units.h>
|
2013-12-24 19:29:45 +00:00
|
|
|
#include <linux/usb.h>
|
2022-08-22 12:01:10 +02:00
|
|
|
#include <linux/workqueue.h>
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
#include <linux/can.h>
|
|
|
|
#include <linux/can/dev.h>
|
|
|
|
#include <linux/can/error.h>
|
2023-06-02 09:29:07 +02:00
|
|
|
#include <linux/can/rx-offload.h>
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
/* Device specific constants */
|
2022-08-27 19:03:25 +02:00
|
|
|
#define USB_GS_USB_1_VENDOR_ID 0x1d50
|
|
|
|
#define USB_GS_USB_1_PRODUCT_ID 0x606f
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2020-11-19 12:45:08 +01:00
|
|
|
#define USB_CANDLELIGHT_VENDOR_ID 0x1209
|
2016-06-08 18:00:26 +00:00
|
|
|
#define USB_CANDLELIGHT_PRODUCT_ID 0x2323
|
|
|
|
|
2021-11-08 11:41:57 +01:00
|
|
|
#define USB_CES_CANEXT_FD_VENDOR_ID 0x1cd2
|
|
|
|
#define USB_CES_CANEXT_FD_PRODUCT_ID 0x606f
|
|
|
|
|
2021-12-03 15:16:26 +10:00
|
|
|
#define USB_ABE_CANDEBUGGER_FD_VENDOR_ID 0x16d0
|
|
|
|
#define USB_ABE_CANDEBUGGER_FD_PRODUCT_ID 0x10b8
|
|
|
|
|
2022-08-27 19:03:25 +02:00
|
|
|
#define GS_USB_ENDPOINT_IN 1
|
|
|
|
#define GS_USB_ENDPOINT_OUT 2
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2022-08-22 12:01:10 +02:00
|
|
|
/* Timestamp 32 bit timer runs at 1 MHz (1 µs tick). Worker accounts
|
|
|
|
* for timer overflow (will be after ~71 minutes)
|
|
|
|
*/
|
|
|
|
#define GS_USB_TIMESTAMP_TIMER_HZ (1 * HZ_PER_MHZ)
|
|
|
|
#define GS_USB_TIMESTAMP_WORK_DELAY_SEC 1800
|
|
|
|
static_assert(GS_USB_TIMESTAMP_WORK_DELAY_SEC <
|
|
|
|
CYCLECOUNTER_MASK(32) / GS_USB_TIMESTAMP_TIMER_HZ / 2);
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
/* Device specific constants */
|
|
|
|
enum gs_usb_breq {
|
|
|
|
GS_USB_BREQ_HOST_FORMAT = 0,
|
|
|
|
GS_USB_BREQ_BITTIMING,
|
|
|
|
GS_USB_BREQ_MODE,
|
|
|
|
GS_USB_BREQ_BERR,
|
|
|
|
GS_USB_BREQ_BT_CONST,
|
2016-06-10 20:39:22 +00:00
|
|
|
GS_USB_BREQ_DEVICE_CONFIG,
|
|
|
|
GS_USB_BREQ_TIMESTAMP,
|
|
|
|
GS_USB_BREQ_IDENTIFY,
|
2021-11-07 14:50:11 +01:00
|
|
|
GS_USB_BREQ_GET_USER_ID,
|
2021-12-30 20:22:07 +01:00
|
|
|
GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING = GS_USB_BREQ_GET_USER_ID,
|
2021-11-07 14:50:11 +01:00
|
|
|
GS_USB_BREQ_SET_USER_ID,
|
2021-12-07 19:35:31 +01:00
|
|
|
GS_USB_BREQ_DATA_BITTIMING,
|
2021-11-08 17:05:44 +01:00
|
|
|
GS_USB_BREQ_BT_CONST_EXT,
|
2022-09-18 16:41:38 +02:00
|
|
|
GS_USB_BREQ_SET_TERMINATION,
|
|
|
|
GS_USB_BREQ_GET_TERMINATION,
|
2022-08-28 12:25:02 +02:00
|
|
|
GS_USB_BREQ_GET_STATE,
|
2013-12-24 19:29:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum gs_can_mode {
|
|
|
|
/* reset a channel. turns it off */
|
|
|
|
GS_CAN_MODE_RESET = 0,
|
|
|
|
/* starts a channel */
|
|
|
|
GS_CAN_MODE_START
|
|
|
|
};
|
|
|
|
|
|
|
|
enum gs_can_state {
|
|
|
|
GS_CAN_STATE_ERROR_ACTIVE = 0,
|
|
|
|
GS_CAN_STATE_ERROR_WARNING,
|
|
|
|
GS_CAN_STATE_ERROR_PASSIVE,
|
|
|
|
GS_CAN_STATE_BUS_OFF,
|
|
|
|
GS_CAN_STATE_STOPPED,
|
|
|
|
GS_CAN_STATE_SLEEPING
|
|
|
|
};
|
|
|
|
|
2016-06-10 20:39:22 +00:00
|
|
|
enum gs_can_identify_mode {
|
|
|
|
GS_CAN_IDENTIFY_OFF = 0,
|
|
|
|
GS_CAN_IDENTIFY_ON
|
|
|
|
};
|
|
|
|
|
2022-09-18 16:41:38 +02:00
|
|
|
enum gs_can_termination_state {
|
|
|
|
GS_CAN_TERMINATION_STATE_OFF = 0,
|
|
|
|
GS_CAN_TERMINATION_STATE_ON
|
|
|
|
};
|
|
|
|
|
|
|
|
#define GS_USB_TERMINATION_DISABLED CAN_TERMINATION_DISABLED
|
|
|
|
#define GS_USB_TERMINATION_ENABLED 120
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
/* data types passed between host and device */
|
2020-11-19 14:03:17 +01:00
|
|
|
|
|
|
|
/* The firmware on the original USB2CAN by Geschwister Schneider
|
|
|
|
* Technologie Entwicklungs- und Vertriebs UG exchanges all data
|
|
|
|
* between the host and the device in host byte order. This is done
|
|
|
|
* with the struct gs_host_config::byte_order member, which is sent
|
|
|
|
* first to indicate the desired byte order.
|
|
|
|
*
|
|
|
|
* The widely used open source firmware candleLight doesn't support
|
|
|
|
* this feature and exchanges the data in little endian byte order.
|
|
|
|
*/
|
2013-12-24 19:29:45 +00:00
|
|
|
struct gs_host_config {
|
2020-11-19 14:03:17 +01:00
|
|
|
__le32 byte_order;
|
2013-12-24 19:29:45 +00:00
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct gs_device_config {
|
|
|
|
u8 reserved1;
|
|
|
|
u8 reserved2;
|
|
|
|
u8 reserved3;
|
|
|
|
u8 icount;
|
2020-11-19 14:03:17 +01:00
|
|
|
__le32 sw_version;
|
|
|
|
__le32 hw_version;
|
2013-12-24 19:29:45 +00:00
|
|
|
} __packed;
|
|
|
|
|
2020-11-19 12:45:08 +01:00
|
|
|
#define GS_CAN_MODE_NORMAL 0
|
|
|
|
#define GS_CAN_MODE_LISTEN_ONLY BIT(0)
|
|
|
|
#define GS_CAN_MODE_LOOP_BACK BIT(1)
|
|
|
|
#define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2)
|
|
|
|
#define GS_CAN_MODE_ONE_SHOT BIT(3)
|
2021-11-07 14:36:51 +01:00
|
|
|
#define GS_CAN_MODE_HW_TIMESTAMP BIT(4)
|
2021-11-07 14:44:36 +01:00
|
|
|
/* GS_CAN_FEATURE_IDENTIFY BIT(5) */
|
2021-11-07 14:50:11 +01:00
|
|
|
/* GS_CAN_FEATURE_USER_ID BIT(6) */
|
2021-11-07 14:52:10 +01:00
|
|
|
#define GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
|
2021-12-07 19:35:31 +01:00
|
|
|
#define GS_CAN_MODE_FD BIT(8)
|
2021-12-07 19:43:24 +01:00
|
|
|
/* GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9) */
|
2021-11-08 17:05:44 +01:00
|
|
|
/* GS_CAN_FEATURE_BT_CONST_EXT BIT(10) */
|
2022-09-18 16:41:38 +02:00
|
|
|
/* GS_CAN_FEATURE_TERMINATION BIT(11) */
|
2022-08-28 10:59:10 +02:00
|
|
|
#define GS_CAN_MODE_BERR_REPORTING BIT(12)
|
2022-08-28 12:25:02 +02:00
|
|
|
/* GS_CAN_FEATURE_GET_STATE BIT(13) */
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
struct gs_device_mode {
|
2020-11-19 14:03:17 +01:00
|
|
|
__le32 mode;
|
|
|
|
__le32 flags;
|
2013-12-24 19:29:45 +00:00
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct gs_device_state {
|
2020-11-19 14:03:17 +01:00
|
|
|
__le32 state;
|
|
|
|
__le32 rxerr;
|
|
|
|
__le32 txerr;
|
2013-12-24 19:29:45 +00:00
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct gs_device_bittiming {
|
2020-11-19 14:03:17 +01:00
|
|
|
__le32 prop_seg;
|
|
|
|
__le32 phase_seg1;
|
|
|
|
__le32 phase_seg2;
|
|
|
|
__le32 sjw;
|
|
|
|
__le32 brp;
|
2013-12-24 19:29:45 +00:00
|
|
|
} __packed;
|
|
|
|
|
2016-06-10 20:39:22 +00:00
|
|
|
struct gs_identify_mode {
|
2020-11-19 14:03:17 +01:00
|
|
|
__le32 mode;
|
2016-06-10 20:39:22 +00:00
|
|
|
} __packed;
|
|
|
|
|
2022-09-18 16:41:38 +02:00
|
|
|
struct gs_device_termination_state {
|
|
|
|
__le32 state;
|
|
|
|
} __packed;
|
|
|
|
|
2020-11-19 12:45:08 +01:00
|
|
|
#define GS_CAN_FEATURE_LISTEN_ONLY BIT(0)
|
|
|
|
#define GS_CAN_FEATURE_LOOP_BACK BIT(1)
|
|
|
|
#define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2)
|
|
|
|
#define GS_CAN_FEATURE_ONE_SHOT BIT(3)
|
|
|
|
#define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4)
|
|
|
|
#define GS_CAN_FEATURE_IDENTIFY BIT(5)
|
2021-11-07 14:50:11 +01:00
|
|
|
#define GS_CAN_FEATURE_USER_ID BIT(6)
|
2021-11-07 14:52:10 +01:00
|
|
|
#define GS_CAN_FEATURE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
|
2021-12-07 19:35:31 +01:00
|
|
|
#define GS_CAN_FEATURE_FD BIT(8)
|
2021-12-07 19:43:24 +01:00
|
|
|
#define GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9)
|
2021-11-08 17:05:44 +01:00
|
|
|
#define GS_CAN_FEATURE_BT_CONST_EXT BIT(10)
|
2022-09-18 16:41:38 +02:00
|
|
|
#define GS_CAN_FEATURE_TERMINATION BIT(11)
|
2022-08-28 10:59:10 +02:00
|
|
|
#define GS_CAN_FEATURE_BERR_REPORTING BIT(12)
|
2022-08-28 12:25:02 +02:00
|
|
|
#define GS_CAN_FEATURE_GET_STATE BIT(13)
|
|
|
|
#define GS_CAN_FEATURE_MASK GENMASK(13, 0)
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2021-12-30 20:22:07 +01:00
|
|
|
/* internal quirks - keep in GS_CAN_FEATURE space for now */
|
|
|
|
|
|
|
|
/* CANtact Pro original firmware:
|
|
|
|
* BREQ DATA_BITTIMING overlaps with GET_USER_ID
|
|
|
|
*/
|
|
|
|
#define GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO BIT(31)
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
struct gs_device_bt_const {
|
2020-11-19 14:03:17 +01:00
|
|
|
__le32 feature;
|
|
|
|
__le32 fclk_can;
|
|
|
|
__le32 tseg1_min;
|
|
|
|
__le32 tseg1_max;
|
|
|
|
__le32 tseg2_min;
|
|
|
|
__le32 tseg2_max;
|
|
|
|
__le32 sjw_max;
|
|
|
|
__le32 brp_min;
|
|
|
|
__le32 brp_max;
|
|
|
|
__le32 brp_inc;
|
2013-12-24 19:29:45 +00:00
|
|
|
} __packed;
|
|
|
|
|
2021-11-08 17:05:44 +01:00
|
|
|
struct gs_device_bt_const_extended {
|
|
|
|
__le32 feature;
|
|
|
|
__le32 fclk_can;
|
|
|
|
__le32 tseg1_min;
|
|
|
|
__le32 tseg1_max;
|
|
|
|
__le32 tseg2_min;
|
|
|
|
__le32 tseg2_max;
|
|
|
|
__le32 sjw_max;
|
|
|
|
__le32 brp_min;
|
|
|
|
__le32 brp_max;
|
|
|
|
__le32 brp_inc;
|
|
|
|
|
|
|
|
__le32 dtseg1_min;
|
|
|
|
__le32 dtseg1_max;
|
|
|
|
__le32 dtseg2_min;
|
|
|
|
__le32 dtseg2_max;
|
|
|
|
__le32 dsjw_max;
|
|
|
|
__le32 dbrp_min;
|
|
|
|
__le32 dbrp_max;
|
|
|
|
__le32 dbrp_inc;
|
|
|
|
} __packed;
|
|
|
|
|
2020-11-19 13:23:35 +01:00
|
|
|
#define GS_CAN_FLAG_OVERFLOW BIT(0)
|
2021-12-07 19:35:31 +01:00
|
|
|
#define GS_CAN_FLAG_FD BIT(1)
|
|
|
|
#define GS_CAN_FLAG_BRS BIT(2)
|
|
|
|
#define GS_CAN_FLAG_ESI BIT(3)
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2021-12-07 19:22:50 +01:00
|
|
|
struct classic_can {
|
|
|
|
u8 data[8];
|
|
|
|
} __packed;
|
|
|
|
|
2022-08-22 12:01:10 +02:00
|
|
|
struct classic_can_ts {
|
|
|
|
u8 data[8];
|
|
|
|
__le32 timestamp_us;
|
|
|
|
} __packed;
|
|
|
|
|
2021-12-07 19:43:24 +01:00
|
|
|
struct classic_can_quirk {
|
|
|
|
u8 data[8];
|
|
|
|
u8 quirk;
|
|
|
|
} __packed;
|
|
|
|
|
2021-12-07 19:35:31 +01:00
|
|
|
struct canfd {
|
|
|
|
u8 data[64];
|
|
|
|
} __packed;
|
|
|
|
|
2022-08-22 12:01:10 +02:00
|
|
|
struct canfd_ts {
|
|
|
|
u8 data[64];
|
|
|
|
__le32 timestamp_us;
|
|
|
|
} __packed;
|
|
|
|
|
2021-12-07 19:43:24 +01:00
|
|
|
struct canfd_quirk {
|
|
|
|
u8 data[64];
|
|
|
|
u8 quirk;
|
|
|
|
} __packed;
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
struct gs_host_frame {
|
|
|
|
u32 echo_id;
|
2020-11-19 14:03:17 +01:00
|
|
|
__le32 can_id;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
u8 can_dlc;
|
|
|
|
u8 channel;
|
|
|
|
u8 flags;
|
|
|
|
u8 reserved;
|
|
|
|
|
2021-12-07 19:22:50 +01:00
|
|
|
union {
|
|
|
|
DECLARE_FLEX_ARRAY(struct classic_can, classic_can);
|
2022-08-22 12:01:10 +02:00
|
|
|
DECLARE_FLEX_ARRAY(struct classic_can_ts, classic_can_ts);
|
2021-12-07 19:43:24 +01:00
|
|
|
DECLARE_FLEX_ARRAY(struct classic_can_quirk, classic_can_quirk);
|
2021-12-07 19:35:31 +01:00
|
|
|
DECLARE_FLEX_ARRAY(struct canfd, canfd);
|
2022-08-22 12:01:10 +02:00
|
|
|
DECLARE_FLEX_ARRAY(struct canfd_ts, canfd_ts);
|
2021-12-07 19:43:24 +01:00
|
|
|
DECLARE_FLEX_ARRAY(struct canfd_quirk, canfd_quirk);
|
2021-12-07 19:22:50 +01:00
|
|
|
};
|
2013-12-24 19:29:45 +00:00
|
|
|
} __packed;
|
|
|
|
/* The GS USB devices make use of the same flags and masks as in
|
|
|
|
* linux/can.h and linux/can/error.h, and no additional mapping is necessary.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
|
|
|
|
#define GS_MAX_TX_URBS 10
|
|
|
|
/* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
|
|
|
|
#define GS_MAX_RX_URBS 30
|
2023-06-02 09:29:07 +02:00
|
|
|
#define GS_NAPI_WEIGHT 32
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
/* Maximum number of interfaces the driver supports per device.
|
2022-02-04 15:40:31 +01:00
|
|
|
* Current hardware only supports 3 interfaces. The future may vary.
|
2013-12-24 19:29:45 +00:00
|
|
|
*/
|
2022-02-04 15:40:31 +01:00
|
|
|
#define GS_MAX_INTF 3
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
struct gs_tx_context {
|
|
|
|
struct gs_can *dev;
|
|
|
|
unsigned int echo_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct gs_can {
|
|
|
|
struct can_priv can; /* must be the first member */
|
|
|
|
|
2023-06-02 09:29:07 +02:00
|
|
|
struct can_rx_offload offload;
|
2013-12-24 19:29:45 +00:00
|
|
|
struct gs_usb *parent;
|
|
|
|
|
|
|
|
struct net_device *netdev;
|
|
|
|
struct usb_device *udev;
|
|
|
|
|
2021-11-08 17:05:44 +01:00
|
|
|
struct can_bittiming_const bt_const, data_bt_const;
|
2013-12-24 19:29:45 +00:00
|
|
|
unsigned int channel; /* channel number */
|
|
|
|
|
2021-12-07 19:43:24 +01:00
|
|
|
u32 feature;
|
2021-12-07 19:22:50 +01:00
|
|
|
unsigned int hf_size_tx;
|
|
|
|
|
2015-06-29 19:09:57 -04:00
|
|
|
/* This lock prevents a race condition between xmit and receive. */
|
2013-12-24 19:29:45 +00:00
|
|
|
spinlock_t tx_ctx_lock;
|
|
|
|
struct gs_tx_context tx_context[GS_MAX_TX_URBS];
|
|
|
|
|
|
|
|
struct usb_anchor tx_submitted;
|
|
|
|
atomic_t active_tx_urbs;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* usb interface struct */
|
|
|
|
struct gs_usb {
|
|
|
|
struct gs_can *canch[GS_MAX_INTF];
|
|
|
|
struct usb_anchor rx_submitted;
|
|
|
|
struct usb_device *udev;
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
|
|
|
|
/* time counter for hardware timestamps */
|
|
|
|
struct cyclecounter cc;
|
|
|
|
struct timecounter tc;
|
|
|
|
spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
|
|
|
|
struct delayed_work timestamp;
|
|
|
|
|
2021-12-07 19:22:50 +01:00
|
|
|
unsigned int hf_size_rx;
|
2022-02-15 08:48:14 +09:00
|
|
|
u8 active_channels;
|
2013-12-24 19:29:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* 'allocate' a tx context.
|
|
|
|
* returns a valid tx context or NULL if there is no space.
|
|
|
|
*/
|
|
|
|
static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dev->tx_ctx_lock, flags);
|
|
|
|
|
|
|
|
for (; i < GS_MAX_TX_URBS; i++) {
|
|
|
|
if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
|
|
|
|
dev->tx_context[i].echo_id = i;
|
|
|
|
spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
|
|
|
|
return &dev->tx_context[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* releases a tx context
|
|
|
|
*/
|
|
|
|
static void gs_free_tx_context(struct gs_tx_context *txc)
|
|
|
|
{
|
|
|
|
txc->echo_id = GS_MAX_TX_URBS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a tx context by id.
|
|
|
|
*/
|
2016-06-10 20:39:22 +00:00
|
|
|
static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
|
|
|
|
unsigned int id)
|
2013-12-24 19:29:45 +00:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (id < GS_MAX_TX_URBS) {
|
|
|
|
spin_lock_irqsave(&dev->tx_ctx_lock, flags);
|
|
|
|
if (dev->tx_context[id].echo_id == id) {
|
|
|
|
spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
|
|
|
|
return &dev->tx_context[id];
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-09-20 23:21:42 +02:00
|
|
|
static int gs_cmd_reset(struct gs_can *dev)
|
2013-12-24 19:29:45 +00:00
|
|
|
{
|
2022-09-18 22:42:59 +02:00
|
|
|
struct gs_device_mode dm = {
|
2024-03-04 08:45:30 +01:00
|
|
|
.mode = cpu_to_le32(GS_CAN_MODE_RESET),
|
2022-09-18 22:42:59 +02:00
|
|
|
};
|
2017-06-04 14:03:42 +02:00
|
|
|
|
2022-12-08 17:11:42 +09:00
|
|
|
return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE,
|
2022-09-18 22:42:59 +02:00
|
|
|
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
|
|
|
|
dev->channel, 0, &dm, sizeof(dm), 1000,
|
|
|
|
GFP_KERNEL);
|
2013-12-24 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
static inline int gs_usb_get_timestamp(const struct gs_usb *parent,
|
2022-08-22 12:01:10 +02:00
|
|
|
u32 *timestamp_p)
|
|
|
|
{
|
|
|
|
__le32 timestamp;
|
|
|
|
int rc;
|
|
|
|
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
rc = usb_control_msg_recv(parent->udev, 0, GS_USB_BREQ_TIMESTAMP,
|
2022-08-22 12:01:10 +02:00
|
|
|
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
0, 0,
|
2022-08-22 12:01:10 +02:00
|
|
|
×tamp, sizeof(timestamp),
|
|
|
|
USB_CTRL_GET_TIMEOUT,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
*timestamp_p = le32_to_cpu(timestamp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-19 09:53:45 +02:00
|
|
|
static u64 gs_usb_timestamp_read(const struct cyclecounter *cc) __must_hold(&dev->tc_lock)
|
2022-08-22 12:01:10 +02:00
|
|
|
{
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
struct gs_usb *parent = container_of(cc, struct gs_usb, cc);
|
2022-08-22 12:01:10 +02:00
|
|
|
u32 timestamp = 0;
|
|
|
|
int err;
|
|
|
|
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
lockdep_assert_held(&parent->tc_lock);
|
2022-09-19 09:53:45 +02:00
|
|
|
|
|
|
|
/* drop lock for synchronous USB transfer */
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
spin_unlock_bh(&parent->tc_lock);
|
|
|
|
err = gs_usb_get_timestamp(parent, ×tamp);
|
|
|
|
spin_lock_bh(&parent->tc_lock);
|
2022-08-22 12:01:10 +02:00
|
|
|
if (err)
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
dev_err(&parent->udev->dev,
|
|
|
|
"Error %d while reading timestamp. HW timestamps may be inaccurate.",
|
|
|
|
err);
|
2022-08-22 12:01:10 +02:00
|
|
|
|
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gs_usb_timestamp_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct delayed_work *delayed_work = to_delayed_work(work);
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
struct gs_usb *parent;
|
2022-08-22 12:01:10 +02:00
|
|
|
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
parent = container_of(delayed_work, struct gs_usb, timestamp);
|
|
|
|
spin_lock_bh(&parent->tc_lock);
|
|
|
|
timecounter_read(&parent->tc);
|
|
|
|
spin_unlock_bh(&parent->tc_lock);
|
2022-08-22 12:01:10 +02:00
|
|
|
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
schedule_delayed_work(&parent->timestamp,
|
2022-08-22 12:01:10 +02:00
|
|
|
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
|
|
|
|
}
|
|
|
|
|
2022-09-19 09:53:45 +02:00
|
|
|
static void gs_usb_skb_set_timestamp(struct gs_can *dev,
|
2022-08-22 12:01:10 +02:00
|
|
|
struct sk_buff *skb, u32 timestamp)
|
|
|
|
{
|
|
|
|
struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
struct gs_usb *parent = dev->parent;
|
2022-08-22 12:01:10 +02:00
|
|
|
u64 ns;
|
|
|
|
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
spin_lock_bh(&parent->tc_lock);
|
|
|
|
ns = timecounter_cyc2time(&parent->tc, timestamp);
|
|
|
|
spin_unlock_bh(&parent->tc_lock);
|
2022-09-19 09:53:45 +02:00
|
|
|
|
2022-08-22 12:01:10 +02:00
|
|
|
hwtstamps->hwtstamp = ns_to_ktime(ns);
|
|
|
|
}
|
|
|
|
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
static void gs_usb_timestamp_init(struct gs_usb *parent)
|
2022-08-22 12:01:10 +02:00
|
|
|
{
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
struct cyclecounter *cc = &parent->cc;
|
2022-08-22 12:01:10 +02:00
|
|
|
|
|
|
|
cc->read = gs_usb_timestamp_read;
|
|
|
|
cc->mask = CYCLECOUNTER_MASK(32);
|
|
|
|
cc->shift = 32 - bits_per(NSEC_PER_SEC / GS_USB_TIMESTAMP_TIMER_HZ);
|
|
|
|
cc->mult = clocksource_hz2mult(GS_USB_TIMESTAMP_TIMER_HZ, cc->shift);
|
|
|
|
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
spin_lock_init(&parent->tc_lock);
|
|
|
|
spin_lock_bh(&parent->tc_lock);
|
|
|
|
timecounter_init(&parent->tc, &parent->cc, ktime_get_real_ns());
|
|
|
|
spin_unlock_bh(&parent->tc_lock);
|
2022-08-22 12:01:10 +02:00
|
|
|
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
INIT_DELAYED_WORK(&parent->timestamp, gs_usb_timestamp_work);
|
|
|
|
schedule_delayed_work(&parent->timestamp,
|
2022-08-22 12:01:10 +02:00
|
|
|
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
|
|
|
|
}
|
|
|
|
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
static void gs_usb_timestamp_stop(struct gs_usb *parent)
|
2022-08-22 12:01:10 +02:00
|
|
|
{
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
cancel_delayed_work_sync(&parent->timestamp);
|
2022-08-22 12:01:10 +02:00
|
|
|
}
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
|
|
|
|
{
|
|
|
|
struct can_device_stats *can_stats = &dev->can.can_stats;
|
|
|
|
|
|
|
|
if (cf->can_id & CAN_ERR_RESTARTED) {
|
|
|
|
dev->can.state = CAN_STATE_ERROR_ACTIVE;
|
|
|
|
can_stats->restarts++;
|
|
|
|
} else if (cf->can_id & CAN_ERR_BUSOFF) {
|
|
|
|
dev->can.state = CAN_STATE_BUS_OFF;
|
|
|
|
can_stats->bus_off++;
|
|
|
|
} else if (cf->can_id & CAN_ERR_CRTL) {
|
|
|
|
if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
|
|
|
|
(cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
|
|
|
|
dev->can.state = CAN_STATE_ERROR_WARNING;
|
|
|
|
can_stats->error_warning++;
|
|
|
|
} else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
|
|
|
|
(cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
|
|
|
|
dev->can.state = CAN_STATE_ERROR_PASSIVE;
|
|
|
|
can_stats->error_passive++;
|
|
|
|
} else {
|
|
|
|
dev->can.state = CAN_STATE_ERROR_ACTIVE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 09:29:07 +02:00
|
|
|
static u32 gs_usb_set_timestamp(struct gs_can *dev, struct sk_buff *skb,
|
|
|
|
const struct gs_host_frame *hf)
|
2022-08-22 12:01:10 +02:00
|
|
|
{
|
|
|
|
u32 timestamp;
|
|
|
|
|
|
|
|
if (hf->flags & GS_CAN_FLAG_FD)
|
|
|
|
timestamp = le32_to_cpu(hf->canfd_ts->timestamp_us);
|
|
|
|
else
|
|
|
|
timestamp = le32_to_cpu(hf->classic_can_ts->timestamp_us);
|
|
|
|
|
2023-06-02 09:29:07 +02:00
|
|
|
if (skb)
|
|
|
|
gs_usb_skb_set_timestamp(dev, skb, timestamp);
|
|
|
|
|
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gs_usb_rx_offload(struct gs_can *dev, struct sk_buff *skb,
|
|
|
|
const struct gs_host_frame *hf)
|
|
|
|
{
|
|
|
|
struct can_rx_offload *offload = &dev->offload;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) {
|
|
|
|
const u32 ts = gs_usb_set_timestamp(dev, skb, hf);
|
|
|
|
|
|
|
|
rc = can_rx_offload_queue_timestamp(offload, skb, ts);
|
|
|
|
} else {
|
|
|
|
rc = can_rx_offload_queue_tail(offload, skb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc)
|
|
|
|
dev->netdev->stats.rx_fifo_errors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
gs_usb_get_echo_skb(struct gs_can *dev, struct sk_buff *skb,
|
|
|
|
const struct gs_host_frame *hf)
|
|
|
|
{
|
|
|
|
struct can_rx_offload *offload = &dev->offload;
|
|
|
|
const u32 echo_id = hf->echo_id;
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) {
|
|
|
|
const u32 ts = gs_usb_set_timestamp(dev, skb, hf);
|
|
|
|
|
|
|
|
len = can_rx_offload_get_echo_skb_queue_timestamp(offload, echo_id,
|
|
|
|
ts, NULL);
|
|
|
|
} else {
|
|
|
|
len = can_rx_offload_get_echo_skb_queue_tail(offload, echo_id,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
2022-08-22 12:01:10 +02:00
|
|
|
}
|
|
|
|
|
2015-06-29 19:09:57 -04:00
|
|
|
static void gs_usb_receive_bulk_callback(struct urb *urb)
|
2013-12-24 19:29:45 +00:00
|
|
|
{
|
2023-07-06 16:16:33 +02:00
|
|
|
struct gs_usb *parent = urb->context;
|
2013-12-24 19:29:45 +00:00
|
|
|
struct gs_can *dev;
|
|
|
|
struct net_device *netdev;
|
|
|
|
int rc;
|
|
|
|
struct net_device_stats *stats;
|
|
|
|
struct gs_host_frame *hf = urb->transfer_buffer;
|
|
|
|
struct gs_tx_context *txc;
|
|
|
|
struct can_frame *cf;
|
2021-12-07 19:35:31 +01:00
|
|
|
struct canfd_frame *cfd;
|
2013-12-24 19:29:45 +00:00
|
|
|
struct sk_buff *skb;
|
|
|
|
|
2023-07-06 16:16:33 +02:00
|
|
|
BUG_ON(!parent);
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
switch (urb->status) {
|
|
|
|
case 0: /* success */
|
|
|
|
break;
|
|
|
|
case -ENOENT:
|
|
|
|
case -ESHUTDOWN:
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
/* do not resubmit aborted urbs. eg: when device goes down */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* device reports out of range channel id */
|
|
|
|
if (hf->channel >= GS_MAX_INTF)
|
2021-12-10 10:03:09 +01:00
|
|
|
goto device_detach;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2023-07-06 16:16:33 +02:00
|
|
|
dev = parent->canch[hf->channel];
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
netdev = dev->netdev;
|
|
|
|
stats = &netdev->stats;
|
|
|
|
|
|
|
|
if (!netif_device_present(netdev))
|
|
|
|
return;
|
|
|
|
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
if (!netif_running(netdev))
|
|
|
|
goto resubmit_urb;
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
if (hf->echo_id == -1) { /* normal rx */
|
2021-12-07 19:35:31 +01:00
|
|
|
if (hf->flags & GS_CAN_FLAG_FD) {
|
2023-07-03 20:37:20 +02:00
|
|
|
skb = alloc_canfd_skb(netdev, &cfd);
|
2021-12-07 19:35:31 +01:00
|
|
|
if (!skb)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cfd->can_id = le32_to_cpu(hf->can_id);
|
|
|
|
cfd->len = can_fd_dlc2len(hf->can_dlc);
|
|
|
|
if (hf->flags & GS_CAN_FLAG_BRS)
|
|
|
|
cfd->flags |= CANFD_BRS;
|
|
|
|
if (hf->flags & GS_CAN_FLAG_ESI)
|
|
|
|
cfd->flags |= CANFD_ESI;
|
|
|
|
|
|
|
|
memcpy(cfd->data, hf->canfd->data, cfd->len);
|
|
|
|
} else {
|
2023-07-03 20:37:20 +02:00
|
|
|
skb = alloc_can_skb(netdev, &cf);
|
2021-12-07 19:35:31 +01:00
|
|
|
if (!skb)
|
|
|
|
return;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2021-12-07 19:35:31 +01:00
|
|
|
cf->can_id = le32_to_cpu(hf->can_id);
|
|
|
|
can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2021-12-07 19:35:31 +01:00
|
|
|
memcpy(cf->data, hf->classic_can->data, 8);
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2021-12-07 19:35:31 +01:00
|
|
|
/* ERROR frames tell us information about the controller */
|
|
|
|
if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
|
|
|
|
gs_update_state(dev, cf);
|
|
|
|
}
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2023-06-02 09:29:07 +02:00
|
|
|
gs_usb_rx_offload(dev, skb, hf);
|
2013-12-24 19:29:45 +00:00
|
|
|
} else { /* echo_id == hf->echo_id */
|
|
|
|
if (hf->echo_id >= GS_MAX_TX_URBS) {
|
|
|
|
netdev_err(netdev,
|
2021-09-09 16:39:28 +02:00
|
|
|
"Unexpected out of range echo id %u\n",
|
2013-12-24 19:29:45 +00:00
|
|
|
hf->echo_id);
|
|
|
|
goto resubmit_urb;
|
|
|
|
}
|
|
|
|
|
|
|
|
txc = gs_get_tx_context(dev, hf->echo_id);
|
|
|
|
|
|
|
|
/* bad devices send bad echo_ids. */
|
|
|
|
if (!txc) {
|
|
|
|
netdev_err(netdev,
|
2021-09-09 16:39:28 +02:00
|
|
|
"Unexpected unused echo id %u\n",
|
2013-12-24 19:29:45 +00:00
|
|
|
hf->echo_id);
|
|
|
|
goto resubmit_urb;
|
|
|
|
}
|
|
|
|
|
2022-08-22 12:01:10 +02:00
|
|
|
skb = dev->can.echo_skb[hf->echo_id];
|
2023-07-03 20:39:19 +02:00
|
|
|
stats->tx_packets++;
|
2023-06-02 09:29:07 +02:00
|
|
|
stats->tx_bytes += gs_usb_get_echo_skb(dev, skb, hf);
|
2013-12-24 19:29:45 +00:00
|
|
|
gs_free_tx_context(txc);
|
|
|
|
|
2017-09-14 18:37:14 +02:00
|
|
|
atomic_dec(&dev->active_tx_urbs);
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
netif_wake_queue(netdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
|
2023-07-04 11:23:37 +02:00
|
|
|
stats->rx_over_errors++;
|
|
|
|
stats->rx_errors++;
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
skb = alloc_can_err_skb(netdev, &cf);
|
|
|
|
if (!skb)
|
|
|
|
goto resubmit_urb;
|
|
|
|
|
|
|
|
cf->can_id |= CAN_ERR_CRTL;
|
2020-11-20 11:04:44 +01:00
|
|
|
cf->len = CAN_ERR_DLC;
|
2013-12-24 19:29:45 +00:00
|
|
|
cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
|
2023-06-02 09:29:07 +02:00
|
|
|
|
|
|
|
gs_usb_rx_offload(dev, skb, hf);
|
2013-12-24 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 09:29:07 +02:00
|
|
|
can_rx_offload_irq_finish(&dev->offload);
|
|
|
|
|
2023-07-18 11:11:22 +02:00
|
|
|
resubmit_urb:
|
2023-07-06 16:16:33 +02:00
|
|
|
usb_fill_bulk_urb(urb, parent->udev,
|
|
|
|
usb_rcvbulkpipe(parent->udev, GS_USB_ENDPOINT_IN),
|
2021-12-07 19:22:50 +01:00
|
|
|
hf, dev->parent->hf_size_rx,
|
2023-07-06 16:16:33 +02:00
|
|
|
gs_usb_receive_bulk_callback, parent);
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
rc = usb_submit_urb(urb, GFP_ATOMIC);
|
|
|
|
|
|
|
|
/* USB failure take down all interfaces */
|
|
|
|
if (rc == -ENODEV) {
|
2023-07-18 11:11:22 +02:00
|
|
|
device_detach:
|
2013-12-24 19:29:45 +00:00
|
|
|
for (rc = 0; rc < GS_MAX_INTF; rc++) {
|
2023-07-06 16:16:33 +02:00
|
|
|
if (parent->canch[rc])
|
|
|
|
netif_device_detach(parent->canch[rc]->netdev);
|
2013-12-24 19:29:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gs_usb_set_bittiming(struct net_device *netdev)
|
|
|
|
{
|
|
|
|
struct gs_can *dev = netdev_priv(netdev);
|
|
|
|
struct can_bittiming *bt = &dev->can.bittiming;
|
2022-09-18 22:42:59 +02:00
|
|
|
struct gs_device_bittiming dbt = {
|
|
|
|
.prop_seg = cpu_to_le32(bt->prop_seg),
|
|
|
|
.phase_seg1 = cpu_to_le32(bt->phase_seg1),
|
|
|
|
.phase_seg2 = cpu_to_le32(bt->phase_seg2),
|
|
|
|
.sjw = cpu_to_le32(bt->sjw),
|
|
|
|
.brp = cpu_to_le32(bt->brp),
|
|
|
|
};
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
/* request bit timings */
|
2022-12-08 17:11:42 +09:00
|
|
|
return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_BITTIMING,
|
2022-09-18 22:42:59 +02:00
|
|
|
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
|
|
|
|
dev->channel, 0, &dbt, sizeof(dbt), 1000,
|
|
|
|
GFP_KERNEL);
|
2013-12-24 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 19:35:31 +01:00
|
|
|
static int gs_usb_set_data_bittiming(struct net_device *netdev)
|
|
|
|
{
|
|
|
|
struct gs_can *dev = netdev_priv(netdev);
|
|
|
|
struct can_bittiming *bt = &dev->can.data_bittiming;
|
2022-09-18 22:42:59 +02:00
|
|
|
struct gs_device_bittiming dbt = {
|
|
|
|
.prop_seg = cpu_to_le32(bt->prop_seg),
|
|
|
|
.phase_seg1 = cpu_to_le32(bt->phase_seg1),
|
|
|
|
.phase_seg2 = cpu_to_le32(bt->phase_seg2),
|
|
|
|
.sjw = cpu_to_le32(bt->sjw),
|
|
|
|
.brp = cpu_to_le32(bt->brp),
|
|
|
|
};
|
2021-12-30 20:22:07 +01:00
|
|
|
u8 request = GS_USB_BREQ_DATA_BITTIMING;
|
2021-12-07 19:35:31 +01:00
|
|
|
|
2021-12-30 20:22:07 +01:00
|
|
|
if (dev->feature & GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO)
|
|
|
|
request = GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING;
|
|
|
|
|
2022-09-18 22:42:59 +02:00
|
|
|
/* request data bit timings */
|
2022-12-08 17:11:42 +09:00
|
|
|
return usb_control_msg_send(dev->udev, 0, request,
|
2022-09-18 22:42:59 +02:00
|
|
|
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
|
|
|
|
dev->channel, 0, &dbt, sizeof(dbt), 1000,
|
|
|
|
GFP_KERNEL);
|
2021-12-07 19:35:31 +01:00
|
|
|
}
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
static void gs_usb_xmit_callback(struct urb *urb)
|
|
|
|
{
|
|
|
|
struct gs_tx_context *txc = urb->context;
|
|
|
|
struct gs_can *dev = txc->dev;
|
|
|
|
struct net_device *netdev = dev->netdev;
|
|
|
|
|
|
|
|
if (urb->status)
|
2021-09-09 16:39:28 +02:00
|
|
|
netdev_info(netdev, "usb xmit fail %u\n", txc->echo_id);
|
2013-12-24 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 20:39:22 +00:00
|
|
|
static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
|
|
|
|
struct net_device *netdev)
|
2013-12-24 19:29:45 +00:00
|
|
|
{
|
|
|
|
struct gs_can *dev = netdev_priv(netdev);
|
|
|
|
struct net_device_stats *stats = &dev->netdev->stats;
|
|
|
|
struct urb *urb;
|
|
|
|
struct gs_host_frame *hf;
|
|
|
|
struct can_frame *cf;
|
2021-12-07 19:35:31 +01:00
|
|
|
struct canfd_frame *cfd;
|
2013-12-24 19:29:45 +00:00
|
|
|
int rc;
|
|
|
|
unsigned int idx;
|
|
|
|
struct gs_tx_context *txc;
|
|
|
|
|
2022-11-02 10:54:31 +01:00
|
|
|
if (can_dev_dropped_skb(netdev, skb))
|
2013-12-24 19:29:45 +00:00
|
|
|
return NETDEV_TX_OK;
|
|
|
|
|
|
|
|
/* find an empty context to keep track of transmission */
|
|
|
|
txc = gs_alloc_tx_context(dev);
|
|
|
|
if (!txc)
|
|
|
|
return NETDEV_TX_BUSY;
|
|
|
|
|
|
|
|
/* create a URB, and a buffer for it */
|
|
|
|
urb = usb_alloc_urb(0, GFP_ATOMIC);
|
2016-08-11 23:05:22 +02:00
|
|
|
if (!urb)
|
2013-12-24 19:29:45 +00:00
|
|
|
goto nomem_urb;
|
|
|
|
|
2022-09-20 11:47:24 -04:00
|
|
|
hf = kmalloc(dev->hf_size_tx, GFP_ATOMIC);
|
2023-07-12 15:49:43 +02:00
|
|
|
if (!hf)
|
2013-12-24 19:29:45 +00:00
|
|
|
goto nomem_hf;
|
|
|
|
|
|
|
|
idx = txc->echo_id;
|
|
|
|
|
|
|
|
if (idx >= GS_MAX_TX_URBS) {
|
2021-09-09 16:39:28 +02:00
|
|
|
netdev_err(netdev, "Invalid tx context %u\n", idx);
|
2013-12-24 19:29:45 +00:00
|
|
|
goto badidx;
|
|
|
|
}
|
|
|
|
|
|
|
|
hf->echo_id = idx;
|
|
|
|
hf->channel = dev->channel;
|
2022-01-05 16:29:50 -08:00
|
|
|
hf->flags = 0;
|
|
|
|
hf->reserved = 0;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2021-12-07 19:35:31 +01:00
|
|
|
if (can_is_canfd_skb(skb)) {
|
|
|
|
cfd = (struct canfd_frame *)skb->data;
|
|
|
|
|
|
|
|
hf->can_id = cpu_to_le32(cfd->can_id);
|
|
|
|
hf->can_dlc = can_fd_len2dlc(cfd->len);
|
|
|
|
hf->flags |= GS_CAN_FLAG_FD;
|
|
|
|
if (cfd->flags & CANFD_BRS)
|
|
|
|
hf->flags |= GS_CAN_FLAG_BRS;
|
|
|
|
if (cfd->flags & CANFD_ESI)
|
|
|
|
hf->flags |= GS_CAN_FLAG_ESI;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2021-12-07 19:35:31 +01:00
|
|
|
memcpy(hf->canfd->data, cfd->data, cfd->len);
|
|
|
|
} else {
|
|
|
|
cf = (struct can_frame *)skb->data;
|
2020-11-11 10:59:23 +01:00
|
|
|
|
2021-12-07 19:35:31 +01:00
|
|
|
hf->can_id = cpu_to_le32(cf->can_id);
|
|
|
|
hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
|
|
|
|
|
|
|
|
memcpy(hf->classic_can->data, cf->data, cf->len);
|
|
|
|
}
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
usb_fill_bulk_urb(urb, dev->udev,
|
2022-08-27 19:03:25 +02:00
|
|
|
usb_sndbulkpipe(dev->udev, GS_USB_ENDPOINT_OUT),
|
2021-12-07 19:22:50 +01:00
|
|
|
hf, dev->hf_size_tx,
|
2020-11-19 13:21:24 +01:00
|
|
|
gs_usb_xmit_callback, txc);
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2022-09-20 11:47:24 -04:00
|
|
|
urb->transfer_flags |= URB_FREE_BUFFER;
|
2013-12-24 19:29:45 +00:00
|
|
|
usb_anchor_urb(urb, &dev->tx_submitted);
|
|
|
|
|
2021-01-11 15:19:27 +01:00
|
|
|
can_put_echo_skb(skb, netdev, idx, 0);
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
atomic_inc(&dev->active_tx_urbs);
|
|
|
|
|
|
|
|
rc = usb_submit_urb(urb, GFP_ATOMIC);
|
|
|
|
if (unlikely(rc)) { /* usb send failed */
|
|
|
|
atomic_dec(&dev->active_tx_urbs);
|
|
|
|
|
2021-03-19 15:21:32 +01:00
|
|
|
can_free_echo_skb(netdev, idx, NULL);
|
2013-12-24 19:29:45 +00:00
|
|
|
gs_free_tx_context(txc);
|
|
|
|
|
|
|
|
usb_unanchor_urb(urb);
|
|
|
|
|
|
|
|
if (rc == -ENODEV) {
|
|
|
|
netif_device_detach(netdev);
|
|
|
|
} else {
|
|
|
|
netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
|
|
|
|
stats->tx_dropped++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Slow down tx path */
|
|
|
|
if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
|
|
|
|
netif_stop_queue(netdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* let usb core take care of this urb */
|
|
|
|
usb_free_urb(urb);
|
|
|
|
|
|
|
|
return NETDEV_TX_OK;
|
|
|
|
|
2023-07-18 11:11:22 +02:00
|
|
|
badidx:
|
2022-09-20 11:47:24 -04:00
|
|
|
kfree(hf);
|
2023-07-18 11:11:22 +02:00
|
|
|
nomem_hf:
|
2013-12-24 19:29:45 +00:00
|
|
|
usb_free_urb(urb);
|
|
|
|
|
2023-07-18 11:11:22 +02:00
|
|
|
nomem_urb:
|
2013-12-24 19:29:45 +00:00
|
|
|
gs_free_tx_context(txc);
|
|
|
|
dev_kfree_skb(skb);
|
|
|
|
stats->tx_dropped++;
|
|
|
|
return NETDEV_TX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gs_can_open(struct net_device *netdev)
|
|
|
|
{
|
|
|
|
struct gs_can *dev = netdev_priv(netdev);
|
|
|
|
struct gs_usb *parent = dev->parent;
|
2022-09-18 22:42:59 +02:00
|
|
|
struct gs_device_mode dm = {
|
|
|
|
.mode = cpu_to_le32(GS_CAN_MODE_START),
|
|
|
|
};
|
2021-12-07 19:22:50 +01:00
|
|
|
struct gs_host_frame *hf;
|
2023-07-07 13:43:10 +02:00
|
|
|
struct urb *urb = NULL;
|
2013-12-24 19:29:45 +00:00
|
|
|
u32 ctrlmode;
|
2020-11-19 14:03:17 +01:00
|
|
|
u32 flags = 0;
|
2022-09-18 22:42:59 +02:00
|
|
|
int rc, i;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
rc = open_candev(netdev);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
2021-12-07 19:35:31 +01:00
|
|
|
ctrlmode = dev->can.ctrlmode;
|
|
|
|
if (ctrlmode & CAN_CTRLMODE_FD) {
|
2021-12-07 19:43:24 +01:00
|
|
|
if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
|
|
|
|
dev->hf_size_tx = struct_size(hf, canfd_quirk, 1);
|
|
|
|
else
|
|
|
|
dev->hf_size_tx = struct_size(hf, canfd, 1);
|
2021-12-07 19:35:31 +01:00
|
|
|
} else {
|
2021-12-07 19:43:24 +01:00
|
|
|
if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
|
|
|
|
dev->hf_size_tx = struct_size(hf, classic_can_quirk, 1);
|
|
|
|
else
|
|
|
|
dev->hf_size_tx = struct_size(hf, classic_can, 1);
|
2021-12-07 19:35:31 +01:00
|
|
|
}
|
2021-12-07 19:22:50 +01:00
|
|
|
|
2023-06-02 09:29:07 +02:00
|
|
|
can_rx_offload_enable(&dev->offload);
|
|
|
|
|
2022-02-15 08:48:14 +09:00
|
|
|
if (!parent->active_channels) {
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
|
|
|
|
gs_usb_timestamp_init(parent);
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
for (i = 0; i < GS_MAX_RX_URBS; i++) {
|
|
|
|
u8 *buf;
|
|
|
|
|
|
|
|
/* alloc rx urb */
|
|
|
|
urb = usb_alloc_urb(0, GFP_KERNEL);
|
2023-07-07 13:43:10 +02:00
|
|
|
if (!urb) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto out_usb_kill_anchored_urbs;
|
|
|
|
}
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
/* alloc rx buffer */
|
2022-09-20 11:47:24 -04:00
|
|
|
buf = kmalloc(dev->parent->hf_size_rx,
|
|
|
|
GFP_KERNEL);
|
2013-12-24 19:29:45 +00:00
|
|
|
if (!buf) {
|
2023-07-07 13:43:10 +02:00
|
|
|
rc = -ENOMEM;
|
|
|
|
goto out_usb_free_urb;
|
2013-12-24 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* fill, anchor, and submit rx urb */
|
|
|
|
usb_fill_bulk_urb(urb,
|
|
|
|
dev->udev,
|
|
|
|
usb_rcvbulkpipe(dev->udev,
|
2022-08-27 19:03:25 +02:00
|
|
|
GS_USB_ENDPOINT_IN),
|
2013-12-24 19:29:45 +00:00
|
|
|
buf,
|
2021-12-07 19:22:50 +01:00
|
|
|
dev->parent->hf_size_rx,
|
2020-11-19 13:21:24 +01:00
|
|
|
gs_usb_receive_bulk_callback, parent);
|
2022-09-20 11:47:24 -04:00
|
|
|
urb->transfer_flags |= URB_FREE_BUFFER;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
usb_anchor_urb(urb, &parent->rx_submitted);
|
|
|
|
|
|
|
|
rc = usb_submit_urb(urb, GFP_KERNEL);
|
|
|
|
if (rc) {
|
|
|
|
if (rc == -ENODEV)
|
|
|
|
netif_device_detach(dev->netdev);
|
|
|
|
|
|
|
|
netdev_err(netdev,
|
2023-07-12 15:49:43 +02:00
|
|
|
"usb_submit_urb() failed, error %pe\n",
|
|
|
|
ERR_PTR(rc));
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2023-07-07 13:43:10 +02:00
|
|
|
goto out_usb_unanchor_urb;
|
2013-12-24 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Drop reference,
|
|
|
|
* USB core will take care of freeing it
|
|
|
|
*/
|
|
|
|
usb_free_urb(urb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* flags */
|
|
|
|
if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
|
2020-11-19 14:03:17 +01:00
|
|
|
flags |= GS_CAN_MODE_LOOP_BACK;
|
2022-09-29 09:58:30 +02:00
|
|
|
|
|
|
|
if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
|
2020-11-19 14:03:17 +01:00
|
|
|
flags |= GS_CAN_MODE_LISTEN_ONLY;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2022-09-29 10:01:22 +02:00
|
|
|
if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
|
|
|
|
flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
|
2020-11-19 14:03:17 +01:00
|
|
|
flags |= GS_CAN_MODE_ONE_SHOT;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2022-08-28 10:59:10 +02:00
|
|
|
if (ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
|
|
|
|
flags |= GS_CAN_MODE_BERR_REPORTING;
|
|
|
|
|
2022-09-29 10:01:22 +02:00
|
|
|
if (ctrlmode & CAN_CTRLMODE_FD)
|
|
|
|
flags |= GS_CAN_MODE_FD;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2022-08-22 12:01:10 +02:00
|
|
|
/* if hardware supports timestamps, enable it */
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
|
2022-08-22 12:01:10 +02:00
|
|
|
flags |= GS_CAN_MODE_HW_TIMESTAMP;
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
/* finally start device */
|
2022-09-20 11:40:56 +02:00
|
|
|
dev->can.state = CAN_STATE_ERROR_ACTIVE;
|
2022-09-18 22:42:59 +02:00
|
|
|
dm.flags = cpu_to_le32(flags);
|
2022-12-08 17:11:42 +09:00
|
|
|
rc = usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE,
|
2022-09-18 22:42:59 +02:00
|
|
|
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
|
|
|
|
dev->channel, 0, &dm, sizeof(dm), 1000,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (rc) {
|
2013-12-24 19:29:45 +00:00
|
|
|
netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
|
2022-09-20 11:40:56 +02:00
|
|
|
dev->can.state = CAN_STATE_STOPPED;
|
2023-07-07 13:43:10 +02:00
|
|
|
|
|
|
|
goto out_usb_kill_anchored_urbs;
|
2013-12-24 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 08:48:14 +09:00
|
|
|
parent->active_channels++;
|
2013-12-24 19:29:45 +00:00
|
|
|
if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
|
|
|
|
netif_start_queue(netdev);
|
|
|
|
|
|
|
|
return 0;
|
2023-07-07 13:43:10 +02:00
|
|
|
|
|
|
|
out_usb_unanchor_urb:
|
|
|
|
usb_unanchor_urb(urb);
|
|
|
|
out_usb_free_urb:
|
|
|
|
usb_free_urb(urb);
|
|
|
|
out_usb_kill_anchored_urbs:
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
if (!parent->active_channels) {
|
2023-07-07 13:43:10 +02:00
|
|
|
usb_kill_anchored_urbs(&dev->tx_submitted);
|
|
|
|
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
|
|
|
|
gs_usb_timestamp_stop(parent);
|
|
|
|
}
|
|
|
|
|
2023-06-02 09:29:07 +02:00
|
|
|
can_rx_offload_disable(&dev->offload);
|
2023-07-07 13:43:10 +02:00
|
|
|
close_candev(netdev);
|
|
|
|
|
|
|
|
return rc;
|
2013-12-24 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 12:25:02 +02:00
|
|
|
static int gs_usb_get_state(const struct net_device *netdev,
|
|
|
|
struct can_berr_counter *bec,
|
|
|
|
enum can_state *state)
|
|
|
|
{
|
|
|
|
struct gs_can *dev = netdev_priv(netdev);
|
|
|
|
struct gs_device_state ds;
|
|
|
|
int rc;
|
|
|
|
|
2022-12-08 17:11:42 +09:00
|
|
|
rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_STATE,
|
2022-08-28 12:25:02 +02:00
|
|
|
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
|
|
|
|
dev->channel, 0,
|
|
|
|
&ds, sizeof(ds),
|
|
|
|
USB_CTRL_GET_TIMEOUT,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
if (le32_to_cpu(ds.state) >= CAN_STATE_MAX)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
*state = le32_to_cpu(ds.state);
|
|
|
|
bec->txerr = le32_to_cpu(ds.txerr);
|
|
|
|
bec->rxerr = le32_to_cpu(ds.rxerr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gs_usb_can_get_berr_counter(const struct net_device *netdev,
|
|
|
|
struct can_berr_counter *bec)
|
|
|
|
{
|
|
|
|
enum can_state state;
|
|
|
|
|
|
|
|
return gs_usb_get_state(netdev, bec, &state);
|
|
|
|
}
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
static int gs_can_close(struct net_device *netdev)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct gs_can *dev = netdev_priv(netdev);
|
|
|
|
struct gs_usb *parent = dev->parent;
|
|
|
|
|
|
|
|
netif_stop_queue(netdev);
|
|
|
|
|
|
|
|
/* Stop polling */
|
2022-02-15 08:48:14 +09:00
|
|
|
parent->active_channels--;
|
2022-07-03 19:33:06 +02:00
|
|
|
if (!parent->active_channels) {
|
2013-12-24 19:29:45 +00:00
|
|
|
usb_kill_anchored_urbs(&parent->rx_submitted);
|
can: gs_usb: fix time stamp counter initialization
If the gs_usb device driver is unloaded (or unbound) before the
interface is shut down, the USB stack first calls the struct
usb_driver::disconnect and then the struct net_device_ops::ndo_stop
callback.
In gs_usb_disconnect() all pending bulk URBs are killed, i.e. no more
RX'ed CAN frames are send from the USB device to the host. Later in
gs_can_close() a reset control message is send to each CAN channel to
remove the controller from the CAN bus. In this race window the USB
device can still receive CAN frames from the bus and internally queue
them to be send to the host.
At least in the current version of the candlelight firmware, the queue
of received CAN frames is not emptied during the reset command. After
loading (or binding) the gs_usb driver, new URBs are submitted during
the struct net_device_ops::ndo_open callback and the candlelight
firmware starts sending its already queued CAN frames to the host.
However, this scenario was not considered when implementing the
hardware timestamp function. The cycle counter/time counter
infrastructure is set up (gs_usb_timestamp_init()) after the USBs are
submitted, resulting in a NULL pointer dereference if
timecounter_cyc2time() (via the call chain:
gs_usb_receive_bulk_callback() -> gs_usb_set_timestamp() ->
gs_usb_skb_set_timestamp()) is called too early.
Move the gs_usb_timestamp_init() function before the URBs are
submitted to fix this problem.
For a comprehensive solution, we need to consider gs_usb devices with
more than 1 channel. The cycle counter/time counter infrastructure is
setup per channel, but the RX URBs are per device. Once gs_can_open()
of _a_ channel has been called, and URBs have been submitted, the
gs_usb_receive_bulk_callback() can be called for _all_ available
channels, even for channels that are not running, yet. As cycle
counter/time counter has not set up, this will again lead to a NULL
pointer dereference.
Convert the cycle counter/time counter from a "per channel" to a "per
device" functionality. Also set it up, before submitting any URBs to
the device.
Further in gs_usb_receive_bulk_callback(), don't process any URBs for
not started CAN channels, only resubmit the URB.
Fixes: 45dfa45f52e6 ("can: gs_usb: add RX and TX hardware timestamp support")
Closes: https://github.com/candle-usb/candleLight_fw/issues/137#issuecomment-1623532076
Cc: stable@vger.kernel.org
Cc: John Whittington <git@jbrengineering.co.uk>
Link: https://lore.kernel.org/all/20230716-gs_usb-fix-time-stamp-counter-v1-2-9017cefcd9d5@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2023-07-07 18:44:23 +02:00
|
|
|
|
|
|
|
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
|
|
|
|
gs_usb_timestamp_stop(parent);
|
2022-07-03 19:33:06 +02:00
|
|
|
}
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
/* Stop sending URBs */
|
|
|
|
usb_kill_anchored_urbs(&dev->tx_submitted);
|
|
|
|
atomic_set(&dev->active_tx_urbs, 0);
|
|
|
|
|
2023-07-18 11:43:54 +02:00
|
|
|
dev->can.state = CAN_STATE_STOPPED;
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
/* reset the device */
|
2023-02-08 10:26:33 +01:00
|
|
|
gs_cmd_reset(dev);
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
/* reset tx contexts */
|
|
|
|
for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
|
|
|
|
dev->tx_context[rc].dev = dev;
|
|
|
|
dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
|
|
|
|
}
|
|
|
|
|
2023-06-02 09:29:07 +02:00
|
|
|
can_rx_offload_disable(&dev->offload);
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
/* close the netdev */
|
|
|
|
close_candev(netdev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-22 12:01:10 +02:00
|
|
|
static int gs_can_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
|
|
|
|
{
|
|
|
|
const struct gs_can *dev = netdev_priv(netdev);
|
|
|
|
|
|
|
|
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
|
|
|
|
return can_eth_ioctl_hwts(netdev, ifr, cmd);
|
|
|
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
static const struct net_device_ops gs_usb_netdev_ops = {
|
|
|
|
.ndo_open = gs_can_open,
|
|
|
|
.ndo_stop = gs_can_close,
|
|
|
|
.ndo_start_xmit = gs_can_start_xmit,
|
2014-11-18 13:16:13 +01:00
|
|
|
.ndo_change_mtu = can_change_mtu,
|
2022-08-22 12:01:10 +02:00
|
|
|
.ndo_eth_ioctl = gs_can_eth_ioctl,
|
2013-12-24 19:29:45 +00:00
|
|
|
};
|
|
|
|
|
2016-06-10 20:39:22 +00:00
|
|
|
static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
|
|
|
|
{
|
|
|
|
struct gs_can *dev = netdev_priv(netdev);
|
2022-09-18 22:42:59 +02:00
|
|
|
struct gs_identify_mode imode;
|
2017-04-23 20:31:40 +03:00
|
|
|
|
2016-06-10 20:39:22 +00:00
|
|
|
if (do_identify)
|
2022-09-18 22:42:59 +02:00
|
|
|
imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
|
2016-06-10 20:39:22 +00:00
|
|
|
else
|
2022-09-18 22:42:59 +02:00
|
|
|
imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
|
2017-04-23 20:31:40 +03:00
|
|
|
|
2022-12-08 17:11:42 +09:00
|
|
|
return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_IDENTIFY,
|
2022-09-18 22:42:59 +02:00
|
|
|
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
|
|
|
|
dev->channel, 0, &imode, sizeof(imode), 100,
|
|
|
|
GFP_KERNEL);
|
2016-06-10 20:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* blink LED's for finding the this interface */
|
2022-08-18 14:10:08 +02:00
|
|
|
static int gs_usb_set_phys_id(struct net_device *netdev,
|
2016-06-10 20:39:22 +00:00
|
|
|
enum ethtool_phys_id_state state)
|
|
|
|
{
|
2022-08-18 14:10:08 +02:00
|
|
|
const struct gs_can *dev = netdev_priv(netdev);
|
2016-06-10 20:39:22 +00:00
|
|
|
int rc = 0;
|
|
|
|
|
2022-08-18 14:10:08 +02:00
|
|
|
if (!(dev->feature & GS_CAN_FEATURE_IDENTIFY))
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2016-06-10 20:39:22 +00:00
|
|
|
switch (state) {
|
|
|
|
case ETHTOOL_ID_ACTIVE:
|
2022-08-18 14:10:08 +02:00
|
|
|
rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_ON);
|
2016-06-10 20:39:22 +00:00
|
|
|
break;
|
|
|
|
case ETHTOOL_ID_INACTIVE:
|
2022-08-18 14:10:08 +02:00
|
|
|
rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_OFF);
|
2016-06-10 20:39:22 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-08-22 12:01:10 +02:00
|
|
|
static int gs_usb_get_ts_info(struct net_device *netdev,
|
|
|
|
struct ethtool_ts_info *info)
|
|
|
|
{
|
|
|
|
struct gs_can *dev = netdev_priv(netdev);
|
|
|
|
|
|
|
|
/* report if device supports HW timestamps */
|
|
|
|
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
|
|
|
|
return can_ethtool_op_get_ts_info_hwts(netdev, info);
|
|
|
|
|
|
|
|
return ethtool_op_get_ts_info(netdev, info);
|
|
|
|
}
|
|
|
|
|
2016-06-10 20:39:22 +00:00
|
|
|
static const struct ethtool_ops gs_usb_ethtool_ops = {
|
|
|
|
.set_phys_id = gs_usb_set_phys_id,
|
2022-08-22 12:01:10 +02:00
|
|
|
.get_ts_info = gs_usb_get_ts_info,
|
2016-06-10 20:39:22 +00:00
|
|
|
};
|
|
|
|
|
2022-09-18 16:41:38 +02:00
|
|
|
static int gs_usb_get_termination(struct net_device *netdev, u16 *term)
|
|
|
|
{
|
|
|
|
struct gs_can *dev = netdev_priv(netdev);
|
|
|
|
struct gs_device_termination_state term_state;
|
|
|
|
int rc;
|
|
|
|
|
2022-12-08 17:11:42 +09:00
|
|
|
rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_TERMINATION,
|
2022-09-18 16:41:38 +02:00
|
|
|
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
|
|
|
|
dev->channel, 0,
|
|
|
|
&term_state, sizeof(term_state), 1000,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
if (term_state.state == cpu_to_le32(GS_CAN_TERMINATION_STATE_ON))
|
|
|
|
*term = GS_USB_TERMINATION_ENABLED;
|
|
|
|
else
|
|
|
|
*term = GS_USB_TERMINATION_DISABLED;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gs_usb_set_termination(struct net_device *netdev, u16 term)
|
|
|
|
{
|
|
|
|
struct gs_can *dev = netdev_priv(netdev);
|
|
|
|
struct gs_device_termination_state term_state;
|
|
|
|
|
|
|
|
if (term == GS_USB_TERMINATION_ENABLED)
|
|
|
|
term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_ON);
|
|
|
|
else
|
|
|
|
term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_OFF);
|
|
|
|
|
2022-12-08 17:11:42 +09:00
|
|
|
return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_SET_TERMINATION,
|
2022-09-18 16:41:38 +02:00
|
|
|
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
|
|
|
|
dev->channel, 0,
|
|
|
|
&term_state, sizeof(term_state), 1000,
|
|
|
|
GFP_KERNEL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const u16 gs_usb_termination_const[] = {
|
|
|
|
GS_USB_TERMINATION_DISABLED,
|
|
|
|
GS_USB_TERMINATION_ENABLED
|
|
|
|
};
|
|
|
|
|
2016-06-10 20:39:22 +00:00
|
|
|
static struct gs_can *gs_make_candev(unsigned int channel,
|
|
|
|
struct usb_interface *intf,
|
|
|
|
struct gs_device_config *dconf)
|
2013-12-24 19:29:45 +00:00
|
|
|
{
|
|
|
|
struct gs_can *dev;
|
|
|
|
struct net_device *netdev;
|
|
|
|
int rc;
|
2022-09-18 22:42:59 +02:00
|
|
|
struct gs_device_bt_const_extended bt_const_extended;
|
|
|
|
struct gs_device_bt_const bt_const;
|
2020-11-19 14:03:17 +01:00
|
|
|
u32 feature;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
/* fetch bit timing constants */
|
2022-09-18 22:42:59 +02:00
|
|
|
rc = usb_control_msg_recv(interface_to_usbdev(intf), 0,
|
|
|
|
GS_USB_BREQ_BT_CONST,
|
|
|
|
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
|
|
|
|
channel, 0, &bt_const, sizeof(bt_const), 1000,
|
|
|
|
GFP_KERNEL);
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2022-09-18 22:42:59 +02:00
|
|
|
if (rc) {
|
2013-12-24 19:29:45 +00:00
|
|
|
dev_err(&intf->dev,
|
2022-09-18 22:59:27 +02:00
|
|
|
"Couldn't get bit timing const for channel %d (%pe)\n",
|
|
|
|
channel, ERR_PTR(rc));
|
2013-12-24 19:29:45 +00:00
|
|
|
return ERR_PTR(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create netdev */
|
|
|
|
netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
|
|
|
|
if (!netdev) {
|
|
|
|
dev_err(&intf->dev, "Couldn't allocate candev\n");
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
dev = netdev_priv(netdev);
|
|
|
|
|
|
|
|
netdev->netdev_ops = &gs_usb_netdev_ops;
|
2022-07-27 19:16:32 +09:00
|
|
|
netdev->ethtool_ops = &gs_usb_ethtool_ops;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
|
2022-09-30 15:59:41 +02:00
|
|
|
netdev->dev_id = channel;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2020-09-16 00:34:55 +02:00
|
|
|
/* dev setup */
|
2022-07-26 17:27:03 +09:00
|
|
|
strcpy(dev->bt_const.name, KBUILD_MODNAME);
|
2022-09-18 22:42:59 +02:00
|
|
|
dev->bt_const.tseg1_min = le32_to_cpu(bt_const.tseg1_min);
|
|
|
|
dev->bt_const.tseg1_max = le32_to_cpu(bt_const.tseg1_max);
|
|
|
|
dev->bt_const.tseg2_min = le32_to_cpu(bt_const.tseg2_min);
|
|
|
|
dev->bt_const.tseg2_max = le32_to_cpu(bt_const.tseg2_max);
|
|
|
|
dev->bt_const.sjw_max = le32_to_cpu(bt_const.sjw_max);
|
|
|
|
dev->bt_const.brp_min = le32_to_cpu(bt_const.brp_min);
|
|
|
|
dev->bt_const.brp_max = le32_to_cpu(bt_const.brp_max);
|
|
|
|
dev->bt_const.brp_inc = le32_to_cpu(bt_const.brp_inc);
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
dev->udev = interface_to_usbdev(intf);
|
|
|
|
dev->netdev = netdev;
|
|
|
|
dev->channel = channel;
|
|
|
|
|
|
|
|
init_usb_anchor(&dev->tx_submitted);
|
|
|
|
atomic_set(&dev->active_tx_urbs, 0);
|
|
|
|
spin_lock_init(&dev->tx_ctx_lock);
|
|
|
|
for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
|
|
|
|
dev->tx_context[rc].dev = dev;
|
|
|
|
dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
|
|
|
|
}
|
|
|
|
|
2020-09-16 00:34:55 +02:00
|
|
|
/* can setup */
|
2013-12-24 19:29:45 +00:00
|
|
|
dev->can.state = CAN_STATE_STOPPED;
|
2022-09-18 22:42:59 +02:00
|
|
|
dev->can.clock.freq = le32_to_cpu(bt_const.fclk_can);
|
2013-12-24 19:29:45 +00:00
|
|
|
dev->can.bittiming_const = &dev->bt_const;
|
|
|
|
dev->can.do_set_bittiming = gs_usb_set_bittiming;
|
|
|
|
|
2020-11-11 10:59:23 +01:00
|
|
|
dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2022-09-18 22:42:59 +02:00
|
|
|
feature = le32_to_cpu(bt_const.feature);
|
2021-12-07 19:43:24 +01:00
|
|
|
dev->feature = FIELD_GET(GS_CAN_FEATURE_MASK, feature);
|
2020-11-19 14:03:17 +01:00
|
|
|
if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
|
2013-12-24 19:29:45 +00:00
|
|
|
dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
|
|
|
|
|
2020-11-19 14:03:17 +01:00
|
|
|
if (feature & GS_CAN_FEATURE_LOOP_BACK)
|
2013-12-24 19:29:45 +00:00
|
|
|
dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
|
|
|
|
|
2020-11-19 14:03:17 +01:00
|
|
|
if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
|
2013-12-24 19:29:45 +00:00
|
|
|
dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
|
|
|
|
|
2020-11-19 14:03:17 +01:00
|
|
|
if (feature & GS_CAN_FEATURE_ONE_SHOT)
|
2013-12-24 19:29:45 +00:00
|
|
|
dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
|
|
|
|
|
2021-12-07 19:35:31 +01:00
|
|
|
if (feature & GS_CAN_FEATURE_FD) {
|
|
|
|
dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
|
2021-11-08 17:05:44 +01:00
|
|
|
/* The data bit timing will be overwritten, if
|
|
|
|
* GS_CAN_FEATURE_BT_CONST_EXT is set.
|
|
|
|
*/
|
2021-12-07 19:35:31 +01:00
|
|
|
dev->can.data_bittiming_const = &dev->bt_const;
|
|
|
|
dev->can.do_set_data_bittiming = gs_usb_set_data_bittiming;
|
|
|
|
}
|
|
|
|
|
2022-09-18 16:41:38 +02:00
|
|
|
if (feature & GS_CAN_FEATURE_TERMINATION) {
|
|
|
|
rc = gs_usb_get_termination(netdev, &dev->can.termination);
|
|
|
|
if (rc) {
|
|
|
|
dev->feature &= ~GS_CAN_FEATURE_TERMINATION;
|
|
|
|
|
|
|
|
dev_info(&intf->dev,
|
|
|
|
"Disabling termination support for channel %d (%pe)\n",
|
|
|
|
channel, ERR_PTR(rc));
|
|
|
|
} else {
|
|
|
|
dev->can.termination_const = gs_usb_termination_const;
|
|
|
|
dev->can.termination_const_cnt = ARRAY_SIZE(gs_usb_termination_const);
|
|
|
|
dev->can.do_set_termination = gs_usb_set_termination;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-28 10:59:10 +02:00
|
|
|
if (feature & GS_CAN_FEATURE_BERR_REPORTING)
|
|
|
|
dev->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING;
|
|
|
|
|
2022-08-28 12:25:02 +02:00
|
|
|
if (feature & GS_CAN_FEATURE_GET_STATE)
|
|
|
|
dev->can.do_get_berr_counter = gs_usb_can_get_berr_counter;
|
|
|
|
|
2021-12-07 20:18:23 +01:00
|
|
|
/* The CANtact Pro from LinkLayer Labs is based on the
|
|
|
|
* LPC54616 µC, which is affected by the NXP LPC USB transfer
|
|
|
|
* erratum. However, the current firmware (version 2) doesn't
|
|
|
|
* set the GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX bit. Set the
|
|
|
|
* feature GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX to workaround
|
|
|
|
* this issue.
|
|
|
|
*
|
|
|
|
* For the GS_USB_BREQ_DATA_BITTIMING USB control message the
|
|
|
|
* CANtact Pro firmware uses a request value, which is already
|
|
|
|
* used by the candleLight firmware for a different purpose
|
|
|
|
* (GS_USB_BREQ_GET_USER_ID). Set the feature
|
|
|
|
* GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO to workaround this
|
|
|
|
* issue.
|
|
|
|
*/
|
2022-08-27 19:03:25 +02:00
|
|
|
if (dev->udev->descriptor.idVendor == cpu_to_le16(USB_GS_USB_1_VENDOR_ID) &&
|
|
|
|
dev->udev->descriptor.idProduct == cpu_to_le16(USB_GS_USB_1_PRODUCT_ID) &&
|
2021-12-07 20:18:23 +01:00
|
|
|
dev->udev->manufacturer && dev->udev->product &&
|
|
|
|
!strcmp(dev->udev->manufacturer, "LinkLayer Labs") &&
|
|
|
|
!strcmp(dev->udev->product, "CANtact Pro") &&
|
|
|
|
(le32_to_cpu(dconf->sw_version) <= 2))
|
|
|
|
dev->feature |= GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX |
|
|
|
|
GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO;
|
|
|
|
|
2022-08-18 14:10:08 +02:00
|
|
|
/* GS_CAN_FEATURE_IDENTIFY is only supported for sw_version > 1 */
|
|
|
|
if (!(le32_to_cpu(dconf->sw_version) > 1 &&
|
|
|
|
feature & GS_CAN_FEATURE_IDENTIFY))
|
|
|
|
dev->feature &= ~GS_CAN_FEATURE_IDENTIFY;
|
2016-06-10 20:39:22 +00:00
|
|
|
|
2021-11-08 17:05:44 +01:00
|
|
|
/* fetch extended bit timing constants if device has feature
|
|
|
|
* GS_CAN_FEATURE_FD and GS_CAN_FEATURE_BT_CONST_EXT
|
|
|
|
*/
|
|
|
|
if (feature & GS_CAN_FEATURE_FD &&
|
|
|
|
feature & GS_CAN_FEATURE_BT_CONST_EXT) {
|
2022-09-18 22:42:59 +02:00
|
|
|
rc = usb_control_msg_recv(interface_to_usbdev(intf), 0,
|
|
|
|
GS_USB_BREQ_BT_CONST_EXT,
|
|
|
|
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
|
|
|
|
channel, 0, &bt_const_extended,
|
|
|
|
sizeof(bt_const_extended),
|
|
|
|
1000, GFP_KERNEL);
|
|
|
|
if (rc) {
|
2021-11-08 17:05:44 +01:00
|
|
|
dev_err(&intf->dev,
|
2022-09-18 22:59:27 +02:00
|
|
|
"Couldn't get extended bit timing const for channel %d (%pe)\n",
|
|
|
|
channel, ERR_PTR(rc));
|
|
|
|
goto out_free_candev;
|
2021-11-08 17:05:44 +01:00
|
|
|
}
|
|
|
|
|
2022-07-26 17:27:03 +09:00
|
|
|
strcpy(dev->data_bt_const.name, KBUILD_MODNAME);
|
2022-09-18 22:42:59 +02:00
|
|
|
dev->data_bt_const.tseg1_min = le32_to_cpu(bt_const_extended.dtseg1_min);
|
|
|
|
dev->data_bt_const.tseg1_max = le32_to_cpu(bt_const_extended.dtseg1_max);
|
|
|
|
dev->data_bt_const.tseg2_min = le32_to_cpu(bt_const_extended.dtseg2_min);
|
|
|
|
dev->data_bt_const.tseg2_max = le32_to_cpu(bt_const_extended.dtseg2_max);
|
|
|
|
dev->data_bt_const.sjw_max = le32_to_cpu(bt_const_extended.dsjw_max);
|
|
|
|
dev->data_bt_const.brp_min = le32_to_cpu(bt_const_extended.dbrp_min);
|
|
|
|
dev->data_bt_const.brp_max = le32_to_cpu(bt_const_extended.dbrp_max);
|
|
|
|
dev->data_bt_const.brp_inc = le32_to_cpu(bt_const_extended.dbrp_inc);
|
2021-11-08 17:05:44 +01:00
|
|
|
|
|
|
|
dev->can.data_bittiming_const = &dev->data_bt_const;
|
|
|
|
}
|
|
|
|
|
2023-06-02 09:29:07 +02:00
|
|
|
can_rx_offload_add_manual(netdev, &dev->offload, GS_NAPI_WEIGHT);
|
2020-11-19 13:03:53 +01:00
|
|
|
SET_NETDEV_DEV(netdev, &intf->dev);
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
rc = register_candev(dev->netdev);
|
|
|
|
if (rc) {
|
2022-09-18 22:59:27 +02:00
|
|
|
dev_err(&intf->dev,
|
|
|
|
"Couldn't register candev for channel %d (%pe)\n",
|
|
|
|
channel, ERR_PTR(rc));
|
2023-06-02 09:29:07 +02:00
|
|
|
goto out_can_rx_offload_del;
|
2013-12-24 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dev;
|
2022-09-18 22:59:27 +02:00
|
|
|
|
2023-06-02 09:29:07 +02:00
|
|
|
out_can_rx_offload_del:
|
|
|
|
can_rx_offload_del(&dev->offload);
|
2023-07-18 11:11:22 +02:00
|
|
|
out_free_candev:
|
2022-09-18 22:59:27 +02:00
|
|
|
free_candev(dev->netdev);
|
|
|
|
return ERR_PTR(rc);
|
2013-12-24 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gs_destroy_candev(struct gs_can *dev)
|
|
|
|
{
|
|
|
|
unregister_candev(dev->netdev);
|
2023-06-02 09:29:07 +02:00
|
|
|
can_rx_offload_del(&dev->offload);
|
2016-02-23 01:17:28 +00:00
|
|
|
free_candev(dev->netdev);
|
2013-12-24 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 20:39:22 +00:00
|
|
|
static int gs_usb_probe(struct usb_interface *intf,
|
|
|
|
const struct usb_device_id *id)
|
2013-12-24 19:29:45 +00:00
|
|
|
{
|
2021-11-07 15:11:55 +01:00
|
|
|
struct usb_device *udev = interface_to_usbdev(intf);
|
2021-12-07 19:22:50 +01:00
|
|
|
struct gs_host_frame *hf;
|
2023-07-06 16:16:33 +02:00
|
|
|
struct gs_usb *parent;
|
2022-09-18 22:42:59 +02:00
|
|
|
struct gs_host_config hconf = {
|
|
|
|
.byte_order = cpu_to_le32(0x0000beef),
|
|
|
|
};
|
|
|
|
struct gs_device_config dconf;
|
2013-12-24 19:29:45 +00:00
|
|
|
unsigned int icount, i;
|
2022-09-18 22:42:59 +02:00
|
|
|
int rc;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
/* send host config */
|
2022-09-18 22:42:59 +02:00
|
|
|
rc = usb_control_msg_send(udev, 0,
|
|
|
|
GS_USB_BREQ_HOST_FORMAT,
|
|
|
|
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
|
|
|
|
1, intf->cur_altsetting->desc.bInterfaceNumber,
|
|
|
|
&hconf, sizeof(hconf), 1000,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (rc) {
|
2020-11-19 13:23:13 +01:00
|
|
|
dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc);
|
2013-12-24 19:29:45 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read device config */
|
2022-09-18 22:42:59 +02:00
|
|
|
rc = usb_control_msg_recv(udev, 0,
|
|
|
|
GS_USB_BREQ_DEVICE_CONFIG,
|
|
|
|
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
|
|
|
|
1, intf->cur_altsetting->desc.bInterfaceNumber,
|
|
|
|
&dconf, sizeof(dconf), 1000,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (rc) {
|
2013-12-24 19:29:45 +00:00
|
|
|
dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
|
|
|
|
rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-09-18 22:42:59 +02:00
|
|
|
icount = dconf.icount + 1;
|
2021-09-09 16:39:28 +02:00
|
|
|
dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
if (icount > GS_MAX_INTF) {
|
|
|
|
dev_err(&intf->dev,
|
2021-09-09 16:39:28 +02:00
|
|
|
"Driver cannot handle more that %u CAN interfaces\n",
|
2013-12-24 19:29:45 +00:00
|
|
|
GS_MAX_INTF);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:16:33 +02:00
|
|
|
parent = kzalloc(sizeof(*parent), GFP_KERNEL);
|
|
|
|
if (!parent)
|
2015-03-16 22:42:24 +00:00
|
|
|
return -ENOMEM;
|
2017-02-24 11:27:36 -05:00
|
|
|
|
2023-07-06 16:16:33 +02:00
|
|
|
init_usb_anchor(&parent->rx_submitted);
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2023-07-06 16:16:33 +02:00
|
|
|
usb_set_intfdata(intf, parent);
|
|
|
|
parent->udev = udev;
|
2013-12-24 19:29:45 +00:00
|
|
|
|
|
|
|
for (i = 0; i < icount; i++) {
|
2022-08-22 12:01:10 +02:00
|
|
|
unsigned int hf_size_rx = 0;
|
|
|
|
|
2023-07-06 16:16:33 +02:00
|
|
|
parent->canch[i] = gs_make_candev(i, intf, &dconf);
|
|
|
|
if (IS_ERR_OR_NULL(parent->canch[i])) {
|
2016-02-23 01:17:28 +00:00
|
|
|
/* save error code to return later */
|
2023-07-06 16:16:33 +02:00
|
|
|
rc = PTR_ERR(parent->canch[i]);
|
2016-02-23 01:17:28 +00:00
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
/* on failure destroy previously created candevs */
|
|
|
|
icount = i;
|
2016-02-23 01:17:28 +00:00
|
|
|
for (i = 0; i < icount; i++)
|
2023-07-06 16:16:33 +02:00
|
|
|
gs_destroy_candev(parent->canch[i]);
|
2016-02-23 01:17:28 +00:00
|
|
|
|
2023-07-06 16:16:33 +02:00
|
|
|
usb_kill_anchored_urbs(&parent->rx_submitted);
|
|
|
|
kfree(parent);
|
2013-12-24 19:29:45 +00:00
|
|
|
return rc;
|
|
|
|
}
|
2023-07-06 16:16:33 +02:00
|
|
|
parent->canch[i]->parent = parent;
|
2021-12-07 19:35:31 +01:00
|
|
|
|
2022-08-22 12:01:10 +02:00
|
|
|
/* set RX packet size based on FD and if hardware
|
2023-07-06 13:08:43 +02:00
|
|
|
* timestamps are supported.
|
|
|
|
*/
|
2023-07-06 16:16:33 +02:00
|
|
|
if (parent->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD) {
|
|
|
|
if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
|
2022-08-22 12:01:10 +02:00
|
|
|
hf_size_rx = struct_size(hf, canfd_ts, 1);
|
|
|
|
else
|
|
|
|
hf_size_rx = struct_size(hf, canfd, 1);
|
|
|
|
} else {
|
2023-07-06 16:16:33 +02:00
|
|
|
if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
|
2022-08-22 12:01:10 +02:00
|
|
|
hf_size_rx = struct_size(hf, classic_can_ts, 1);
|
|
|
|
else
|
|
|
|
hf_size_rx = struct_size(hf, classic_can, 1);
|
|
|
|
}
|
2023-07-06 16:16:33 +02:00
|
|
|
parent->hf_size_rx = max(parent->hf_size_rx, hf_size_rx);
|
2013-12-24 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gs_usb_disconnect(struct usb_interface *intf)
|
|
|
|
{
|
2023-07-06 16:16:33 +02:00
|
|
|
struct gs_usb *parent = usb_get_intfdata(intf);
|
2020-11-19 12:51:28 +01:00
|
|
|
unsigned int i;
|
|
|
|
|
2013-12-24 19:29:45 +00:00
|
|
|
usb_set_intfdata(intf, NULL);
|
|
|
|
|
2023-07-06 16:16:33 +02:00
|
|
|
if (!parent) {
|
2013-12-24 19:29:45 +00:00
|
|
|
dev_err(&intf->dev, "Disconnect (nodata)\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-23 01:17:28 +00:00
|
|
|
for (i = 0; i < GS_MAX_INTF; i++)
|
2023-07-06 16:16:33 +02:00
|
|
|
if (parent->canch[i])
|
|
|
|
gs_destroy_candev(parent->canch[i]);
|
2013-12-24 19:29:45 +00:00
|
|
|
|
2023-07-06 16:16:33 +02:00
|
|
|
kfree(parent);
|
2013-12-24 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct usb_device_id gs_usb_table[] = {
|
2022-08-27 19:03:25 +02:00
|
|
|
{ USB_DEVICE_INTERFACE_NUMBER(USB_GS_USB_1_VENDOR_ID,
|
|
|
|
USB_GS_USB_1_PRODUCT_ID, 0) },
|
2016-06-08 18:00:26 +00:00
|
|
|
{ USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
|
|
|
|
USB_CANDLELIGHT_PRODUCT_ID, 0) },
|
2021-11-08 11:41:57 +01:00
|
|
|
{ USB_DEVICE_INTERFACE_NUMBER(USB_CES_CANEXT_FD_VENDOR_ID,
|
|
|
|
USB_CES_CANEXT_FD_PRODUCT_ID, 0) },
|
2021-12-03 15:16:26 +10:00
|
|
|
{ USB_DEVICE_INTERFACE_NUMBER(USB_ABE_CANDEBUGGER_FD_VENDOR_ID,
|
|
|
|
USB_ABE_CANDEBUGGER_FD_PRODUCT_ID, 0) },
|
2013-12-24 19:29:45 +00:00
|
|
|
{} /* Terminating entry */
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(usb, gs_usb_table);
|
|
|
|
|
|
|
|
static struct usb_driver gs_usb_driver = {
|
2022-07-26 17:27:03 +09:00
|
|
|
.name = KBUILD_MODNAME,
|
2020-11-19 12:45:08 +01:00
|
|
|
.probe = gs_usb_probe,
|
2013-12-24 19:29:45 +00:00
|
|
|
.disconnect = gs_usb_disconnect,
|
2020-11-19 12:45:08 +01:00
|
|
|
.id_table = gs_usb_table,
|
2013-12-24 19:29:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module_usb_driver(gs_usb_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
|
|
|
|
MODULE_DESCRIPTION(
|
|
|
|
"Socket CAN device driver for Geschwister Schneider Technologie-, "
|
2016-06-08 18:00:26 +00:00
|
|
|
"Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
|
|
|
|
"and bytewerk.org candleLight USB CAN interfaces.");
|
2013-12-24 19:29:45 +00:00
|
|
|
MODULE_LICENSE("GPL v2");
|