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