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 <kernel@pengutronix.de> 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; 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 758 /* USB failure take down all interfaces */ 759 if (rc == -ENODEV) { 760 device_detach: 761 for (rc = 0; rc < parent->channel_cnt; rc++) { 762 if (parent->canch[rc]) 763 netif_device_detach(parent->canch[rc]->netdev); 764 } 765 } 766 } 767 768 static int gs_usb_set_bittiming(struct net_device *netdev) 769 { 770 struct gs_can *dev = netdev_priv(netdev); 771 struct can_bittiming *bt = &dev->can.bittiming; 772 struct gs_device_bittiming dbt = { 773 .prop_seg = cpu_to_le32(bt->prop_seg), 774 .phase_seg1 = cpu_to_le32(bt->phase_seg1), 775 .phase_seg2 = cpu_to_le32(bt->phase_seg2), 776 .sjw = cpu_to_le32(bt->sjw), 777 .brp = cpu_to_le32(bt->brp), 778 }; 779 780 /* request bit timings */ 781 return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_BITTIMING, 782 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 783 dev->channel, 0, &dbt, sizeof(dbt), 1000, 784 GFP_KERNEL); 785 } 786 787 static int gs_usb_set_data_bittiming(struct net_device *netdev) 788 { 789 struct gs_can *dev = netdev_priv(netdev); 790 struct can_bittiming *bt = &dev->can.fd.data_bittiming; 791 struct gs_device_bittiming dbt = { 792 .prop_seg = cpu_to_le32(bt->prop_seg), 793 .phase_seg1 = cpu_to_le32(bt->phase_seg1), 794 .phase_seg2 = cpu_to_le32(bt->phase_seg2), 795 .sjw = cpu_to_le32(bt->sjw), 796 .brp = cpu_to_le32(bt->brp), 797 }; 798 u8 request = GS_USB_BREQ_DATA_BITTIMING; 799 800 if (dev->feature & GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO) 801 request = GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING; 802 803 /* request data bit timings */ 804 return usb_control_msg_send(dev->udev, 0, request, 805 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 806 dev->channel, 0, &dbt, sizeof(dbt), 1000, 807 GFP_KERNEL); 808 } 809 810 static void gs_usb_xmit_callback(struct urb *urb) 811 { 812 struct gs_tx_context *txc = urb->context; 813 struct gs_can *dev = txc->dev; 814 struct net_device *netdev = dev->netdev; 815 816 if (!urb->status) 817 return; 818 819 if (urb->status != -ESHUTDOWN && net_ratelimit()) 820 netdev_info(netdev, "failed to xmit URB %u: %pe\n", 821 txc->echo_id, ERR_PTR(urb->status)); 822 823 netdev->stats.tx_dropped++; 824 netdev->stats.tx_errors++; 825 826 can_free_echo_skb(netdev, txc->echo_id, NULL); 827 gs_free_tx_context(txc); 828 atomic_dec(&dev->active_tx_urbs); 829 830 netif_wake_queue(netdev); 831 } 832 833 static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb, 834 struct net_device *netdev) 835 { 836 struct gs_can *dev = netdev_priv(netdev); 837 struct net_device_stats *stats = &dev->netdev->stats; 838 struct urb *urb; 839 struct gs_host_frame *hf; 840 struct can_frame *cf; 841 struct canfd_frame *cfd; 842 int rc; 843 unsigned int idx; 844 struct gs_tx_context *txc; 845 846 if (can_dev_dropped_skb(netdev, skb)) 847 return NETDEV_TX_OK; 848 849 /* find an empty context to keep track of transmission */ 850 txc = gs_alloc_tx_context(dev); 851 if (!txc) 852 return NETDEV_TX_BUSY; 853 854 /* create a URB, and a buffer for it */ 855 urb = usb_alloc_urb(0, GFP_ATOMIC); 856 if (!urb) 857 goto nomem_urb; 858 859 hf = kmalloc(dev->hf_size_tx, GFP_ATOMIC); 860 if (!hf) 861 goto nomem_hf; 862 863 idx = txc->echo_id; 864 865 if (idx >= GS_MAX_TX_URBS) { 866 netdev_err(netdev, "Invalid tx context %u\n", idx); 867 goto badidx; 868 } 869 870 hf->echo_id = idx; 871 hf->channel = dev->channel; 872 hf->flags = 0; 873 hf->reserved = 0; 874 875 if (can_is_canfd_skb(skb)) { 876 cfd = (struct canfd_frame *)skb->data; 877 878 hf->can_id = cpu_to_le32(cfd->can_id); 879 hf->can_dlc = can_fd_len2dlc(cfd->len); 880 hf->flags |= GS_CAN_FLAG_FD; 881 if (cfd->flags & CANFD_BRS) 882 hf->flags |= GS_CAN_FLAG_BRS; 883 if (cfd->flags & CANFD_ESI) 884 hf->flags |= GS_CAN_FLAG_ESI; 885 886 memcpy(hf->canfd->data, cfd->data, cfd->len); 887 } else { 888 cf = (struct can_frame *)skb->data; 889 890 hf->can_id = cpu_to_le32(cf->can_id); 891 hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode); 892 893 memcpy(hf->classic_can->data, cf->data, cf->len); 894 } 895 896 usb_fill_bulk_urb(urb, dev->udev, 897 dev->parent->pipe_out, 898 hf, dev->hf_size_tx, 899 gs_usb_xmit_callback, txc); 900 901 urb->transfer_flags |= URB_FREE_BUFFER; 902 usb_anchor_urb(urb, &dev->tx_submitted); 903 904 can_put_echo_skb(skb, netdev, idx, 0); 905 906 atomic_inc(&dev->active_tx_urbs); 907 908 rc = usb_submit_urb(urb, GFP_ATOMIC); 909 if (unlikely(rc)) { /* usb send failed */ 910 atomic_dec(&dev->active_tx_urbs); 911 912 can_free_echo_skb(netdev, idx, NULL); 913 gs_free_tx_context(txc); 914 915 usb_unanchor_urb(urb); 916 917 if (rc == -ENODEV) { 918 netif_device_detach(netdev); 919 } else { 920 netdev_err(netdev, "usb_submit failed (err=%d)\n", rc); 921 stats->tx_dropped++; 922 } 923 } else { 924 /* Slow down tx path */ 925 if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS) 926 netif_stop_queue(netdev); 927 } 928 929 /* let usb core take care of this urb */ 930 usb_free_urb(urb); 931 932 return NETDEV_TX_OK; 933 934 badidx: 935 kfree(hf); 936 nomem_hf: 937 usb_free_urb(urb); 938 939 nomem_urb: 940 gs_free_tx_context(txc); 941 dev_kfree_skb(skb); 942 stats->tx_dropped++; 943 return NETDEV_TX_OK; 944 } 945 946 static int gs_can_open(struct net_device *netdev) 947 { 948 struct gs_can *dev = netdev_priv(netdev); 949 struct gs_usb *parent = dev->parent; 950 struct gs_device_mode dm = { 951 .mode = cpu_to_le32(GS_CAN_MODE_START), 952 }; 953 struct gs_host_frame *hf; 954 struct urb *urb = NULL; 955 u32 ctrlmode; 956 u32 flags = 0; 957 int rc, i; 958 959 rc = open_candev(netdev); 960 if (rc) 961 return rc; 962 963 ctrlmode = dev->can.ctrlmode; 964 if (ctrlmode & CAN_CTRLMODE_FD) { 965 if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX) 966 dev->hf_size_tx = struct_size(hf, canfd_quirk, 1); 967 else 968 dev->hf_size_tx = struct_size(hf, canfd, 1); 969 } else { 970 if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX) 971 dev->hf_size_tx = struct_size(hf, classic_can_quirk, 1); 972 else 973 dev->hf_size_tx = struct_size(hf, classic_can, 1); 974 } 975 976 can_rx_offload_enable(&dev->offload); 977 978 if (!parent->active_channels) { 979 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) 980 gs_usb_timestamp_init(parent); 981 982 for (i = 0; i < GS_MAX_RX_URBS; i++) { 983 u8 *buf; 984 985 /* alloc rx urb */ 986 urb = usb_alloc_urb(0, GFP_KERNEL); 987 if (!urb) { 988 rc = -ENOMEM; 989 goto out_usb_kill_anchored_urbs; 990 } 991 992 /* alloc rx buffer */ 993 buf = kmalloc(dev->parent->hf_size_rx, 994 GFP_KERNEL); 995 if (!buf) { 996 rc = -ENOMEM; 997 goto out_usb_free_urb; 998 } 999 1000 /* fill, anchor, and submit rx urb */ 1001 usb_fill_bulk_urb(urb, 1002 dev->udev, 1003 dev->parent->pipe_in, 1004 buf, 1005 dev->parent->hf_size_rx, 1006 gs_usb_receive_bulk_callback, parent); 1007 urb->transfer_flags |= URB_FREE_BUFFER; 1008 1009 usb_anchor_urb(urb, &parent->rx_submitted); 1010 1011 rc = usb_submit_urb(urb, GFP_KERNEL); 1012 if (rc) { 1013 if (rc == -ENODEV) 1014 netif_device_detach(dev->netdev); 1015 1016 netdev_err(netdev, 1017 "usb_submit_urb() failed, error %pe\n", 1018 ERR_PTR(rc)); 1019 1020 goto out_usb_unanchor_urb; 1021 } 1022 1023 /* Drop reference, 1024 * USB core will take care of freeing it 1025 */ 1026 usb_free_urb(urb); 1027 } 1028 } 1029 1030 /* flags */ 1031 if (ctrlmode & CAN_CTRLMODE_LOOPBACK) 1032 flags |= GS_CAN_MODE_LOOP_BACK; 1033 1034 if (ctrlmode & CAN_CTRLMODE_LISTENONLY) 1035 flags |= GS_CAN_MODE_LISTEN_ONLY; 1036 1037 if (ctrlmode & CAN_CTRLMODE_3_SAMPLES) 1038 flags |= GS_CAN_MODE_TRIPLE_SAMPLE; 1039 1040 if (ctrlmode & CAN_CTRLMODE_ONE_SHOT) 1041 flags |= GS_CAN_MODE_ONE_SHOT; 1042 1043 if (ctrlmode & CAN_CTRLMODE_BERR_REPORTING) 1044 flags |= GS_CAN_MODE_BERR_REPORTING; 1045 1046 if (ctrlmode & CAN_CTRLMODE_FD) 1047 flags |= GS_CAN_MODE_FD; 1048 1049 /* if hardware supports timestamps, enable it */ 1050 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) 1051 flags |= GS_CAN_MODE_HW_TIMESTAMP; 1052 1053 /* finally start device */ 1054 dev->can.state = CAN_STATE_ERROR_ACTIVE; 1055 dm.flags = cpu_to_le32(flags); 1056 rc = usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE, 1057 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 1058 dev->channel, 0, &dm, sizeof(dm), 1000, 1059 GFP_KERNEL); 1060 if (rc) { 1061 netdev_err(netdev, "Couldn't start device (err=%d)\n", rc); 1062 dev->can.state = CAN_STATE_STOPPED; 1063 1064 goto out_usb_kill_anchored_urbs; 1065 } 1066 1067 parent->active_channels++; 1068 if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)) 1069 netif_start_queue(netdev); 1070 1071 return 0; 1072 1073 out_usb_unanchor_urb: 1074 usb_unanchor_urb(urb); 1075 out_usb_free_urb: 1076 usb_free_urb(urb); 1077 out_usb_kill_anchored_urbs: 1078 if (!parent->active_channels) { 1079 usb_kill_anchored_urbs(&parent->rx_submitted); 1080 1081 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) 1082 gs_usb_timestamp_stop(parent); 1083 } 1084 1085 can_rx_offload_disable(&dev->offload); 1086 close_candev(netdev); 1087 1088 return rc; 1089 } 1090 1091 static int gs_usb_get_state(const struct net_device *netdev, 1092 struct can_berr_counter *bec, 1093 enum can_state *state) 1094 { 1095 struct gs_can *dev = netdev_priv(netdev); 1096 struct gs_device_state ds; 1097 int rc; 1098 1099 rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_STATE, 1100 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 1101 dev->channel, 0, 1102 &ds, sizeof(ds), 1103 USB_CTRL_GET_TIMEOUT, 1104 GFP_KERNEL); 1105 if (rc) 1106 return rc; 1107 1108 if (le32_to_cpu(ds.state) >= CAN_STATE_MAX) 1109 return -EOPNOTSUPP; 1110 1111 *state = le32_to_cpu(ds.state); 1112 bec->txerr = le32_to_cpu(ds.txerr); 1113 bec->rxerr = le32_to_cpu(ds.rxerr); 1114 1115 return 0; 1116 } 1117 1118 static int gs_usb_can_get_berr_counter(const struct net_device *netdev, 1119 struct can_berr_counter *bec) 1120 { 1121 enum can_state state; 1122 1123 return gs_usb_get_state(netdev, bec, &state); 1124 } 1125 1126 static int gs_can_close(struct net_device *netdev) 1127 { 1128 int rc; 1129 struct gs_can *dev = netdev_priv(netdev); 1130 struct gs_usb *parent = dev->parent; 1131 1132 netif_stop_queue(netdev); 1133 1134 /* Stop polling */ 1135 parent->active_channels--; 1136 if (!parent->active_channels) { 1137 usb_kill_anchored_urbs(&parent->rx_submitted); 1138 1139 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) 1140 gs_usb_timestamp_stop(parent); 1141 } 1142 1143 /* Stop sending URBs */ 1144 usb_kill_anchored_urbs(&dev->tx_submitted); 1145 atomic_set(&dev->active_tx_urbs, 0); 1146 1147 dev->can.state = CAN_STATE_STOPPED; 1148 1149 /* reset the device */ 1150 gs_cmd_reset(dev); 1151 1152 /* reset tx contexts */ 1153 for (rc = 0; rc < GS_MAX_TX_URBS; rc++) { 1154 dev->tx_context[rc].dev = dev; 1155 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS; 1156 } 1157 1158 can_rx_offload_disable(&dev->offload); 1159 1160 /* close the netdev */ 1161 close_candev(netdev); 1162 1163 return 0; 1164 } 1165 1166 static int gs_can_hwtstamp_get(struct net_device *netdev, 1167 struct kernel_hwtstamp_config *cfg) 1168 { 1169 const struct gs_can *dev = netdev_priv(netdev); 1170 1171 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) 1172 return can_hwtstamp_get(netdev, cfg); 1173 1174 return -EOPNOTSUPP; 1175 } 1176 1177 static int gs_can_hwtstamp_set(struct net_device *netdev, 1178 struct kernel_hwtstamp_config *cfg, 1179 struct netlink_ext_ack *extack) 1180 { 1181 const struct gs_can *dev = netdev_priv(netdev); 1182 1183 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) 1184 return can_hwtstamp_set(netdev, cfg, extack); 1185 1186 return -EOPNOTSUPP; 1187 } 1188 1189 static const struct net_device_ops gs_usb_netdev_ops = { 1190 .ndo_open = gs_can_open, 1191 .ndo_stop = gs_can_close, 1192 .ndo_start_xmit = gs_can_start_xmit, 1193 .ndo_hwtstamp_get = gs_can_hwtstamp_get, 1194 .ndo_hwtstamp_set = gs_can_hwtstamp_set, 1195 }; 1196 1197 static int gs_usb_set_identify(struct net_device *netdev, bool do_identify) 1198 { 1199 struct gs_can *dev = netdev_priv(netdev); 1200 struct gs_identify_mode imode; 1201 1202 if (do_identify) 1203 imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_ON); 1204 else 1205 imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF); 1206 1207 return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_IDENTIFY, 1208 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 1209 dev->channel, 0, &imode, sizeof(imode), 100, 1210 GFP_KERNEL); 1211 } 1212 1213 /* blink LED's for finding the this interface */ 1214 static int gs_usb_set_phys_id(struct net_device *netdev, 1215 enum ethtool_phys_id_state state) 1216 { 1217 const struct gs_can *dev = netdev_priv(netdev); 1218 int rc = 0; 1219 1220 if (!(dev->feature & GS_CAN_FEATURE_IDENTIFY)) 1221 return -EOPNOTSUPP; 1222 1223 switch (state) { 1224 case ETHTOOL_ID_ACTIVE: 1225 rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_ON); 1226 break; 1227 case ETHTOOL_ID_INACTIVE: 1228 rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_OFF); 1229 break; 1230 default: 1231 break; 1232 } 1233 1234 return rc; 1235 } 1236 1237 static int gs_usb_get_ts_info(struct net_device *netdev, 1238 struct kernel_ethtool_ts_info *info) 1239 { 1240 struct gs_can *dev = netdev_priv(netdev); 1241 1242 /* report if device supports HW timestamps */ 1243 if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) 1244 return can_ethtool_op_get_ts_info_hwts(netdev, info); 1245 1246 return ethtool_op_get_ts_info(netdev, info); 1247 } 1248 1249 static const struct ethtool_ops gs_usb_ethtool_ops = { 1250 .set_phys_id = gs_usb_set_phys_id, 1251 .get_ts_info = gs_usb_get_ts_info, 1252 }; 1253 1254 static int gs_usb_get_termination(struct net_device *netdev, u16 *term) 1255 { 1256 struct gs_can *dev = netdev_priv(netdev); 1257 struct gs_device_termination_state term_state; 1258 int rc; 1259 1260 rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_TERMINATION, 1261 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 1262 dev->channel, 0, 1263 &term_state, sizeof(term_state), 1000, 1264 GFP_KERNEL); 1265 if (rc) 1266 return rc; 1267 1268 if (term_state.state == cpu_to_le32(GS_CAN_TERMINATION_STATE_ON)) 1269 *term = GS_USB_TERMINATION_ENABLED; 1270 else 1271 *term = GS_USB_TERMINATION_DISABLED; 1272 1273 return 0; 1274 } 1275 1276 static int gs_usb_set_termination(struct net_device *netdev, u16 term) 1277 { 1278 struct gs_can *dev = netdev_priv(netdev); 1279 struct gs_device_termination_state term_state; 1280 1281 if (term == GS_USB_TERMINATION_ENABLED) 1282 term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_ON); 1283 else 1284 term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_OFF); 1285 1286 return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_SET_TERMINATION, 1287 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 1288 dev->channel, 0, 1289 &term_state, sizeof(term_state), 1000, 1290 GFP_KERNEL); 1291 } 1292 1293 static const u16 gs_usb_termination_const[] = { 1294 GS_USB_TERMINATION_DISABLED, 1295 GS_USB_TERMINATION_ENABLED 1296 }; 1297 1298 static struct gs_can *gs_make_candev(unsigned int channel, 1299 struct usb_interface *intf, 1300 struct gs_device_config *dconf) 1301 { 1302 struct gs_can *dev; 1303 struct net_device *netdev; 1304 int rc; 1305 struct gs_device_bt_const_extended bt_const_extended; 1306 struct gs_device_bt_const bt_const; 1307 u32 feature; 1308 1309 /* fetch bit timing constants */ 1310 rc = usb_control_msg_recv(interface_to_usbdev(intf), 0, 1311 GS_USB_BREQ_BT_CONST, 1312 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 1313 channel, 0, &bt_const, sizeof(bt_const), 1000, 1314 GFP_KERNEL); 1315 1316 if (rc) { 1317 dev_err(&intf->dev, 1318 "Couldn't get bit timing const for channel %d (%pe)\n", 1319 channel, ERR_PTR(rc)); 1320 return ERR_PTR(rc); 1321 } 1322 1323 /* create netdev */ 1324 netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS); 1325 if (!netdev) { 1326 dev_err(&intf->dev, "Couldn't allocate candev\n"); 1327 return ERR_PTR(-ENOMEM); 1328 } 1329 1330 dev = netdev_priv(netdev); 1331 1332 netdev->netdev_ops = &gs_usb_netdev_ops; 1333 netdev->ethtool_ops = &gs_usb_ethtool_ops; 1334 1335 netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */ 1336 netdev->dev_id = channel; 1337 netdev->dev_port = channel; 1338 1339 /* dev setup */ 1340 strcpy(dev->bt_const.name, KBUILD_MODNAME); 1341 dev->bt_const.tseg1_min = le32_to_cpu(bt_const.tseg1_min); 1342 dev->bt_const.tseg1_max = le32_to_cpu(bt_const.tseg1_max); 1343 dev->bt_const.tseg2_min = le32_to_cpu(bt_const.tseg2_min); 1344 dev->bt_const.tseg2_max = le32_to_cpu(bt_const.tseg2_max); 1345 dev->bt_const.sjw_max = le32_to_cpu(bt_const.sjw_max); 1346 dev->bt_const.brp_min = le32_to_cpu(bt_const.brp_min); 1347 dev->bt_const.brp_max = le32_to_cpu(bt_const.brp_max); 1348 dev->bt_const.brp_inc = le32_to_cpu(bt_const.brp_inc); 1349 1350 dev->udev = interface_to_usbdev(intf); 1351 dev->netdev = netdev; 1352 dev->channel = channel; 1353 1354 init_usb_anchor(&dev->tx_submitted); 1355 atomic_set(&dev->active_tx_urbs, 0); 1356 spin_lock_init(&dev->tx_ctx_lock); 1357 for (rc = 0; rc < GS_MAX_TX_URBS; rc++) { 1358 dev->tx_context[rc].dev = dev; 1359 dev->tx_context[rc].echo_id = GS_MAX_TX_URBS; 1360 } 1361 1362 /* can setup */ 1363 dev->can.state = CAN_STATE_STOPPED; 1364 dev->can.clock.freq = le32_to_cpu(bt_const.fclk_can); 1365 dev->can.bittiming_const = &dev->bt_const; 1366 dev->can.do_set_bittiming = gs_usb_set_bittiming; 1367 1368 dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC; 1369 1370 feature = le32_to_cpu(bt_const.feature); 1371 dev->feature = FIELD_GET(GS_CAN_FEATURE_MASK, feature); 1372 if (feature & GS_CAN_FEATURE_LISTEN_ONLY) 1373 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY; 1374 1375 if (feature & GS_CAN_FEATURE_LOOP_BACK) 1376 dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK; 1377 1378 if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE) 1379 dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES; 1380 1381 if (feature & GS_CAN_FEATURE_ONE_SHOT) 1382 dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT; 1383 1384 if (feature & GS_CAN_FEATURE_FD) { 1385 dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD; 1386 /* The data bit timing will be overwritten, if 1387 * GS_CAN_FEATURE_BT_CONST_EXT is set. 1388 */ 1389 dev->can.fd.data_bittiming_const = &dev->bt_const; 1390 dev->can.fd.do_set_data_bittiming = gs_usb_set_data_bittiming; 1391 } 1392 1393 if (feature & GS_CAN_FEATURE_TERMINATION) { 1394 rc = gs_usb_get_termination(netdev, &dev->can.termination); 1395 if (rc) { 1396 dev->feature &= ~GS_CAN_FEATURE_TERMINATION; 1397 1398 dev_info(&intf->dev, 1399 "Disabling termination support for channel %d (%pe)\n", 1400 channel, ERR_PTR(rc)); 1401 } else { 1402 dev->can.termination_const = gs_usb_termination_const; 1403 dev->can.termination_const_cnt = ARRAY_SIZE(gs_usb_termination_const); 1404 dev->can.do_set_termination = gs_usb_set_termination; 1405 } 1406 } 1407 1408 if (feature & GS_CAN_FEATURE_BERR_REPORTING) 1409 dev->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING; 1410 1411 if (feature & GS_CAN_FEATURE_GET_STATE) 1412 dev->can.do_get_berr_counter = gs_usb_can_get_berr_counter; 1413 1414 /* The CANtact Pro from LinkLayer Labs is based on the 1415 * LPC54616 µC, which is affected by the NXP LPC USB transfer 1416 * erratum. However, the current firmware (version 2) doesn't 1417 * set the GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX bit. Set the 1418 * feature GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX to workaround 1419 * this issue. 1420 * 1421 * For the GS_USB_BREQ_DATA_BITTIMING USB control message the 1422 * CANtact Pro firmware uses a request value, which is already 1423 * used by the candleLight firmware for a different purpose 1424 * (GS_USB_BREQ_GET_USER_ID). Set the feature 1425 * GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO to workaround this 1426 * issue. 1427 */ 1428 if (dev->udev->descriptor.idVendor == cpu_to_le16(USB_GS_USB_1_VENDOR_ID) && 1429 dev->udev->descriptor.idProduct == cpu_to_le16(USB_GS_USB_1_PRODUCT_ID) && 1430 dev->udev->manufacturer && dev->udev->product && 1431 !strcmp(dev->udev->manufacturer, "LinkLayer Labs") && 1432 !strcmp(dev->udev->product, "CANtact Pro") && 1433 (le32_to_cpu(dconf->sw_version) <= 2)) 1434 dev->feature |= GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX | 1435 GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO; 1436 1437 /* GS_CAN_FEATURE_IDENTIFY is only supported for sw_version > 1 */ 1438 if (!(le32_to_cpu(dconf->sw_version) > 1 && 1439 feature & GS_CAN_FEATURE_IDENTIFY)) 1440 dev->feature &= ~GS_CAN_FEATURE_IDENTIFY; 1441 1442 /* fetch extended bit timing constants if device has feature 1443 * GS_CAN_FEATURE_FD and GS_CAN_FEATURE_BT_CONST_EXT 1444 */ 1445 if (feature & GS_CAN_FEATURE_FD && 1446 feature & GS_CAN_FEATURE_BT_CONST_EXT) { 1447 rc = usb_control_msg_recv(interface_to_usbdev(intf), 0, 1448 GS_USB_BREQ_BT_CONST_EXT, 1449 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 1450 channel, 0, &bt_const_extended, 1451 sizeof(bt_const_extended), 1452 1000, GFP_KERNEL); 1453 if (rc) { 1454 dev_err(&intf->dev, 1455 "Couldn't get extended bit timing const for channel %d (%pe)\n", 1456 channel, ERR_PTR(rc)); 1457 goto out_free_candev; 1458 } 1459 1460 strcpy(dev->data_bt_const.name, KBUILD_MODNAME); 1461 dev->data_bt_const.tseg1_min = le32_to_cpu(bt_const_extended.dtseg1_min); 1462 dev->data_bt_const.tseg1_max = le32_to_cpu(bt_const_extended.dtseg1_max); 1463 dev->data_bt_const.tseg2_min = le32_to_cpu(bt_const_extended.dtseg2_min); 1464 dev->data_bt_const.tseg2_max = le32_to_cpu(bt_const_extended.dtseg2_max); 1465 dev->data_bt_const.sjw_max = le32_to_cpu(bt_const_extended.dsjw_max); 1466 dev->data_bt_const.brp_min = le32_to_cpu(bt_const_extended.dbrp_min); 1467 dev->data_bt_const.brp_max = le32_to_cpu(bt_const_extended.dbrp_max); 1468 dev->data_bt_const.brp_inc = le32_to_cpu(bt_const_extended.dbrp_inc); 1469 1470 dev->can.fd.data_bittiming_const = &dev->data_bt_const; 1471 } 1472 1473 can_rx_offload_add_manual(netdev, &dev->offload, GS_NAPI_WEIGHT); 1474 SET_NETDEV_DEV(netdev, &intf->dev); 1475 1476 rc = register_candev(dev->netdev); 1477 if (rc) { 1478 dev_err(&intf->dev, 1479 "Couldn't register candev for channel %d (%pe)\n", 1480 channel, ERR_PTR(rc)); 1481 goto out_can_rx_offload_del; 1482 } 1483 1484 return dev; 1485 1486 out_can_rx_offload_del: 1487 can_rx_offload_del(&dev->offload); 1488 out_free_candev: 1489 free_candev(dev->netdev); 1490 return ERR_PTR(rc); 1491 } 1492 1493 static void gs_destroy_candev(struct gs_can *dev) 1494 { 1495 unregister_candev(dev->netdev); 1496 can_rx_offload_del(&dev->offload); 1497 free_candev(dev->netdev); 1498 } 1499 1500 static int gs_usb_probe(struct usb_interface *intf, 1501 const struct usb_device_id *id) 1502 { 1503 struct usb_device *udev = interface_to_usbdev(intf); 1504 struct usb_endpoint_descriptor *ep_in, *ep_out; 1505 struct gs_host_frame *hf; 1506 struct gs_usb *parent; 1507 struct gs_host_config hconf = { 1508 .byte_order = cpu_to_le32(0x0000beef), 1509 }; 1510 struct gs_device_config dconf; 1511 unsigned int icount, i; 1512 int rc; 1513 1514 rc = usb_find_common_endpoints(intf->cur_altsetting, 1515 &ep_in, &ep_out, NULL, NULL); 1516 if (rc) { 1517 dev_err(&intf->dev, "Required endpoints not found\n"); 1518 return rc; 1519 } 1520 1521 /* send host config */ 1522 rc = usb_control_msg_send(udev, 0, 1523 GS_USB_BREQ_HOST_FORMAT, 1524 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 1525 1, intf->cur_altsetting->desc.bInterfaceNumber, 1526 &hconf, sizeof(hconf), 1000, 1527 GFP_KERNEL); 1528 if (rc) { 1529 dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc); 1530 return rc; 1531 } 1532 1533 /* read device config */ 1534 rc = usb_control_msg_recv(udev, 0, 1535 GS_USB_BREQ_DEVICE_CONFIG, 1536 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 1537 1, intf->cur_altsetting->desc.bInterfaceNumber, 1538 &dconf, sizeof(dconf), 1000, 1539 GFP_KERNEL); 1540 if (rc) { 1541 dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n", 1542 rc); 1543 return rc; 1544 } 1545 1546 icount = dconf.icount + 1; 1547 dev_info(&intf->dev, "Configuring for %u interfaces\n", icount); 1548 1549 if (icount > type_max(parent->channel_cnt)) { 1550 dev_err(&intf->dev, 1551 "Driver cannot handle more that %u CAN interfaces\n", 1552 type_max(parent->channel_cnt)); 1553 return -EINVAL; 1554 } 1555 1556 parent = kzalloc(struct_size(parent, canch, icount), GFP_KERNEL); 1557 if (!parent) 1558 return -ENOMEM; 1559 1560 parent->channel_cnt = icount; 1561 1562 init_usb_anchor(&parent->rx_submitted); 1563 1564 usb_set_intfdata(intf, parent); 1565 parent->udev = udev; 1566 1567 /* store the detected endpoints */ 1568 parent->pipe_in = usb_rcvbulkpipe(parent->udev, ep_in->bEndpointAddress); 1569 parent->pipe_out = usb_sndbulkpipe(parent->udev, ep_out->bEndpointAddress); 1570 1571 for (i = 0; i < icount; i++) { 1572 unsigned int hf_size_rx = 0; 1573 1574 parent->canch[i] = gs_make_candev(i, intf, &dconf); 1575 if (IS_ERR_OR_NULL(parent->canch[i])) { 1576 /* save error code to return later */ 1577 rc = PTR_ERR(parent->canch[i]); 1578 1579 /* on failure destroy previously created candevs */ 1580 icount = i; 1581 for (i = 0; i < icount; i++) 1582 gs_destroy_candev(parent->canch[i]); 1583 1584 usb_kill_anchored_urbs(&parent->rx_submitted); 1585 kfree(parent); 1586 return rc; 1587 } 1588 parent->canch[i]->parent = parent; 1589 1590 /* set RX packet size based on FD and if hardware 1591 * timestamps are supported. 1592 */ 1593 if (parent->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD) { 1594 if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP) 1595 hf_size_rx = struct_size(hf, canfd_ts, 1); 1596 else 1597 hf_size_rx = struct_size(hf, canfd, 1); 1598 } else { 1599 if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP) 1600 hf_size_rx = struct_size(hf, classic_can_ts, 1); 1601 else 1602 hf_size_rx = struct_size(hf, classic_can, 1); 1603 } 1604 parent->hf_size_rx = max(parent->hf_size_rx, hf_size_rx); 1605 } 1606 1607 return 0; 1608 } 1609 1610 static void gs_usb_disconnect(struct usb_interface *intf) 1611 { 1612 struct gs_usb *parent = usb_get_intfdata(intf); 1613 unsigned int i; 1614 1615 usb_set_intfdata(intf, NULL); 1616 1617 if (!parent) { 1618 dev_err(&intf->dev, "Disconnect (nodata)\n"); 1619 return; 1620 } 1621 1622 for (i = 0; i < parent->channel_cnt; i++) 1623 if (parent->canch[i]) 1624 gs_destroy_candev(parent->canch[i]); 1625 1626 kfree(parent); 1627 } 1628 1629 static const struct usb_device_id gs_usb_table[] = { 1630 { USB_DEVICE_INTERFACE_NUMBER(USB_GS_USB_1_VENDOR_ID, 1631 USB_GS_USB_1_PRODUCT_ID, 0) }, 1632 { USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID, 1633 USB_CANDLELIGHT_PRODUCT_ID, 0) }, 1634 { USB_DEVICE_INTERFACE_NUMBER(USB_CES_CANEXT_FD_VENDOR_ID, 1635 USB_CES_CANEXT_FD_PRODUCT_ID, 0) }, 1636 { USB_DEVICE_INTERFACE_NUMBER(USB_ABE_CANDEBUGGER_FD_VENDOR_ID, 1637 USB_ABE_CANDEBUGGER_FD_PRODUCT_ID, 0) }, 1638 { USB_DEVICE_INTERFACE_NUMBER(USB_XYLANTA_SAINT3_VENDOR_ID, 1639 USB_XYLANTA_SAINT3_PRODUCT_ID, 0) }, 1640 { USB_DEVICE_INTERFACE_NUMBER(USB_CANNECTIVITY_VENDOR_ID, 1641 USB_CANNECTIVITY_PRODUCT_ID, 0) }, 1642 {} /* Terminating entry */ 1643 }; 1644 1645 MODULE_DEVICE_TABLE(usb, gs_usb_table); 1646 1647 static struct usb_driver gs_usb_driver = { 1648 .name = KBUILD_MODNAME, 1649 .probe = gs_usb_probe, 1650 .disconnect = gs_usb_disconnect, 1651 .id_table = gs_usb_table, 1652 }; 1653 1654 module_usb_driver(gs_usb_driver); 1655 1656 MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>"); 1657 MODULE_DESCRIPTION( 1658 "Socket CAN device driver for Geschwister Schneider Technologie-, " 1659 "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n" 1660 "and bytewerk.org candleLight USB CAN interfaces."); 1661 MODULE_LICENSE("GPL v2"); 1662