| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* CAN driver for Geschwister Schneider USB/CAN devices |
| 3 |
* and bytewerk.org candleLight USB CAN interfaces. |
| 4 |
* |
| 5 |
* Copyright (C) 2013-2016 Geschwister Schneider Technologie-, |
| 6 |
* Entwicklungs- und Vertriebs UG (Haftungsbeschränkt). |
| 7 |
* Copyright (C) 2016 Hubert Denkmair |
| 8 |
* Copyright (c) 2023 Pengutronix, Marc Kleine-Budde <[email protected]> |
| 9 |
* |
| 10 |
* Many thanks to all socketcan devs! |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <linux/bitfield.h> |
| 14 |
#include <linux/clocksource.h> |
| 15 |
#include <linux/ethtool.h> |
| 16 |
#include <linux/init.h> |
| 17 |
#include <linux/module.h> |
| 18 |
#include <linux/netdevice.h> |
| 19 |
#include <linux/signal.h> |
| 20 |
#include <linux/timecounter.h> |
| 21 |
#include <linux/units.h> |
| 22 |
#include <linux/usb.h> |
| 23 |
#include <linux/workqueue.h> |
| 24 |
|
| 25 |
#include <linux/can.h> |
| 26 |
#include <linux/can/dev.h> |
| 27 |
#include <linux/can/error.h> |
| 28 |
#include <linux/can/rx-offload.h> |
| 29 |
|
| 30 |
/* Device specific constants */ |
| 31 |
#define USB_GS_USB_1_VENDOR_ID 0x1d50 |
| 32 |
#define USB_GS_USB_1_PRODUCT_ID 0x606f |
| 33 |
|
| 34 |
#define USB_CANDLELIGHT_VENDOR_ID 0x1209 |
| 35 |
#define USB_CANDLELIGHT_PRODUCT_ID 0x2323 |
| 36 |
|
| 37 |
#define USB_CES_CANEXT_FD_VENDOR_ID 0x1cd2 |
| 38 |
#define USB_CES_CANEXT_FD_PRODUCT_ID 0x606f |
| 39 |
|
| 40 |
#define USB_ABE_CANDEBUGGER_FD_VENDOR_ID 0x16d0 |
| 41 |
#define USB_ABE_CANDEBUGGER_FD_PRODUCT_ID 0x10b8 |
| 42 |
|
| 43 |
#define USB_XYLANTA_SAINT3_VENDOR_ID 0x16d0 |
| 44 |
#define USB_XYLANTA_SAINT3_PRODUCT_ID 0x0f30 |
| 45 |
|
| 46 |
#define USB_CANNECTIVITY_VENDOR_ID 0x1209 |
| 47 |
#define USB_CANNECTIVITY_PRODUCT_ID 0xca01 |
| 48 |
|
| 49 |
/* Timestamp 32 bit timer runs at 1 MHz (1 µs tick). Worker accounts |
| 50 |
* for timer overflow (will be after ~71 minutes) |
| 51 |
*/ |
| 52 |
#define GS_USB_TIMESTAMP_TIMER_HZ (1 * HZ_PER_MHZ) |
| 53 |
#define GS_USB_TIMESTAMP_WORK_DELAY_SEC 1800 |
| 54 |
static_assert(GS_USB_TIMESTAMP_WORK_DELAY_SEC < |
| 55 |
CYCLECOUNTER_MASK(32) / GS_USB_TIMESTAMP_TIMER_HZ / 2); |
| 56 |
|
| 57 |
/* Device specific constants */ |
| 58 |
enum gs_usb_breq { |
| 59 |
GS_USB_BREQ_HOST_FORMAT = 0, |
| 60 |
GS_USB_BREQ_BITTIMING, |
| 61 |
GS_USB_BREQ_MODE, |
| 62 |
GS_USB_BREQ_BERR, |
| 63 |
GS_USB_BREQ_BT_CONST, |
| 64 |
GS_USB_BREQ_DEVICE_CONFIG, |
| 65 |
GS_USB_BREQ_TIMESTAMP, |
| 66 |
GS_USB_BREQ_IDENTIFY, |
| 67 |
GS_USB_BREQ_GET_USER_ID, |
| 68 |
GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING = GS_USB_BREQ_GET_USER_ID, |
| 69 |
GS_USB_BREQ_SET_USER_ID, |
| 70 |
GS_USB_BREQ_DATA_BITTIMING, |
| 71 |
GS_USB_BREQ_BT_CONST_EXT, |
| 72 |
GS_USB_BREQ_SET_TERMINATION, |
| 73 |
GS_USB_BREQ_GET_TERMINATION, |
| 74 |
GS_USB_BREQ_GET_STATE, |
| 75 |
}; |
| 76 |
|
| 77 |
enum gs_can_mode { |
| 78 |
/* reset a channel. turns it off */ |
| 79 |
GS_CAN_MODE_RESET = 0, |
| 80 |
/* starts a channel */ |
| 81 |
GS_CAN_MODE_START |
| 82 |
}; |
| 83 |
|
| 84 |
enum gs_can_state { |
| 85 |
GS_CAN_STATE_ERROR_ACTIVE = 0, |
| 86 |
GS_CAN_STATE_ERROR_WARNING, |
| 87 |
GS_CAN_STATE_ERROR_PASSIVE, |
| 88 |
GS_CAN_STATE_BUS_OFF, |
| 89 |
GS_CAN_STATE_STOPPED, |
| 90 |
GS_CAN_STATE_SLEEPING |
| 91 |
}; |
| 92 |
|
| 93 |
enum gs_can_identify_mode { |
| 94 |
GS_CAN_IDENTIFY_OFF = 0, |
| 95 |
GS_CAN_IDENTIFY_ON |
| 96 |
}; |
| 97 |
|
| 98 |
enum gs_can_termination_state { |
| 99 |
GS_CAN_TERMINATION_STATE_OFF = 0, |
| 100 |
GS_CAN_TERMINATION_STATE_ON |
| 101 |
}; |
| 102 |
|
| 103 |
#define GS_USB_TERMINATION_DISABLED CAN_TERMINATION_DISABLED |
| 104 |
#define GS_USB_TERMINATION_ENABLED 120 |
| 105 |
|
| 106 |
/* data types passed between host and device */ |
| 107 |
|
| 108 |
/* The firmware on the original USB2CAN by Geschwister Schneider |
| 109 |
* Technologie Entwicklungs- und Vertriebs UG exchanges all data |
| 110 |
* between the host and the device in host byte order. This is done |
| 111 |
* with the struct gs_host_config::byte_order member, which is sent |
| 112 |
* first to indicate the desired byte order. |
| 113 |
* |
| 114 |
* The widely used open source firmware candleLight doesn't support |
| 115 |
* this feature and exchanges the data in little endian byte order. |
| 116 |
*/ |
| 117 |
struct gs_host_config { |
| 118 |
__le32 byte_order; |
| 119 |
} __packed; |
| 120 |
|
| 121 |
struct gs_device_config { |
| 122 |
u8 reserved1; |
| 123 |
u8 reserved2; |
| 124 |
u8 reserved3; |
| 125 |
u8 icount; |
| 126 |
__le32 sw_version; |
| 127 |
__le32 hw_version; |
| 128 |
} __packed; |
| 129 |
|
| 130 |
#define GS_CAN_MODE_NORMAL 0 |
| 131 |
#define GS_CAN_MODE_LISTEN_ONLY BIT(0) |
| 132 |
#define GS_CAN_MODE_LOOP_BACK BIT(1) |
| 133 |
#define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2) |
| 134 |
#define GS_CAN_MODE_ONE_SHOT BIT(3) |
| 135 |
#define GS_CAN_MODE_HW_TIMESTAMP BIT(4) |
| 136 |
/* GS_CAN_FEATURE_IDENTIFY BIT(5) */ |
| 137 |
/* GS_CAN_FEATURE_USER_ID BIT(6) */ |
| 138 |
#define GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7) |
| 139 |
#define GS_CAN_MODE_FD BIT(8) |
| 140 |
/* GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9) */ |
| 141 |
/* GS_CAN_FEATURE_BT_CONST_EXT BIT(10) */ |
| 142 |
/* GS_CAN_FEATURE_TERMINATION BIT(11) */ |
| 143 |
#define GS_CAN_MODE_BERR_REPORTING BIT(12) |
| 144 |
/* GS_CAN_FEATURE_GET_STATE BIT(13) */ |
| 145 |
|
| 146 |
struct gs_device_mode { |
| 147 |
__le32 mode; |
| 148 |
__le32 flags; |
| 149 |
} __packed; |
| 150 |
|
| 151 |
struct gs_device_state { |
| 152 |
__le32 state; |
| 153 |
__le32 rxerr; |
| 154 |
__le32 txerr; |
| 155 |
} __packed; |
| 156 |
|
| 157 |
struct gs_device_bittiming { |
| 158 |
__le32 prop_seg; |
| 159 |
__le32 phase_seg1; |
| 160 |
__le32 phase_seg2; |
| 161 |
__le32 sjw; |
| 162 |
__le32 brp; |
| 163 |
} __packed; |
| 164 |
|
| 165 |
struct gs_identify_mode { |
| 166 |
__le32 mode; |
| 167 |
} __packed; |
| 168 |
|
| 169 |
struct gs_device_termination_state { |
| 170 |
__le32 state; |
| 171 |
} __packed; |
| 172 |
|
| 173 |
#define GS_CAN_FEATURE_LISTEN_ONLY BIT(0) |
| 174 |
#define GS_CAN_FEATURE_LOOP_BACK BIT(1) |
| 175 |
#define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2) |
| 176 |
#define GS_CAN_FEATURE_ONE_SHOT BIT(3) |
| 177 |
#define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4) |
| 178 |
#define GS_CAN_FEATURE_IDENTIFY BIT(5) |
| 179 |
#define GS_CAN_FEATURE_USER_ID BIT(6) |
| 180 |
#define GS_CAN_FEATURE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7) |
| 181 |
#define GS_CAN_FEATURE_FD BIT(8) |
| 182 |
#define GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9) |
| 183 |
#define GS_CAN_FEATURE_BT_CONST_EXT BIT(10) |
| 184 |
#define GS_CAN_FEATURE_TERMINATION BIT(11) |
| 185 |
#define GS_CAN_FEATURE_BERR_REPORTING BIT(12) |
| 186 |
#define GS_CAN_FEATURE_GET_STATE BIT(13) |
| 187 |
#define GS_CAN_FEATURE_MASK GENMASK(13, 0) |
| 188 |
|
| 189 |
/* internal quirks - keep in GS_CAN_FEATURE space for now */ |
| 190 |
|
| 191 |
/* CANtact Pro original firmware: |
| 192 |
* BREQ DATA_BITTIMING overlaps with GET_USER_ID |
| 193 |
*/ |
| 194 |
#define GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO BIT(31) |
| 195 |
|
| 196 |
struct gs_device_bt_const { |
| 197 |
__le32 feature; |
| 198 |
__le32 fclk_can; |
| 199 |
__le32 tseg1_min; |
| 200 |
__le32 tseg1_max; |
| 201 |
__le32 tseg2_min; |
| 202 |
__le32 tseg2_max; |
| 203 |
__le32 sjw_max; |
| 204 |
__le32 brp_min; |
| 205 |
__le32 brp_max; |
| 206 |
__le32 brp_inc; |
| 207 |
} __packed; |
| 208 |
|
| 209 |
struct gs_device_bt_const_extended { |
| 210 |
__le32 feature; |
| 211 |
__le32 fclk_can; |
| 212 |
__le32 tseg1_min; |
| 213 |
__le32 tseg1_max; |
| 214 |
__le32 tseg2_min; |
| 215 |
__le32 tseg2_max; |
| 216 |
__le32 sjw_max; |
| 217 |
__le32 brp_min; |
| 218 |
__le32 brp_max; |
| 219 |
__le32 brp_inc; |
| 220 |
|
| 221 |
__le32 dtseg1_min; |
| 222 |
__le32 dtseg1_max; |
| 223 |
__le32 dtseg2_min; |
| 224 |
__le32 dtseg2_max; |
| 225 |
__le32 dsjw_max; |
| 226 |
__le32 dbrp_min; |
| 227 |
__le32 dbrp_max; |
| 228 |
__le32 dbrp_inc; |
| 229 |
} __packed; |
| 230 |
|
| 231 |
#define GS_CAN_FLAG_OVERFLOW BIT(0) |
| 232 |
#define GS_CAN_FLAG_FD BIT(1) |
| 233 |
#define GS_CAN_FLAG_BRS BIT(2) |
| 234 |
#define GS_CAN_FLAG_ESI BIT(3) |
| 235 |
|
| 236 |
struct classic_can { |
| 237 |
u8 data[8]; |
| 238 |
} __packed; |
| 239 |
|
| 240 |
struct classic_can_ts { |
| 241 |
u8 data[8]; |
| 242 |
__le32 timestamp_us; |
| 243 |
} __packed; |
| 244 |
|
| 245 |
struct classic_can_quirk { |
| 246 |
u8 data[8]; |
| 247 |
u8 quirk; |
| 248 |
} __packed; |
| 249 |
|
| 250 |
struct canfd { |
| 251 |
u8 data[64]; |
| 252 |
} __packed; |
| 253 |
|
| 254 |
struct canfd_ts { |
| 255 |
u8 data[64]; |
| 256 |
__le32 timestamp_us; |
| 257 |
} __packed; |
| 258 |
|
| 259 |
struct canfd_quirk { |
| 260 |
u8 data[64]; |
| 261 |
u8 quirk; |
| 262 |
} __packed; |
| 263 |
|
| 264 |
/* struct gs_host_frame::echo_id == GS_HOST_FRAME_ECHO_ID_RX indicates |
| 265 |
* a regular RX'ed CAN frame |
| 266 |
*/ |
| 267 |
#define GS_HOST_FRAME_ECHO_ID_RX 0xffffffff |
| 268 |
|
| 269 |
struct gs_host_frame { |
| 270 |
struct_group(header, |
| 271 |
u32 echo_id; |
| 272 |
__le32 can_id; |
| 273 |
|
| 274 |
u8 can_dlc; |
| 275 |
u8 channel; |
| 276 |
u8 flags; |
| 277 |
u8 reserved; |
| 278 |
); |
| 279 |
|
| 280 |
union { |
| 281 |
DECLARE_FLEX_ARRAY(struct classic_can, classic_can); |
| 282 |
DECLARE_FLEX_ARRAY(struct classic_can_ts, classic_can_ts); |
| 283 |
DECLARE_FLEX_ARRAY(struct classic_can_quirk, classic_can_quirk); |
| 284 |
DECLARE_FLEX_ARRAY(struct canfd, canfd); |
| 285 |
DECLARE_FLEX_ARRAY(struct canfd_ts, canfd_ts); |
| 286 |
DECLARE_FLEX_ARRAY(struct canfd_quirk, canfd_quirk); |
| 287 |
}; |
| 288 |
} __packed; |
| 289 |
/* The GS USB devices make use of the same flags and masks as in |
| 290 |
* linux/can.h and linux/can/error.h, and no additional mapping is necessary. |
| 291 |
*/ |
| 292 |
|
| 293 |
/* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */ |
| 294 |
#define GS_MAX_TX_URBS 10 |
| 295 |
/* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */ |
| 296 |
#define GS_MAX_RX_URBS 30 |
| 297 |
#define GS_NAPI_WEIGHT 32 |
| 298 |
|
| 299 |
struct gs_tx_context { |
| 300 |
struct gs_can *dev; |
| 301 |
unsigned int echo_id; |
| 302 |
}; |
| 303 |
|
| 304 |
struct gs_can { |
| 305 |
struct can_priv can; /* must be the first member */ |
| 306 |
|
| 307 |
struct can_rx_offload offload; |
| 308 |
struct gs_usb *parent; |
| 309 |
|
| 310 |
struct net_device *netdev; |
| 311 |
struct usb_device *udev; |
| 312 |
|
| 313 |
struct can_bittiming_const bt_const, data_bt_const; |
| 314 |
unsigned int channel; /* channel number */ |
| 315 |
|
| 316 |
u32 feature; |
| 317 |
unsigned int hf_size_tx; |
| 318 |
|
| 319 |
/* This lock prevents a race condition between xmit and receive. */ |
| 320 |
spinlock_t tx_ctx_lock; |
| 321 |
struct gs_tx_context tx_context[GS_MAX_TX_URBS]; |
| 322 |
|
| 323 |
struct usb_anchor tx_submitted; |
| 324 |
atomic_t active_tx_urbs; |
| 325 |
}; |
| 326 |
|
| 327 |
/* usb interface struct */ |
| 328 |
struct gs_usb { |
| 329 |
struct usb_anchor rx_submitted; |
| 330 |
struct usb_device *udev; |
| 331 |
|
| 332 |
/* time counter for hardware timestamps */ |
| 333 |
struct cyclecounter cc; |
| 334 |
struct timecounter tc; |
| 335 |
spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */ |
| 336 |
struct delayed_work timestamp; |
| 337 |
|
| 338 |
unsigned int hf_size_rx; |
| 339 |
u8 active_channels; |
| 340 |
u8 channel_cnt; |
| 341 |
|
| 342 |
unsigned int pipe_in; |
| 343 |
unsigned int pipe_out; |
| 344 |
struct gs_can *canch[] __counted_by(channel_cnt); |
| 345 |
}; |
| 346 |
|
| 347 |
/* 'allocate' a tx context. |
| 348 |
* returns a valid tx context or NULL if there is no space. |
| 349 |
*/ |
| 350 |
static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev) |
| 351 |
{ |
| 352 |
int i = 0; |
| 353 |
unsigned long flags; |
| 354 |
|
| 355 |
spin_lock_irqsave(&dev->tx_ctx_lock, flags); |
| 356 |
|
| 357 |
for (; i < GS_MAX_TX_URBS; i++) { |
| 358 |
if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) { |
| 359 |
dev->tx_context[i].echo_id = i; |
| 360 |
spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); |
| 361 |
return &dev->tx_context[i]; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); |
| 366 |
return NULL; |
| 367 |
} |
| 368 |
|
| 369 |
/* releases a tx context |
| 370 |
*/ |
| 371 |
static void gs_free_tx_context(struct gs_tx_context *txc) |
| 372 |
{ |
| 373 |
txc->echo_id = GS_MAX_TX_URBS; |
| 374 |
} |
| 375 |
|
| 376 |
/* Get a tx context by id. |
| 377 |
*/ |
| 378 |
static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev, |
| 379 |
unsigned int id) |
| 380 |
{ |
| 381 |
unsigned long flags; |
| 382 |
|
| 383 |
if (id < GS_MAX_TX_URBS) { |
| 384 |
spin_lock_irqsave(&dev->tx_ctx_lock, flags); |
| 385 |
if (dev->tx_context[id].echo_id == id) { |
| 386 |
spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); |
| 387 |
return &dev->tx_context[id]; |
| 388 |
} |
| 389 |
spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); |
| 390 |
} |
| 391 |
return NULL; |
| 392 |
} |
| 393 |
|
| 394 |
static int gs_cmd_reset(struct gs_can *dev) |
| 395 |
{ |
| 396 |
struct gs_device_mode dm = { |
| 397 |
.mode = cpu_to_le32(GS_CAN_MODE_RESET), |
| 398 |
}; |
| 399 |
|
| 400 |
return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE, |
| 401 |
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 402 |
dev->channel, 0, &dm, sizeof(dm), 1000, |
| 403 |
GFP_KERNEL); |
| 404 |
} |
| 405 |
|
| 406 |
static inline int gs_usb_get_timestamp(const struct gs_usb *parent, |
| 407 |
u32 *timestamp_p) |
| 408 |
{ |
| 409 |
__le32 timestamp; |
| 410 |
int rc; |
| 411 |
|
| 412 |
rc = usb_control_msg_recv(parent->udev, 0, GS_USB_BREQ_TIMESTAMP, |
| 413 |
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 414 |
0, 0, |
| 415 |
×tamp, sizeof(timestamp), |
| 416 |
USB_CTRL_GET_TIMEOUT, |
| 417 |
GFP_KERNEL); |
| 418 |
if (rc) |
| 419 |
return rc; |
| 420 |
|
| 421 |
*timestamp_p = le32_to_cpu(timestamp); |
| 422 |
|
| 423 |
return 0; |
| 424 |
} |
| 425 |
|
| 426 |
static u64 gs_usb_timestamp_read(struct cyclecounter *cc) __must_hold(&dev->tc_lock) |
| 427 |
{ |
| 428 |
struct gs_usb *parent = container_of(cc, struct gs_usb, cc); |
| 429 |
u32 timestamp = 0; |
| 430 |
int err; |
| 431 |
|
| 432 |
lockdep_assert_held(&parent->tc_lock); |
| 433 |
|
| 434 |
/* drop lock for synchronous USB transfer */ |
| 435 |
spin_unlock_bh(&parent->tc_lock); |
| 436 |
err = gs_usb_get_timestamp(parent, ×tamp); |
| 437 |
spin_lock_bh(&parent->tc_lock); |
| 438 |
if (err) |
| 439 |
dev_err(&parent->udev->dev, |
| 440 |
"Error %d while reading timestamp. HW timestamps may be inaccurate.", |
| 441 |
err); |
| 442 |
|
| 443 |
return timestamp; |
| 444 |
} |
| 445 |
|
| 446 |
static void gs_usb_timestamp_work(struct work_struct *work) |
| 447 |
{ |
| 448 |
struct delayed_work *delayed_work = to_delayed_work(work); |
| 449 |
struct gs_usb *parent; |
| 450 |
|
| 451 |
parent = container_of(delayed_work, struct gs_usb, timestamp); |
| 452 |
spin_lock_bh(&parent->tc_lock); |
| 453 |
timecounter_read(&parent->tc); |
| 454 |
spin_unlock_bh(&parent->tc_lock); |
| 455 |
|
| 456 |
schedule_delayed_work(&parent->timestamp, |
| 457 |
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ); |
| 458 |
} |
| 459 |
|
| 460 |
static void gs_usb_skb_set_timestamp(struct gs_can *dev, |
| 461 |
struct sk_buff *skb, u32 timestamp) |
| 462 |
{ |
| 463 |
struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb); |
| 464 |
struct gs_usb *parent = dev->parent; |
| 465 |
u64 ns; |
| 466 |
|
| 467 |
spin_lock_bh(&parent->tc_lock); |
| 468 |
ns = timecounter_cyc2time(&parent->tc, timestamp); |
| 469 |
spin_unlock_bh(&parent->tc_lock); |
| 470 |
|
| 471 |
hwtstamps->hwtstamp = ns_to_ktime(ns); |
| 472 |
} |
| 473 |
|
| 474 |
static void gs_usb_timestamp_init(struct gs_usb *parent) |
| 475 |
{ |
| 476 |
struct cyclecounter *cc = &parent->cc; |
| 477 |
|
| 478 |
cc->read = gs_usb_timestamp_read; |
| 479 |
cc->mask = CYCLECOUNTER_MASK(32); |
| 480 |
cc->shift = 32 - bits_per(NSEC_PER_SEC / GS_USB_TIMESTAMP_TIMER_HZ); |
| 481 |
cc->mult = clocksource_hz2mult(GS_USB_TIMESTAMP_TIMER_HZ, cc->shift); |
| 482 |
|
| 483 |
spin_lock_init(&parent->tc_lock); |
| 484 |
spin_lock_bh(&parent->tc_lock); |
| 485 |
timecounter_init(&parent->tc, &parent->cc, ktime_get_real_ns()); |
| 486 |
spin_unlock_bh(&parent->tc_lock); |
| 487 |
|
| 488 |
INIT_DELAYED_WORK(&parent->timestamp, gs_usb_timestamp_work); |
| 489 |
schedule_delayed_work(&parent->timestamp, |
| 490 |
GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ); |
| 491 |
} |
| 492 |
|
| 493 |
static void gs_usb_timestamp_stop(struct gs_usb *parent) |
| 494 |
{ |
| 495 |
cancel_delayed_work_sync(&parent->timestamp); |
| 496 |
} |
| 497 |
|
| 498 |
static void gs_update_state(struct gs_can *dev, struct can_frame *cf) |
| 499 |
{ |
| 500 |
struct can_device_stats *can_stats = &dev->can.can_stats; |
| 501 |
|
| 502 |
if (cf->can_id & CAN_ERR_RESTARTED) { |
| 503 |
dev->can.state = CAN_STATE_ERROR_ACTIVE; |
| 504 |
can_stats->restarts++; |
| 505 |
} else if (cf->can_id & CAN_ERR_BUSOFF) { |
| 506 |
dev->can.state = CAN_STATE_BUS_OFF; |
| 507 |
can_stats->bus_off++; |
| 508 |
} else if (cf->can_id & CAN_ERR_CRTL) { |
| 509 |
if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) || |
| 510 |
(cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) { |
| 511 |
dev->can.state = CAN_STATE_ERROR_WARNING; |
| 512 |
can_stats->error_warning++; |
| 513 |
} else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) || |
| 514 |
(cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) { |
| 515 |
dev->can.state = CAN_STATE_ERROR_PASSIVE; |
| 516 |
can_stats->error_passive++; |
| 517 |
} else { |
| 518 |
dev->can.state = CAN_STATE_ERROR_ACTIVE; |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
static u32 gs_usb_set_timestamp(struct gs_can *dev, struct sk_buff *skb, |
| 524 |
const struct gs_host_frame *hf) |
| 525 |
{ |
| 526 |
u32 timestamp; |
| 527 |
|
| 528 |
if (hf->flags & GS_CAN_FLAG_FD) |
| 529 |
timestamp = le32_to_cpu(hf->canfd_ts->timestamp_us); |
| 530 |
else |
| 531 |
timestamp = le32_to_cpu(hf->classic_can_ts->timestamp_us); |
| 532 |
|
| 533 |
if (skb) |
| 534 |
gs_usb_skb_set_timestamp(dev, skb, timestamp); |
| 535 |
|
| 536 |
return timestamp; |
| 537 |
} |
| 538 |
|
| 539 |
static void gs_usb_rx_offload(struct gs_can *dev, struct sk_buff *skb, |
| 540 |
const struct gs_host_frame *hf) |
| 541 |
{ |
| 542 |
struct can_rx_offload *offload = &dev->offload; |
| 543 |
int rc; |
| 544 |
|
| 545 |
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) { |
| 546 |
const u32 ts = gs_usb_set_timestamp(dev, skb, hf); |
| 547 |
|
| 548 |
rc = can_rx_offload_queue_timestamp(offload, skb, ts); |
| 549 |
} else { |
| 550 |
rc = can_rx_offload_queue_tail(offload, skb); |
| 551 |
} |
| 552 |
|
| 553 |
if (rc) |
| 554 |
dev->netdev->stats.rx_fifo_errors++; |
| 555 |
} |
| 556 |
|
| 557 |
static unsigned int |
| 558 |
gs_usb_get_echo_skb(struct gs_can *dev, struct sk_buff *skb, |
| 559 |
const struct gs_host_frame *hf) |
| 560 |
{ |
| 561 |
struct can_rx_offload *offload = &dev->offload; |
| 562 |
const u32 echo_id = hf->echo_id; |
| 563 |
unsigned int len; |
| 564 |
|
| 565 |
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) { |
| 566 |
const u32 ts = gs_usb_set_timestamp(dev, skb, hf); |
| 567 |
|
| 568 |
len = can_rx_offload_get_echo_skb_queue_timestamp(offload, echo_id, |
| 569 |
ts, NULL); |
| 570 |
} else { |
| 571 |
len = can_rx_offload_get_echo_skb_queue_tail(offload, echo_id, |
| 572 |
NULL); |
| 573 |
} |
| 574 |
|
| 575 |
return len; |
| 576 |
} |
| 577 |
|
| 578 |
static unsigned int |
| 579 |
gs_usb_get_minimum_rx_length(const struct gs_can *dev, const struct gs_host_frame *hf, |
| 580 |
unsigned int *data_length_p) |
| 581 |
{ |
| 582 |
unsigned int minimum_length, data_length = 0; |
| 583 |
|
| 584 |
if (hf->flags & GS_CAN_FLAG_FD) { |
| 585 |
if (hf->echo_id == GS_HOST_FRAME_ECHO_ID_RX) |
| 586 |
data_length = can_fd_dlc2len(hf->can_dlc); |
| 587 |
|
| 588 |
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) |
| 589 |
/* timestamp follows data field of max size */ |
| 590 |
minimum_length = struct_size(hf, canfd_ts, 1); |
| 591 |
else |
| 592 |
minimum_length = sizeof(hf->header) + data_length; |
| 593 |
} else { |
| 594 |
if (hf->echo_id == GS_HOST_FRAME_ECHO_ID_RX && |
| 595 |
!(hf->can_id & cpu_to_le32(CAN_RTR_FLAG))) |
| 596 |
data_length = can_cc_dlc2len(hf->can_dlc); |
| 597 |
|
| 598 |
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) |
| 599 |
/* timestamp follows data field of max size */ |
| 600 |
minimum_length = struct_size(hf, classic_can_ts, 1); |
| 601 |
else |
| 602 |
minimum_length = sizeof(hf->header) + data_length; |
| 603 |
} |
| 604 |
|
| 605 |
*data_length_p = data_length; |
| 606 |
return minimum_length; |
| 607 |
} |
| 608 |
|
| 609 |
static void gs_usb_receive_bulk_callback(struct urb *urb) |
| 610 |
{ |
| 611 |
struct gs_usb *parent = urb->context; |
| 612 |
struct gs_can *dev; |
| 613 |
struct net_device *netdev = NULL; |
| 614 |
int rc; |
| 615 |
struct net_device_stats *stats; |
| 616 |
struct gs_host_frame *hf = urb->transfer_buffer; |
| 617 |
unsigned int minimum_length, data_length; |
| 618 |
struct gs_tx_context *txc; |
| 619 |
struct can_frame *cf; |
| 620 |
struct canfd_frame *cfd; |
| 621 |
struct sk_buff *skb; |
| 622 |
|
| 623 |
BUG_ON(!parent); |
| 624 |
|
| 625 |
switch (urb->status) { |
| 626 |
case 0: /* success */ |
| 627 |
break; |
| 628 |
case -ENOENT: |
| 629 |
case -ESHUTDOWN: |
| 630 |
return; |
| 631 |
default: |
| 632 |
/* do not resubmit aborted urbs. eg: when device goes down */ |
| 633 |
return; |
| 634 |
} |
| 635 |
|
| 636 |
minimum_length = sizeof(hf->header); |
| 637 |
if (urb->actual_length < minimum_length) { |
| 638 |
dev_err_ratelimited(&parent->udev->dev, |
| 639 |
"short read (actual_length=%u, minimum_length=%u)\n", |
| 640 |
urb->actual_length, minimum_length); |
| 641 |
|
| 642 |
goto resubmit_urb; |
| 643 |
} |
| 644 |
|
| 645 |
/* device reports out of range channel id */ |
| 646 |
if (hf->channel >= parent->channel_cnt) |
| 647 |
goto device_detach; |
| 648 |
|
| 649 |
dev = parent->canch[hf->channel]; |
| 650 |
|
| 651 |
netdev = dev->netdev; |
| 652 |
stats = &netdev->stats; |
| 653 |
|
| 654 |
if (!netif_device_present(netdev)) |
| 655 |
return; |
| 656 |
|
| 657 |
if (!netif_running(netdev)) |
| 658 |
goto resubmit_urb; |
| 659 |
|
| 660 |
minimum_length = gs_usb_get_minimum_rx_length(dev, hf, &data_length); |
| 661 |
if (urb->actual_length < minimum_length) { |
| 662 |
stats->rx_errors++; |
| 663 |
stats->rx_length_errors++; |
| 664 |
|
| 665 |
if (net_ratelimit()) |
| 666 |
netdev_err(netdev, |
| 667 |
"short read (actual_length=%u, minimum_length=%u)\n", |
| 668 |
urb->actual_length, minimum_length); |
| 669 |
|
| 670 |
goto resubmit_urb; |
| 671 |
} |
| 672 |
|
| 673 |
if (hf->echo_id == GS_HOST_FRAME_ECHO_ID_RX) { /* normal rx */ |
| 674 |
if (hf->flags & GS_CAN_FLAG_FD) { |
| 675 |
skb = alloc_canfd_skb(netdev, &cfd); |
| 676 |
if (!skb) |
| 677 |
return; |
| 678 |
|
| 679 |
cfd->can_id = le32_to_cpu(hf->can_id); |
| 680 |
cfd->len = data_length; |
| 681 |
if (hf->flags & GS_CAN_FLAG_BRS) |
| 682 |
cfd->flags |= CANFD_BRS; |
| 683 |
if (hf->flags & GS_CAN_FLAG_ESI) |
| 684 |
cfd->flags |= CANFD_ESI; |
| 685 |
|
| 686 |
memcpy(cfd->data, hf->canfd->data, data_length); |
| 687 |
} else { |
| 688 |
skb = alloc_can_skb(netdev, &cf); |
| 689 |
if (!skb) |
| 690 |
return; |
| 691 |
|
| 692 |
cf->can_id = le32_to_cpu(hf->can_id); |
| 693 |
can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode); |
| 694 |
|
| 695 |
memcpy(cf->data, hf->classic_can->data, data_length); |
| 696 |
|
| 697 |
/* ERROR frames tell us information about the controller */ |
| 698 |
if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG) |
| 699 |
gs_update_state(dev, cf); |
| 700 |
} |
| 701 |
|
| 702 |
gs_usb_rx_offload(dev, skb, hf); |
| 703 |
} else { /* echo_id == hf->echo_id */ |
| 704 |
if (hf->echo_id >= GS_MAX_TX_URBS) { |
| 705 |
netdev_err(netdev, |
| 706 |
"Unexpected out of range echo id %u\n", |
| 707 |
hf->echo_id); |
| 708 |
goto resubmit_urb; |
| 709 |
} |
| 710 |
|
| 711 |
txc = gs_get_tx_context(dev, hf->echo_id); |
| 712 |
|
| 713 |
/* bad devices send bad echo_ids. */ |
| 714 |
if (!txc) { |
| 715 |
netdev_err(netdev, |
| 716 |
"Unexpected unused echo id %u\n", |
| 717 |
hf->echo_id); |
| 718 |
goto resubmit_urb; |
| 719 |
} |
| 720 |
|
| 721 |
skb = dev->can.echo_skb[hf->echo_id]; |
| 722 |
stats->tx_packets++; |
| 723 |
stats->tx_bytes += gs_usb_get_echo_skb(dev, skb, hf); |
| 724 |
gs_free_tx_context(txc); |
| 725 |
|
| 726 |
atomic_dec(&dev->active_tx_urbs); |
| 727 |
|
| 728 |
netif_wake_queue(netdev); |
| 729 |
} |
| 730 |
|
| 731 |
if (hf->flags & GS_CAN_FLAG_OVERFLOW) { |
| 732 |
stats->rx_over_errors++; |
| 733 |
stats->rx_errors++; |
| 734 |
|
| 735 |
skb = alloc_can_err_skb(netdev, &cf); |
| 736 |
if (!skb) |
| 737 |
goto resubmit_urb; |
| 738 |
|
| 739 |
cf->can_id |= CAN_ERR_CRTL; |
| 740 |
cf->len = CAN_ERR_DLC; |
| 741 |
cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; |
| 742 |
|
| 743 |
gs_usb_rx_offload(dev, skb, hf); |
| 744 |
} |
| 745 |
|
| 746 |
can_rx_offload_irq_finish(&dev->offload); |
| 747 |
|
| 748 |
resubmit_urb: |
| 749 |
usb_fill_bulk_urb(urb, parent->udev, |
| 750 |
parent->pipe_in, |
| 751 |
hf, parent->hf_size_rx, |
| 752 |
gs_usb_receive_bulk_callback, parent); |
| 753 |
|
| 754 |
usb_anchor_urb(urb, &parent->rx_submitted); |
| 755 |
|
| 756 |
rc = usb_submit_urb(urb, GFP_ATOMIC); |
| 757 |
if (!rc) |
| 758 |
return; |
| 759 |
|
| 760 |
usb_unanchor_urb(urb); |
| 761 |
|
| 762 |
/* USB failure take down all interfaces */ |
| 763 |
if (rc == -ENODEV) { |
| 764 |
device_detach: |
| 765 |
for (rc = 0; rc < parent->channel_cnt; rc++) { |
| 766 |
if (parent->canch[rc]) |
| 767 |
netif_device_detach(parent->canch[rc]->netdev); |
| 768 |
} |
| 769 |
} else if (rc != -ESHUTDOWN && net_ratelimit()) { |
| 770 |
netdev_info(netdev, "failed to re-submit IN URB: %pe\n", |
| 771 |
ERR_PTR(rc)); |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
static int gs_usb_set_bittiming(struct gs_can *dev) |
| 776 |
{ |
| 777 |
struct can_bittiming *bt = &dev->can.bittiming; |
| 778 |
struct gs_device_bittiming dbt = { |
| 779 |
.prop_seg = cpu_to_le32(bt->prop_seg), |
| 780 |
.phase_seg1 = cpu_to_le32(bt->phase_seg1), |
| 781 |
.phase_seg2 = cpu_to_le32(bt->phase_seg2), |
| 782 |
.sjw = cpu_to_le32(bt->sjw), |
| 783 |
.brp = cpu_to_le32(bt->brp), |
| 784 |
}; |
| 785 |
|
| 786 |
/* request bit timings */ |
| 787 |
return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_BITTIMING, |
| 788 |
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 789 |
dev->channel, 0, &dbt, sizeof(dbt), 1000, |
| 790 |
GFP_KERNEL); |
| 791 |
} |
| 792 |
|
| 793 |
static int gs_usb_set_data_bittiming(struct gs_can *dev) |
| 794 |
{ |
| 795 |
struct can_bittiming *bt = &dev->can.fd.data_bittiming; |
| 796 |
struct gs_device_bittiming dbt = { |
| 797 |
.prop_seg = cpu_to_le32(bt->prop_seg), |
| 798 |
.phase_seg1 = cpu_to_le32(bt->phase_seg1), |
| 799 |
.phase_seg2 = cpu_to_le32(bt->phase_seg2), |
| 800 |
.sjw = cpu_to_le32(bt->sjw), |
| 801 |
.brp = cpu_to_le32(bt->brp), |
| 802 |
}; |
| 803 |
u8 request = GS_USB_BREQ_DATA_BITTIMING; |
| 804 |
|
| 805 |
if (dev->feature & GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO) |
| 806 |
request = GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING; |
| 807 |
|
| 808 |
/* request data bit timings */ |
| 809 |
return usb_control_msg_send(dev->udev, 0, request, |
| 810 |
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 811 |
dev->channel, 0, &dbt, sizeof(dbt), 1000, |
| 812 |
GFP_KERNEL); |
| 813 |
} |
| 814 |
|
| 815 |
static void gs_usb_xmit_callback(struct urb *urb) |
| 816 |
{ |
| 817 |
struct gs_tx_context *txc = urb->context; |
| 818 |
struct gs_can *dev = txc->dev; |
| 819 |
struct net_device *netdev = dev->netdev; |
| 820 |
|
| 821 |
if (!urb->status) |
| 822 |
return; |
| 823 |
|
| 824 |
if (urb->status != -ESHUTDOWN && net_ratelimit()) |
| 825 |
netdev_info(netdev, "failed to xmit URB %u: %pe\n", |
| 826 |
txc->echo_id, ERR_PTR(urb->status)); |
| 827 |
|
| 828 |
netdev->stats.tx_dropped++; |
| 829 |
netdev->stats.tx_errors++; |
| 830 |
|
| 831 |
can_free_echo_skb(netdev, txc->echo_id, NULL); |
| 832 |
gs_free_tx_context(txc); |
| 833 |
atomic_dec(&dev->active_tx_urbs); |
| 834 |
|
| 835 |
netif_wake_queue(netdev); |
| 836 |
} |
| 837 |
|
| 838 |
static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb, |
| 839 |
struct net_device *netdev) |
| 840 |
{ |
| 841 |
struct gs_can *dev = netdev_priv(netdev); |
| 842 |
struct net_device_stats *stats = &dev->netdev->stats; |
| 843 |
struct urb *urb; |
| 844 |
struct gs_host_frame *hf; |
| 845 |
struct can_frame *cf; |
| 846 |
struct canfd_frame *cfd; |
| 847 |
int rc; |
| 848 |
unsigned int idx; |
| 849 |
struct gs_tx_context *txc; |
| 850 |
|
| 851 |
if (can_dev_dropped_skb(netdev, skb)) |
| 852 |
return NETDEV_TX_OK; |
| 853 |
|
| 854 |
/* find an empty context to keep track of transmission */ |
| 855 |
txc = gs_alloc_tx_context(dev); |
| 856 |
if (!txc) |
| 857 |
return NETDEV_TX_BUSY; |
| 858 |
|
| 859 |
/* create a URB, and a buffer for it */ |
| 860 |
urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 861 |
if (!urb) |
| 862 |
goto nomem_urb; |
| 863 |
|
| 864 |
hf = kmalloc(dev->hf_size_tx, GFP_ATOMIC); |
| 865 |
if (!hf) |
| 866 |
goto nomem_hf; |
| 867 |
|
| 868 |
idx = txc->echo_id; |
| 869 |
|
| 870 |
if (idx >= GS_MAX_TX_URBS) { |
| 871 |
netdev_err(netdev, "Invalid tx context %u\n", idx); |
| 872 |
goto badidx; |
| 873 |
} |
| 874 |
|
| 875 |
hf->echo_id = idx; |
| 876 |
hf->channel = dev->channel; |
| 877 |
hf->flags = 0; |
| 878 |
hf->reserved = 0; |
| 879 |
|
| 880 |
if (can_is_canfd_skb(skb)) { |
| 881 |
cfd = (struct canfd_frame *)skb->data; |
| 882 |
|
| 883 |
hf->can_id = cpu_to_le32(cfd->can_id); |
| 884 |
hf->can_dlc = can_fd_len2dlc(cfd->len); |
| 885 |
hf->flags |= GS_CAN_FLAG_FD; |
| 886 |
if (cfd->flags & CANFD_BRS) |
| 887 |
hf->flags |= GS_CAN_FLAG_BRS; |
| 888 |
if (cfd->flags & CANFD_ESI) |
| 889 |
hf->flags |= GS_CAN_FLAG_ESI; |
| 890 |
|
| 891 |
memcpy(hf->canfd->data, cfd->data, cfd->len); |
| 892 |
} else { |
| 893 |
cf = (struct can_frame *)skb->data; |
| 894 |
|
| 895 |
hf->can_id = cpu_to_le32(cf->can_id); |
| 896 |
hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode); |
| 897 |
|
| 898 |
memcpy(hf->classic_can->data, cf->data, cf->len); |
| 899 |
} |
| 900 |
|
| 901 |
usb_fill_bulk_urb(urb, dev->udev, |
| 902 |
dev->parent->pipe_out, |
| 903 |
hf, dev->hf_size_tx, |
| 904 |
gs_usb_xmit_callback, txc); |
| 905 |
|
| 906 |
urb->transfer_flags |= URB_FREE_BUFFER; |
| 907 |
usb_anchor_urb(urb, &dev->tx_submitted); |
| 908 |
|
| 909 |
can_put_echo_skb(skb, netdev, idx, 0); |
| 910 |
|
| 911 |
atomic_inc(&dev->active_tx_urbs); |
| 912 |
|
| 913 |
rc = usb_submit_urb(urb, GFP_ATOMIC); |
| 914 |
if (unlikely(rc)) { /* usb send failed */ |
| 915 |
atomic_dec(&dev->active_tx_urbs); |
| 916 |
|
| 917 |
can_free_echo_skb(netdev, idx, NULL); |
| 918 |
gs_free_tx_context(txc); |
| 919 |
|
| 920 |
usb_unanchor_urb(urb); |
| 921 |
|
| 922 |
if (rc == -ENODEV) { |
| 923 |
netif_device_detach(netdev); |
| 924 |
} else { |
| 925 |
netdev_err(netdev, "usb_submit failed (err=%d)\n", rc); |
| 926 |
stats->tx_dropped++; |
| 927 |
} |
| 928 |
} else { |
| 929 |
/* Slow down tx path */ |
| 930 |
if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS) |
| 931 |
netif_stop_queue(netdev); |
| 932 |
} |
| 933 |
|
| 934 |
/* let usb core take care of this urb */ |
| 935 |
usb_free_urb(urb); |
| 936 |
|
| 937 |
return NETDEV_TX_OK; |
| 938 |
|
| 939 |
badidx: |
| 940 |
kfree(hf); |
| 941 |
nomem_hf: |
| 942 |
usb_free_urb(urb); |
| 943 |
|
| 944 |
nomem_urb: |
| 945 |
gs_free_tx_context(txc); |
| 946 |
dev_kfree_skb(skb); |
| 947 |
stats->tx_dropped++; |
| 948 |
return NETDEV_TX_OK; |
| 949 |
} |
| 950 |
|
| 951 |
static int gs_can_open(struct net_device *netdev) |
| 952 |
{ |
| 953 |
struct gs_can *dev = netdev_priv(netdev); |
| 954 |
struct gs_usb *parent = dev->parent; |
| 955 |
struct gs_device_mode dm = { |
| 956 |
.mode = cpu_to_le32(GS_CAN_MODE_START), |
| 957 |
}; |
| 958 |
struct gs_host_frame *hf; |
| 959 |
struct urb *urb = NULL; |
| 960 |
u32 ctrlmode; |
| 961 |
u32 flags = 0; |
| 962 |
int rc, i; |
| 963 |
|
| 964 |
rc = open_candev(netdev); |
| 965 |
if (rc) |
| 966 |
return rc; |
| 967 |
|
| 968 |
ctrlmode = dev->can.ctrlmode; |
| 969 |
if (ctrlmode & CAN_CTRLMODE_FD) { |
| 970 |
if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX) |
| 971 |
dev->hf_size_tx = struct_size(hf, canfd_quirk, 1); |
| 972 |
else |
| 973 |
dev->hf_size_tx = struct_size(hf, canfd, 1); |
| 974 |
} else { |
| 975 |
if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX) |
| 976 |
dev->hf_size_tx = struct_size(hf, classic_can_quirk, 1); |
| 977 |
else |
| 978 |
dev->hf_size_tx = struct_size(hf, classic_can, 1); |
| 979 |
} |
| 980 |
|
| 981 |
can_rx_offload_enable(&dev->offload); |
| 982 |
|
| 983 |
if (!parent->active_channels) { |
| 984 |
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) |
| 985 |
gs_usb_timestamp_init(parent); |
| 986 |
|
| 987 |
for (i = 0; i < GS_MAX_RX_URBS; i++) { |
| 988 |
u8 *buf; |
| 989 |
|
| 990 |
/* alloc rx urb */ |
| 991 |
urb = usb_alloc_urb(0, GFP_KERNEL); |
| 992 |
if (!urb) { |
| 993 |
rc = -ENOMEM; |
| 994 |
goto out_usb_kill_anchored_urbs; |
| 995 |
} |
| 996 |
|
| 997 |
/* alloc rx buffer */ |
| 998 |
buf = kmalloc(dev->parent->hf_size_rx, |
| 999 |
GFP_KERNEL); |
| 1000 |
if (!buf) { |
| 1001 |
rc = -ENOMEM; |
| 1002 |
goto out_usb_free_urb; |
| 1003 |
} |
| 1004 |
|
| 1005 |
/* fill, anchor, and submit rx urb */ |
| 1006 |
usb_fill_bulk_urb(urb, |
| 1007 |
dev->udev, |
| 1008 |
dev->parent->pipe_in, |
| 1009 |
buf, |
| 1010 |
dev->parent->hf_size_rx, |
| 1011 |
gs_usb_receive_bulk_callback, parent); |
| 1012 |
urb->transfer_flags |= URB_FREE_BUFFER; |
| 1013 |
|
| 1014 |
usb_anchor_urb(urb, &parent->rx_submitted); |
| 1015 |
|
| 1016 |
rc = usb_submit_urb(urb, GFP_KERNEL); |
| 1017 |
if (rc) { |
| 1018 |
if (rc == -ENODEV) |
| 1019 |
netif_device_detach(dev->netdev); |
| 1020 |
|
| 1021 |
netdev_err(netdev, |
| 1022 |
"usb_submit_urb() failed, error %pe\n", |
| 1023 |
ERR_PTR(rc)); |
| 1024 |
|
| 1025 |
goto out_usb_unanchor_urb; |
| 1026 |
} |
| 1027 |
|
| 1028 |
/* Drop reference, |
| 1029 |
* USB core will take care of freeing it |
| 1030 |
*/ |
| 1031 |
usb_free_urb(urb); |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
/* flags */ |
| 1036 |
if (ctrlmode & CAN_CTRLMODE_LOOPBACK) |
| 1037 |
flags |= GS_CAN_MODE_LOOP_BACK; |
| 1038 |
|
| 1039 |
if (ctrlmode & CAN_CTRLMODE_LISTENONLY) |
| 1040 |
flags |= GS_CAN_MODE_LISTEN_ONLY; |
| 1041 |
|
| 1042 |
if (ctrlmode & CAN_CTRLMODE_3_SAMPLES) |
| 1043 |
flags |= GS_CAN_MODE_TRIPLE_SAMPLE; |
| 1044 |
|
| 1045 |
if (ctrlmode & CAN_CTRLMODE_ONE_SHOT) |
| 1046 |
flags |= GS_CAN_MODE_ONE_SHOT; |
| 1047 |
|
| 1048 |
if (ctrlmode & CAN_CTRLMODE_BERR_REPORTING) |
| 1049 |
flags |= GS_CAN_MODE_BERR_REPORTING; |
| 1050 |
|
| 1051 |
if (ctrlmode & CAN_CTRLMODE_FD) |
| 1052 |
flags |= GS_CAN_MODE_FD; |
| 1053 |
|
| 1054 |
/* if hardware supports timestamps, enable it */ |
| 1055 |
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) |
| 1056 |
flags |= GS_CAN_MODE_HW_TIMESTAMP; |
| 1057 |
|
| 1058 |
rc = gs_usb_set_bittiming(dev); |
| 1059 |
if (rc) { |
| 1060 |
netdev_err(netdev, "failed to set bittiming: %pe\n", ERR_PTR(rc)); |
| 1061 |
goto out_usb_kill_anchored_urbs; |
| 1062 |
} |
| 1063 |
|
| 1064 |
if (ctrlmode & CAN_CTRLMODE_FD) { |
| 1065 |
rc = gs_usb_set_data_bittiming(dev); |
| 1066 |
if (rc) { |
| 1067 |
netdev_err(netdev, "failed to set data bittiming: %pe\n", ERR_PTR(rc)); |
| 1068 |
goto out_usb_kill_anchored_urbs; |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
/* finally start device */ |
| 1073 |
dev->can.state = CAN_STATE_ERROR_ACTIVE; |
| 1074 |
dm.flags = cpu_to_le32(flags); |
| 1075 |
rc = usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE, |
| 1076 |
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 1077 |
dev->channel, 0, &dm, sizeof(dm), 1000, |
| 1078 |
GFP_KERNEL); |
| 1079 |
if (rc) { |
| 1080 |
netdev_err(netdev, "Couldn't start device (err=%d)\n", rc); |
| 1081 |
dev->can.state = CAN_STATE_STOPPED; |
| 1082 |
|
| 1083 |
goto out_usb_kill_anchored_urbs; |
| 1084 |
} |
| 1085 |
|
| 1086 |
parent->active_channels++; |
| 1087 |
if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)) |
| 1088 |
netif_start_queue(netdev); |
| 1089 |
|
| 1090 |
return 0; |
| 1091 |
|
| 1092 |
out_usb_unanchor_urb: |
| 1093 |
usb_unanchor_urb(urb); |
| 1094 |
out_usb_free_urb: |
| 1095 |
usb_free_urb(urb); |
| 1096 |
out_usb_kill_anchored_urbs: |
| 1097 |
if (!parent->active_channels) { |
| 1098 |
usb_kill_anchored_urbs(&parent->rx_submitted); |
| 1099 |
|
| 1100 |
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) |
| 1101 |
gs_usb_timestamp_stop(parent); |
| 1102 |
} |
| 1103 |
|
| 1104 |
can_rx_offload_disable(&dev->offload); |
| 1105 |
close_candev(netdev); |
| 1106 |
|
| 1107 |
return rc; |
| 1108 |
} |
| 1109 |
|
| 1110 |
static int gs_usb_get_state(const struct net_device *netdev, |
| 1111 |
struct can_berr_counter *bec, |
| 1112 |
enum can_state *state) |
| 1113 |
{ |
| 1114 |
struct gs_can *dev = netdev_priv(netdev); |
| 1115 |
struct gs_device_state ds; |
| 1116 |
int rc; |
| 1117 |
|
| 1118 |
rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_STATE, |
| 1119 |
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 1120 |
dev->channel, 0, |
| 1121 |
&ds, sizeof(ds), |
| 1122 |
USB_CTRL_GET_TIMEOUT, |
| 1123 |
GFP_KERNEL); |
| 1124 |
if (rc) |
| 1125 |
return rc; |
| 1126 |
|
| 1127 |
if (le32_to_cpu(ds.state) >= CAN_STATE_MAX) |
| 1128 |
return -EOPNOTSUPP; |
| 1129 |
|
| 1130 |
*state = le32_to_cpu(ds.state); |
| 1131 |
bec->txerr = le32_to_cpu(ds.txerr); |
| 1132 |
bec->rxerr = le32_to_cpu(ds.rxerr); |
| 1133 |
|
| 1134 |
return 0; |
| 1135 |
} |
| 1136 |
|
| 1137 |
static int gs_usb_can_get_berr_counter(const struct net_device *netdev, |
| 1138 |
struct can_berr_counter *bec) |
| 1139 |
{ |
| 1140 |
enum can_state state; |
| 1141 |
|
| 1142 |
return gs_usb_get_state(netdev, bec, &state); |
| 1143 |
} |
| 1144 |
|
| 1145 |
static int gs_can_close(struct net_device *netdev) |
| 1146 |
{ |
| 1147 |
int rc; |
| 1148 |
struct gs_can *dev = netdev_priv(netdev); |
| 1149 |
struct gs_usb *parent = dev->parent; |
| 1150 |
|
| 1151 |
netif_stop_queue(netdev); |
| 1152 |
|
| 1153 |
/* Stop polling */ |
| 1154 |
parent->active_channels--; |
| 1155 |
if (!parent->active_channels) { |
| 1156 |
usb_kill_anchored_urbs(&parent->rx_submitted); |
| 1157 |
|
| 1158 |
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) |
| 1159 |
gs_usb_timestamp_stop(parent); |
| 1160 |
} |
| 1161 |
|
| 1162 |
/* Stop sending URBs */ |
| 1163 |
usb_kill_anchored_urbs(&dev->tx_submitted); |
| 1164 |
atomic_set(&dev->active_tx_urbs, 0); |
| 1165 |
|
| 1166 |
dev->can.state = CAN_STATE_STOPPED; |
| 1167 |
|
| 1168 |
/* reset the device */ |
| 1169 |
gs_cmd_reset(dev); |
| 1170 |
|
| 1171 |
/* reset tx contexts */ |
| 1172 |
for (rc = 0; rc < GS_MAX_TX_URBS; rc++) { |
| 1173 |
dev->tx_context[rc].dev = dev; |
| 1174 |
dev->tx_context[rc].echo_id = GS_MAX_TX_URBS; |
| 1175 |
} |
| 1176 |
|
| 1177 |
can_rx_offload_disable(&dev->offload); |
| 1178 |
|
| 1179 |
/* close the netdev */ |
| 1180 |
close_candev(netdev); |
| 1181 |
|
| 1182 |
return 0; |
| 1183 |
} |
| 1184 |
|
| 1185 |
static int gs_can_hwtstamp_get(struct net_device *netdev, |
| 1186 |
struct kernel_hwtstamp_config *cfg) |
| 1187 |
{ |
| 1188 |
const struct gs_can *dev = netdev_priv(netdev); |
| 1189 |
|
| 1190 |
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) |
| 1191 |
return can_hwtstamp_get(netdev, cfg); |
| 1192 |
|
| 1193 |
return -EOPNOTSUPP; |
| 1194 |
} |
| 1195 |
|
| 1196 |
static int gs_can_hwtstamp_set(struct net_device *netdev, |
| 1197 |
struct kernel_hwtstamp_config *cfg, |
| 1198 |
struct netlink_ext_ack *extack) |
| 1199 |
{ |
| 1200 |
const struct gs_can *dev = netdev_priv(netdev); |
| 1201 |
|
| 1202 |
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) |
| 1203 |
return can_hwtstamp_set(netdev, cfg, extack); |
| 1204 |
|
| 1205 |
return -EOPNOTSUPP; |
| 1206 |
} |
| 1207 |
|
| 1208 |
static const struct net_device_ops gs_usb_netdev_ops = { |
| 1209 |
.ndo_open = gs_can_open, |
| 1210 |
.ndo_stop = gs_can_close, |
| 1211 |
.ndo_start_xmit = gs_can_start_xmit, |
| 1212 |
.ndo_hwtstamp_get = gs_can_hwtstamp_get, |
| 1213 |
.ndo_hwtstamp_set = gs_can_hwtstamp_set, |
| 1214 |
}; |
| 1215 |
|
| 1216 |
static int gs_usb_set_identify(struct net_device *netdev, bool do_identify) |
| 1217 |
{ |
| 1218 |
struct gs_can *dev = netdev_priv(netdev); |
| 1219 |
struct gs_identify_mode imode; |
| 1220 |
|
| 1221 |
if (do_identify) |
| 1222 |
imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_ON); |
| 1223 |
else |
| 1224 |
imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF); |
| 1225 |
|
| 1226 |
return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_IDENTIFY, |
| 1227 |
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 1228 |
dev->channel, 0, &imode, sizeof(imode), 100, |
| 1229 |
GFP_KERNEL); |
| 1230 |
} |
| 1231 |
|
| 1232 |
/* blink LED's for finding the this interface */ |
| 1233 |
static int gs_usb_set_phys_id(struct net_device *netdev, |
| 1234 |
enum ethtool_phys_id_state state) |
| 1235 |
{ |
| 1236 |
const struct gs_can *dev = netdev_priv(netdev); |
| 1237 |
int rc = 0; |
| 1238 |
|
| 1239 |
if (!(dev->feature & GS_CAN_FEATURE_IDENTIFY)) |
| 1240 |
return -EOPNOTSUPP; |
| 1241 |
|
| 1242 |
switch (state) { |
| 1243 |
case ETHTOOL_ID_ACTIVE: |
| 1244 |
rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_ON); |
| 1245 |
break; |
| 1246 |
case ETHTOOL_ID_INACTIVE: |
| 1247 |
rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_OFF); |
| 1248 |
break; |
| 1249 |
default: |
| 1250 |
break; |
| 1251 |
} |
| 1252 |
|
| 1253 |
return rc; |
| 1254 |
} |
| 1255 |
|
| 1256 |
static int gs_usb_get_ts_info(struct net_device *netdev, |
| 1257 |
struct kernel_ethtool_ts_info *info) |
| 1258 |
{ |
| 1259 |
struct gs_can *dev = netdev_priv(netdev); |
| 1260 |
|
| 1261 |
/* report if device supports HW timestamps */ |
| 1262 |
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) |
| 1263 |
return can_ethtool_op_get_ts_info_hwts(netdev, info); |
| 1264 |
|
| 1265 |
return ethtool_op_get_ts_info(netdev, info); |
| 1266 |
} |
| 1267 |
|
| 1268 |
static const struct ethtool_ops gs_usb_ethtool_ops = { |
| 1269 |
.set_phys_id = gs_usb_set_phys_id, |
| 1270 |
.get_ts_info = gs_usb_get_ts_info, |
| 1271 |
}; |
| 1272 |
|
| 1273 |
static int gs_usb_get_termination(struct net_device *netdev, u16 *term) |
| 1274 |
{ |
| 1275 |
struct gs_can *dev = netdev_priv(netdev); |
| 1276 |
struct gs_device_termination_state term_state; |
| 1277 |
int rc; |
| 1278 |
|
| 1279 |
rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_TERMINATION, |
| 1280 |
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 1281 |
dev->channel, 0, |
| 1282 |
&term_state, sizeof(term_state), 1000, |
| 1283 |
GFP_KERNEL); |
| 1284 |
if (rc) |
| 1285 |
return rc; |
| 1286 |
|
| 1287 |
if (term_state.state == cpu_to_le32(GS_CAN_TERMINATION_STATE_ON)) |
| 1288 |
*term = GS_USB_TERMINATION_ENABLED; |
| 1289 |
else |
| 1290 |
*term = GS_USB_TERMINATION_DISABLED; |
| 1291 |
|
| 1292 |
return 0; |
| 1293 |
} |
| 1294 |
|
| 1295 |
static int gs_usb_set_termination(struct net_device *netdev, u16 term) |
| 1296 |
{ |
| 1297 |
struct gs_can *dev = netdev_priv(netdev); |
| 1298 |
struct gs_device_termination_state term_state; |
| 1299 |
|
| 1300 |
if (term == GS_USB_TERMINATION_ENABLED) |
| 1301 |
term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_ON); |
| 1302 |
else |
| 1303 |
term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_OFF); |
| 1304 |
|
| 1305 |
return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_SET_TERMINATION, |
| 1306 |
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 1307 |
dev->channel, 0, |
| 1308 |
&term_state, sizeof(term_state), 1000, |
| 1309 |
GFP_KERNEL); |
| 1310 |
} |
| 1311 |
|
| 1312 |
static const u16 gs_usb_termination_const[] = { |
| 1313 |
GS_USB_TERMINATION_DISABLED, |
| 1314 |
GS_USB_TERMINATION_ENABLED |
| 1315 |
}; |
| 1316 |
|
| 1317 |
static struct gs_can *gs_make_candev(unsigned int channel, |
| 1318 |
struct usb_interface *intf, |
| 1319 |
struct gs_device_config *dconf) |
| 1320 |
{ |
| 1321 |
struct gs_can *dev; |
| 1322 |
struct net_device *netdev; |
| 1323 |
int rc; |
| 1324 |
struct gs_device_bt_const_extended bt_const_extended; |
| 1325 |
struct gs_device_bt_const bt_const; |
| 1326 |
u32 feature; |
| 1327 |
|
| 1328 |
/* fetch bit timing constants */ |
| 1329 |
rc = usb_control_msg_recv(interface_to_usbdev(intf), 0, |
| 1330 |
GS_USB_BREQ_BT_CONST, |
| 1331 |
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 1332 |
channel, 0, &bt_const, sizeof(bt_const), 1000, |
| 1333 |
GFP_KERNEL); |
| 1334 |
|
| 1335 |
if (rc) { |
| 1336 |
dev_err(&intf->dev, |
| 1337 |
"Couldn't get bit timing const for channel %d (%pe)\n", |
| 1338 |
channel, ERR_PTR(rc)); |
| 1339 |
return ERR_PTR(rc); |
| 1340 |
} |
| 1341 |
|
| 1342 |
/* create netdev */ |
| 1343 |
netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS); |
| 1344 |
if (!netdev) { |
| 1345 |
dev_err(&intf->dev, "Couldn't allocate candev\n"); |
| 1346 |
return ERR_PTR(-ENOMEM); |
| 1347 |
} |
| 1348 |
|
| 1349 |
dev = netdev_priv(netdev); |
| 1350 |
|
| 1351 |
netdev->netdev_ops = &gs_usb_netdev_ops; |
| 1352 |
netdev->ethtool_ops = &gs_usb_ethtool_ops; |
| 1353 |
|
| 1354 |
netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */ |
| 1355 |
netdev->dev_id = channel; |
| 1356 |
netdev->dev_port = channel; |
| 1357 |
|
| 1358 |
/* dev setup */ |
| 1359 |
strcpy(dev->bt_const.name, KBUILD_MODNAME); |
| 1360 |
dev->bt_const.tseg1_min = le32_to_cpu(bt_const.tseg1_min); |
| 1361 |
dev->bt_const.tseg1_max = le32_to_cpu(bt_const.tseg1_max); |
| 1362 |
dev->bt_const.tseg2_min = le32_to_cpu(bt_const.tseg2_min); |
| 1363 |
dev->bt_const.tseg2_max = le32_to_cpu(bt_const.tseg2_max); |
| 1364 |
dev->bt_const.sjw_max = le32_to_cpu(bt_const.sjw_max); |
| 1365 |
dev->bt_const.brp_min = le32_to_cpu(bt_const.brp_min); |
| 1366 |
dev->bt_const.brp_max = le32_to_cpu(bt_const.brp_max); |
| 1367 |
dev->bt_const.brp_inc = le32_to_cpu(bt_const.brp_inc); |
| 1368 |
|
| 1369 |
dev->udev = interface_to_usbdev(intf); |
| 1370 |
dev->netdev = netdev; |
| 1371 |
dev->channel = channel; |
| 1372 |
|
| 1373 |
init_usb_anchor(&dev->tx_submitted); |
| 1374 |
atomic_set(&dev->active_tx_urbs, 0); |
| 1375 |
spin_lock_init(&dev->tx_ctx_lock); |
| 1376 |
for (rc = 0; rc < GS_MAX_TX_URBS; rc++) { |
| 1377 |
dev->tx_context[rc].dev = dev; |
| 1378 |
dev->tx_context[rc].echo_id = GS_MAX_TX_URBS; |
| 1379 |
} |
| 1380 |
|
| 1381 |
/* can setup */ |
| 1382 |
dev->can.state = CAN_STATE_STOPPED; |
| 1383 |
dev->can.clock.freq = le32_to_cpu(bt_const.fclk_can); |
| 1384 |
dev->can.bittiming_const = &dev->bt_const; |
| 1385 |
|
| 1386 |
dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC; |
| 1387 |
|
| 1388 |
feature = le32_to_cpu(bt_const.feature); |
| 1389 |
dev->feature = FIELD_GET(GS_CAN_FEATURE_MASK, feature); |
| 1390 |
if (feature & GS_CAN_FEATURE_LISTEN_ONLY) |
| 1391 |
dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY; |
| 1392 |
|
| 1393 |
if (feature & GS_CAN_FEATURE_LOOP_BACK) |
| 1394 |
dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK; |
| 1395 |
|
| 1396 |
if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE) |
| 1397 |
dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES; |
| 1398 |
|
| 1399 |
if (feature & GS_CAN_FEATURE_ONE_SHOT) |
| 1400 |
dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT; |
| 1401 |
|
| 1402 |
if (feature & GS_CAN_FEATURE_FD) { |
| 1403 |
dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD; |
| 1404 |
/* The data bit timing will be overwritten, if |
| 1405 |
* GS_CAN_FEATURE_BT_CONST_EXT is set. |
| 1406 |
*/ |
| 1407 |
dev->can.fd.data_bittiming_const = &dev->bt_const; |
| 1408 |
} |
| 1409 |
|
| 1410 |
if (feature & GS_CAN_FEATURE_TERMINATION) { |
| 1411 |
rc = gs_usb_get_termination(netdev, &dev->can.termination); |
| 1412 |
if (rc) { |
| 1413 |
dev->feature &= ~GS_CAN_FEATURE_TERMINATION; |
| 1414 |
|
| 1415 |
dev_info(&intf->dev, |
| 1416 |
"Disabling termination support for channel %d (%pe)\n", |
| 1417 |
channel, ERR_PTR(rc)); |
| 1418 |
} else { |
| 1419 |
dev->can.termination_const = gs_usb_termination_const; |
| 1420 |
dev->can.termination_const_cnt = ARRAY_SIZE(gs_usb_termination_const); |
| 1421 |
dev->can.do_set_termination = gs_usb_set_termination; |
| 1422 |
} |
| 1423 |
} |
| 1424 |
|
| 1425 |
if (feature & GS_CAN_FEATURE_BERR_REPORTING) |
| 1426 |
dev->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING; |
| 1427 |
|
| 1428 |
if (feature & GS_CAN_FEATURE_GET_STATE) |
| 1429 |
dev->can.do_get_berr_counter = gs_usb_can_get_berr_counter; |
| 1430 |
|
| 1431 |
/* The CANtact Pro from LinkLayer Labs is based on the |
| 1432 |
* LPC54616 µC, which is affected by the NXP LPC USB transfer |
| 1433 |
* erratum. However, the current firmware (version 2) doesn't |
| 1434 |
* set the GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX bit. Set the |
| 1435 |
* feature GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX to workaround |
| 1436 |
* this issue. |
| 1437 |
* |
| 1438 |
* For the GS_USB_BREQ_DATA_BITTIMING USB control message the |
| 1439 |
* CANtact Pro firmware uses a request value, which is already |
| 1440 |
* used by the candleLight firmware for a different purpose |
| 1441 |
* (GS_USB_BREQ_GET_USER_ID). Set the feature |
| 1442 |
* GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO to workaround this |
| 1443 |
* issue. |
| 1444 |
*/ |
| 1445 |
if (dev->udev->descriptor.idVendor == cpu_to_le16(USB_GS_USB_1_VENDOR_ID) && |
| 1446 |
dev->udev->descriptor.idProduct == cpu_to_le16(USB_GS_USB_1_PRODUCT_ID) && |
| 1447 |
dev->udev->manufacturer && dev->udev->product && |
| 1448 |
!strcmp(dev->udev->manufacturer, "LinkLayer Labs") && |
| 1449 |
!strcmp(dev->udev->product, "CANtact Pro") && |
| 1450 |
(le32_to_cpu(dconf->sw_version) <= 2)) |
| 1451 |
dev->feature |= GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX | |
| 1452 |
GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO; |
| 1453 |
|
| 1454 |
/* GS_CAN_FEATURE_IDENTIFY is only supported for sw_version > 1 */ |
| 1455 |
if (!(le32_to_cpu(dconf->sw_version) > 1 && |
| 1456 |
feature & GS_CAN_FEATURE_IDENTIFY)) |
| 1457 |
dev->feature &= ~GS_CAN_FEATURE_IDENTIFY; |
| 1458 |
|
| 1459 |
/* fetch extended bit timing constants if device has feature |
| 1460 |
* GS_CAN_FEATURE_FD and GS_CAN_FEATURE_BT_CONST_EXT |
| 1461 |
*/ |
| 1462 |
if (feature & GS_CAN_FEATURE_FD && |
| 1463 |
feature & GS_CAN_FEATURE_BT_CONST_EXT) { |
| 1464 |
rc = usb_control_msg_recv(interface_to_usbdev(intf), 0, |
| 1465 |
GS_USB_BREQ_BT_CONST_EXT, |
| 1466 |
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 1467 |
channel, 0, &bt_const_extended, |
| 1468 |
sizeof(bt_const_extended), |
| 1469 |
1000, GFP_KERNEL); |
| 1470 |
if (rc) { |
| 1471 |
dev_err(&intf->dev, |
| 1472 |
"Couldn't get extended bit timing const for channel %d (%pe)\n", |
| 1473 |
channel, ERR_PTR(rc)); |
| 1474 |
goto out_free_candev; |
| 1475 |
} |
| 1476 |
|
| 1477 |
strcpy(dev->data_bt_const.name, KBUILD_MODNAME); |
| 1478 |
dev->data_bt_const.tseg1_min = le32_to_cpu(bt_const_extended.dtseg1_min); |
| 1479 |
dev->data_bt_const.tseg1_max = le32_to_cpu(bt_const_extended.dtseg1_max); |
| 1480 |
dev->data_bt_const.tseg2_min = le32_to_cpu(bt_const_extended.dtseg2_min); |
| 1481 |
dev->data_bt_const.tseg2_max = le32_to_cpu(bt_const_extended.dtseg2_max); |
| 1482 |
dev->data_bt_const.sjw_max = le32_to_cpu(bt_const_extended.dsjw_max); |
| 1483 |
dev->data_bt_const.brp_min = le32_to_cpu(bt_const_extended.dbrp_min); |
| 1484 |
dev->data_bt_const.brp_max = le32_to_cpu(bt_const_extended.dbrp_max); |
| 1485 |
dev->data_bt_const.brp_inc = le32_to_cpu(bt_const_extended.dbrp_inc); |
| 1486 |
|
| 1487 |
dev->can.fd.data_bittiming_const = &dev->data_bt_const; |
| 1488 |
} |
| 1489 |
|
| 1490 |
can_rx_offload_add_manual(netdev, &dev->offload, GS_NAPI_WEIGHT); |
| 1491 |
SET_NETDEV_DEV(netdev, &intf->dev); |
| 1492 |
|
| 1493 |
rc = register_candev(dev->netdev); |
| 1494 |
if (rc) { |
| 1495 |
dev_err(&intf->dev, |
| 1496 |
"Couldn't register candev for channel %d (%pe)\n", |
| 1497 |
channel, ERR_PTR(rc)); |
| 1498 |
goto out_can_rx_offload_del; |
| 1499 |
} |
| 1500 |
|
| 1501 |
return dev; |
| 1502 |
|
| 1503 |
out_can_rx_offload_del: |
| 1504 |
can_rx_offload_del(&dev->offload); |
| 1505 |
out_free_candev: |
| 1506 |
free_candev(dev->netdev); |
| 1507 |
return ERR_PTR(rc); |
| 1508 |
} |
| 1509 |
|
| 1510 |
static void gs_destroy_candev(struct gs_can *dev) |
| 1511 |
{ |
| 1512 |
unregister_candev(dev->netdev); |
| 1513 |
can_rx_offload_del(&dev->offload); |
| 1514 |
free_candev(dev->netdev); |
| 1515 |
} |
| 1516 |
|
| 1517 |
static int gs_usb_probe(struct usb_interface *intf, |
| 1518 |
const struct usb_device_id *id) |
| 1519 |
{ |
| 1520 |
struct usb_device *udev = interface_to_usbdev(intf); |
| 1521 |
struct usb_endpoint_descriptor *ep_in, *ep_out; |
| 1522 |
struct gs_host_frame *hf; |
| 1523 |
struct gs_usb *parent; |
| 1524 |
struct gs_host_config hconf = { |
| 1525 |
.byte_order = cpu_to_le32(0x0000beef), |
| 1526 |
}; |
| 1527 |
struct gs_device_config dconf; |
| 1528 |
unsigned int icount, i; |
| 1529 |
int rc; |
| 1530 |
|
| 1531 |
rc = usb_find_common_endpoints(intf->cur_altsetting, |
| 1532 |
&ep_in, &ep_out, NULL, NULL); |
| 1533 |
if (rc) { |
| 1534 |
dev_err(&intf->dev, "Required endpoints not found\n"); |
| 1535 |
return rc; |
| 1536 |
} |
| 1537 |
|
| 1538 |
/* send host config */ |
| 1539 |
rc = usb_control_msg_send(udev, 0, |
| 1540 |
GS_USB_BREQ_HOST_FORMAT, |
| 1541 |
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 1542 |
1, intf->cur_altsetting->desc.bInterfaceNumber, |
| 1543 |
&hconf, sizeof(hconf), 1000, |
| 1544 |
GFP_KERNEL); |
| 1545 |
if (rc) { |
| 1546 |
dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc); |
| 1547 |
return rc; |
| 1548 |
} |
| 1549 |
|
| 1550 |
/* read device config */ |
| 1551 |
rc = usb_control_msg_recv(udev, 0, |
| 1552 |
GS_USB_BREQ_DEVICE_CONFIG, |
| 1553 |
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 1554 |
1, intf->cur_altsetting->desc.bInterfaceNumber, |
| 1555 |
&dconf, sizeof(dconf), 1000, |
| 1556 |
GFP_KERNEL); |
| 1557 |
if (rc) { |
| 1558 |
dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n", |
| 1559 |
rc); |
| 1560 |
return rc; |
| 1561 |
} |
| 1562 |
|
| 1563 |
icount = dconf.icount + 1; |
| 1564 |
dev_info(&intf->dev, "Configuring for %u interfaces\n", icount); |
| 1565 |
|
| 1566 |
if (icount > type_max(parent->channel_cnt)) { |
| 1567 |
dev_err(&intf->dev, |
| 1568 |
"Driver cannot handle more that %u CAN interfaces\n", |
| 1569 |
type_max(parent->channel_cnt)); |
| 1570 |
return -EINVAL; |
| 1571 |
} |
| 1572 |
|
| 1573 |
parent = kzalloc_flex(*parent, canch, icount); |
| 1574 |
if (!parent) |
| 1575 |
return -ENOMEM; |
| 1576 |
|
| 1577 |
parent->channel_cnt = icount; |
| 1578 |
|
| 1579 |
init_usb_anchor(&parent->rx_submitted); |
| 1580 |
|
| 1581 |
usb_set_intfdata(intf, parent); |
| 1582 |
parent->udev = udev; |
| 1583 |
|
| 1584 |
/* store the detected endpoints */ |
| 1585 |
parent->pipe_in = usb_rcvbulkpipe(parent->udev, ep_in->bEndpointAddress); |
| 1586 |
parent->pipe_out = usb_sndbulkpipe(parent->udev, ep_out->bEndpointAddress); |
| 1587 |
|
| 1588 |
for (i = 0; i < icount; i++) { |
| 1589 |
unsigned int hf_size_rx = 0; |
| 1590 |
|
| 1591 |
parent->canch[i] = gs_make_candev(i, intf, &dconf); |
| 1592 |
if (IS_ERR_OR_NULL(parent->canch[i])) { |
| 1593 |
/* save error code to return later */ |
| 1594 |
rc = PTR_ERR(parent->canch[i]); |
| 1595 |
|
| 1596 |
/* on failure destroy previously created candevs */ |
| 1597 |
icount = i; |
| 1598 |
for (i = 0; i < icount; i++) |
| 1599 |
gs_destroy_candev(parent->canch[i]); |
| 1600 |
|
| 1601 |
usb_kill_anchored_urbs(&parent->rx_submitted); |
| 1602 |
kfree(parent); |
| 1603 |
return rc; |
| 1604 |
} |
| 1605 |
parent->canch[i]->parent = parent; |
| 1606 |
|
| 1607 |
/* set RX packet size based on FD and if hardware |
| 1608 |
* timestamps are supported. |
| 1609 |
*/ |
| 1610 |
if (parent->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD) { |
| 1611 |
if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP) |
| 1612 |
hf_size_rx = struct_size(hf, canfd_ts, 1); |
| 1613 |
else |
| 1614 |
hf_size_rx = struct_size(hf, canfd, 1); |
| 1615 |
} else { |
| 1616 |
if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP) |
| 1617 |
hf_size_rx = struct_size(hf, classic_can_ts, 1); |
| 1618 |
else |
| 1619 |
hf_size_rx = struct_size(hf, classic_can, 1); |
| 1620 |
} |
| 1621 |
parent->hf_size_rx = max(parent->hf_size_rx, hf_size_rx); |
| 1622 |
} |
| 1623 |
|
| 1624 |
return 0; |
| 1625 |
} |
| 1626 |
|
| 1627 |
static void gs_usb_disconnect(struct usb_interface *intf) |
| 1628 |
{ |
| 1629 |
struct gs_usb *parent = usb_get_intfdata(intf); |
| 1630 |
unsigned int i; |
| 1631 |
|
| 1632 |
usb_set_intfdata(intf, NULL); |
| 1633 |
|
| 1634 |
if (!parent) { |
| 1635 |
dev_err(&intf->dev, "Disconnect (nodata)\n"); |
| 1636 |
return; |
| 1637 |
} |
| 1638 |
|
| 1639 |
for (i = 0; i < parent->channel_cnt; i++) |
| 1640 |
if (parent->canch[i]) |
| 1641 |
gs_destroy_candev(parent->canch[i]); |
| 1642 |
|
| 1643 |
kfree(parent); |
| 1644 |
} |
| 1645 |
|
| 1646 |
static const struct usb_device_id gs_usb_table[] = { |
| 1647 |
{ USB_DEVICE_INTERFACE_NUMBER(USB_GS_USB_1_VENDOR_ID, |
| 1648 |
USB_GS_USB_1_PRODUCT_ID, 0) }, |
| 1649 |
{ USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID, |
| 1650 |
USB_CANDLELIGHT_PRODUCT_ID, 0) }, |
| 1651 |
{ USB_DEVICE_INTERFACE_NUMBER(USB_CES_CANEXT_FD_VENDOR_ID, |
| 1652 |
USB_CES_CANEXT_FD_PRODUCT_ID, 0) }, |
| 1653 |
{ USB_DEVICE_INTERFACE_NUMBER(USB_ABE_CANDEBUGGER_FD_VENDOR_ID, |
| 1654 |
USB_ABE_CANDEBUGGER_FD_PRODUCT_ID, 0) }, |
| 1655 |
{ USB_DEVICE_INTERFACE_NUMBER(USB_XYLANTA_SAINT3_VENDOR_ID, |
| 1656 |
USB_XYLANTA_SAINT3_PRODUCT_ID, 0) }, |
| 1657 |
{ USB_DEVICE_INTERFACE_NUMBER(USB_CANNECTIVITY_VENDOR_ID, |
| 1658 |
USB_CANNECTIVITY_PRODUCT_ID, 0) }, |
| 1659 |
{} /* Terminating entry */ |
| 1660 |
}; |
| 1661 |
|
| 1662 |
MODULE_DEVICE_TABLE(usb, gs_usb_table); |
| 1663 |
|
| 1664 |
static struct usb_driver gs_usb_driver = { |
| 1665 |
.name = KBUILD_MODNAME, |
| 1666 |
.probe = gs_usb_probe, |
| 1667 |
.disconnect = gs_usb_disconnect, |
| 1668 |
.id_table = gs_usb_table, |
| 1669 |
}; |
| 1670 |
|
| 1671 |
module_usb_driver(gs_usb_driver); |
| 1672 |
|
| 1673 |
MODULE_AUTHOR("Maximilian Schneider <[email protected]>"); |
| 1674 |
MODULE_DESCRIPTION( |
| 1675 |
"Socket CAN device driver for Geschwister Schneider Technologie-, " |
| 1676 |
"Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n" |
| 1677 |
"and bytewerk.org candleLight USB CAN interfaces."); |
| 1678 |
MODULE_LICENSE("GPL v2"); |