1 /* 2 * CAN driver for PEAK System USB adapters 3 * Derived from the PCAN project file driver/src/pcan_usb_core.c 4 * 5 * Copyright (C) 2003-2010 PEAK System-Technik GmbH 6 * Copyright (C) 2010-2012 Stephane Grosjean <s.grosjean@peak-system.com> 7 * 8 * Many thanks to Klaus Hitschler <klaus.hitschler@gmx.de> 9 * 10 * This program is free software; you can redistribute it and/or modify it 11 * under the terms of the GNU General Public License as published 12 * by the Free Software Foundation; version 2 of the License. 13 * 14 * This program is distributed in the hope that it will be useful, but 15 * WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * General Public License for more details. 18 */ 19 #include <linux/init.h> 20 #include <linux/signal.h> 21 #include <linux/slab.h> 22 #include <linux/module.h> 23 #include <linux/netdevice.h> 24 #include <linux/usb.h> 25 26 #include <linux/can.h> 27 #include <linux/can/dev.h> 28 #include <linux/can/error.h> 29 30 #include "pcan_usb_core.h" 31 32 MODULE_AUTHOR("Stephane Grosjean <s.grosjean@peak-system.com>"); 33 MODULE_DESCRIPTION("CAN driver for PEAK-System USB adapters"); 34 MODULE_LICENSE("GPL v2"); 35 36 /* Table of devices that work with this driver */ 37 static struct usb_device_id peak_usb_table[] = { 38 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USB_PRODUCT_ID)}, 39 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPRO_PRODUCT_ID)}, 40 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBFD_PRODUCT_ID)}, 41 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBPROFD_PRODUCT_ID)}, 42 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBCHIP_PRODUCT_ID)}, 43 {USB_DEVICE(PCAN_USB_VENDOR_ID, PCAN_USBX6_PRODUCT_ID)}, 44 {} /* Terminating entry */ 45 }; 46 47 MODULE_DEVICE_TABLE(usb, peak_usb_table); 48 49 /* List of supported PCAN-USB adapters (NULL terminated list) */ 50 static const struct peak_usb_adapter *const peak_usb_adapters_list[] = { 51 &pcan_usb, 52 &pcan_usb_pro, 53 &pcan_usb_fd, 54 &pcan_usb_pro_fd, 55 &pcan_usb_chip, 56 &pcan_usb_x6, 57 }; 58 59 /* 60 * dump memory 61 */ 62 #define DUMP_WIDTH 16 63 void pcan_dump_mem(char *prompt, void *p, int l) 64 { 65 pr_info("%s dumping %s (%d bytes):\n", 66 PCAN_USB_DRIVER_NAME, prompt ? prompt : "memory", l); 67 print_hex_dump(KERN_INFO, PCAN_USB_DRIVER_NAME " ", DUMP_PREFIX_NONE, 68 DUMP_WIDTH, 1, p, l, false); 69 } 70 71 /* 72 * initialize a time_ref object with usb adapter own settings 73 */ 74 void peak_usb_init_time_ref(struct peak_time_ref *time_ref, 75 const struct peak_usb_adapter *adapter) 76 { 77 if (time_ref) { 78 memset(time_ref, 0, sizeof(struct peak_time_ref)); 79 time_ref->adapter = adapter; 80 } 81 } 82 83 /* 84 * sometimes, another now may be more recent than current one... 85 */ 86 void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now) 87 { 88 time_ref->ts_dev_2 = ts_now; 89 90 /* should wait at least two passes before computing */ 91 if (ktime_to_ns(time_ref->tv_host) > 0) { 92 u32 delta_ts = time_ref->ts_dev_2 - time_ref->ts_dev_1; 93 94 if (time_ref->ts_dev_2 < time_ref->ts_dev_1) 95 delta_ts &= (1 << time_ref->adapter->ts_used_bits) - 1; 96 97 time_ref->ts_total += delta_ts; 98 } 99 } 100 101 /* 102 * register device timestamp as now 103 */ 104 void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now) 105 { 106 if (ktime_to_ns(time_ref->tv_host_0) == 0) { 107 /* use monotonic clock to correctly compute further deltas */ 108 time_ref->tv_host_0 = ktime_get(); 109 time_ref->tv_host = ktime_set(0, 0); 110 } else { 111 /* 112 * delta_us should not be >= 2^32 => delta should be < 4294s 113 * handle 32-bits wrapping here: if count of s. reaches 4200, 114 * reset counters and change time base 115 */ 116 if (ktime_to_ns(time_ref->tv_host)) { 117 ktime_t delta = ktime_sub(time_ref->tv_host, 118 time_ref->tv_host_0); 119 if (ktime_to_ns(delta) > (4200ull * NSEC_PER_SEC)) { 120 time_ref->tv_host_0 = time_ref->tv_host; 121 time_ref->ts_total = 0; 122 } 123 } 124 125 time_ref->tv_host = ktime_get(); 126 time_ref->tick_count++; 127 } 128 129 time_ref->ts_dev_1 = time_ref->ts_dev_2; 130 peak_usb_update_ts_now(time_ref, ts_now); 131 } 132 133 /* 134 * compute time according to current ts and time_ref data 135 */ 136 void peak_usb_get_ts_time(struct peak_time_ref *time_ref, u32 ts, ktime_t *time) 137 { 138 /* protect from getting time before setting now */ 139 if (ktime_to_ns(time_ref->tv_host)) { 140 u64 delta_us; 141 142 delta_us = ts - time_ref->ts_dev_2; 143 if (ts < time_ref->ts_dev_2) 144 delta_us &= (1 << time_ref->adapter->ts_used_bits) - 1; 145 146 delta_us += time_ref->ts_total; 147 148 delta_us *= time_ref->adapter->us_per_ts_scale; 149 delta_us >>= time_ref->adapter->us_per_ts_shift; 150 151 *time = ktime_add_us(time_ref->tv_host_0, delta_us); 152 } else { 153 *time = ktime_get(); 154 } 155 } 156 157 /* 158 * post received skb after having set any hw timestamp 159 */ 160 int peak_usb_netif_rx(struct sk_buff *skb, 161 struct peak_time_ref *time_ref, u32 ts_low) 162 { 163 struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb); 164 165 peak_usb_get_ts_time(time_ref, ts_low, &hwts->hwtstamp); 166 167 return netif_rx(skb); 168 } 169 170 /* 171 * callback for bulk Rx urb 172 */ 173 static void peak_usb_read_bulk_callback(struct urb *urb) 174 { 175 struct peak_usb_device *dev = urb->context; 176 struct net_device *netdev; 177 int err; 178 179 netdev = dev->netdev; 180 181 if (!netif_device_present(netdev)) 182 return; 183 184 /* check reception status */ 185 switch (urb->status) { 186 case 0: 187 /* success */ 188 break; 189 190 case -EILSEQ: 191 case -ENOENT: 192 case -ECONNRESET: 193 case -ESHUTDOWN: 194 return; 195 196 default: 197 if (net_ratelimit()) 198 netdev_err(netdev, 199 "Rx urb aborted (%d)\n", urb->status); 200 goto resubmit_urb; 201 } 202 203 /* protect from any incoming empty msgs */ 204 if ((urb->actual_length > 0) && (dev->adapter->dev_decode_buf)) { 205 /* handle these kinds of msgs only if _start callback called */ 206 if (dev->state & PCAN_USB_STATE_STARTED) { 207 err = dev->adapter->dev_decode_buf(dev, urb); 208 if (err) 209 pcan_dump_mem("received usb message", 210 urb->transfer_buffer, 211 urb->transfer_buffer_length); 212 } 213 } 214 215 resubmit_urb: 216 usb_fill_bulk_urb(urb, dev->udev, 217 usb_rcvbulkpipe(dev->udev, dev->ep_msg_in), 218 urb->transfer_buffer, dev->adapter->rx_buffer_size, 219 peak_usb_read_bulk_callback, dev); 220 221 usb_anchor_urb(urb, &dev->rx_submitted); 222 err = usb_submit_urb(urb, GFP_ATOMIC); 223 if (!err) 224 return; 225 226 usb_unanchor_urb(urb); 227 228 if (err == -ENODEV) 229 netif_device_detach(netdev); 230 else 231 netdev_err(netdev, "failed resubmitting read bulk urb: %d\n", 232 err); 233 } 234 235 /* 236 * callback for bulk Tx urb 237 */ 238 static void peak_usb_write_bulk_callback(struct urb *urb) 239 { 240 struct peak_tx_urb_context *context = urb->context; 241 struct peak_usb_device *dev; 242 struct net_device *netdev; 243 244 BUG_ON(!context); 245 246 dev = context->dev; 247 netdev = dev->netdev; 248 249 atomic_dec(&dev->active_tx_urbs); 250 251 if (!netif_device_present(netdev)) 252 return; 253 254 /* check tx status */ 255 switch (urb->status) { 256 case 0: 257 /* transmission complete */ 258 netdev->stats.tx_packets++; 259 netdev->stats.tx_bytes += context->data_len; 260 261 /* prevent tx timeout */ 262 netif_trans_update(netdev); 263 break; 264 265 default: 266 if (net_ratelimit()) 267 netdev_err(netdev, "Tx urb aborted (%d)\n", 268 urb->status); 269 case -EPROTO: 270 case -ENOENT: 271 case -ECONNRESET: 272 case -ESHUTDOWN: 273 274 break; 275 } 276 277 /* should always release echo skb and corresponding context */ 278 can_get_echo_skb(netdev, context->echo_index); 279 context->echo_index = PCAN_USB_MAX_TX_URBS; 280 281 /* do wakeup tx queue in case of success only */ 282 if (!urb->status) 283 netif_wake_queue(netdev); 284 } 285 286 /* 287 * called by netdev to send one skb on the CAN interface. 288 */ 289 static netdev_tx_t peak_usb_ndo_start_xmit(struct sk_buff *skb, 290 struct net_device *netdev) 291 { 292 struct peak_usb_device *dev = netdev_priv(netdev); 293 struct peak_tx_urb_context *context = NULL; 294 struct net_device_stats *stats = &netdev->stats; 295 struct canfd_frame *cfd = (struct canfd_frame *)skb->data; 296 struct urb *urb; 297 u8 *obuf; 298 int i, err; 299 size_t size = dev->adapter->tx_buffer_size; 300 301 if (can_dropped_invalid_skb(netdev, skb)) 302 return NETDEV_TX_OK; 303 304 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) 305 if (dev->tx_contexts[i].echo_index == PCAN_USB_MAX_TX_URBS) { 306 context = dev->tx_contexts + i; 307 break; 308 } 309 310 if (!context) { 311 /* should not occur except during restart */ 312 return NETDEV_TX_BUSY; 313 } 314 315 urb = context->urb; 316 obuf = urb->transfer_buffer; 317 318 err = dev->adapter->dev_encode_msg(dev, skb, obuf, &size); 319 if (err) { 320 if (net_ratelimit()) 321 netdev_err(netdev, "packet dropped\n"); 322 dev_kfree_skb(skb); 323 stats->tx_dropped++; 324 return NETDEV_TX_OK; 325 } 326 327 context->echo_index = i; 328 329 /* Note: this works with CANFD frames too */ 330 context->data_len = cfd->len; 331 332 usb_anchor_urb(urb, &dev->tx_submitted); 333 334 can_put_echo_skb(skb, netdev, context->echo_index); 335 336 atomic_inc(&dev->active_tx_urbs); 337 338 err = usb_submit_urb(urb, GFP_ATOMIC); 339 if (err) { 340 can_free_echo_skb(netdev, context->echo_index); 341 342 usb_unanchor_urb(urb); 343 344 /* this context is not used in fact */ 345 context->echo_index = PCAN_USB_MAX_TX_URBS; 346 347 atomic_dec(&dev->active_tx_urbs); 348 349 switch (err) { 350 case -ENODEV: 351 netif_device_detach(netdev); 352 break; 353 default: 354 netdev_warn(netdev, "tx urb submitting failed err=%d\n", 355 err); 356 case -ENOENT: 357 /* cable unplugged */ 358 stats->tx_dropped++; 359 } 360 } else { 361 netif_trans_update(netdev); 362 363 /* slow down tx path */ 364 if (atomic_read(&dev->active_tx_urbs) >= PCAN_USB_MAX_TX_URBS) 365 netif_stop_queue(netdev); 366 } 367 368 return NETDEV_TX_OK; 369 } 370 371 /* 372 * start the CAN interface. 373 * Rx and Tx urbs are allocated here. Rx urbs are submitted here. 374 */ 375 static int peak_usb_start(struct peak_usb_device *dev) 376 { 377 struct net_device *netdev = dev->netdev; 378 int err, i; 379 380 for (i = 0; i < PCAN_USB_MAX_RX_URBS; i++) { 381 struct urb *urb; 382 u8 *buf; 383 384 /* create a URB, and a buffer for it, to receive usb messages */ 385 urb = usb_alloc_urb(0, GFP_KERNEL); 386 if (!urb) { 387 err = -ENOMEM; 388 break; 389 } 390 391 buf = kmalloc(dev->adapter->rx_buffer_size, GFP_KERNEL); 392 if (!buf) { 393 usb_free_urb(urb); 394 err = -ENOMEM; 395 break; 396 } 397 398 usb_fill_bulk_urb(urb, dev->udev, 399 usb_rcvbulkpipe(dev->udev, dev->ep_msg_in), 400 buf, dev->adapter->rx_buffer_size, 401 peak_usb_read_bulk_callback, dev); 402 403 /* ask last usb_free_urb() to also kfree() transfer_buffer */ 404 urb->transfer_flags |= URB_FREE_BUFFER; 405 usb_anchor_urb(urb, &dev->rx_submitted); 406 407 err = usb_submit_urb(urb, GFP_KERNEL); 408 if (err) { 409 if (err == -ENODEV) 410 netif_device_detach(dev->netdev); 411 412 usb_unanchor_urb(urb); 413 kfree(buf); 414 usb_free_urb(urb); 415 break; 416 } 417 418 /* drop reference, USB core will take care of freeing it */ 419 usb_free_urb(urb); 420 } 421 422 /* did we submit any URBs? Warn if we was not able to submit all urbs */ 423 if (i < PCAN_USB_MAX_RX_URBS) { 424 if (i == 0) { 425 netdev_err(netdev, "couldn't setup any rx URB\n"); 426 return err; 427 } 428 429 netdev_warn(netdev, "rx performance may be slow\n"); 430 } 431 432 /* pre-alloc tx buffers and corresponding urbs */ 433 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) { 434 struct peak_tx_urb_context *context; 435 struct urb *urb; 436 u8 *buf; 437 438 /* create a URB and a buffer for it, to transmit usb messages */ 439 urb = usb_alloc_urb(0, GFP_KERNEL); 440 if (!urb) { 441 err = -ENOMEM; 442 break; 443 } 444 445 buf = kmalloc(dev->adapter->tx_buffer_size, GFP_KERNEL); 446 if (!buf) { 447 usb_free_urb(urb); 448 err = -ENOMEM; 449 break; 450 } 451 452 context = dev->tx_contexts + i; 453 context->dev = dev; 454 context->urb = urb; 455 456 usb_fill_bulk_urb(urb, dev->udev, 457 usb_sndbulkpipe(dev->udev, dev->ep_msg_out), 458 buf, dev->adapter->tx_buffer_size, 459 peak_usb_write_bulk_callback, context); 460 461 /* ask last usb_free_urb() to also kfree() transfer_buffer */ 462 urb->transfer_flags |= URB_FREE_BUFFER; 463 } 464 465 /* warn if we were not able to allocate enough tx contexts */ 466 if (i < PCAN_USB_MAX_TX_URBS) { 467 if (i == 0) { 468 netdev_err(netdev, "couldn't setup any tx URB\n"); 469 goto err_tx; 470 } 471 472 netdev_warn(netdev, "tx performance may be slow\n"); 473 } 474 475 if (dev->adapter->dev_start) { 476 err = dev->adapter->dev_start(dev); 477 if (err) 478 goto err_adapter; 479 } 480 481 dev->state |= PCAN_USB_STATE_STARTED; 482 483 /* can set bus on now */ 484 if (dev->adapter->dev_set_bus) { 485 err = dev->adapter->dev_set_bus(dev, 1); 486 if (err) 487 goto err_adapter; 488 } 489 490 dev->can.state = CAN_STATE_ERROR_ACTIVE; 491 492 return 0; 493 494 err_adapter: 495 if (err == -ENODEV) 496 netif_device_detach(dev->netdev); 497 498 netdev_warn(netdev, "couldn't submit control: %d\n", err); 499 500 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) { 501 usb_free_urb(dev->tx_contexts[i].urb); 502 dev->tx_contexts[i].urb = NULL; 503 } 504 err_tx: 505 usb_kill_anchored_urbs(&dev->rx_submitted); 506 507 return err; 508 } 509 510 /* 511 * called by netdev to open the corresponding CAN interface. 512 */ 513 static int peak_usb_ndo_open(struct net_device *netdev) 514 { 515 struct peak_usb_device *dev = netdev_priv(netdev); 516 int err; 517 518 /* common open */ 519 err = open_candev(netdev); 520 if (err) 521 return err; 522 523 /* finally start device */ 524 err = peak_usb_start(dev); 525 if (err) { 526 netdev_err(netdev, "couldn't start device: %d\n", err); 527 close_candev(netdev); 528 return err; 529 } 530 531 netif_start_queue(netdev); 532 533 return 0; 534 } 535 536 /* 537 * unlink in-flight Rx and Tx urbs and free their memory. 538 */ 539 static void peak_usb_unlink_all_urbs(struct peak_usb_device *dev) 540 { 541 int i; 542 543 /* free all Rx (submitted) urbs */ 544 usb_kill_anchored_urbs(&dev->rx_submitted); 545 546 /* free unsubmitted Tx urbs first */ 547 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) { 548 struct urb *urb = dev->tx_contexts[i].urb; 549 550 if (!urb || 551 dev->tx_contexts[i].echo_index != PCAN_USB_MAX_TX_URBS) { 552 /* 553 * this urb is already released or always submitted, 554 * let usb core free by itself 555 */ 556 continue; 557 } 558 559 usb_free_urb(urb); 560 dev->tx_contexts[i].urb = NULL; 561 } 562 563 /* then free all submitted Tx urbs */ 564 usb_kill_anchored_urbs(&dev->tx_submitted); 565 atomic_set(&dev->active_tx_urbs, 0); 566 } 567 568 /* 569 * called by netdev to close the corresponding CAN interface. 570 */ 571 static int peak_usb_ndo_stop(struct net_device *netdev) 572 { 573 struct peak_usb_device *dev = netdev_priv(netdev); 574 575 dev->state &= ~PCAN_USB_STATE_STARTED; 576 netif_stop_queue(netdev); 577 578 /* unlink all pending urbs and free used memory */ 579 peak_usb_unlink_all_urbs(dev); 580 581 if (dev->adapter->dev_stop) 582 dev->adapter->dev_stop(dev); 583 584 close_candev(netdev); 585 586 dev->can.state = CAN_STATE_STOPPED; 587 588 /* can set bus off now */ 589 if (dev->adapter->dev_set_bus) { 590 int err = dev->adapter->dev_set_bus(dev, 0); 591 if (err) 592 return err; 593 } 594 595 return 0; 596 } 597 598 /* 599 * handle end of waiting for the device to reset 600 */ 601 void peak_usb_restart_complete(struct peak_usb_device *dev) 602 { 603 /* finally MUST update can state */ 604 dev->can.state = CAN_STATE_ERROR_ACTIVE; 605 606 /* netdev queue can be awaken now */ 607 netif_wake_queue(dev->netdev); 608 } 609 610 void peak_usb_async_complete(struct urb *urb) 611 { 612 kfree(urb->transfer_buffer); 613 usb_free_urb(urb); 614 } 615 616 /* 617 * device (auto-)restart mechanism runs in a timer context => 618 * MUST handle restart with asynchronous usb transfers 619 */ 620 static int peak_usb_restart(struct peak_usb_device *dev) 621 { 622 struct urb *urb; 623 int err; 624 u8 *buf; 625 626 /* 627 * if device doesn't define any asynchronous restart handler, simply 628 * wake the netdev queue up 629 */ 630 if (!dev->adapter->dev_restart_async) { 631 peak_usb_restart_complete(dev); 632 return 0; 633 } 634 635 /* first allocate a urb to handle the asynchronous steps */ 636 urb = usb_alloc_urb(0, GFP_ATOMIC); 637 if (!urb) 638 return -ENOMEM; 639 640 /* also allocate enough space for the commands to send */ 641 buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_ATOMIC); 642 if (!buf) { 643 usb_free_urb(urb); 644 return -ENOMEM; 645 } 646 647 /* call the device specific handler for the restart */ 648 err = dev->adapter->dev_restart_async(dev, urb, buf); 649 if (!err) 650 return 0; 651 652 kfree(buf); 653 usb_free_urb(urb); 654 655 return err; 656 } 657 658 /* 659 * candev callback used to change CAN mode. 660 * Warning: this is called from a timer context! 661 */ 662 static int peak_usb_set_mode(struct net_device *netdev, enum can_mode mode) 663 { 664 struct peak_usb_device *dev = netdev_priv(netdev); 665 int err = 0; 666 667 switch (mode) { 668 case CAN_MODE_START: 669 err = peak_usb_restart(dev); 670 if (err) 671 netdev_err(netdev, "couldn't start device (err %d)\n", 672 err); 673 break; 674 675 default: 676 return -EOPNOTSUPP; 677 } 678 679 return err; 680 } 681 682 /* 683 * candev callback used to set device nominal/arbitration bitrate. 684 */ 685 static int peak_usb_set_bittiming(struct net_device *netdev) 686 { 687 struct peak_usb_device *dev = netdev_priv(netdev); 688 const struct peak_usb_adapter *pa = dev->adapter; 689 690 if (pa->dev_set_bittiming) { 691 struct can_bittiming *bt = &dev->can.bittiming; 692 int err = pa->dev_set_bittiming(dev, bt); 693 694 if (err) 695 netdev_info(netdev, "couldn't set bitrate (err %d)\n", 696 err); 697 return err; 698 } 699 700 return 0; 701 } 702 703 /* 704 * candev callback used to set device data bitrate. 705 */ 706 static int peak_usb_set_data_bittiming(struct net_device *netdev) 707 { 708 struct peak_usb_device *dev = netdev_priv(netdev); 709 const struct peak_usb_adapter *pa = dev->adapter; 710 711 if (pa->dev_set_data_bittiming) { 712 struct can_bittiming *bt = &dev->can.data_bittiming; 713 int err = pa->dev_set_data_bittiming(dev, bt); 714 715 if (err) 716 netdev_info(netdev, 717 "couldn't set data bitrate (err %d)\n", 718 err); 719 720 return err; 721 } 722 723 return 0; 724 } 725 726 static const struct net_device_ops peak_usb_netdev_ops = { 727 .ndo_open = peak_usb_ndo_open, 728 .ndo_stop = peak_usb_ndo_stop, 729 .ndo_start_xmit = peak_usb_ndo_start_xmit, 730 .ndo_change_mtu = can_change_mtu, 731 }; 732 733 /* 734 * create one device which is attached to CAN controller #ctrl_idx of the 735 * usb adapter. 736 */ 737 static int peak_usb_create_dev(const struct peak_usb_adapter *peak_usb_adapter, 738 struct usb_interface *intf, int ctrl_idx) 739 { 740 struct usb_device *usb_dev = interface_to_usbdev(intf); 741 int sizeof_candev = peak_usb_adapter->sizeof_dev_private; 742 struct peak_usb_device *dev; 743 struct net_device *netdev; 744 int i, err; 745 u16 tmp16; 746 747 if (sizeof_candev < sizeof(struct peak_usb_device)) 748 sizeof_candev = sizeof(struct peak_usb_device); 749 750 netdev = alloc_candev(sizeof_candev, PCAN_USB_MAX_TX_URBS); 751 if (!netdev) { 752 dev_err(&intf->dev, "%s: couldn't alloc candev\n", 753 PCAN_USB_DRIVER_NAME); 754 return -ENOMEM; 755 } 756 757 dev = netdev_priv(netdev); 758 759 /* allocate a buffer large enough to send commands */ 760 dev->cmd_buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_KERNEL); 761 if (!dev->cmd_buf) { 762 err = -ENOMEM; 763 goto lbl_free_candev; 764 } 765 766 dev->udev = usb_dev; 767 dev->netdev = netdev; 768 dev->adapter = peak_usb_adapter; 769 dev->ctrl_idx = ctrl_idx; 770 dev->state = PCAN_USB_STATE_CONNECTED; 771 772 dev->ep_msg_in = peak_usb_adapter->ep_msg_in; 773 dev->ep_msg_out = peak_usb_adapter->ep_msg_out[ctrl_idx]; 774 775 dev->can.clock = peak_usb_adapter->clock; 776 dev->can.bittiming_const = peak_usb_adapter->bittiming_const; 777 dev->can.do_set_bittiming = peak_usb_set_bittiming; 778 dev->can.data_bittiming_const = peak_usb_adapter->data_bittiming_const; 779 dev->can.do_set_data_bittiming = peak_usb_set_data_bittiming; 780 dev->can.do_set_mode = peak_usb_set_mode; 781 dev->can.do_get_berr_counter = peak_usb_adapter->do_get_berr_counter; 782 dev->can.ctrlmode_supported = peak_usb_adapter->ctrlmode_supported; 783 784 netdev->netdev_ops = &peak_usb_netdev_ops; 785 786 netdev->flags |= IFF_ECHO; /* we support local echo */ 787 788 init_usb_anchor(&dev->rx_submitted); 789 790 init_usb_anchor(&dev->tx_submitted); 791 atomic_set(&dev->active_tx_urbs, 0); 792 793 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) 794 dev->tx_contexts[i].echo_index = PCAN_USB_MAX_TX_URBS; 795 796 dev->prev_siblings = usb_get_intfdata(intf); 797 usb_set_intfdata(intf, dev); 798 799 SET_NETDEV_DEV(netdev, &intf->dev); 800 netdev->dev_id = ctrl_idx; 801 802 err = register_candev(netdev); 803 if (err) { 804 dev_err(&intf->dev, "couldn't register CAN device: %d\n", err); 805 goto lbl_restore_intf_data; 806 } 807 808 if (dev->prev_siblings) 809 (dev->prev_siblings)->next_siblings = dev; 810 811 /* keep hw revision into the netdevice */ 812 tmp16 = le16_to_cpu(usb_dev->descriptor.bcdDevice); 813 dev->device_rev = tmp16 >> 8; 814 815 if (dev->adapter->dev_init) { 816 err = dev->adapter->dev_init(dev); 817 if (err) 818 goto lbl_unregister_candev; 819 } 820 821 /* set bus off */ 822 if (dev->adapter->dev_set_bus) { 823 err = dev->adapter->dev_set_bus(dev, 0); 824 if (err) 825 goto lbl_unregister_candev; 826 } 827 828 /* get device number early */ 829 if (dev->adapter->dev_get_device_id) 830 dev->adapter->dev_get_device_id(dev, &dev->device_number); 831 832 netdev_info(netdev, "attached to %s channel %u (device %u)\n", 833 peak_usb_adapter->name, ctrl_idx, dev->device_number); 834 835 return 0; 836 837 lbl_unregister_candev: 838 unregister_candev(netdev); 839 840 lbl_restore_intf_data: 841 usb_set_intfdata(intf, dev->prev_siblings); 842 kfree(dev->cmd_buf); 843 844 lbl_free_candev: 845 free_candev(netdev); 846 847 return err; 848 } 849 850 /* 851 * called by the usb core when the device is unplugged from the system 852 */ 853 static void peak_usb_disconnect(struct usb_interface *intf) 854 { 855 struct peak_usb_device *dev; 856 struct peak_usb_device *dev_prev_siblings; 857 858 /* unregister as many netdev devices as siblings */ 859 for (dev = usb_get_intfdata(intf); dev; dev = dev_prev_siblings) { 860 struct net_device *netdev = dev->netdev; 861 char name[IFNAMSIZ]; 862 863 dev_prev_siblings = dev->prev_siblings; 864 dev->state &= ~PCAN_USB_STATE_CONNECTED; 865 strncpy(name, netdev->name, IFNAMSIZ); 866 867 unregister_netdev(netdev); 868 869 kfree(dev->cmd_buf); 870 dev->next_siblings = NULL; 871 if (dev->adapter->dev_free) 872 dev->adapter->dev_free(dev); 873 874 free_candev(netdev); 875 dev_info(&intf->dev, "%s removed\n", name); 876 } 877 878 usb_set_intfdata(intf, NULL); 879 } 880 881 /* 882 * probe function for new PEAK-System devices 883 */ 884 static int peak_usb_probe(struct usb_interface *intf, 885 const struct usb_device_id *id) 886 { 887 struct usb_device *usb_dev = interface_to_usbdev(intf); 888 const u16 usb_id_product = le16_to_cpu(usb_dev->descriptor.idProduct); 889 const struct peak_usb_adapter *peak_usb_adapter = NULL; 890 int i, err = -ENOMEM; 891 892 /* get corresponding PCAN-USB adapter */ 893 for (i = 0; i < ARRAY_SIZE(peak_usb_adapters_list); i++) 894 if (peak_usb_adapters_list[i]->device_id == usb_id_product) { 895 peak_usb_adapter = peak_usb_adapters_list[i]; 896 break; 897 } 898 899 if (!peak_usb_adapter) { 900 /* should never come except device_id bad usage in this file */ 901 pr_err("%s: didn't find device id. 0x%x in devices list\n", 902 PCAN_USB_DRIVER_NAME, usb_id_product); 903 return -ENODEV; 904 } 905 906 /* got corresponding adapter: check if it handles current interface */ 907 if (peak_usb_adapter->intf_probe) { 908 err = peak_usb_adapter->intf_probe(intf); 909 if (err) 910 return err; 911 } 912 913 for (i = 0; i < peak_usb_adapter->ctrl_count; i++) { 914 err = peak_usb_create_dev(peak_usb_adapter, intf, i); 915 if (err) { 916 /* deregister already created devices */ 917 peak_usb_disconnect(intf); 918 break; 919 } 920 } 921 922 return err; 923 } 924 925 /* usb specific object needed to register this driver with the usb subsystem */ 926 static struct usb_driver peak_usb_driver = { 927 .name = PCAN_USB_DRIVER_NAME, 928 .disconnect = peak_usb_disconnect, 929 .probe = peak_usb_probe, 930 .id_table = peak_usb_table, 931 }; 932 933 static int __init peak_usb_init(void) 934 { 935 int err; 936 937 /* register this driver with the USB subsystem */ 938 err = usb_register(&peak_usb_driver); 939 if (err) 940 pr_err("%s: usb_register failed (err %d)\n", 941 PCAN_USB_DRIVER_NAME, err); 942 943 return err; 944 } 945 946 static int peak_usb_do_device_exit(struct device *d, void *arg) 947 { 948 struct usb_interface *intf = to_usb_interface(d); 949 struct peak_usb_device *dev; 950 951 /* stop as many netdev devices as siblings */ 952 for (dev = usb_get_intfdata(intf); dev; dev = dev->prev_siblings) { 953 struct net_device *netdev = dev->netdev; 954 955 if (netif_device_present(netdev)) 956 if (dev->adapter->dev_exit) 957 dev->adapter->dev_exit(dev); 958 } 959 960 return 0; 961 } 962 963 static void __exit peak_usb_exit(void) 964 { 965 int err; 966 967 /* last chance do send any synchronous commands here */ 968 err = driver_for_each_device(&peak_usb_driver.drvwrap.driver, NULL, 969 NULL, peak_usb_do_device_exit); 970 if (err) 971 pr_err("%s: failed to stop all can devices (err %d)\n", 972 PCAN_USB_DRIVER_NAME, err); 973 974 /* deregister this driver with the USB subsystem */ 975 usb_deregister(&peak_usb_driver); 976 977 pr_info("%s: PCAN-USB interfaces driver unloaded\n", 978 PCAN_USB_DRIVER_NAME); 979 } 980 981 module_init(peak_usb_init); 982 module_exit(peak_usb_exit); 983