| 1 |
/* SPDX-License-Identifier: GPL-2.0 */ |
| 2 |
/* Parts of this driver are based on the following: |
| 3 |
* - Kvaser linux leaf driver (version 4.78) |
| 4 |
* - CAN driver for esd CAN-USB/2 |
| 5 |
* - Kvaser linux usbcanII driver (version 5.3) |
| 6 |
* - Kvaser linux mhydra driver (version 5.24) |
| 7 |
* |
| 8 |
* Copyright (C) 2002-2018 KVASER AB, Sweden. All rights reserved. |
| 9 |
* Copyright (C) 2010 Matthias Fuchs <[email protected]>, esd gmbh |
| 10 |
* Copyright (C) 2012 Olivier Sobrie <[email protected]> |
| 11 |
* Copyright (C) 2015 Valeo S.A. |
| 12 |
*/ |
| 13 |
|
| 14 |
#ifndef KVASER_USB_H |
| 15 |
#define KVASER_USB_H |
| 16 |
|
| 17 |
/* Kvaser USB CAN dongles are divided into three major platforms: |
| 18 |
* - Hydra: Running firmware labeled as 'mhydra' |
| 19 |
* - Leaf: Based on Renesas M32C or Freescale i.MX28, running firmware labeled |
| 20 |
* as 'filo' |
| 21 |
* - UsbcanII: Based on Renesas M16C, running firmware labeled as 'helios' |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <linux/completion.h> |
| 25 |
#include <linux/ktime.h> |
| 26 |
#include <linux/math64.h> |
| 27 |
#include <linux/spinlock.h> |
| 28 |
#include <linux/types.h> |
| 29 |
#include <linux/usb.h> |
| 30 |
#include <net/devlink.h> |
| 31 |
|
| 32 |
#include <linux/can.h> |
| 33 |
#include <linux/can/dev.h> |
| 34 |
|
| 35 |
#define KVASER_USB_MAX_RX_URBS 4 |
| 36 |
#define KVASER_USB_MAX_TX_URBS 128 |
| 37 |
#define KVASER_USB_TIMEOUT 1000 /* msecs */ |
| 38 |
#define KVASER_USB_RX_BUFFER_SIZE 3072 |
| 39 |
#define KVASER_USB_MAX_NET_DEVICES 5 |
| 40 |
|
| 41 |
/* Kvaser USB device quirks */ |
| 42 |
#define KVASER_USB_QUIRK_HAS_SILENT_MODE BIT(0) |
| 43 |
#define KVASER_USB_QUIRK_HAS_TXRX_ERRORS BIT(1) |
| 44 |
#define KVASER_USB_QUIRK_IGNORE_CLK_FREQ BIT(2) |
| 45 |
|
| 46 |
/* Device capabilities */ |
| 47 |
#define KVASER_USB_CAP_BERR_CAP 0x01 |
| 48 |
#define KVASER_USB_CAP_EXT_CAP 0x02 |
| 49 |
#define KVASER_USB_HYDRA_CAP_EXT_CMD 0x04 |
| 50 |
|
| 51 |
#define KVASER_USB_SW_VERSION_MAJOR_MASK GENMASK(31, 24) |
| 52 |
#define KVASER_USB_SW_VERSION_MINOR_MASK GENMASK(23, 16) |
| 53 |
#define KVASER_USB_SW_VERSION_BUILD_MASK GENMASK(15, 0) |
| 54 |
|
| 55 |
struct kvaser_usb_dev_cfg; |
| 56 |
|
| 57 |
enum kvaser_usb_leaf_family { |
| 58 |
KVASER_LEAF, |
| 59 |
KVASER_USBCAN, |
| 60 |
}; |
| 61 |
|
| 62 |
enum kvaser_usb_led_state { |
| 63 |
KVASER_USB_LED_ON = 0, |
| 64 |
KVASER_USB_LED_OFF = 1, |
| 65 |
}; |
| 66 |
|
| 67 |
#define KVASER_USB_HYDRA_MAX_CMD_LEN 128 |
| 68 |
struct kvaser_usb_dev_card_data_hydra { |
| 69 |
u8 channel_to_he[KVASER_USB_MAX_NET_DEVICES]; |
| 70 |
u8 sysdbg_he; |
| 71 |
spinlock_t transid_lock; /* lock for transid */ |
| 72 |
u16 transid; |
| 73 |
/* lock for usb_rx_leftover and usb_rx_leftover_len */ |
| 74 |
spinlock_t usb_rx_leftover_lock; |
| 75 |
u8 usb_rx_leftover[KVASER_USB_HYDRA_MAX_CMD_LEN]; |
| 76 |
u8 usb_rx_leftover_len; |
| 77 |
}; |
| 78 |
struct kvaser_usb_dev_card_data { |
| 79 |
u32 ctrlmode_supported; |
| 80 |
u32 capabilities; |
| 81 |
struct kvaser_usb_dev_card_data_hydra hydra; |
| 82 |
u32 usbcan_timestamp_msb; |
| 83 |
}; |
| 84 |
|
| 85 |
/* Context for an outstanding, not yet ACKed, transmission */ |
| 86 |
struct kvaser_usb_tx_urb_context { |
| 87 |
struct kvaser_usb_net_priv *priv; |
| 88 |
u32 echo_index; |
| 89 |
}; |
| 90 |
|
| 91 |
struct kvaser_usb_fw_version { |
| 92 |
u8 major; |
| 93 |
u8 minor; |
| 94 |
u16 build; |
| 95 |
}; |
| 96 |
|
| 97 |
struct kvaser_usb_busparams { |
| 98 |
__le32 bitrate; |
| 99 |
u8 tseg1; |
| 100 |
u8 tseg2; |
| 101 |
u8 sjw; |
| 102 |
u8 nsamples; |
| 103 |
} __packed; |
| 104 |
|
| 105 |
struct kvaser_usb { |
| 106 |
struct usb_device *udev; |
| 107 |
struct usb_interface *intf; |
| 108 |
struct kvaser_usb_net_priv *nets[KVASER_USB_MAX_NET_DEVICES]; |
| 109 |
const struct kvaser_usb_driver_info *driver_info; |
| 110 |
const struct kvaser_usb_dev_cfg *cfg; |
| 111 |
|
| 112 |
struct usb_endpoint_descriptor *bulk_in, *bulk_out; |
| 113 |
struct usb_anchor rx_submitted; |
| 114 |
|
| 115 |
u32 ean[2]; |
| 116 |
u32 serial_number; |
| 117 |
struct kvaser_usb_fw_version fw_version; |
| 118 |
u8 hw_revision; |
| 119 |
unsigned int nchannels; |
| 120 |
/* @max_tx_urbs: Firmware-reported maximum number of outstanding, |
| 121 |
* not yet ACKed, transmissions on this device. This value is |
| 122 |
* also used as a sentinel for marking free tx contexts. |
| 123 |
*/ |
| 124 |
unsigned int max_tx_urbs; |
| 125 |
struct kvaser_usb_dev_card_data card_data; |
| 126 |
|
| 127 |
bool rxinitdone; |
| 128 |
void *rxbuf[KVASER_USB_MAX_RX_URBS]; |
| 129 |
dma_addr_t rxbuf_dma[KVASER_USB_MAX_RX_URBS]; |
| 130 |
}; |
| 131 |
|
| 132 |
struct kvaser_usb_net_priv { |
| 133 |
struct can_priv can; |
| 134 |
struct devlink_port devlink_port; |
| 135 |
struct can_berr_counter bec; |
| 136 |
|
| 137 |
/* subdriver-specific data */ |
| 138 |
void *sub_priv; |
| 139 |
|
| 140 |
struct kvaser_usb *dev; |
| 141 |
struct net_device *netdev; |
| 142 |
int channel; |
| 143 |
|
| 144 |
struct completion start_comp, stop_comp, flush_comp, |
| 145 |
get_busparams_comp; |
| 146 |
struct usb_anchor tx_submitted; |
| 147 |
|
| 148 |
struct kvaser_usb_busparams busparams_nominal, busparams_data; |
| 149 |
|
| 150 |
spinlock_t tx_contexts_lock; /* lock for active_tx_contexts */ |
| 151 |
int active_tx_contexts; |
| 152 |
struct kvaser_usb_tx_urb_context tx_contexts[]; |
| 153 |
}; |
| 154 |
|
| 155 |
/** |
| 156 |
* struct kvaser_usb_dev_ops - Device specific functions |
| 157 |
* @dev_set_mode: used for can.do_set_mode |
| 158 |
* @dev_set_bittiming: used for can.do_set_bittiming |
| 159 |
* @dev_get_busparams: readback arbitration busparams |
| 160 |
* @dev_set_data_bittiming: used for can.fd.do_set_data_bittiming |
| 161 |
* @dev_get_data_busparams: readback data busparams |
| 162 |
* @dev_get_berr_counter: used for can.do_get_berr_counter |
| 163 |
* |
| 164 |
* @dev_setup_endpoints: setup USB in and out endpoints |
| 165 |
* @dev_init_card: initialize card |
| 166 |
* @dev_init_channel: initialize channel |
| 167 |
* @dev_remove_channel: uninitialize channel |
| 168 |
* @dev_get_software_info: get software info |
| 169 |
* @dev_get_software_details: get software details |
| 170 |
* @dev_get_card_info: get card info |
| 171 |
* @dev_get_capabilities: discover device capabilities |
| 172 |
* @dev_set_led: turn on/off device LED |
| 173 |
* |
| 174 |
* @dev_set_opt_mode: set ctrlmod |
| 175 |
* @dev_start_chip: start the CAN controller |
| 176 |
* @dev_stop_chip: stop the CAN controller |
| 177 |
* @dev_reset_chip: reset the CAN controller |
| 178 |
* @dev_flush_queue: flush outstanding CAN messages |
| 179 |
* @dev_read_bulk_callback: handle incoming commands |
| 180 |
* @dev_frame_to_cmd: translate struct can_frame into device command |
| 181 |
*/ |
| 182 |
struct kvaser_usb_dev_ops { |
| 183 |
int (*dev_set_mode)(struct net_device *netdev, enum can_mode mode); |
| 184 |
int (*dev_set_bittiming)(const struct net_device *netdev, |
| 185 |
const struct kvaser_usb_busparams *busparams); |
| 186 |
int (*dev_get_busparams)(struct kvaser_usb_net_priv *priv); |
| 187 |
int (*dev_set_data_bittiming)(const struct net_device *netdev, |
| 188 |
const struct kvaser_usb_busparams *busparams); |
| 189 |
int (*dev_get_data_busparams)(struct kvaser_usb_net_priv *priv); |
| 190 |
int (*dev_get_berr_counter)(const struct net_device *netdev, |
| 191 |
struct can_berr_counter *bec); |
| 192 |
int (*dev_setup_endpoints)(struct kvaser_usb *dev); |
| 193 |
int (*dev_init_card)(struct kvaser_usb *dev); |
| 194 |
int (*dev_init_channel)(struct kvaser_usb_net_priv *priv); |
| 195 |
void (*dev_remove_channel)(struct kvaser_usb_net_priv *priv); |
| 196 |
int (*dev_get_software_info)(struct kvaser_usb *dev); |
| 197 |
int (*dev_get_software_details)(struct kvaser_usb *dev); |
| 198 |
int (*dev_get_card_info)(struct kvaser_usb *dev); |
| 199 |
int (*dev_get_capabilities)(struct kvaser_usb *dev); |
| 200 |
int (*dev_set_led)(struct kvaser_usb_net_priv *priv, |
| 201 |
enum kvaser_usb_led_state state, |
| 202 |
u16 duration_ms); |
| 203 |
int (*dev_set_opt_mode)(const struct kvaser_usb_net_priv *priv); |
| 204 |
int (*dev_start_chip)(struct kvaser_usb_net_priv *priv); |
| 205 |
int (*dev_stop_chip)(struct kvaser_usb_net_priv *priv); |
| 206 |
int (*dev_reset_chip)(struct kvaser_usb *dev, int channel); |
| 207 |
int (*dev_flush_queue)(struct kvaser_usb_net_priv *priv); |
| 208 |
void (*dev_read_bulk_callback)(struct kvaser_usb *dev, void *buf, |
| 209 |
int len); |
| 210 |
void *(*dev_frame_to_cmd)(const struct kvaser_usb_net_priv *priv, |
| 211 |
const struct sk_buff *skb, int *cmd_len, |
| 212 |
u16 transid); |
| 213 |
}; |
| 214 |
|
| 215 |
struct kvaser_usb_driver_info { |
| 216 |
u32 quirks; |
| 217 |
enum kvaser_usb_leaf_family family; |
| 218 |
const struct kvaser_usb_dev_ops *ops; |
| 219 |
}; |
| 220 |
|
| 221 |
struct kvaser_usb_dev_cfg { |
| 222 |
const struct can_clock clock; |
| 223 |
const unsigned int timestamp_freq; |
| 224 |
const struct can_bittiming_const * const bittiming_const; |
| 225 |
const struct can_bittiming_const * const data_bittiming_const; |
| 226 |
}; |
| 227 |
|
| 228 |
extern const struct kvaser_usb_dev_ops kvaser_usb_hydra_dev_ops; |
| 229 |
extern const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops; |
| 230 |
|
| 231 |
extern const struct devlink_ops kvaser_usb_devlink_ops; |
| 232 |
|
| 233 |
int kvaser_usb_devlink_port_register(struct kvaser_usb_net_priv *priv); |
| 234 |
void kvaser_usb_devlink_port_unregister(struct kvaser_usb_net_priv *priv); |
| 235 |
|
| 236 |
void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv); |
| 237 |
|
| 238 |
int kvaser_usb_recv_cmd(const struct kvaser_usb *dev, void *cmd, int len, |
| 239 |
int *actual_len); |
| 240 |
|
| 241 |
int kvaser_usb_send_cmd(const struct kvaser_usb *dev, void *cmd, int len); |
| 242 |
|
| 243 |
int kvaser_usb_send_cmd_async(struct kvaser_usb_net_priv *priv, void *cmd, |
| 244 |
int len); |
| 245 |
|
| 246 |
int kvaser_usb_can_rx_over_error(struct net_device *netdev); |
| 247 |
|
| 248 |
extern const struct can_bittiming_const kvaser_usb_flexc_bittiming_const; |
| 249 |
|
| 250 |
static inline ktime_t kvaser_usb_ticks_to_ktime(const struct kvaser_usb_dev_cfg *cfg, |
| 251 |
u64 ticks) |
| 252 |
{ |
| 253 |
return ns_to_ktime(div_u64(ticks * 1000, cfg->timestamp_freq)); |
| 254 |
} |
| 255 |
|
| 256 |
static inline ktime_t kvaser_usb_timestamp48_to_ktime(const struct kvaser_usb_dev_cfg *cfg, |
| 257 |
const __le16 *timestamp) |
| 258 |
{ |
| 259 |
u64 ticks = le16_to_cpu(timestamp[0]) | |
| 260 |
(u64)(le16_to_cpu(timestamp[1])) << 16 | |
| 261 |
(u64)(le16_to_cpu(timestamp[2])) << 32; |
| 262 |
|
| 263 |
return kvaser_usb_ticks_to_ktime(cfg, ticks); |
| 264 |
} |
| 265 |
|
| 266 |
static inline ktime_t kvaser_usb_timestamp64_to_ktime(const struct kvaser_usb_dev_cfg *cfg, |
| 267 |
__le64 timestamp) |
| 268 |
{ |
| 269 |
return kvaser_usb_ticks_to_ktime(cfg, le64_to_cpu(timestamp)); |
| 270 |
} |
| 271 |
|
| 272 |
#endif /* KVASER_USB_H */ |