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