├── Makefile ├── README.md └── gs_usb_fd.c /Makefile: -------------------------------------------------------------------------------- 1 | obj-m := gs_usb_fd.o 2 | 3 | KDIR := /lib/modules/$(shell uname -r)/build 4 | PWD := $(shell pwd) 5 | LINDENT := /usr/src/kernels/$(shell uname -r)/scripts/Lindent 6 | 7 | default: 8 | $(MAKE) -C $(KDIR) M=$(PWD) modules 9 | 10 | clean: 11 | $(MAKE) -C $(KDIR) M=$(PWD) clean 12 | 13 | format: 14 | $(LINDENT) gs_usb_fd.c 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gs_usb_fd 2 | 3 | A modified gs_usb driver which supports CAN-FD. 4 | 5 | ## Supported Devices 6 | 7 | - [CANtact Pro](https://cantact.io) 8 | 9 | ## Building 10 | 11 | 1. Install the kernel sources for your platform: 12 | 13 | Fedora: 14 | 15 | ``` 16 | sudo dnf install -y kernel-devel 17 | ``` 18 | 19 | Ubuntu: 20 | 21 | ``` 22 | sudo apt install kernel-headers-$(uname -r) 23 | ``` 24 | 25 | 2. Use `make` to build the driver 26 | 27 | 3. Load the driver: 28 | 29 | ``` 30 | sudo modprobe can-dev 31 | sudo insmod gs_usb_fd.ko 32 | ``` 33 | 34 | ## Credits 35 | 36 | Thanks to Maximilian Schneider for authoring the original gs_usb driver. 37 | -------------------------------------------------------------------------------- /gs_usb_fd.c: -------------------------------------------------------------------------------- 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 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | /* Device specific constants */ 23 | #define USB_GSUSB_1_VENDOR_ID 0x1d50 24 | #define USB_GSUSB_1_PRODUCT_ID 0x606f 25 | 26 | #define USB_CANDLELIGHT_VENDOR_ID 0x1209 27 | #define USB_CANDLELIGHT_PRODUCT_ID 0x2323 28 | 29 | #define GSUSB_ENDPOINT_IN 1 30 | #define GSUSB_ENDPOINT_OUT 2 31 | 32 | /* Device specific constants */ 33 | enum gs_usb_breq { 34 | GS_USB_BREQ_HOST_FORMAT = 0, 35 | GS_USB_BREQ_BITTIMING, 36 | GS_USB_BREQ_MODE, 37 | GS_USB_BREQ_BERR, 38 | GS_USB_BREQ_BT_CONST, 39 | GS_USB_BREQ_DEVICE_CONFIG, 40 | GS_USB_BREQ_TIMESTAMP, 41 | GS_USB_BREQ_IDENTIFY, 42 | GS_USB_BREQ_DATA_BITTIMING, 43 | }; 44 | 45 | enum gs_can_mode { 46 | /* reset a channel. turns it off */ 47 | GS_CAN_MODE_RESET = 0, 48 | /* starts a channel */ 49 | GS_CAN_MODE_START 50 | }; 51 | 52 | enum gs_can_state { 53 | GS_CAN_STATE_ERROR_ACTIVE = 0, 54 | GS_CAN_STATE_ERROR_WARNING, 55 | GS_CAN_STATE_ERROR_PASSIVE, 56 | GS_CAN_STATE_BUS_OFF, 57 | GS_CAN_STATE_STOPPED, 58 | GS_CAN_STATE_SLEEPING 59 | }; 60 | 61 | enum gs_can_identify_mode { 62 | GS_CAN_IDENTIFY_OFF = 0, 63 | GS_CAN_IDENTIFY_ON 64 | }; 65 | 66 | /* data types passed between host and device */ 67 | struct gs_host_config { 68 | u32 byte_order; 69 | } __packed; 70 | /* All data exchanged between host and device is exchanged in host byte order, 71 | * thanks to the struct gs_host_config byte_order member, which is sent first 72 | * to indicate the desired byte order. 73 | */ 74 | 75 | struct gs_device_config { 76 | u8 reserved1; 77 | u8 reserved2; 78 | u8 reserved3; 79 | u8 icount; 80 | u32 sw_version; 81 | u32 hw_version; 82 | } __packed; 83 | 84 | #define GS_CAN_MODE_NORMAL 0 85 | #define GS_CAN_MODE_LISTEN_ONLY BIT(0) 86 | #define GS_CAN_MODE_LOOP_BACK BIT(1) 87 | #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2) 88 | #define GS_CAN_MODE_ONE_SHOT BIT(3) 89 | #define GS_CAN_MODE_HW_TIMESTAMP BIT(4) 90 | #define GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7) 91 | #define GS_CAN_MODE_FD BIT(8) 92 | 93 | struct gs_device_mode { 94 | u32 mode; 95 | u32 flags; 96 | } __packed; 97 | 98 | struct gs_device_state { 99 | u32 state; 100 | u32 rxerr; 101 | u32 txerr; 102 | } __packed; 103 | 104 | struct gs_device_bittiming { 105 | u32 prop_seg; 106 | u32 phase_seg1; 107 | u32 phase_seg2; 108 | u32 sjw; 109 | u32 brp; 110 | } __packed; 111 | 112 | struct gs_identify_mode { 113 | u32 mode; 114 | } __packed; 115 | 116 | #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0) 117 | #define GS_CAN_FEATURE_LOOP_BACK BIT(1) 118 | #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2) 119 | #define GS_CAN_FEATURE_ONE_SHOT BIT(3) 120 | #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4) 121 | #define GS_CAN_FEATURE_IDENTIFY BIT(5) 122 | #define GS_CAN_FEATURE_USER_ID BIT(6) 123 | #define GS_CAN_FEATURE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7) 124 | #define GS_CAN_FEATURE_FD BIT(8) 125 | 126 | struct gs_device_bt_const { 127 | u32 feature; 128 | u32 fclk_can; 129 | u32 tseg1_min; 130 | u32 tseg1_max; 131 | u32 tseg2_min; 132 | u32 tseg2_max; 133 | u32 sjw_max; 134 | u32 brp_min; 135 | u32 brp_max; 136 | u32 brp_inc; 137 | } __packed; 138 | 139 | #define GS_CAN_FLAG_OVERFLOW BIT(0) 140 | #define GS_CAN_FLAG_FD BIT(1) 141 | #define GS_CAN_FLAG_BRS BIT(2) 142 | #define GS_CAN_FLAG_ESI BIT(3) 143 | 144 | struct gs_host_frame { 145 | u32 echo_id; 146 | u32 can_id; 147 | 148 | u8 can_dlc; 149 | u8 channel; 150 | u8 flags; 151 | u8 reserved; 152 | 153 | u8 data[64]; 154 | } __packed; 155 | /* The GS USB devices make use of the same flags and masks as in 156 | * linux/can.h and linux/can/error.h, and no additional mapping is necessary. 157 | */ 158 | 159 | /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */ 160 | #define GS_MAX_TX_URBS 10 161 | /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */ 162 | #define GS_MAX_RX_URBS 30 163 | /* Maximum number of interfaces the driver supports per device. 164 | * Current hardware only supports 2 interfaces. The future may vary. 165 | */ 166 | #define GS_MAX_INTF 2 167 | 168 | struct gs_tx_context { 169 | struct gs_can *dev; 170 | unsigned int echo_id; 171 | }; 172 | 173 | struct gs_can { 174 | struct can_priv can; /* must be the first member */ 175 | 176 | struct gs_usb *parent; 177 | 178 | struct net_device *netdev; 179 | struct usb_device *udev; 180 | struct usb_interface *iface; 181 | 182 | struct can_bittiming_const bt_const; 183 | unsigned int channel; /* channel number */ 184 | 185 | /* This lock prevents a race condition between xmit and receive. */ 186 | spinlock_t tx_ctx_lock; 187 | struct gs_tx_context tx_context[GS_MAX_TX_URBS]; 188 | 189 | struct usb_anchor tx_submitted; 190 | atomic_t active_tx_urbs; 191 | }; 192 | 193 | /* usb interface struct */ 194 | struct gs_usb { 195 | struct gs_can *canch[GS_MAX_INTF]; 196 | struct usb_anchor rx_submitted; 197 | atomic_t active_channels; 198 | struct usb_device *udev; 199 | }; 200 | 201 | /* 'allocate' a tx context. 202 | * returns a valid tx context or NULL if there is no space. 203 | */ 204 | static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev) 205 | { 206 | int i = 0; 207 | unsigned long flags; 208 | 209 | spin_lock_irqsave(&dev->tx_ctx_lock, flags); 210 | 211 | for (; i < GS_MAX_TX_URBS; i++) { 212 | if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) { 213 | dev->tx_context[i].echo_id = i; 214 | spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); 215 | return &dev->tx_context[i]; 216 | } 217 | } 218 | 219 | spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); 220 | return NULL; 221 | } 222 | 223 | /* releases a tx context 224 | */ 225 | static void gs_free_tx_context(struct gs_tx_context *txc) 226 | { 227 | txc->echo_id = GS_MAX_TX_URBS; 228 | } 229 | 230 | /* Get a tx context by id. 231 | */ 232 | static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev, 233 | unsigned int id) 234 | { 235 | unsigned long flags; 236 | 237 | if (id < GS_MAX_TX_URBS) { 238 | spin_lock_irqsave(&dev->tx_ctx_lock, flags); 239 | if (dev->tx_context[id].echo_id == id) { 240 | spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); 241 | return &dev->tx_context[id]; 242 | } 243 | spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); 244 | } 245 | return NULL; 246 | } 247 | 248 | static int gs_cmd_reset(struct gs_can *gsdev) 249 | { 250 | struct gs_device_mode *dm; 251 | struct usb_interface *intf = gsdev->iface; 252 | int rc; 253 | 254 | dm = kzalloc(sizeof(*dm), GFP_KERNEL); 255 | if (!dm) 256 | return -ENOMEM; 257 | 258 | dm->mode = GS_CAN_MODE_RESET; 259 | 260 | rc = usb_control_msg(interface_to_usbdev(intf), 261 | usb_sndctrlpipe(interface_to_usbdev(intf), 0), 262 | GS_USB_BREQ_MODE, 263 | USB_DIR_OUT | USB_TYPE_VENDOR | 264 | USB_RECIP_INTERFACE, gsdev->channel, 0, dm, 265 | sizeof(*dm), 1000); 266 | 267 | kfree(dm); 268 | 269 | return rc; 270 | } 271 | 272 | static void gs_update_state(struct gs_can *dev, struct can_frame *cf) 273 | { 274 | struct can_device_stats *can_stats = &dev->can.can_stats; 275 | 276 | if (cf->can_id & CAN_ERR_RESTARTED) { 277 | dev->can.state = CAN_STATE_ERROR_ACTIVE; 278 | can_stats->restarts++; 279 | } else if (cf->can_id & CAN_ERR_BUSOFF) { 280 | dev->can.state = CAN_STATE_BUS_OFF; 281 | can_stats->bus_off++; 282 | } else if (cf->can_id & CAN_ERR_CRTL) { 283 | if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) || 284 | (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) { 285 | dev->can.state = CAN_STATE_ERROR_WARNING; 286 | can_stats->error_warning++; 287 | } else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) || 288 | (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) { 289 | dev->can.state = CAN_STATE_ERROR_PASSIVE; 290 | can_stats->error_passive++; 291 | } else { 292 | dev->can.state = CAN_STATE_ERROR_ACTIVE; 293 | } 294 | } 295 | } 296 | 297 | static void gs_usb_receive_bulk_callback(struct urb *urb) 298 | { 299 | struct gs_usb *usbcan = urb->context; 300 | struct gs_can *dev; 301 | struct net_device *netdev; 302 | int rc; 303 | struct net_device_stats *stats; 304 | struct gs_host_frame *hf = urb->transfer_buffer; 305 | struct gs_tx_context *txc; 306 | struct can_frame *cf; 307 | struct canfd_frame *cfd; 308 | struct sk_buff *skb; 309 | 310 | BUG_ON(!usbcan); 311 | 312 | switch (urb->status) { 313 | case 0: /* success */ 314 | break; 315 | case -ENOENT: 316 | case -ESHUTDOWN: 317 | return; 318 | default: 319 | /* do not resubmit aborted urbs. eg: when device goes down */ 320 | return; 321 | } 322 | 323 | /* device reports out of range channel id */ 324 | if (hf->channel >= GS_MAX_INTF) 325 | goto resubmit_urb; 326 | 327 | dev = usbcan->canch[hf->channel]; 328 | 329 | netdev = dev->netdev; 330 | stats = &netdev->stats; 331 | 332 | if (!netif_device_present(netdev)) 333 | return; 334 | 335 | if (hf->echo_id == -1) { /* normal rx */ 336 | if (hf->flags & GS_CAN_FLAG_FD) { 337 | skb = alloc_canfd_skb(dev->netdev, &cfd); 338 | if (!skb) 339 | return; 340 | 341 | cfd->can_id = hf->can_id; 342 | cfd->len = can_dlc2len(hf->can_dlc); 343 | if (hf->flags & GS_CAN_FLAG_BRS) { 344 | cfd->flags |= CANFD_BRS; 345 | } 346 | if (hf->flags & GS_CAN_FLAG_ESI) { 347 | cfd->flags |= CANFD_ESI; 348 | } 349 | memcpy(cfd->data, hf->data, cfd->len); 350 | } else { 351 | skb = alloc_can_skb(dev->netdev, &cf); 352 | if (!skb) 353 | return; 354 | cf->can_id = hf->can_id; 355 | cf->can_dlc = get_can_dlc(hf->can_dlc); 356 | memcpy(cf->data, hf->data, 8); 357 | 358 | /* ERROR frames tell us information about the controller */ 359 | if (hf->can_id & CAN_ERR_FLAG) 360 | gs_update_state(dev, cf); 361 | } 362 | 363 | netdev->stats.rx_packets++; 364 | netdev->stats.rx_bytes += hf->can_dlc; 365 | 366 | netif_rx(skb); 367 | } else { /* echo_id == hf->echo_id */ 368 | if (hf->echo_id >= GS_MAX_TX_URBS) { 369 | netdev_err(netdev, 370 | "Unexpected out of range echo id %d\n", 371 | hf->echo_id); 372 | goto resubmit_urb; 373 | } 374 | 375 | netdev->stats.tx_packets++; 376 | netdev->stats.tx_bytes += hf->can_dlc; 377 | 378 | txc = gs_get_tx_context(dev, hf->echo_id); 379 | 380 | /* bad devices send bad echo_ids. */ 381 | if (!txc) { 382 | netdev_err(netdev, 383 | "Unexpected unused echo id %d\n", 384 | hf->echo_id); 385 | goto resubmit_urb; 386 | } 387 | 388 | can_get_echo_skb(netdev, hf->echo_id); 389 | 390 | gs_free_tx_context(txc); 391 | 392 | atomic_dec(&dev->active_tx_urbs); 393 | 394 | netif_wake_queue(netdev); 395 | } 396 | 397 | if (hf->flags & GS_CAN_FLAG_OVERFLOW) { 398 | skb = alloc_can_err_skb(netdev, &cf); 399 | if (!skb) 400 | goto resubmit_urb; 401 | 402 | cf->can_id |= CAN_ERR_CRTL; 403 | cf->can_dlc = CAN_ERR_DLC; 404 | cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; 405 | stats->rx_over_errors++; 406 | stats->rx_errors++; 407 | netif_rx(skb); 408 | } 409 | 410 | resubmit_urb: 411 | usb_fill_bulk_urb(urb, 412 | usbcan->udev, 413 | usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN), 414 | hf, 415 | sizeof(struct gs_host_frame), 416 | gs_usb_receive_bulk_callback, usbcan); 417 | 418 | rc = usb_submit_urb(urb, GFP_ATOMIC); 419 | 420 | /* USB failure take down all interfaces */ 421 | if (rc == -ENODEV) { 422 | for (rc = 0; rc < GS_MAX_INTF; rc++) { 423 | if (usbcan->canch[rc]) 424 | netif_device_detach(usbcan->canch[rc]->netdev); 425 | } 426 | } 427 | } 428 | 429 | static int gs_usb_set_data_bittiming(struct net_device *netdev) 430 | { 431 | struct gs_can *dev = netdev_priv(netdev); 432 | struct can_bittiming *bt = &dev->can.data_bittiming; 433 | struct usb_interface *intf = dev->iface; 434 | int rc; 435 | struct gs_device_bittiming *dbt; 436 | 437 | dbt = kmalloc(sizeof(*dbt), GFP_KERNEL); 438 | if (!dbt) 439 | return -ENOMEM; 440 | 441 | dbt->prop_seg = bt->prop_seg; 442 | dbt->phase_seg1 = bt->phase_seg1; 443 | dbt->phase_seg2 = bt->phase_seg2; 444 | dbt->sjw = bt->sjw; 445 | dbt->brp = bt->brp; 446 | 447 | /* request bit timings */ 448 | rc = usb_control_msg(interface_to_usbdev(intf), 449 | usb_sndctrlpipe(interface_to_usbdev(intf), 0), 450 | GS_USB_BREQ_DATA_BITTIMING, 451 | USB_DIR_OUT | USB_TYPE_VENDOR | 452 | USB_RECIP_INTERFACE, dev->channel, 0, dbt, 453 | sizeof(*dbt), 1000); 454 | 455 | kfree(dbt); 456 | 457 | if (rc < 0) 458 | dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)", 459 | rc); 460 | 461 | return (rc > 0) ? 0 : rc; 462 | } 463 | 464 | static int gs_usb_set_bittiming(struct net_device *netdev) 465 | { 466 | struct gs_can *dev = netdev_priv(netdev); 467 | struct can_bittiming *bt = &dev->can.bittiming; 468 | struct usb_interface *intf = dev->iface; 469 | int rc; 470 | struct gs_device_bittiming *dbt; 471 | 472 | dbt = kmalloc(sizeof(*dbt), GFP_KERNEL); 473 | if (!dbt) 474 | return -ENOMEM; 475 | 476 | dbt->prop_seg = bt->prop_seg; 477 | dbt->phase_seg1 = bt->phase_seg1; 478 | dbt->phase_seg2 = bt->phase_seg2; 479 | dbt->sjw = bt->sjw; 480 | dbt->brp = bt->brp; 481 | 482 | /* request bit timings */ 483 | rc = usb_control_msg(interface_to_usbdev(intf), 484 | usb_sndctrlpipe(interface_to_usbdev(intf), 0), 485 | GS_USB_BREQ_BITTIMING, 486 | USB_DIR_OUT | USB_TYPE_VENDOR | 487 | USB_RECIP_INTERFACE, dev->channel, 0, dbt, 488 | sizeof(*dbt), 1000); 489 | 490 | kfree(dbt); 491 | 492 | if (rc < 0) 493 | dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)", 494 | rc); 495 | 496 | return (rc > 0) ? 0 : rc; 497 | } 498 | 499 | static void gs_usb_xmit_callback(struct urb *urb) 500 | { 501 | struct gs_tx_context *txc = urb->context; 502 | struct gs_can *dev = txc->dev; 503 | struct net_device *netdev = dev->netdev; 504 | 505 | if (urb->status) 506 | netdev_info(netdev, "usb xmit fail %d\n", txc->echo_id); 507 | 508 | usb_free_coherent(urb->dev, 509 | urb->transfer_buffer_length, 510 | urb->transfer_buffer, urb->transfer_dma); 511 | } 512 | 513 | static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb, 514 | struct net_device *netdev) 515 | { 516 | struct gs_can *dev = netdev_priv(netdev); 517 | struct net_device_stats *stats = &dev->netdev->stats; 518 | struct urb *urb; 519 | struct gs_host_frame *hf; 520 | struct can_frame *cf; 521 | struct canfd_frame *cfd; 522 | int rc; 523 | unsigned int idx; 524 | struct gs_tx_context *txc; 525 | 526 | if (can_dropped_invalid_skb(netdev, skb)) 527 | return NETDEV_TX_OK; 528 | 529 | /* find an empty context to keep track of transmission */ 530 | txc = gs_alloc_tx_context(dev); 531 | if (!txc) 532 | return NETDEV_TX_BUSY; 533 | 534 | /* create a URB, and a buffer for it */ 535 | urb = usb_alloc_urb(0, GFP_ATOMIC); 536 | if (!urb) 537 | goto nomem_urb; 538 | 539 | hf = usb_alloc_coherent(dev->udev, sizeof(*hf), GFP_ATOMIC, 540 | &urb->transfer_dma); 541 | if (!hf) { 542 | netdev_err(netdev, "No memory left for USB buffer\n"); 543 | goto nomem_hf; 544 | } 545 | 546 | idx = txc->echo_id; 547 | 548 | if (idx >= GS_MAX_TX_URBS) { 549 | netdev_err(netdev, "Invalid tx context %d\n", idx); 550 | goto badidx; 551 | } 552 | 553 | hf->echo_id = idx; 554 | hf->channel = dev->channel; 555 | hf->flags = 0; 556 | 557 | if (skb->len == CANFD_MTU) { 558 | cfd = (struct canfd_frame *)skb->data; 559 | hf->can_id = cfd->can_id; 560 | 561 | hf->flags |= GS_CAN_FLAG_FD; 562 | if (cfd->flags & CANFD_BRS) { 563 | hf->flags |= GS_CAN_FLAG_BRS; 564 | } 565 | if (cfd->flags & CANFD_ESI) { 566 | hf->flags |= GS_CAN_FLAG_ESI; 567 | } 568 | 569 | hf->can_dlc = can_len2dlc(cfd->len); 570 | memcpy(hf->data, cfd->data, cfd->len); 571 | } else { 572 | cf = (struct can_frame *)skb->data; 573 | hf->can_id = cf->can_id; 574 | hf->can_dlc = cf->can_dlc; 575 | memcpy(hf->data, cf->data, cf->can_dlc); 576 | } 577 | 578 | usb_fill_bulk_urb(urb, dev->udev, 579 | usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT), 580 | hf, sizeof(*hf), gs_usb_xmit_callback, txc); 581 | 582 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 583 | usb_anchor_urb(urb, &dev->tx_submitted); 584 | 585 | can_put_echo_skb(skb, netdev, idx); 586 | 587 | atomic_inc(&dev->active_tx_urbs); 588 | 589 | rc = usb_submit_urb(urb, GFP_ATOMIC); 590 | if (unlikely(rc)) { /* usb send failed */ 591 | atomic_dec(&dev->active_tx_urbs); 592 | 593 | can_free_echo_skb(netdev, idx); 594 | gs_free_tx_context(txc); 595 | 596 | usb_unanchor_urb(urb); 597 | usb_free_coherent(dev->udev, 598 | sizeof(*hf), hf, urb->transfer_dma); 599 | 600 | if (rc == -ENODEV) { 601 | netif_device_detach(netdev); 602 | } else { 603 | netdev_err(netdev, "usb_submit failed (err=%d)\n", rc); 604 | stats->tx_dropped++; 605 | } 606 | } else { 607 | /* Slow down tx path */ 608 | if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS) 609 | netif_stop_queue(netdev); 610 | } 611 | 612 | /* let usb core take care of this urb */ 613 | usb_free_urb(urb); 614 | 615 | return NETDEV_TX_OK; 616 | 617 | badidx: 618 | usb_free_coherent(dev->udev, sizeof(*hf), hf, urb->transfer_dma); 619 | nomem_hf: 620 | usb_free_urb(urb); 621 | 622 | nomem_urb: 623 | gs_free_tx_context(txc); 624 | dev_kfree_skb(skb); 625 | stats->tx_dropped++; 626 | return NETDEV_TX_OK; 627 | } 628 | 629 | static int gs_can_open(struct net_device *netdev) 630 | { 631 | struct gs_can *dev = netdev_priv(netdev); 632 | struct gs_usb *parent = dev->parent; 633 | int rc, i; 634 | struct gs_device_mode *dm; 635 | u32 ctrlmode; 636 | 637 | rc = open_candev(netdev); 638 | if (rc) 639 | return rc; 640 | 641 | if (atomic_add_return(1, &parent->active_channels) == 1) { 642 | for (i = 0; i < GS_MAX_RX_URBS; i++) { 643 | struct urb *urb; 644 | u8 *buf; 645 | 646 | /* alloc rx urb */ 647 | urb = usb_alloc_urb(0, GFP_KERNEL); 648 | if (!urb) 649 | return -ENOMEM; 650 | 651 | /* alloc rx buffer */ 652 | buf = usb_alloc_coherent(dev->udev, 653 | sizeof(struct gs_host_frame), 654 | GFP_KERNEL, 655 | &urb->transfer_dma); 656 | if (!buf) { 657 | netdev_err(netdev, 658 | "No memory left for USB buffer\n"); 659 | usb_free_urb(urb); 660 | return -ENOMEM; 661 | } 662 | 663 | /* fill, anchor, and submit rx urb */ 664 | usb_fill_bulk_urb(urb, 665 | dev->udev, 666 | usb_rcvbulkpipe(dev->udev, 667 | GSUSB_ENDPOINT_IN), 668 | buf, 669 | sizeof(struct gs_host_frame), 670 | gs_usb_receive_bulk_callback, parent); 671 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 672 | 673 | usb_anchor_urb(urb, &parent->rx_submitted); 674 | 675 | rc = usb_submit_urb(urb, GFP_KERNEL); 676 | if (rc) { 677 | if (rc == -ENODEV) 678 | netif_device_detach(dev->netdev); 679 | 680 | netdev_err(netdev, 681 | "usb_submit failed (err=%d)\n", rc); 682 | 683 | usb_unanchor_urb(urb); 684 | usb_free_urb(urb); 685 | break; 686 | } 687 | 688 | /* Drop reference, 689 | * USB core will take care of freeing it 690 | */ 691 | usb_free_urb(urb); 692 | } 693 | } 694 | 695 | dm = kmalloc(sizeof(*dm), GFP_KERNEL); 696 | if (!dm) 697 | return -ENOMEM; 698 | 699 | /* flags */ 700 | ctrlmode = dev->can.ctrlmode; 701 | dm->flags = 0; 702 | 703 | if (ctrlmode & CAN_CTRLMODE_LOOPBACK) 704 | dm->flags |= GS_CAN_MODE_LOOP_BACK; 705 | else if (ctrlmode & CAN_CTRLMODE_LISTENONLY) 706 | dm->flags |= GS_CAN_MODE_LISTEN_ONLY; 707 | 708 | /* Controller is not allowed to retry TX 709 | * this mode is unavailable on atmels uc3c hardware 710 | */ 711 | if (ctrlmode & CAN_CTRLMODE_ONE_SHOT) 712 | dm->flags |= GS_CAN_MODE_ONE_SHOT; 713 | 714 | if (ctrlmode & CAN_CTRLMODE_3_SAMPLES) 715 | dm->flags |= GS_CAN_MODE_TRIPLE_SAMPLE; 716 | 717 | if (ctrlmode & CAN_CTRLMODE_FD) 718 | dm->flags |= GS_CAN_MODE_FD; 719 | 720 | /* finally start device */ 721 | dm->mode = GS_CAN_MODE_START; 722 | rc = usb_control_msg(interface_to_usbdev(dev->iface), 723 | usb_sndctrlpipe(interface_to_usbdev(dev->iface), 724 | 0), GS_USB_BREQ_MODE, 725 | USB_DIR_OUT | USB_TYPE_VENDOR | 726 | USB_RECIP_INTERFACE, dev->channel, 0, dm, 727 | sizeof(*dm), 1000); 728 | 729 | if (rc < 0) { 730 | netdev_err(netdev, "Couldn't start device (err=%d)\n", rc); 731 | kfree(dm); 732 | return rc; 733 | } 734 | 735 | kfree(dm); 736 | 737 | dev->can.state = CAN_STATE_ERROR_ACTIVE; 738 | 739 | if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)) 740 | netif_start_queue(netdev); 741 | 742 | return 0; 743 | } 744 | 745 | static int gs_can_close(struct net_device *netdev) 746 | { 747 | int rc; 748 | struct gs_can *dev = netdev_priv(netdev); 749 | struct gs_usb *parent = dev->parent; 750 | 751 | netif_stop_queue(netdev); 752 | 753 | /* Stop polling */ 754 | if (atomic_dec_and_test(&parent->active_channels)) 755 | usb_kill_anchored_urbs(&parent->rx_submitted); 756 | 757 | /* Stop sending URBs */ 758 | usb_kill_anchored_urbs(&dev->tx_submitted); 759 | atomic_set(&dev->active_tx_urbs, 0); 760 | 761 | /* reset the device */ 762 | rc = gs_cmd_reset(dev); 763 | if (rc < 0) 764 | netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc); 765 | 766 | /* reset tx contexts */ 767 | for (rc = 0; rc < GS_MAX_TX_URBS; rc++) { 768 | dev->tx_context[rc].dev = dev; 769 | dev->tx_context[rc].echo_id = GS_MAX_TX_URBS; 770 | } 771 | 772 | /* close the netdev */ 773 | close_candev(netdev); 774 | 775 | return 0; 776 | } 777 | 778 | static const struct net_device_ops gs_usb_netdev_ops = { 779 | .ndo_open = gs_can_open, 780 | .ndo_stop = gs_can_close, 781 | .ndo_start_xmit = gs_can_start_xmit, 782 | .ndo_change_mtu = can_change_mtu, 783 | }; 784 | 785 | static int gs_usb_set_identify(struct net_device *netdev, bool do_identify) 786 | { 787 | struct gs_can *dev = netdev_priv(netdev); 788 | struct gs_identify_mode *imode; 789 | int rc; 790 | 791 | imode = kmalloc(sizeof(*imode), GFP_KERNEL); 792 | 793 | if (!imode) 794 | return -ENOMEM; 795 | 796 | if (do_identify) 797 | imode->mode = GS_CAN_IDENTIFY_ON; 798 | else 799 | imode->mode = GS_CAN_IDENTIFY_OFF; 800 | 801 | rc = usb_control_msg(interface_to_usbdev(dev->iface), 802 | usb_sndctrlpipe(interface_to_usbdev(dev->iface), 803 | 0), 804 | GS_USB_BREQ_IDENTIFY, 805 | USB_DIR_OUT | USB_TYPE_VENDOR | 806 | USB_RECIP_INTERFACE, 807 | dev->channel, 0, imode, sizeof(*imode), 100); 808 | 809 | kfree(imode); 810 | 811 | return (rc > 0) ? 0 : rc; 812 | } 813 | 814 | /* blink LED's for finding the this interface */ 815 | static int gs_usb_set_phys_id(struct net_device *dev, 816 | enum ethtool_phys_id_state state) 817 | { 818 | int rc = 0; 819 | 820 | switch (state) { 821 | case ETHTOOL_ID_ACTIVE: 822 | rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON); 823 | break; 824 | case ETHTOOL_ID_INACTIVE: 825 | rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF); 826 | break; 827 | default: 828 | break; 829 | } 830 | 831 | return rc; 832 | } 833 | 834 | static const struct ethtool_ops gs_usb_ethtool_ops = { 835 | .set_phys_id = gs_usb_set_phys_id, 836 | }; 837 | 838 | static struct gs_can *gs_make_candev(unsigned int channel, 839 | struct usb_interface *intf, 840 | struct gs_device_config *dconf) 841 | { 842 | struct gs_can *dev; 843 | struct net_device *netdev; 844 | int rc; 845 | struct gs_device_bt_const *bt_const; 846 | 847 | bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL); 848 | if (!bt_const) 849 | return ERR_PTR(-ENOMEM); 850 | 851 | /* fetch bit timing constants */ 852 | rc = usb_control_msg(interface_to_usbdev(intf), 853 | usb_rcvctrlpipe(interface_to_usbdev(intf), 0), 854 | GS_USB_BREQ_BT_CONST, 855 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 856 | channel, 0, bt_const, sizeof(*bt_const), 1000); 857 | 858 | if (rc < 0) { 859 | dev_err(&intf->dev, 860 | "Couldn't get bit timing const for channel (err=%d)\n", 861 | rc); 862 | kfree(bt_const); 863 | return ERR_PTR(rc); 864 | } 865 | 866 | /* create netdev */ 867 | netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS); 868 | if (!netdev) { 869 | dev_err(&intf->dev, "Couldn't allocate candev\n"); 870 | kfree(bt_const); 871 | return ERR_PTR(-ENOMEM); 872 | } 873 | 874 | dev = netdev_priv(netdev); 875 | 876 | netdev->netdev_ops = &gs_usb_netdev_ops; 877 | 878 | netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */ 879 | 880 | /* dev settup */ 881 | strcpy(dev->bt_const.name, "gs_usb_fd"); 882 | dev->bt_const.tseg1_min = bt_const->tseg1_min; 883 | dev->bt_const.tseg1_max = bt_const->tseg1_max; 884 | dev->bt_const.tseg2_min = bt_const->tseg2_min; 885 | dev->bt_const.tseg2_max = bt_const->tseg2_max; 886 | dev->bt_const.sjw_max = bt_const->sjw_max; 887 | dev->bt_const.brp_min = bt_const->brp_min; 888 | dev->bt_const.brp_max = bt_const->brp_max; 889 | dev->bt_const.brp_inc = bt_const->brp_inc; 890 | 891 | dev->udev = interface_to_usbdev(intf); 892 | dev->iface = intf; 893 | dev->netdev = netdev; 894 | dev->channel = channel; 895 | 896 | init_usb_anchor(&dev->tx_submitted); 897 | atomic_set(&dev->active_tx_urbs, 0); 898 | spin_lock_init(&dev->tx_ctx_lock); 899 | for (rc = 0; rc < GS_MAX_TX_URBS; rc++) { 900 | dev->tx_context[rc].dev = dev; 901 | dev->tx_context[rc].echo_id = GS_MAX_TX_URBS; 902 | } 903 | 904 | /* can setup */ 905 | dev->can.state = CAN_STATE_STOPPED; 906 | dev->can.clock.freq = bt_const->fclk_can; 907 | dev->can.bittiming_const = &dev->bt_const; 908 | dev->can.do_set_bittiming = gs_usb_set_bittiming; 909 | dev->can.data_bittiming_const = &dev->bt_const; 910 | dev->can.do_set_data_bittiming = gs_usb_set_data_bittiming; 911 | 912 | dev->can.ctrlmode_supported = 0; 913 | 914 | if (bt_const->feature & GS_CAN_FEATURE_LISTEN_ONLY) 915 | dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY; 916 | 917 | if (bt_const->feature & GS_CAN_FEATURE_LOOP_BACK) 918 | dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK; 919 | 920 | if (bt_const->feature & GS_CAN_FEATURE_TRIPLE_SAMPLE) 921 | dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES; 922 | 923 | if (bt_const->feature & GS_CAN_FEATURE_ONE_SHOT) 924 | dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT; 925 | 926 | if (bt_const->feature & GS_CAN_FEATURE_FD) 927 | dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD; 928 | 929 | SET_NETDEV_DEV(netdev, &intf->dev); 930 | 931 | if (dconf->sw_version > 1) 932 | if (bt_const->feature & GS_CAN_FEATURE_IDENTIFY) 933 | netdev->ethtool_ops = &gs_usb_ethtool_ops; 934 | 935 | kfree(bt_const); 936 | 937 | rc = register_candev(dev->netdev); 938 | if (rc) { 939 | free_candev(dev->netdev); 940 | dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc); 941 | return ERR_PTR(rc); 942 | } 943 | 944 | return dev; 945 | } 946 | 947 | static void gs_destroy_candev(struct gs_can *dev) 948 | { 949 | unregister_candev(dev->netdev); 950 | usb_kill_anchored_urbs(&dev->tx_submitted); 951 | free_candev(dev->netdev); 952 | } 953 | 954 | static int gs_usb_probe(struct usb_interface *intf, 955 | const struct usb_device_id *id) 956 | { 957 | struct gs_usb *dev; 958 | int rc = -ENOMEM; 959 | unsigned int icount, i; 960 | struct gs_host_config *hconf; 961 | struct gs_device_config *dconf; 962 | 963 | hconf = kmalloc(sizeof(*hconf), GFP_KERNEL); 964 | if (!hconf) 965 | return -ENOMEM; 966 | 967 | hconf->byte_order = 0x0000beef; 968 | 969 | /* send host config */ 970 | rc = usb_control_msg(interface_to_usbdev(intf), 971 | usb_sndctrlpipe(interface_to_usbdev(intf), 0), 972 | GS_USB_BREQ_HOST_FORMAT, 973 | USB_DIR_OUT | USB_TYPE_VENDOR | 974 | USB_RECIP_INTERFACE, 1, 975 | intf->cur_altsetting->desc.bInterfaceNumber, hconf, 976 | sizeof(*hconf), 1000); 977 | 978 | kfree(hconf); 979 | 980 | if (rc < 0) { 981 | dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc); 982 | return rc; 983 | } 984 | 985 | dconf = kmalloc(sizeof(*dconf), GFP_KERNEL); 986 | if (!dconf) 987 | return -ENOMEM; 988 | 989 | /* read device config */ 990 | rc = usb_control_msg(interface_to_usbdev(intf), 991 | usb_rcvctrlpipe(interface_to_usbdev(intf), 0), 992 | GS_USB_BREQ_DEVICE_CONFIG, 993 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 994 | 1, 995 | intf->cur_altsetting->desc.bInterfaceNumber, 996 | dconf, sizeof(*dconf), 1000); 997 | if (rc < 0) { 998 | dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n", 999 | rc); 1000 | kfree(dconf); 1001 | return rc; 1002 | } 1003 | 1004 | icount = dconf->icount + 1; 1005 | dev_info(&intf->dev, "Configuring for %d interfaces\n", icount); 1006 | 1007 | if (icount > GS_MAX_INTF) { 1008 | dev_err(&intf->dev, 1009 | "Driver cannot handle more that %d CAN interfaces\n", 1010 | GS_MAX_INTF); 1011 | kfree(dconf); 1012 | return -EINVAL; 1013 | } 1014 | 1015 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1016 | if (!dev) { 1017 | kfree(dconf); 1018 | return -ENOMEM; 1019 | } 1020 | 1021 | init_usb_anchor(&dev->rx_submitted); 1022 | 1023 | atomic_set(&dev->active_channels, 0); 1024 | 1025 | usb_set_intfdata(intf, dev); 1026 | dev->udev = interface_to_usbdev(intf); 1027 | 1028 | for (i = 0; i < icount; i++) { 1029 | dev->canch[i] = gs_make_candev(i, intf, dconf); 1030 | if (IS_ERR_OR_NULL(dev->canch[i])) { 1031 | /* save error code to return later */ 1032 | rc = PTR_ERR(dev->canch[i]); 1033 | 1034 | /* on failure destroy previously created candevs */ 1035 | icount = i; 1036 | for (i = 0; i < icount; i++) 1037 | gs_destroy_candev(dev->canch[i]); 1038 | 1039 | usb_kill_anchored_urbs(&dev->rx_submitted); 1040 | kfree(dconf); 1041 | kfree(dev); 1042 | return rc; 1043 | } 1044 | dev->canch[i]->parent = dev; 1045 | } 1046 | 1047 | kfree(dconf); 1048 | 1049 | return 0; 1050 | } 1051 | 1052 | static void gs_usb_disconnect(struct usb_interface *intf) 1053 | { 1054 | unsigned i; 1055 | struct gs_usb *dev = usb_get_intfdata(intf); 1056 | usb_set_intfdata(intf, NULL); 1057 | 1058 | if (!dev) { 1059 | dev_err(&intf->dev, "Disconnect (nodata)\n"); 1060 | return; 1061 | } 1062 | 1063 | for (i = 0; i < GS_MAX_INTF; i++) 1064 | if (dev->canch[i]) 1065 | gs_destroy_candev(dev->canch[i]); 1066 | 1067 | usb_kill_anchored_urbs(&dev->rx_submitted); 1068 | kfree(dev); 1069 | } 1070 | 1071 | static const struct usb_device_id gs_usb_table[] = { 1072 | { USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID, 1073 | USB_GSUSB_1_PRODUCT_ID, 0) }, 1074 | { USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID, 1075 | USB_CANDLELIGHT_PRODUCT_ID, 0) }, 1076 | { } /* Terminating entry */ 1077 | }; 1078 | 1079 | MODULE_DEVICE_TABLE(usb, gs_usb_table); 1080 | 1081 | static struct usb_driver gs_usb_fd_driver = { 1082 | .name = "gs_usb_fd", 1083 | .probe = gs_usb_probe, 1084 | .disconnect = gs_usb_disconnect, 1085 | .id_table = gs_usb_table, 1086 | }; 1087 | 1088 | module_usb_driver(gs_usb_fd_driver); 1089 | 1090 | MODULE_AUTHOR("Eric Evenchick "); 1091 | MODULE_DESCRIPTION("Socket CAN device driver for the CANtact Pro device. " 1092 | "This driver is based on gs_usb with added support for CAN FD"); 1093 | MODULE_LICENSE("GPL v2"); 1094 | --------------------------------------------------------------------------------