aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/can/usb
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-01-17 16:15:55 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-01-17 16:15:55 -0500
commit8dea78da5cee153b8af9c07a2745f6c55057fe12 (patch)
treea8f4d49d63b1ecc92f2fddceba0655b2472c5bd9 /drivers/net/can/usb
parent406089d01562f1e2bf9f089fd7637009ebaad589 (diff)
Patched in Tegra support.
Diffstat (limited to 'drivers/net/can/usb')
-rw-r--r--drivers/net/can/usb/Kconfig35
-rw-r--r--drivers/net/can/usb/Makefile2
-rw-r--r--drivers/net/can/usb/ems_usb.c107
-rw-r--r--drivers/net/can/usb/esd_usb2.c97
-rw-r--r--drivers/net/can/usb/kvaser_usb.c1627
-rw-r--r--drivers/net/can/usb/peak_usb/Makefile2
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb.c903
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb_core.c946
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb_core.h145
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb_pro.c1042
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb_pro.h178
11 files changed, 132 insertions, 4952 deletions
diff --git a/drivers/net/can/usb/Kconfig b/drivers/net/can/usb/Kconfig
index a4e4bee3571..04525495b15 100644
--- a/drivers/net/can/usb/Kconfig
+++ b/drivers/net/can/usb/Kconfig
@@ -13,39 +13,4 @@ config CAN_ESD_USB2
13 This driver supports the CAN-USB/2 interface 13 This driver supports the CAN-USB/2 interface
14 from esd electronic system design gmbh (http://www.esd.eu). 14 from esd electronic system design gmbh (http://www.esd.eu).
15 15
16config CAN_KVASER_USB
17 tristate "Kvaser CAN/USB interface"
18 ---help---
19 This driver adds support for Kvaser CAN/USB devices like Kvaser
20 Leaf Light.
21
22 The driver gives support for the following devices:
23 - Kvaser Leaf Light
24 - Kvaser Leaf Professional HS
25 - Kvaser Leaf SemiPro HS
26 - Kvaser Leaf Professional LS
27 - Kvaser Leaf Professional SWC
28 - Kvaser Leaf Professional LIN
29 - Kvaser Leaf SemiPro LS
30 - Kvaser Leaf SemiPro SWC
31 - Kvaser Memorator II HS/HS
32 - Kvaser USBcan Professional HS/HS
33 - Kvaser Leaf Light GI
34 - Kvaser Leaf Professional HS (OBD-II connector)
35 - Kvaser Memorator Professional HS/LS
36 - Kvaser Leaf Light "China"
37 - Kvaser BlackBird SemiPro
38 - Kvaser USBcan R
39
40 If unsure, say N.
41
42 To compile this driver as a module, choose M here: the
43 module will be called kvaser_usb.
44
45config CAN_PEAK_USB
46 tristate "PEAK PCAN-USB/USB Pro interfaces"
47 ---help---
48 This driver supports the PCAN-USB and PCAN-USB Pro adapters
49 from PEAK-System Technik (http://www.peak-system.com).
50
51endmenu 16endmenu
diff --git a/drivers/net/can/usb/Makefile b/drivers/net/can/usb/Makefile
index 80a2ee41fd6..fce3cf11719 100644
--- a/drivers/net/can/usb/Makefile
+++ b/drivers/net/can/usb/Makefile
@@ -4,7 +4,5 @@
4 4
5obj-$(CONFIG_CAN_EMS_USB) += ems_usb.o 5obj-$(CONFIG_CAN_EMS_USB) += ems_usb.o
6obj-$(CONFIG_CAN_ESD_USB2) += esd_usb2.o 6obj-$(CONFIG_CAN_ESD_USB2) += esd_usb2.o
7obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb.o
8obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/
9 7
10ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG 8ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG
diff --git a/drivers/net/can/usb/ems_usb.c b/drivers/net/can/usb/ems_usb.c
index c69f0b72b35..a72c7bfb409 100644
--- a/drivers/net/can/usb/ems_usb.c
+++ b/drivers/net/can/usb/ems_usb.c
@@ -245,6 +245,7 @@ struct ems_tx_urb_context {
245 245
246struct ems_usb { 246struct ems_usb {
247 struct can_priv can; /* must be the first member */ 247 struct can_priv can; /* must be the first member */
248 int open_time;
248 249
249 struct sk_buff *echo_skb[MAX_TX_URBS]; 250 struct sk_buff *echo_skb[MAX_TX_URBS];
250 251
@@ -287,7 +288,8 @@ static void ems_usb_read_interrupt_callback(struct urb *urb)
287 return; 288 return;
288 289
289 default: 290 default:
290 netdev_info(netdev, "Rx interrupt aborted %d\n", urb->status); 291 dev_info(netdev->dev.parent, "Rx interrupt aborted %d\n",
292 urb->status);
291 break; 293 break;
292 } 294 }
293 295
@@ -296,7 +298,8 @@ static void ems_usb_read_interrupt_callback(struct urb *urb)
296 if (err == -ENODEV) 298 if (err == -ENODEV)
297 netif_device_detach(netdev); 299 netif_device_detach(netdev);
298 else if (err) 300 else if (err)
299 netdev_err(netdev, "failed resubmitting intr urb: %d\n", err); 301 dev_err(netdev->dev.parent,
302 "failed resubmitting intr urb: %d\n", err);
300} 303}
301 304
302static void ems_usb_rx_can_msg(struct ems_usb *dev, struct ems_cpc_msg *msg) 305static void ems_usb_rx_can_msg(struct ems_usb *dev, struct ems_cpc_msg *msg)
@@ -428,7 +431,8 @@ static void ems_usb_read_bulk_callback(struct urb *urb)
428 return; 431 return;
429 432
430 default: 433 default:
431 netdev_info(netdev, "Rx URB aborted (%d)\n", urb->status); 434 dev_info(netdev->dev.parent, "Rx URB aborted (%d)\n",
435 urb->status);
432 goto resubmit_urb; 436 goto resubmit_urb;
433 } 437 }
434 438
@@ -473,7 +477,7 @@ static void ems_usb_read_bulk_callback(struct urb *urb)
473 msg_count--; 477 msg_count--;
474 478
475 if (start > urb->transfer_buffer_length) { 479 if (start > urb->transfer_buffer_length) {
476 netdev_err(netdev, "format error\n"); 480 dev_err(netdev->dev.parent, "format error\n");
477 break; 481 break;
478 } 482 }
479 } 483 }
@@ -489,8 +493,8 @@ resubmit_urb:
489 if (retval == -ENODEV) 493 if (retval == -ENODEV)
490 netif_device_detach(netdev); 494 netif_device_detach(netdev);
491 else if (retval) 495 else if (retval)
492 netdev_err(netdev, 496 dev_err(netdev->dev.parent,
493 "failed resubmitting read bulk urb: %d\n", retval); 497 "failed resubmitting read bulk urb: %d\n", retval);
494} 498}
495 499
496/* 500/*
@@ -517,7 +521,8 @@ static void ems_usb_write_bulk_callback(struct urb *urb)
517 return; 521 return;
518 522
519 if (urb->status) 523 if (urb->status)
520 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status); 524 dev_info(netdev->dev.parent, "Tx URB aborted (%d)\n",
525 urb->status);
521 526
522 netdev->trans_start = jiffies; 527 netdev->trans_start = jiffies;
523 528
@@ -600,18 +605,18 @@ static int ems_usb_start(struct ems_usb *dev)
600 /* create a URB, and a buffer for it */ 605 /* create a URB, and a buffer for it */
601 urb = usb_alloc_urb(0, GFP_KERNEL); 606 urb = usb_alloc_urb(0, GFP_KERNEL);
602 if (!urb) { 607 if (!urb) {
603 netdev_err(netdev, "No memory left for URBs\n"); 608 dev_err(netdev->dev.parent,
604 err = -ENOMEM; 609 "No memory left for URBs\n");
605 break; 610 return -ENOMEM;
606 } 611 }
607 612
608 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL, 613 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
609 &urb->transfer_dma); 614 &urb->transfer_dma);
610 if (!buf) { 615 if (!buf) {
611 netdev_err(netdev, "No memory left for USB buffer\n"); 616 dev_err(netdev->dev.parent,
617 "No memory left for USB buffer\n");
612 usb_free_urb(urb); 618 usb_free_urb(urb);
613 err = -ENOMEM; 619 return -ENOMEM;
614 break;
615 } 620 }
616 621
617 usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2), 622 usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2),
@@ -622,6 +627,9 @@ static int ems_usb_start(struct ems_usb *dev)
622 627
623 err = usb_submit_urb(urb, GFP_KERNEL); 628 err = usb_submit_urb(urb, GFP_KERNEL);
624 if (err) { 629 if (err) {
630 if (err == -ENODEV)
631 netif_device_detach(dev->netdev);
632
625 usb_unanchor_urb(urb); 633 usb_unanchor_urb(urb);
626 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf, 634 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
627 urb->transfer_dma); 635 urb->transfer_dma);
@@ -634,13 +642,13 @@ static int ems_usb_start(struct ems_usb *dev)
634 642
635 /* Did we submit any URBs */ 643 /* Did we submit any URBs */
636 if (i == 0) { 644 if (i == 0) {
637 netdev_warn(netdev, "couldn't setup read URBs\n"); 645 dev_warn(netdev->dev.parent, "couldn't setup read URBs\n");
638 return err; 646 return err;
639 } 647 }
640 648
641 /* Warn if we've couldn't transmit all the URBs */ 649 /* Warn if we've couldn't transmit all the URBs */
642 if (i < MAX_RX_URBS) 650 if (i < MAX_RX_URBS)
643 netdev_warn(netdev, "rx performance may be slow\n"); 651 dev_warn(netdev->dev.parent, "rx performance may be slow\n");
644 652
645 /* Setup and start interrupt URB */ 653 /* Setup and start interrupt URB */
646 usb_fill_int_urb(dev->intr_urb, dev->udev, 654 usb_fill_int_urb(dev->intr_urb, dev->udev,
@@ -651,7 +659,11 @@ static int ems_usb_start(struct ems_usb *dev)
651 659
652 err = usb_submit_urb(dev->intr_urb, GFP_KERNEL); 660 err = usb_submit_urb(dev->intr_urb, GFP_KERNEL);
653 if (err) { 661 if (err) {
654 netdev_warn(netdev, "intr URB submit failed: %d\n", err); 662 if (err == -ENODEV)
663 netif_device_detach(dev->netdev);
664
665 dev_warn(netdev->dev.parent, "intr URB submit failed: %d\n",
666 err);
655 667
656 return err; 668 return err;
657 } 669 }
@@ -680,7 +692,10 @@ static int ems_usb_start(struct ems_usb *dev)
680 return 0; 692 return 0;
681 693
682failed: 694failed:
683 netdev_warn(netdev, "couldn't submit control: %d\n", err); 695 if (err == -ENODEV)
696 netif_device_detach(dev->netdev);
697
698 dev_warn(netdev->dev.parent, "couldn't submit control: %d\n", err);
684 699
685 return err; 700 return err;
686} 701}
@@ -720,13 +735,15 @@ static int ems_usb_open(struct net_device *netdev)
720 if (err == -ENODEV) 735 if (err == -ENODEV)
721 netif_device_detach(dev->netdev); 736 netif_device_detach(dev->netdev);
722 737
723 netdev_warn(netdev, "couldn't start device: %d\n", err); 738 dev_warn(netdev->dev.parent, "couldn't start device: %d\n",
739 err);
724 740
725 close_candev(netdev); 741 close_candev(netdev);
726 742
727 return err; 743 return err;
728 } 744 }
729 745
746 dev->open_time = jiffies;
730 747
731 netif_start_queue(netdev); 748 netif_start_queue(netdev);
732 749
@@ -752,13 +769,13 @@ static netdev_tx_t ems_usb_start_xmit(struct sk_buff *skb, struct net_device *ne
752 /* create a URB, and a buffer for it, and copy the data to the URB */ 769 /* create a URB, and a buffer for it, and copy the data to the URB */
753 urb = usb_alloc_urb(0, GFP_ATOMIC); 770 urb = usb_alloc_urb(0, GFP_ATOMIC);
754 if (!urb) { 771 if (!urb) {
755 netdev_err(netdev, "No memory left for URBs\n"); 772 dev_err(netdev->dev.parent, "No memory left for URBs\n");
756 goto nomem; 773 goto nomem;
757 } 774 }
758 775
759 buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC, &urb->transfer_dma); 776 buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC, &urb->transfer_dma);
760 if (!buf) { 777 if (!buf) {
761 netdev_err(netdev, "No memory left for USB buffer\n"); 778 dev_err(netdev->dev.parent, "No memory left for USB buffer\n");
762 usb_free_urb(urb); 779 usb_free_urb(urb);
763 goto nomem; 780 goto nomem;
764 } 781 }
@@ -801,7 +818,7 @@ static netdev_tx_t ems_usb_start_xmit(struct sk_buff *skb, struct net_device *ne
801 usb_unanchor_urb(urb); 818 usb_unanchor_urb(urb);
802 usb_free_coherent(dev->udev, size, buf, urb->transfer_dma); 819 usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
803 820
804 netdev_warn(netdev, "couldn't find free context\n"); 821 dev_warn(netdev->dev.parent, "couldn't find free context\n");
805 822
806 return NETDEV_TX_BUSY; 823 return NETDEV_TX_BUSY;
807 } 824 }
@@ -832,7 +849,7 @@ static netdev_tx_t ems_usb_start_xmit(struct sk_buff *skb, struct net_device *ne
832 if (err == -ENODEV) { 849 if (err == -ENODEV) {
833 netif_device_detach(netdev); 850 netif_device_detach(netdev);
834 } else { 851 } else {
835 netdev_warn(netdev, "failed tx_urb %d\n", err); 852 dev_warn(netdev->dev.parent, "failed tx_urb %d\n", err);
836 853
837 stats->tx_dropped++; 854 stats->tx_dropped++;
838 } 855 }
@@ -872,10 +889,12 @@ static int ems_usb_close(struct net_device *netdev)
872 889
873 /* Set CAN controller to reset mode */ 890 /* Set CAN controller to reset mode */
874 if (ems_usb_write_mode(dev, SJA1000_MOD_RM)) 891 if (ems_usb_write_mode(dev, SJA1000_MOD_RM))
875 netdev_warn(netdev, "couldn't stop device"); 892 dev_warn(netdev->dev.parent, "couldn't stop device");
876 893
877 close_candev(netdev); 894 close_candev(netdev);
878 895
896 dev->open_time = 0;
897
879 return 0; 898 return 0;
880} 899}
881 900
@@ -885,7 +904,7 @@ static const struct net_device_ops ems_usb_netdev_ops = {
885 .ndo_start_xmit = ems_usb_start_xmit, 904 .ndo_start_xmit = ems_usb_start_xmit,
886}; 905};
887 906
888static const struct can_bittiming_const ems_usb_bittiming_const = { 907static struct can_bittiming_const ems_usb_bittiming_const = {
889 .name = "ems_usb", 908 .name = "ems_usb",
890 .tseg1_min = 1, 909 .tseg1_min = 1,
891 .tseg1_max = 16, 910 .tseg1_max = 16,
@@ -901,10 +920,13 @@ static int ems_usb_set_mode(struct net_device *netdev, enum can_mode mode)
901{ 920{
902 struct ems_usb *dev = netdev_priv(netdev); 921 struct ems_usb *dev = netdev_priv(netdev);
903 922
923 if (!dev->open_time)
924 return -EINVAL;
925
904 switch (mode) { 926 switch (mode) {
905 case CAN_MODE_START: 927 case CAN_MODE_START:
906 if (ems_usb_write_mode(dev, SJA1000_MOD_NORMAL)) 928 if (ems_usb_write_mode(dev, SJA1000_MOD_NORMAL))
907 netdev_warn(netdev, "couldn't start device"); 929 dev_warn(netdev->dev.parent, "couldn't start device");
908 930
909 if (netif_queue_stopped(netdev)) 931 if (netif_queue_stopped(netdev))
910 netif_wake_queue(netdev); 932 netif_wake_queue(netdev);
@@ -929,7 +951,8 @@ static int ems_usb_set_bittiming(struct net_device *netdev)
929 if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) 951 if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
930 btr1 |= 0x80; 952 btr1 |= 0x80;
931 953
932 netdev_info(netdev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1); 954 dev_info(netdev->dev.parent, "setting BTR0=0x%02x BTR1=0x%02x\n",
955 btr0, btr1);
933 956
934 dev->active_params.msg.can_params.cc_params.sja1000.btr0 = btr0; 957 dev->active_params.msg.can_params.cc_params.sja1000.btr0 = btr0;
935 dev->active_params.msg.can_params.cc_params.sja1000.btr1 = btr1; 958 dev->active_params.msg.can_params.cc_params.sja1000.btr1 = btr1;
@@ -1034,13 +1057,15 @@ static int ems_usb_probe(struct usb_interface *intf,
1034 1057
1035 err = ems_usb_command_msg(dev, &dev->active_params); 1058 err = ems_usb_command_msg(dev, &dev->active_params);
1036 if (err) { 1059 if (err) {
1037 netdev_err(netdev, "couldn't initialize controller: %d\n", err); 1060 dev_err(netdev->dev.parent,
1061 "couldn't initialize controller: %d\n", err);
1038 goto cleanup_tx_msg_buffer; 1062 goto cleanup_tx_msg_buffer;
1039 } 1063 }
1040 1064
1041 err = register_candev(netdev); 1065 err = register_candev(netdev);
1042 if (err) { 1066 if (err) {
1043 netdev_err(netdev, "couldn't register CAN device: %d\n", err); 1067 dev_err(netdev->dev.parent,
1068 "couldn't register CAN device: %d\n", err);
1044 goto cleanup_tx_msg_buffer; 1069 goto cleanup_tx_msg_buffer;
1045 } 1070 }
1046 1071
@@ -1090,4 +1115,28 @@ static struct usb_driver ems_usb_driver = {
1090 .id_table = ems_usb_table, 1115 .id_table = ems_usb_table,
1091}; 1116};
1092 1117
1093module_usb_driver(ems_usb_driver); 1118static int __init ems_usb_init(void)
1119{
1120 int err;
1121
1122 printk(KERN_INFO "CPC-USB kernel driver loaded\n");
1123
1124 /* register this driver with the USB subsystem */
1125 err = usb_register(&ems_usb_driver);
1126
1127 if (err) {
1128 err("usb_register failed. Error number %d\n", err);
1129 return err;
1130 }
1131
1132 return 0;
1133}
1134
1135static void __exit ems_usb_exit(void)
1136{
1137 /* deregister this driver with the USB subsystem */
1138 usb_deregister(&ems_usb_driver);
1139}
1140
1141module_init(ems_usb_init);
1142module_exit(ems_usb_exit);
diff --git a/drivers/net/can/usb/esd_usb2.c b/drivers/net/can/usb/esd_usb2.c
index 9b74d1e3ad4..eb8b0e60028 100644
--- a/drivers/net/can/usb/esd_usb2.c
+++ b/drivers/net/can/usb/esd_usb2.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * CAN driver for esd CAN-USB/2 and CAN-USB/Micro 2 * CAN driver for esd CAN-USB/2
3 * 3 *
4 * Copyright (C) 2010-2012 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh 4 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify it 6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published 7 * under the terms of the GNU General Public License as published
@@ -28,16 +28,14 @@
28#include <linux/can/error.h> 28#include <linux/can/error.h>
29 29
30MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd.eu>"); 30MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd.eu>");
31MODULE_DESCRIPTION("CAN driver for esd CAN-USB/2 and CAN-USB/Micro interfaces"); 31MODULE_DESCRIPTION("CAN driver for esd CAN-USB/2 interfaces");
32MODULE_LICENSE("GPL v2"); 32MODULE_LICENSE("GPL v2");
33 33
34/* Define these values to match your devices */ 34/* Define these values to match your devices */
35#define USB_ESDGMBH_VENDOR_ID 0x0ab4 35#define USB_ESDGMBH_VENDOR_ID 0x0ab4
36#define USB_CANUSB2_PRODUCT_ID 0x0010 36#define USB_CANUSB2_PRODUCT_ID 0x0010
37#define USB_CANUSBM_PRODUCT_ID 0x0011
38 37
39#define ESD_USB2_CAN_CLOCK 60000000 38#define ESD_USB2_CAN_CLOCK 60000000
40#define ESD_USBM_CAN_CLOCK 36000000
41#define ESD_USB2_MAX_NETS 2 39#define ESD_USB2_MAX_NETS 2
42 40
43/* USB2 commands */ 41/* USB2 commands */
@@ -71,7 +69,6 @@ MODULE_LICENSE("GPL v2");
71#define ESD_USB2_TSEG2_SHIFT 20 69#define ESD_USB2_TSEG2_SHIFT 20
72#define ESD_USB2_SJW_MAX 4 70#define ESD_USB2_SJW_MAX 4
73#define ESD_USB2_SJW_SHIFT 14 71#define ESD_USB2_SJW_SHIFT 14
74#define ESD_USBM_SJW_SHIFT 24
75#define ESD_USB2_BRP_MIN 1 72#define ESD_USB2_BRP_MIN 1
76#define ESD_USB2_BRP_MAX 1024 73#define ESD_USB2_BRP_MAX 1024
77#define ESD_USB2_BRP_INC 1 74#define ESD_USB2_BRP_INC 1
@@ -186,7 +183,6 @@ struct __attribute__ ((packed)) esd_usb2_msg {
186 183
187static struct usb_device_id esd_usb2_table[] = { 184static struct usb_device_id esd_usb2_table[] = {
188 {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)}, 185 {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)},
189 {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSBM_PRODUCT_ID)},
190 {} 186 {}
191}; 187};
192MODULE_DEVICE_TABLE(usb, esd_usb2_table); 188MODULE_DEVICE_TABLE(usb, esd_usb2_table);
@@ -217,6 +213,7 @@ struct esd_usb2_net_priv {
217 struct usb_anchor tx_submitted; 213 struct usb_anchor tx_submitted;
218 struct esd_tx_urb_context tx_contexts[MAX_TX_URBS]; 214 struct esd_tx_urb_context tx_contexts[MAX_TX_URBS];
219 215
216 int open_time;
220 struct esd_usb2 *usb2; 217 struct esd_usb2 *usb2;
221 struct net_device *netdev; 218 struct net_device *netdev;
222 int index; 219 int index;
@@ -473,7 +470,8 @@ static void esd_usb2_write_bulk_callback(struct urb *urb)
473 return; 470 return;
474 471
475 if (urb->status) 472 if (urb->status)
476 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status); 473 dev_info(netdev->dev.parent, "Tx URB aborted (%d)\n",
474 urb->status);
477 475
478 netdev->trans_start = jiffies; 476 netdev->trans_start = jiffies;
479} 477}
@@ -653,7 +651,7 @@ failed:
653 if (err == -ENODEV) 651 if (err == -ENODEV)
654 netif_device_detach(netdev); 652 netif_device_detach(netdev);
655 653
656 netdev_err(netdev, "couldn't start device: %d\n", err); 654 dev_err(netdev->dev.parent, "couldn't start device: %d\n", err);
657 655
658 return err; 656 return err;
659} 657}
@@ -689,11 +687,14 @@ static int esd_usb2_open(struct net_device *netdev)
689 /* finally start device */ 687 /* finally start device */
690 err = esd_usb2_start(priv); 688 err = esd_usb2_start(priv);
691 if (err) { 689 if (err) {
692 netdev_warn(netdev, "couldn't start device: %d\n", err); 690 dev_warn(netdev->dev.parent,
691 "couldn't start device: %d\n", err);
693 close_candev(netdev); 692 close_candev(netdev);
694 return err; 693 return err;
695 } 694 }
696 695
696 priv->open_time = jiffies;
697
697 netif_start_queue(netdev); 698 netif_start_queue(netdev);
698 699
699 return 0; 700 return 0;
@@ -720,7 +721,7 @@ static netdev_tx_t esd_usb2_start_xmit(struct sk_buff *skb,
720 /* create a URB, and a buffer for it, and copy the data to the URB */ 721 /* create a URB, and a buffer for it, and copy the data to the URB */
721 urb = usb_alloc_urb(0, GFP_ATOMIC); 722 urb = usb_alloc_urb(0, GFP_ATOMIC);
722 if (!urb) { 723 if (!urb) {
723 netdev_err(netdev, "No memory left for URBs\n"); 724 dev_err(netdev->dev.parent, "No memory left for URBs\n");
724 stats->tx_dropped++; 725 stats->tx_dropped++;
725 dev_kfree_skb(skb); 726 dev_kfree_skb(skb);
726 goto nourbmem; 727 goto nourbmem;
@@ -729,7 +730,7 @@ static netdev_tx_t esd_usb2_start_xmit(struct sk_buff *skb,
729 buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC, 730 buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC,
730 &urb->transfer_dma); 731 &urb->transfer_dma);
731 if (!buf) { 732 if (!buf) {
732 netdev_err(netdev, "No memory left for USB buffer\n"); 733 dev_err(netdev->dev.parent, "No memory left for USB buffer\n");
733 stats->tx_dropped++; 734 stats->tx_dropped++;
734 dev_kfree_skb(skb); 735 dev_kfree_skb(skb);
735 goto nobufmem; 736 goto nobufmem;
@@ -765,7 +766,7 @@ static netdev_tx_t esd_usb2_start_xmit(struct sk_buff *skb,
765 * This may never happen. 766 * This may never happen.
766 */ 767 */
767 if (!context) { 768 if (!context) {
768 netdev_warn(netdev, "couldn't find free context\n"); 769 dev_warn(netdev->dev.parent, "couldn't find free context\n");
769 ret = NETDEV_TX_BUSY; 770 ret = NETDEV_TX_BUSY;
770 goto releasebuf; 771 goto releasebuf;
771 } 772 }
@@ -805,7 +806,7 @@ static netdev_tx_t esd_usb2_start_xmit(struct sk_buff *skb,
805 if (err == -ENODEV) 806 if (err == -ENODEV)
806 netif_device_detach(netdev); 807 netif_device_detach(netdev);
807 else 808 else
808 netdev_warn(netdev, "failed tx_urb %d\n", err); 809 dev_warn(netdev->dev.parent, "failed tx_urb %d\n", err);
809 810
810 goto releasebuf; 811 goto releasebuf;
811 } 812 }
@@ -844,7 +845,7 @@ static int esd_usb2_close(struct net_device *netdev)
844 for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++) 845 for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++)
845 msg.msg.filter.mask[i] = 0; 846 msg.msg.filter.mask[i] = 0;
846 if (esd_usb2_send_msg(priv->usb2, &msg) < 0) 847 if (esd_usb2_send_msg(priv->usb2, &msg) < 0)
847 netdev_err(netdev, "sending idadd message failed\n"); 848 dev_err(netdev->dev.parent, "sending idadd message failed\n");
848 849
849 /* set CAN controller to reset mode */ 850 /* set CAN controller to reset mode */
850 msg.msg.hdr.len = 2; 851 msg.msg.hdr.len = 2;
@@ -853,7 +854,7 @@ static int esd_usb2_close(struct net_device *netdev)
853 msg.msg.setbaud.rsvd = 0; 854 msg.msg.setbaud.rsvd = 0;
854 msg.msg.setbaud.baud = cpu_to_le32(ESD_USB2_NO_BAUDRATE); 855 msg.msg.setbaud.baud = cpu_to_le32(ESD_USB2_NO_BAUDRATE);
855 if (esd_usb2_send_msg(priv->usb2, &msg) < 0) 856 if (esd_usb2_send_msg(priv->usb2, &msg) < 0)
856 netdev_err(netdev, "sending setbaud message failed\n"); 857 dev_err(netdev->dev.parent, "sending setbaud message failed\n");
857 858
858 priv->can.state = CAN_STATE_STOPPED; 859 priv->can.state = CAN_STATE_STOPPED;
859 860
@@ -861,6 +862,8 @@ static int esd_usb2_close(struct net_device *netdev)
861 862
862 close_candev(netdev); 863 close_candev(netdev);
863 864
865 priv->open_time = 0;
866
864 return 0; 867 return 0;
865} 868}
866 869
@@ -870,7 +873,7 @@ static const struct net_device_ops esd_usb2_netdev_ops = {
870 .ndo_start_xmit = esd_usb2_start_xmit, 873 .ndo_start_xmit = esd_usb2_start_xmit,
871}; 874};
872 875
873static const struct can_bittiming_const esd_usb2_bittiming_const = { 876static struct can_bittiming_const esd_usb2_bittiming_const = {
874 .name = "esd_usb2", 877 .name = "esd_usb2",
875 .tseg1_min = ESD_USB2_TSEG1_MIN, 878 .tseg1_min = ESD_USB2_TSEG1_MIN,
876 .tseg1_max = ESD_USB2_TSEG1_MAX, 879 .tseg1_max = ESD_USB2_TSEG1_MAX,
@@ -888,22 +891,11 @@ static int esd_usb2_set_bittiming(struct net_device *netdev)
888 struct can_bittiming *bt = &priv->can.bittiming; 891 struct can_bittiming *bt = &priv->can.bittiming;
889 struct esd_usb2_msg msg; 892 struct esd_usb2_msg msg;
890 u32 canbtr; 893 u32 canbtr;
891 int sjw_shift;
892 894
893 canbtr = ESD_USB2_UBR; 895 canbtr = ESD_USB2_UBR;
894 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
895 canbtr |= ESD_USB2_LOM;
896
897 canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1); 896 canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1);
898
899 if (le16_to_cpu(priv->usb2->udev->descriptor.idProduct) ==
900 USB_CANUSBM_PRODUCT_ID)
901 sjw_shift = ESD_USBM_SJW_SHIFT;
902 else
903 sjw_shift = ESD_USB2_SJW_SHIFT;
904
905 canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1)) 897 canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1))
906 << sjw_shift; 898 << ESD_USB2_SJW_SHIFT;
907 canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1) 899 canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1)
908 & (ESD_USB2_TSEG1_MAX - 1)) 900 & (ESD_USB2_TSEG1_MAX - 1))
909 << ESD_USB2_TSEG1_SHIFT; 901 << ESD_USB2_TSEG1_SHIFT;
@@ -918,7 +910,7 @@ static int esd_usb2_set_bittiming(struct net_device *netdev)
918 msg.msg.setbaud.rsvd = 0; 910 msg.msg.setbaud.rsvd = 0;
919 msg.msg.setbaud.baud = cpu_to_le32(canbtr); 911 msg.msg.setbaud.baud = cpu_to_le32(canbtr);
920 912
921 netdev_info(netdev, "setting BTR=%#x\n", canbtr); 913 dev_info(netdev->dev.parent, "setting BTR=%#x\n", canbtr);
922 914
923 return esd_usb2_send_msg(priv->usb2, &msg); 915 return esd_usb2_send_msg(priv->usb2, &msg);
924} 916}
@@ -936,6 +928,11 @@ static int esd_usb2_get_berr_counter(const struct net_device *netdev,
936 928
937static int esd_usb2_set_mode(struct net_device *netdev, enum can_mode mode) 929static int esd_usb2_set_mode(struct net_device *netdev, enum can_mode mode)
938{ 930{
931 struct esd_usb2_net_priv *priv = netdev_priv(netdev);
932
933 if (!priv->open_time)
934 return -EINVAL;
935
939 switch (mode) { 936 switch (mode) {
940 case CAN_MODE_START: 937 case CAN_MODE_START:
941 netif_wake_queue(netdev); 938 netif_wake_queue(netdev);
@@ -976,20 +973,12 @@ static int esd_usb2_probe_one_net(struct usb_interface *intf, int index)
976 priv->index = index; 973 priv->index = index;
977 974
978 priv->can.state = CAN_STATE_STOPPED; 975 priv->can.state = CAN_STATE_STOPPED;
979 priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY; 976 priv->can.clock.freq = ESD_USB2_CAN_CLOCK;
980
981 if (le16_to_cpu(dev->udev->descriptor.idProduct) ==
982 USB_CANUSBM_PRODUCT_ID)
983 priv->can.clock.freq = ESD_USBM_CAN_CLOCK;
984 else {
985 priv->can.clock.freq = ESD_USB2_CAN_CLOCK;
986 priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
987 }
988
989 priv->can.bittiming_const = &esd_usb2_bittiming_const; 977 priv->can.bittiming_const = &esd_usb2_bittiming_const;
990 priv->can.do_set_bittiming = esd_usb2_set_bittiming; 978 priv->can.do_set_bittiming = esd_usb2_set_bittiming;
991 priv->can.do_set_mode = esd_usb2_set_mode; 979 priv->can.do_set_mode = esd_usb2_set_mode;
992 priv->can.do_get_berr_counter = esd_usb2_get_berr_counter; 980 priv->can.do_get_berr_counter = esd_usb2_get_berr_counter;
981 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
993 982
994 netdev->flags |= IFF_ECHO; /* we support local echo */ 983 netdev->flags |= IFF_ECHO; /* we support local echo */
995 984
@@ -999,14 +988,15 @@ static int esd_usb2_probe_one_net(struct usb_interface *intf, int index)
999 988
1000 err = register_candev(netdev); 989 err = register_candev(netdev);
1001 if (err) { 990 if (err) {
1002 dev_err(&intf->dev, "couldn't register CAN device: %d\n", err); 991 dev_err(&intf->dev,
992 "couldn't register CAN device: %d\n", err);
1003 free_candev(netdev); 993 free_candev(netdev);
1004 err = -ENOMEM; 994 err = -ENOMEM;
1005 goto done; 995 goto done;
1006 } 996 }
1007 997
1008 dev->nets[index] = priv; 998 dev->nets[index] = priv;
1009 netdev_info(netdev, "device %s registered\n", netdev->name); 999 dev_info(netdev->dev.parent, "device %s registered\n", netdev->name);
1010 1000
1011done: 1001done:
1012 return err; 1002 return err;
@@ -1118,4 +1108,25 @@ static struct usb_driver esd_usb2_driver = {
1118 .id_table = esd_usb2_table, 1108 .id_table = esd_usb2_table,
1119}; 1109};
1120 1110
1121module_usb_driver(esd_usb2_driver); 1111static int __init esd_usb2_init(void)
1112{
1113 int err;
1114
1115 /* register this driver with the USB subsystem */
1116 err = usb_register(&esd_usb2_driver);
1117
1118 if (err) {
1119 err("usb_register failed. Error number %d\n", err);
1120 return err;
1121 }
1122
1123 return 0;
1124}
1125module_init(esd_usb2_init);
1126
1127static void __exit esd_usb2_exit(void)
1128{
1129 /* deregister this driver with the USB subsystem */
1130 usb_deregister(&esd_usb2_driver);
1131}
1132module_exit(esd_usb2_exit);
diff --git a/drivers/net/can/usb/kvaser_usb.c b/drivers/net/can/usb/kvaser_usb.c
deleted file mode 100644
index 5b58a4d8739..00000000000
--- a/drivers/net/can/usb/kvaser_usb.c
+++ /dev/null
@@ -1,1627 +0,0 @@
1/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation version 2.
5 *
6 * Parts of this driver are based on the following:
7 * - Kvaser linux leaf driver (version 4.78)
8 * - CAN driver for esd CAN-USB/2
9 *
10 * Copyright (C) 2002-2006 KVASER AB, Sweden. All rights reserved.
11 * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
12 * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
13 */
14
15#include <linux/init.h>
16#include <linux/completion.h>
17#include <linux/module.h>
18#include <linux/netdevice.h>
19#include <linux/usb.h>
20
21#include <linux/can.h>
22#include <linux/can/dev.h>
23#include <linux/can/error.h>
24
25#define MAX_TX_URBS 16
26#define MAX_RX_URBS 4
27#define START_TIMEOUT 1000 /* msecs */
28#define STOP_TIMEOUT 1000 /* msecs */
29#define USB_SEND_TIMEOUT 1000 /* msecs */
30#define USB_RECV_TIMEOUT 1000 /* msecs */
31#define RX_BUFFER_SIZE 3072
32#define CAN_USB_CLOCK 8000000
33#define MAX_NET_DEVICES 3
34
35/* Kvaser USB devices */
36#define KVASER_VENDOR_ID 0x0bfd
37#define USB_LEAF_DEVEL_PRODUCT_ID 10
38#define USB_LEAF_LITE_PRODUCT_ID 11
39#define USB_LEAF_PRO_PRODUCT_ID 12
40#define USB_LEAF_SPRO_PRODUCT_ID 14
41#define USB_LEAF_PRO_LS_PRODUCT_ID 15
42#define USB_LEAF_PRO_SWC_PRODUCT_ID 16
43#define USB_LEAF_PRO_LIN_PRODUCT_ID 17
44#define USB_LEAF_SPRO_LS_PRODUCT_ID 18
45#define USB_LEAF_SPRO_SWC_PRODUCT_ID 19
46#define USB_MEMO2_DEVEL_PRODUCT_ID 22
47#define USB_MEMO2_HSHS_PRODUCT_ID 23
48#define USB_UPRO_HSHS_PRODUCT_ID 24
49#define USB_LEAF_LITE_GI_PRODUCT_ID 25
50#define USB_LEAF_PRO_OBDII_PRODUCT_ID 26
51#define USB_MEMO2_HSLS_PRODUCT_ID 27
52#define USB_LEAF_LITE_CH_PRODUCT_ID 28
53#define USB_BLACKBIRD_SPRO_PRODUCT_ID 29
54#define USB_OEM_MERCURY_PRODUCT_ID 34
55#define USB_OEM_LEAF_PRODUCT_ID 35
56#define USB_CAN_R_PRODUCT_ID 39
57
58/* USB devices features */
59#define KVASER_HAS_SILENT_MODE BIT(0)
60#define KVASER_HAS_TXRX_ERRORS BIT(1)
61
62/* Message header size */
63#define MSG_HEADER_LEN 2
64
65/* Can message flags */
66#define MSG_FLAG_ERROR_FRAME BIT(0)
67#define MSG_FLAG_OVERRUN BIT(1)
68#define MSG_FLAG_NERR BIT(2)
69#define MSG_FLAG_WAKEUP BIT(3)
70#define MSG_FLAG_REMOTE_FRAME BIT(4)
71#define MSG_FLAG_RESERVED BIT(5)
72#define MSG_FLAG_TX_ACK BIT(6)
73#define MSG_FLAG_TX_REQUEST BIT(7)
74
75/* Can states */
76#define M16C_STATE_BUS_RESET BIT(0)
77#define M16C_STATE_BUS_ERROR BIT(4)
78#define M16C_STATE_BUS_PASSIVE BIT(5)
79#define M16C_STATE_BUS_OFF BIT(6)
80
81/* Can msg ids */
82#define CMD_RX_STD_MESSAGE 12
83#define CMD_TX_STD_MESSAGE 13
84#define CMD_RX_EXT_MESSAGE 14
85#define CMD_TX_EXT_MESSAGE 15
86#define CMD_SET_BUS_PARAMS 16
87#define CMD_GET_BUS_PARAMS 17
88#define CMD_GET_BUS_PARAMS_REPLY 18
89#define CMD_GET_CHIP_STATE 19
90#define CMD_CHIP_STATE_EVENT 20
91#define CMD_SET_CTRL_MODE 21
92#define CMD_GET_CTRL_MODE 22
93#define CMD_GET_CTRL_MODE_REPLY 23
94#define CMD_RESET_CHIP 24
95#define CMD_RESET_CARD 25
96#define CMD_START_CHIP 26
97#define CMD_START_CHIP_REPLY 27
98#define CMD_STOP_CHIP 28
99#define CMD_STOP_CHIP_REPLY 29
100#define CMD_GET_CARD_INFO2 32
101#define CMD_GET_CARD_INFO 34
102#define CMD_GET_CARD_INFO_REPLY 35
103#define CMD_GET_SOFTWARE_INFO 38
104#define CMD_GET_SOFTWARE_INFO_REPLY 39
105#define CMD_ERROR_EVENT 45
106#define CMD_FLUSH_QUEUE 48
107#define CMD_RESET_ERROR_COUNTER 49
108#define CMD_TX_ACKNOWLEDGE 50
109#define CMD_CAN_ERROR_EVENT 51
110#define CMD_USB_THROTTLE 77
111#define CMD_LOG_MESSAGE 106
112
113/* error factors */
114#define M16C_EF_ACKE BIT(0)
115#define M16C_EF_CRCE BIT(1)
116#define M16C_EF_FORME BIT(2)
117#define M16C_EF_STFE BIT(3)
118#define M16C_EF_BITE0 BIT(4)
119#define M16C_EF_BITE1 BIT(5)
120#define M16C_EF_RCVE BIT(6)
121#define M16C_EF_TRE BIT(7)
122
123/* bittiming parameters */
124#define KVASER_USB_TSEG1_MIN 1
125#define KVASER_USB_TSEG1_MAX 16
126#define KVASER_USB_TSEG2_MIN 1
127#define KVASER_USB_TSEG2_MAX 8
128#define KVASER_USB_SJW_MAX 4
129#define KVASER_USB_BRP_MIN 1
130#define KVASER_USB_BRP_MAX 64
131#define KVASER_USB_BRP_INC 1
132
133/* ctrl modes */
134#define KVASER_CTRL_MODE_NORMAL 1
135#define KVASER_CTRL_MODE_SILENT 2
136#define KVASER_CTRL_MODE_SELFRECEPTION 3
137#define KVASER_CTRL_MODE_OFF 4
138
139struct kvaser_msg_simple {
140 u8 tid;
141 u8 channel;
142} __packed;
143
144struct kvaser_msg_cardinfo {
145 u8 tid;
146 u8 nchannels;
147 __le32 serial_number;
148 __le32 padding;
149 __le32 clock_resolution;
150 __le32 mfgdate;
151 u8 ean[8];
152 u8 hw_revision;
153 u8 usb_hs_mode;
154 __le16 padding2;
155} __packed;
156
157struct kvaser_msg_cardinfo2 {
158 u8 tid;
159 u8 channel;
160 u8 pcb_id[24];
161 __le32 oem_unlock_code;
162} __packed;
163
164struct kvaser_msg_softinfo {
165 u8 tid;
166 u8 channel;
167 __le32 sw_options;
168 __le32 fw_version;
169 __le16 max_outstanding_tx;
170 __le16 padding[9];
171} __packed;
172
173struct kvaser_msg_busparams {
174 u8 tid;
175 u8 channel;
176 __le32 bitrate;
177 u8 tseg1;
178 u8 tseg2;
179 u8 sjw;
180 u8 no_samp;
181} __packed;
182
183struct kvaser_msg_tx_can {
184 u8 channel;
185 u8 tid;
186 u8 msg[14];
187 u8 padding;
188 u8 flags;
189} __packed;
190
191struct kvaser_msg_rx_can {
192 u8 channel;
193 u8 flag;
194 __le16 time[3];
195 u8 msg[14];
196} __packed;
197
198struct kvaser_msg_chip_state_event {
199 u8 tid;
200 u8 channel;
201 __le16 time[3];
202 u8 tx_errors_count;
203 u8 rx_errors_count;
204 u8 status;
205 u8 padding[3];
206} __packed;
207
208struct kvaser_msg_tx_acknowledge {
209 u8 channel;
210 u8 tid;
211 __le16 time[3];
212 u8 flags;
213 u8 time_offset;
214} __packed;
215
216struct kvaser_msg_error_event {
217 u8 tid;
218 u8 flags;
219 __le16 time[3];
220 u8 channel;
221 u8 padding;
222 u8 tx_errors_count;
223 u8 rx_errors_count;
224 u8 status;
225 u8 error_factor;
226} __packed;
227
228struct kvaser_msg_ctrl_mode {
229 u8 tid;
230 u8 channel;
231 u8 ctrl_mode;
232 u8 padding[3];
233} __packed;
234
235struct kvaser_msg_flush_queue {
236 u8 tid;
237 u8 channel;
238 u8 flags;
239 u8 padding[3];
240} __packed;
241
242struct kvaser_msg_log_message {
243 u8 channel;
244 u8 flags;
245 __le16 time[3];
246 u8 dlc;
247 u8 time_offset;
248 __le32 id;
249 u8 data[8];
250} __packed;
251
252struct kvaser_msg {
253 u8 len;
254 u8 id;
255 union {
256 struct kvaser_msg_simple simple;
257 struct kvaser_msg_cardinfo cardinfo;
258 struct kvaser_msg_cardinfo2 cardinfo2;
259 struct kvaser_msg_softinfo softinfo;
260 struct kvaser_msg_busparams busparams;
261 struct kvaser_msg_tx_can tx_can;
262 struct kvaser_msg_rx_can rx_can;
263 struct kvaser_msg_chip_state_event chip_state_event;
264 struct kvaser_msg_tx_acknowledge tx_acknowledge;
265 struct kvaser_msg_error_event error_event;
266 struct kvaser_msg_ctrl_mode ctrl_mode;
267 struct kvaser_msg_flush_queue flush_queue;
268 struct kvaser_msg_log_message log_message;
269 } u;
270} __packed;
271
272struct kvaser_usb_tx_urb_context {
273 struct kvaser_usb_net_priv *priv;
274 u32 echo_index;
275 int dlc;
276};
277
278struct kvaser_usb {
279 struct usb_device *udev;
280 struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES];
281
282 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
283 struct usb_anchor rx_submitted;
284
285 u32 fw_version;
286 unsigned int nchannels;
287
288 bool rxinitdone;
289 void *rxbuf[MAX_RX_URBS];
290 dma_addr_t rxbuf_dma[MAX_RX_URBS];
291};
292
293struct kvaser_usb_net_priv {
294 struct can_priv can;
295
296 atomic_t active_tx_urbs;
297 struct usb_anchor tx_submitted;
298 struct kvaser_usb_tx_urb_context tx_contexts[MAX_TX_URBS];
299
300 struct completion start_comp, stop_comp;
301
302 struct kvaser_usb *dev;
303 struct net_device *netdev;
304 int channel;
305
306 struct can_berr_counter bec;
307};
308
309static const struct usb_device_id kvaser_usb_table[] = {
310 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) },
311 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) },
312 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID),
313 .driver_info = KVASER_HAS_TXRX_ERRORS |
314 KVASER_HAS_SILENT_MODE },
315 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID),
316 .driver_info = KVASER_HAS_TXRX_ERRORS |
317 KVASER_HAS_SILENT_MODE },
318 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID),
319 .driver_info = KVASER_HAS_TXRX_ERRORS |
320 KVASER_HAS_SILENT_MODE },
321 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID),
322 .driver_info = KVASER_HAS_TXRX_ERRORS |
323 KVASER_HAS_SILENT_MODE },
324 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID),
325 .driver_info = KVASER_HAS_TXRX_ERRORS |
326 KVASER_HAS_SILENT_MODE },
327 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID),
328 .driver_info = KVASER_HAS_TXRX_ERRORS |
329 KVASER_HAS_SILENT_MODE },
330 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID),
331 .driver_info = KVASER_HAS_TXRX_ERRORS |
332 KVASER_HAS_SILENT_MODE },
333 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID),
334 .driver_info = KVASER_HAS_TXRX_ERRORS |
335 KVASER_HAS_SILENT_MODE },
336 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID),
337 .driver_info = KVASER_HAS_TXRX_ERRORS |
338 KVASER_HAS_SILENT_MODE },
339 { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID),
340 .driver_info = KVASER_HAS_TXRX_ERRORS },
341 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) },
342 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID),
343 .driver_info = KVASER_HAS_TXRX_ERRORS |
344 KVASER_HAS_SILENT_MODE },
345 { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID),
346 .driver_info = KVASER_HAS_TXRX_ERRORS },
347 { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID),
348 .driver_info = KVASER_HAS_TXRX_ERRORS },
349 { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID),
350 .driver_info = KVASER_HAS_TXRX_ERRORS },
351 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID),
352 .driver_info = KVASER_HAS_TXRX_ERRORS },
353 { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID),
354 .driver_info = KVASER_HAS_TXRX_ERRORS },
355 { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID),
356 .driver_info = KVASER_HAS_TXRX_ERRORS },
357 { }
358};
359MODULE_DEVICE_TABLE(usb, kvaser_usb_table);
360
361static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev,
362 struct kvaser_msg *msg)
363{
364 int actual_len;
365
366 return usb_bulk_msg(dev->udev,
367 usb_sndbulkpipe(dev->udev,
368 dev->bulk_out->bEndpointAddress),
369 msg, msg->len, &actual_len,
370 USB_SEND_TIMEOUT);
371}
372
373static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
374 struct kvaser_msg *msg)
375{
376 struct kvaser_msg *tmp;
377 void *buf;
378 int actual_len;
379 int err;
380 int pos = 0;
381
382 buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL);
383 if (!buf)
384 return -ENOMEM;
385
386 err = usb_bulk_msg(dev->udev,
387 usb_rcvbulkpipe(dev->udev,
388 dev->bulk_in->bEndpointAddress),
389 buf, RX_BUFFER_SIZE, &actual_len,
390 USB_RECV_TIMEOUT);
391 if (err < 0)
392 goto end;
393
394 while (pos <= actual_len - MSG_HEADER_LEN) {
395 tmp = buf + pos;
396
397 if (!tmp->len)
398 break;
399
400 if (pos + tmp->len > actual_len) {
401 dev_err(dev->udev->dev.parent, "Format error\n");
402 break;
403 }
404
405 if (tmp->id == id) {
406 memcpy(msg, tmp, tmp->len);
407 goto end;
408 }
409
410 pos += tmp->len;
411 }
412
413 err = -EINVAL;
414
415end:
416 kfree(buf);
417
418 return err;
419}
420
421static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev,
422 u8 msg_id, int channel)
423{
424 struct kvaser_msg *msg;
425 int rc;
426
427 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
428 if (!msg)
429 return -ENOMEM;
430
431 msg->id = msg_id;
432 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
433 msg->u.simple.channel = channel;
434 msg->u.simple.tid = 0xff;
435
436 rc = kvaser_usb_send_msg(dev, msg);
437
438 kfree(msg);
439 return rc;
440}
441
442static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
443{
444 struct kvaser_msg msg;
445 int err;
446
447 err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0);
448 if (err)
449 return err;
450
451 err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg);
452 if (err)
453 return err;
454
455 dev->fw_version = le32_to_cpu(msg.u.softinfo.fw_version);
456
457 return 0;
458}
459
460static int kvaser_usb_get_card_info(struct kvaser_usb *dev)
461{
462 struct kvaser_msg msg;
463 int err;
464
465 err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0);
466 if (err)
467 return err;
468
469 err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg);
470 if (err)
471 return err;
472
473 dev->nchannels = msg.u.cardinfo.nchannels;
474
475 return 0;
476}
477
478static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
479 const struct kvaser_msg *msg)
480{
481 struct net_device_stats *stats;
482 struct kvaser_usb_tx_urb_context *context;
483 struct kvaser_usb_net_priv *priv;
484 struct sk_buff *skb;
485 struct can_frame *cf;
486 u8 channel = msg->u.tx_acknowledge.channel;
487 u8 tid = msg->u.tx_acknowledge.tid;
488
489 if (channel >= dev->nchannels) {
490 dev_err(dev->udev->dev.parent,
491 "Invalid channel number (%d)\n", channel);
492 return;
493 }
494
495 priv = dev->nets[channel];
496
497 if (!netif_device_present(priv->netdev))
498 return;
499
500 stats = &priv->netdev->stats;
501
502 context = &priv->tx_contexts[tid % MAX_TX_URBS];
503
504 /* Sometimes the state change doesn't come after a bus-off event */
505 if (priv->can.restart_ms &&
506 (priv->can.state >= CAN_STATE_BUS_OFF)) {
507 skb = alloc_can_err_skb(priv->netdev, &cf);
508 if (skb) {
509 cf->can_id |= CAN_ERR_RESTARTED;
510 netif_rx(skb);
511
512 stats->rx_packets++;
513 stats->rx_bytes += cf->can_dlc;
514 } else {
515 netdev_err(priv->netdev,
516 "No memory left for err_skb\n");
517 }
518
519 priv->can.can_stats.restarts++;
520 netif_carrier_on(priv->netdev);
521
522 priv->can.state = CAN_STATE_ERROR_ACTIVE;
523 }
524
525 stats->tx_packets++;
526 stats->tx_bytes += context->dlc;
527 can_get_echo_skb(priv->netdev, context->echo_index);
528
529 context->echo_index = MAX_TX_URBS;
530 atomic_dec(&priv->active_tx_urbs);
531
532 netif_wake_queue(priv->netdev);
533}
534
535static void kvaser_usb_simple_msg_callback(struct urb *urb)
536{
537 struct net_device *netdev = urb->context;
538
539 kfree(urb->transfer_buffer);
540
541 if (urb->status)
542 netdev_warn(netdev, "urb status received: %d\n",
543 urb->status);
544}
545
546static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
547 u8 msg_id)
548{
549 struct kvaser_usb *dev = priv->dev;
550 struct net_device *netdev = priv->netdev;
551 struct kvaser_msg *msg;
552 struct urb *urb;
553 void *buf;
554 int err;
555
556 urb = usb_alloc_urb(0, GFP_ATOMIC);
557 if (!urb) {
558 netdev_err(netdev, "No memory left for URBs\n");
559 return -ENOMEM;
560 }
561
562 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
563 if (!buf) {
564 netdev_err(netdev, "No memory left for USB buffer\n");
565 usb_free_urb(urb);
566 return -ENOMEM;
567 }
568
569 msg = (struct kvaser_msg *)buf;
570 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple);
571 msg->id = msg_id;
572 msg->u.simple.channel = priv->channel;
573
574 usb_fill_bulk_urb(urb, dev->udev,
575 usb_sndbulkpipe(dev->udev,
576 dev->bulk_out->bEndpointAddress),
577 buf, msg->len,
578 kvaser_usb_simple_msg_callback, priv);
579 usb_anchor_urb(urb, &priv->tx_submitted);
580
581 err = usb_submit_urb(urb, GFP_ATOMIC);
582 if (err) {
583 netdev_err(netdev, "Error transmitting URB\n");
584 usb_unanchor_urb(urb);
585 usb_free_urb(urb);
586 kfree(buf);
587 return err;
588 }
589
590 usb_free_urb(urb);
591
592 return 0;
593}
594
595static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
596{
597 int i;
598
599 usb_kill_anchored_urbs(&priv->tx_submitted);
600 atomic_set(&priv->active_tx_urbs, 0);
601
602 for (i = 0; i < MAX_TX_URBS; i++)
603 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
604}
605
606static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
607 const struct kvaser_msg *msg)
608{
609 struct can_frame *cf;
610 struct sk_buff *skb;
611 struct net_device_stats *stats;
612 struct kvaser_usb_net_priv *priv;
613 unsigned int new_state;
614 u8 channel, status, txerr, rxerr, error_factor;
615
616 switch (msg->id) {
617 case CMD_CAN_ERROR_EVENT:
618 channel = msg->u.error_event.channel;
619 status = msg->u.error_event.status;
620 txerr = msg->u.error_event.tx_errors_count;
621 rxerr = msg->u.error_event.rx_errors_count;
622 error_factor = msg->u.error_event.error_factor;
623 break;
624 case CMD_LOG_MESSAGE:
625 channel = msg->u.log_message.channel;
626 status = msg->u.log_message.data[0];
627 txerr = msg->u.log_message.data[2];
628 rxerr = msg->u.log_message.data[3];
629 error_factor = msg->u.log_message.data[1];
630 break;
631 case CMD_CHIP_STATE_EVENT:
632 channel = msg->u.chip_state_event.channel;
633 status = msg->u.chip_state_event.status;
634 txerr = msg->u.chip_state_event.tx_errors_count;
635 rxerr = msg->u.chip_state_event.rx_errors_count;
636 error_factor = 0;
637 break;
638 default:
639 dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n",
640 msg->id);
641 return;
642 }
643
644 if (channel >= dev->nchannels) {
645 dev_err(dev->udev->dev.parent,
646 "Invalid channel number (%d)\n", channel);
647 return;
648 }
649
650 priv = dev->nets[channel];
651 stats = &priv->netdev->stats;
652
653 if (status & M16C_STATE_BUS_RESET) {
654 kvaser_usb_unlink_tx_urbs(priv);
655 return;
656 }
657
658 skb = alloc_can_err_skb(priv->netdev, &cf);
659 if (!skb) {
660 stats->rx_dropped++;
661 return;
662 }
663
664 new_state = priv->can.state;
665
666 netdev_dbg(priv->netdev, "Error status: 0x%02x\n", status);
667
668 if (status & M16C_STATE_BUS_OFF) {
669 cf->can_id |= CAN_ERR_BUSOFF;
670
671 priv->can.can_stats.bus_off++;
672 if (!priv->can.restart_ms)
673 kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP);
674
675 netif_carrier_off(priv->netdev);
676
677 new_state = CAN_STATE_BUS_OFF;
678 } else if (status & M16C_STATE_BUS_PASSIVE) {
679 if (priv->can.state != CAN_STATE_ERROR_PASSIVE) {
680 cf->can_id |= CAN_ERR_CRTL;
681
682 if (txerr || rxerr)
683 cf->data[1] = (txerr > rxerr)
684 ? CAN_ERR_CRTL_TX_PASSIVE
685 : CAN_ERR_CRTL_RX_PASSIVE;
686 else
687 cf->data[1] = CAN_ERR_CRTL_TX_PASSIVE |
688 CAN_ERR_CRTL_RX_PASSIVE;
689
690 priv->can.can_stats.error_passive++;
691 }
692
693 new_state = CAN_STATE_ERROR_PASSIVE;
694 }
695
696 if (status == M16C_STATE_BUS_ERROR) {
697 if ((priv->can.state < CAN_STATE_ERROR_WARNING) &&
698 ((txerr >= 96) || (rxerr >= 96))) {
699 cf->can_id |= CAN_ERR_CRTL;
700 cf->data[1] = (txerr > rxerr)
701 ? CAN_ERR_CRTL_TX_WARNING
702 : CAN_ERR_CRTL_RX_WARNING;
703
704 priv->can.can_stats.error_warning++;
705 new_state = CAN_STATE_ERROR_WARNING;
706 } else if (priv->can.state > CAN_STATE_ERROR_ACTIVE) {
707 cf->can_id |= CAN_ERR_PROT;
708 cf->data[2] = CAN_ERR_PROT_ACTIVE;
709
710 new_state = CAN_STATE_ERROR_ACTIVE;
711 }
712 }
713
714 if (!status) {
715 cf->can_id |= CAN_ERR_PROT;
716 cf->data[2] = CAN_ERR_PROT_ACTIVE;
717
718 new_state = CAN_STATE_ERROR_ACTIVE;
719 }
720
721 if (priv->can.restart_ms &&
722 (priv->can.state >= CAN_STATE_BUS_OFF) &&
723 (new_state < CAN_STATE_BUS_OFF)) {
724 cf->can_id |= CAN_ERR_RESTARTED;
725 netif_carrier_on(priv->netdev);
726
727 priv->can.can_stats.restarts++;
728 }
729
730 if (error_factor) {
731 priv->can.can_stats.bus_error++;
732 stats->rx_errors++;
733
734 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
735
736 if (error_factor & M16C_EF_ACKE)
737 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK);
738 if (error_factor & M16C_EF_CRCE)
739 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
740 CAN_ERR_PROT_LOC_CRC_DEL);
741 if (error_factor & M16C_EF_FORME)
742 cf->data[2] |= CAN_ERR_PROT_FORM;
743 if (error_factor & M16C_EF_STFE)
744 cf->data[2] |= CAN_ERR_PROT_STUFF;
745 if (error_factor & M16C_EF_BITE0)
746 cf->data[2] |= CAN_ERR_PROT_BIT0;
747 if (error_factor & M16C_EF_BITE1)
748 cf->data[2] |= CAN_ERR_PROT_BIT1;
749 if (error_factor & M16C_EF_TRE)
750 cf->data[2] |= CAN_ERR_PROT_TX;
751 }
752
753 cf->data[6] = txerr;
754 cf->data[7] = rxerr;
755
756 priv->bec.txerr = txerr;
757 priv->bec.rxerr = rxerr;
758
759 priv->can.state = new_state;
760
761 netif_rx(skb);
762
763 stats->rx_packets++;
764 stats->rx_bytes += cf->can_dlc;
765}
766
767static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv,
768 const struct kvaser_msg *msg)
769{
770 struct can_frame *cf;
771 struct sk_buff *skb;
772 struct net_device_stats *stats = &priv->netdev->stats;
773
774 if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME |
775 MSG_FLAG_NERR)) {
776 netdev_err(priv->netdev, "Unknow error (flags: 0x%02x)\n",
777 msg->u.rx_can.flag);
778
779 stats->rx_errors++;
780 return;
781 }
782
783 if (msg->u.rx_can.flag & MSG_FLAG_OVERRUN) {
784 skb = alloc_can_err_skb(priv->netdev, &cf);
785 if (!skb) {
786 stats->rx_dropped++;
787 return;
788 }
789
790 cf->can_id |= CAN_ERR_CRTL;
791 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
792
793 stats->rx_over_errors++;
794 stats->rx_errors++;
795
796 netif_rx(skb);
797
798 stats->rx_packets++;
799 stats->rx_bytes += cf->can_dlc;
800 }
801}
802
803static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev,
804 const struct kvaser_msg *msg)
805{
806 struct kvaser_usb_net_priv *priv;
807 struct can_frame *cf;
808 struct sk_buff *skb;
809 struct net_device_stats *stats;
810 u8 channel = msg->u.rx_can.channel;
811
812 if (channel >= dev->nchannels) {
813 dev_err(dev->udev->dev.parent,
814 "Invalid channel number (%d)\n", channel);
815 return;
816 }
817
818 priv = dev->nets[channel];
819 stats = &priv->netdev->stats;
820
821 if (msg->u.rx_can.flag & (MSG_FLAG_ERROR_FRAME | MSG_FLAG_NERR |
822 MSG_FLAG_OVERRUN)) {
823 kvaser_usb_rx_can_err(priv, msg);
824 return;
825 } else if (msg->u.rx_can.flag & ~MSG_FLAG_REMOTE_FRAME) {
826 netdev_warn(priv->netdev,
827 "Unhandled frame (flags: 0x%02x)",
828 msg->u.rx_can.flag);
829 return;
830 }
831
832 skb = alloc_can_skb(priv->netdev, &cf);
833 if (!skb) {
834 stats->tx_dropped++;
835 return;
836 }
837
838 cf->can_id = ((msg->u.rx_can.msg[0] & 0x1f) << 6) |
839 (msg->u.rx_can.msg[1] & 0x3f);
840 cf->can_dlc = get_can_dlc(msg->u.rx_can.msg[5]);
841
842 if (msg->id == CMD_RX_EXT_MESSAGE) {
843 cf->can_id <<= 18;
844 cf->can_id |= ((msg->u.rx_can.msg[2] & 0x0f) << 14) |
845 ((msg->u.rx_can.msg[3] & 0xff) << 6) |
846 (msg->u.rx_can.msg[4] & 0x3f);
847 cf->can_id |= CAN_EFF_FLAG;
848 }
849
850 if (msg->u.rx_can.flag & MSG_FLAG_REMOTE_FRAME)
851 cf->can_id |= CAN_RTR_FLAG;
852 else
853 memcpy(cf->data, &msg->u.rx_can.msg[6], cf->can_dlc);
854
855 netif_rx(skb);
856
857 stats->rx_packets++;
858 stats->rx_bytes += cf->can_dlc;
859}
860
861static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev,
862 const struct kvaser_msg *msg)
863{
864 struct kvaser_usb_net_priv *priv;
865 u8 channel = msg->u.simple.channel;
866
867 if (channel >= dev->nchannels) {
868 dev_err(dev->udev->dev.parent,
869 "Invalid channel number (%d)\n", channel);
870 return;
871 }
872
873 priv = dev->nets[channel];
874
875 if (completion_done(&priv->start_comp) &&
876 netif_queue_stopped(priv->netdev)) {
877 netif_wake_queue(priv->netdev);
878 } else {
879 netif_start_queue(priv->netdev);
880 complete(&priv->start_comp);
881 }
882}
883
884static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev,
885 const struct kvaser_msg *msg)
886{
887 struct kvaser_usb_net_priv *priv;
888 u8 channel = msg->u.simple.channel;
889
890 if (channel >= dev->nchannels) {
891 dev_err(dev->udev->dev.parent,
892 "Invalid channel number (%d)\n", channel);
893 return;
894 }
895
896 priv = dev->nets[channel];
897
898 complete(&priv->stop_comp);
899}
900
901static void kvaser_usb_handle_message(const struct kvaser_usb *dev,
902 const struct kvaser_msg *msg)
903{
904 switch (msg->id) {
905 case CMD_START_CHIP_REPLY:
906 kvaser_usb_start_chip_reply(dev, msg);
907 break;
908
909 case CMD_STOP_CHIP_REPLY:
910 kvaser_usb_stop_chip_reply(dev, msg);
911 break;
912
913 case CMD_RX_STD_MESSAGE:
914 case CMD_RX_EXT_MESSAGE:
915 kvaser_usb_rx_can_msg(dev, msg);
916 break;
917
918 case CMD_CHIP_STATE_EVENT:
919 case CMD_CAN_ERROR_EVENT:
920 kvaser_usb_rx_error(dev, msg);
921 break;
922
923 case CMD_LOG_MESSAGE:
924 if (msg->u.log_message.flags & MSG_FLAG_ERROR_FRAME)
925 kvaser_usb_rx_error(dev, msg);
926 break;
927
928 case CMD_TX_ACKNOWLEDGE:
929 kvaser_usb_tx_acknowledge(dev, msg);
930 break;
931
932 default:
933 dev_warn(dev->udev->dev.parent,
934 "Unhandled message (%d)\n", msg->id);
935 break;
936 }
937}
938
939static void kvaser_usb_read_bulk_callback(struct urb *urb)
940{
941 struct kvaser_usb *dev = urb->context;
942 struct kvaser_msg *msg;
943 int pos = 0;
944 int err, i;
945
946 switch (urb->status) {
947 case 0:
948 break;
949 case -ENOENT:
950 case -ESHUTDOWN:
951 return;
952 default:
953 dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n",
954 urb->status);
955 goto resubmit_urb;
956 }
957
958 while (pos <= urb->actual_length - MSG_HEADER_LEN) {
959 msg = urb->transfer_buffer + pos;
960
961 if (!msg->len)
962 break;
963
964 if (pos + msg->len > urb->actual_length) {
965 dev_err(dev->udev->dev.parent, "Format error\n");
966 break;
967 }
968
969 kvaser_usb_handle_message(dev, msg);
970
971 pos += msg->len;
972 }
973
974resubmit_urb:
975 usb_fill_bulk_urb(urb, dev->udev,
976 usb_rcvbulkpipe(dev->udev,
977 dev->bulk_in->bEndpointAddress),
978 urb->transfer_buffer, RX_BUFFER_SIZE,
979 kvaser_usb_read_bulk_callback, dev);
980
981 err = usb_submit_urb(urb, GFP_ATOMIC);
982 if (err == -ENODEV) {
983 for (i = 0; i < dev->nchannels; i++) {
984 if (!dev->nets[i])
985 continue;
986
987 netif_device_detach(dev->nets[i]->netdev);
988 }
989 } else if (err) {
990 dev_err(dev->udev->dev.parent,
991 "Failed resubmitting read bulk urb: %d\n", err);
992 }
993
994 return;
995}
996
997static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev)
998{
999 int i, err = 0;
1000
1001 if (dev->rxinitdone)
1002 return 0;
1003
1004 for (i = 0; i < MAX_RX_URBS; i++) {
1005 struct urb *urb = NULL;
1006 u8 *buf = NULL;
1007 dma_addr_t buf_dma;
1008
1009 urb = usb_alloc_urb(0, GFP_KERNEL);
1010 if (!urb) {
1011 dev_warn(dev->udev->dev.parent,
1012 "No memory left for URBs\n");
1013 err = -ENOMEM;
1014 break;
1015 }
1016
1017 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE,
1018 GFP_KERNEL, &buf_dma);
1019 if (!buf) {
1020 dev_warn(dev->udev->dev.parent,
1021 "No memory left for USB buffer\n");
1022 usb_free_urb(urb);
1023 err = -ENOMEM;
1024 break;
1025 }
1026
1027 usb_fill_bulk_urb(urb, dev->udev,
1028 usb_rcvbulkpipe(dev->udev,
1029 dev->bulk_in->bEndpointAddress),
1030 buf, RX_BUFFER_SIZE,
1031 kvaser_usb_read_bulk_callback,
1032 dev);
1033 urb->transfer_dma = buf_dma;
1034 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1035 usb_anchor_urb(urb, &dev->rx_submitted);
1036
1037 err = usb_submit_urb(urb, GFP_KERNEL);
1038 if (err) {
1039 usb_unanchor_urb(urb);
1040 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
1041 buf_dma);
1042 usb_free_urb(urb);
1043 break;
1044 }
1045
1046 dev->rxbuf[i] = buf;
1047 dev->rxbuf_dma[i] = buf_dma;
1048
1049 usb_free_urb(urb);
1050 }
1051
1052 if (i == 0) {
1053 dev_warn(dev->udev->dev.parent,
1054 "Cannot setup read URBs, error %d\n", err);
1055 return err;
1056 } else if (i < MAX_RX_URBS) {
1057 dev_warn(dev->udev->dev.parent,
1058 "RX performances may be slow\n");
1059 }
1060
1061 dev->rxinitdone = true;
1062
1063 return 0;
1064}
1065
1066static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv)
1067{
1068 struct kvaser_msg *msg;
1069 int rc;
1070
1071 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1072 if (!msg)
1073 return -ENOMEM;
1074
1075 msg->id = CMD_SET_CTRL_MODE;
1076 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode);
1077 msg->u.ctrl_mode.tid = 0xff;
1078 msg->u.ctrl_mode.channel = priv->channel;
1079
1080 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1081 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT;
1082 else
1083 msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL;
1084
1085 rc = kvaser_usb_send_msg(priv->dev, msg);
1086
1087 kfree(msg);
1088 return rc;
1089}
1090
1091static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv)
1092{
1093 int err;
1094
1095 init_completion(&priv->start_comp);
1096
1097 err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP,
1098 priv->channel);
1099 if (err)
1100 return err;
1101
1102 if (!wait_for_completion_timeout(&priv->start_comp,
1103 msecs_to_jiffies(START_TIMEOUT)))
1104 return -ETIMEDOUT;
1105
1106 return 0;
1107}
1108
1109static int kvaser_usb_open(struct net_device *netdev)
1110{
1111 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1112 struct kvaser_usb *dev = priv->dev;
1113 int err;
1114
1115 err = open_candev(netdev);
1116 if (err)
1117 return err;
1118
1119 err = kvaser_usb_setup_rx_urbs(dev);
1120 if (err)
1121 goto error;
1122
1123 err = kvaser_usb_set_opt_mode(priv);
1124 if (err)
1125 goto error;
1126
1127 err = kvaser_usb_start_chip(priv);
1128 if (err) {
1129 netdev_warn(netdev, "Cannot start device, error %d\n", err);
1130 goto error;
1131 }
1132
1133 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1134
1135 return 0;
1136
1137error:
1138 close_candev(netdev);
1139 return err;
1140}
1141
1142static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1143{
1144 int i;
1145
1146 usb_kill_anchored_urbs(&dev->rx_submitted);
1147
1148 for (i = 0; i < MAX_RX_URBS; i++)
1149 usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
1150 dev->rxbuf[i],
1151 dev->rxbuf_dma[i]);
1152
1153 for (i = 0; i < MAX_NET_DEVICES; i++) {
1154 struct kvaser_usb_net_priv *priv = dev->nets[i];
1155
1156 if (priv)
1157 kvaser_usb_unlink_tx_urbs(priv);
1158 }
1159}
1160
1161static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv)
1162{
1163 int err;
1164
1165 init_completion(&priv->stop_comp);
1166
1167 err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP,
1168 priv->channel);
1169 if (err)
1170 return err;
1171
1172 if (!wait_for_completion_timeout(&priv->stop_comp,
1173 msecs_to_jiffies(STOP_TIMEOUT)))
1174 return -ETIMEDOUT;
1175
1176 return 0;
1177}
1178
1179static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv)
1180{
1181 struct kvaser_msg *msg;
1182 int rc;
1183
1184 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1185 if (!msg)
1186 return -ENOMEM;
1187
1188 msg->id = CMD_FLUSH_QUEUE;
1189 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue);
1190 msg->u.flush_queue.channel = priv->channel;
1191 msg->u.flush_queue.flags = 0x00;
1192
1193 rc = kvaser_usb_send_msg(priv->dev, msg);
1194
1195 kfree(msg);
1196 return rc;
1197}
1198
1199static int kvaser_usb_close(struct net_device *netdev)
1200{
1201 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1202 struct kvaser_usb *dev = priv->dev;
1203 int err;
1204
1205 netif_stop_queue(netdev);
1206
1207 err = kvaser_usb_flush_queue(priv);
1208 if (err)
1209 netdev_warn(netdev, "Cannot flush queue, error %d\n", err);
1210
1211 if (kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel))
1212 netdev_warn(netdev, "Cannot reset card, error %d\n", err);
1213
1214 err = kvaser_usb_stop_chip(priv);
1215 if (err)
1216 netdev_warn(netdev, "Cannot stop device, error %d\n", err);
1217
1218 priv->can.state = CAN_STATE_STOPPED;
1219 close_candev(priv->netdev);
1220
1221 return 0;
1222}
1223
1224static void kvaser_usb_write_bulk_callback(struct urb *urb)
1225{
1226 struct kvaser_usb_tx_urb_context *context = urb->context;
1227 struct kvaser_usb_net_priv *priv;
1228 struct net_device *netdev;
1229
1230 if (WARN_ON(!context))
1231 return;
1232
1233 priv = context->priv;
1234 netdev = priv->netdev;
1235
1236 kfree(urb->transfer_buffer);
1237
1238 if (!netif_device_present(netdev))
1239 return;
1240
1241 if (urb->status)
1242 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
1243}
1244
1245static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1246 struct net_device *netdev)
1247{
1248 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1249 struct kvaser_usb *dev = priv->dev;
1250 struct net_device_stats *stats = &netdev->stats;
1251 struct can_frame *cf = (struct can_frame *)skb->data;
1252 struct kvaser_usb_tx_urb_context *context = NULL;
1253 struct urb *urb;
1254 void *buf;
1255 struct kvaser_msg *msg;
1256 int i, err;
1257 int ret = NETDEV_TX_OK;
1258
1259 if (can_dropped_invalid_skb(netdev, skb))
1260 return NETDEV_TX_OK;
1261
1262 urb = usb_alloc_urb(0, GFP_ATOMIC);
1263 if (!urb) {
1264 netdev_err(netdev, "No memory left for URBs\n");
1265 stats->tx_dropped++;
1266 goto nourbmem;
1267 }
1268
1269 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1270 if (!buf) {
1271 netdev_err(netdev, "No memory left for USB buffer\n");
1272 stats->tx_dropped++;
1273 goto nobufmem;
1274 }
1275
1276 msg = buf;
1277 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can);
1278 msg->u.tx_can.flags = 0;
1279 msg->u.tx_can.channel = priv->channel;
1280
1281 if (cf->can_id & CAN_EFF_FLAG) {
1282 msg->id = CMD_TX_EXT_MESSAGE;
1283 msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f;
1284 msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f;
1285 msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f;
1286 msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff;
1287 msg->u.tx_can.msg[4] = cf->can_id & 0x3f;
1288 } else {
1289 msg->id = CMD_TX_STD_MESSAGE;
1290 msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f;
1291 msg->u.tx_can.msg[1] = cf->can_id & 0x3f;
1292 }
1293
1294 msg->u.tx_can.msg[5] = cf->can_dlc;
1295 memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc);
1296
1297 if (cf->can_id & CAN_RTR_FLAG)
1298 msg->u.tx_can.flags |= MSG_FLAG_REMOTE_FRAME;
1299
1300 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) {
1301 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
1302 context = &priv->tx_contexts[i];
1303 break;
1304 }
1305 }
1306
1307 if (!context) {
1308 netdev_warn(netdev, "cannot find free context\n");
1309 ret = NETDEV_TX_BUSY;
1310 goto releasebuf;
1311 }
1312
1313 context->priv = priv;
1314 context->echo_index = i;
1315 context->dlc = cf->can_dlc;
1316
1317 msg->u.tx_can.tid = context->echo_index;
1318
1319 usb_fill_bulk_urb(urb, dev->udev,
1320 usb_sndbulkpipe(dev->udev,
1321 dev->bulk_out->bEndpointAddress),
1322 buf, msg->len,
1323 kvaser_usb_write_bulk_callback, context);
1324 usb_anchor_urb(urb, &priv->tx_submitted);
1325
1326 can_put_echo_skb(skb, netdev, context->echo_index);
1327
1328 atomic_inc(&priv->active_tx_urbs);
1329
1330 if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
1331 netif_stop_queue(netdev);
1332
1333 err = usb_submit_urb(urb, GFP_ATOMIC);
1334 if (unlikely(err)) {
1335 can_free_echo_skb(netdev, context->echo_index);
1336
1337 skb = NULL; /* set to NULL to avoid double free in
1338 * dev_kfree_skb(skb) */
1339
1340 atomic_dec(&priv->active_tx_urbs);
1341 usb_unanchor_urb(urb);
1342
1343 stats->tx_dropped++;
1344
1345 if (err == -ENODEV)
1346 netif_device_detach(netdev);
1347 else
1348 netdev_warn(netdev, "Failed tx_urb %d\n", err);
1349
1350 goto releasebuf;
1351 }
1352
1353 usb_free_urb(urb);
1354
1355 return NETDEV_TX_OK;
1356
1357releasebuf:
1358 kfree(buf);
1359nobufmem:
1360 usb_free_urb(urb);
1361nourbmem:
1362 dev_kfree_skb(skb);
1363 return ret;
1364}
1365
1366static const struct net_device_ops kvaser_usb_netdev_ops = {
1367 .ndo_open = kvaser_usb_open,
1368 .ndo_stop = kvaser_usb_close,
1369 .ndo_start_xmit = kvaser_usb_start_xmit,
1370};
1371
1372static const struct can_bittiming_const kvaser_usb_bittiming_const = {
1373 .name = "kvaser_usb",
1374 .tseg1_min = KVASER_USB_TSEG1_MIN,
1375 .tseg1_max = KVASER_USB_TSEG1_MAX,
1376 .tseg2_min = KVASER_USB_TSEG2_MIN,
1377 .tseg2_max = KVASER_USB_TSEG2_MAX,
1378 .sjw_max = KVASER_USB_SJW_MAX,
1379 .brp_min = KVASER_USB_BRP_MIN,
1380 .brp_max = KVASER_USB_BRP_MAX,
1381 .brp_inc = KVASER_USB_BRP_INC,
1382};
1383
1384static int kvaser_usb_set_bittiming(struct net_device *netdev)
1385{
1386 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1387 struct can_bittiming *bt = &priv->can.bittiming;
1388 struct kvaser_usb *dev = priv->dev;
1389 struct kvaser_msg *msg;
1390 int rc;
1391
1392 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1393 if (!msg)
1394 return -ENOMEM;
1395
1396 msg->id = CMD_SET_BUS_PARAMS;
1397 msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams);
1398 msg->u.busparams.channel = priv->channel;
1399 msg->u.busparams.tid = 0xff;
1400 msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate);
1401 msg->u.busparams.sjw = bt->sjw;
1402 msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1;
1403 msg->u.busparams.tseg2 = bt->phase_seg2;
1404
1405 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
1406 msg->u.busparams.no_samp = 3;
1407 else
1408 msg->u.busparams.no_samp = 1;
1409
1410 rc = kvaser_usb_send_msg(dev, msg);
1411
1412 kfree(msg);
1413 return rc;
1414}
1415
1416static int kvaser_usb_set_mode(struct net_device *netdev,
1417 enum can_mode mode)
1418{
1419 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1420 int err;
1421
1422 switch (mode) {
1423 case CAN_MODE_START:
1424 err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP);
1425 if (err)
1426 return err;
1427 break;
1428 default:
1429 return -EOPNOTSUPP;
1430 }
1431
1432 return 0;
1433}
1434
1435static int kvaser_usb_get_berr_counter(const struct net_device *netdev,
1436 struct can_berr_counter *bec)
1437{
1438 struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
1439
1440 *bec = priv->bec;
1441
1442 return 0;
1443}
1444
1445static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev)
1446{
1447 int i;
1448
1449 for (i = 0; i < dev->nchannels; i++) {
1450 if (!dev->nets[i])
1451 continue;
1452
1453 unregister_netdev(dev->nets[i]->netdev);
1454 }
1455
1456 kvaser_usb_unlink_all_urbs(dev);
1457
1458 for (i = 0; i < dev->nchannels; i++) {
1459 if (!dev->nets[i])
1460 continue;
1461
1462 free_candev(dev->nets[i]->netdev);
1463 }
1464}
1465
1466static int kvaser_usb_init_one(struct usb_interface *intf,
1467 const struct usb_device_id *id, int channel)
1468{
1469 struct kvaser_usb *dev = usb_get_intfdata(intf);
1470 struct net_device *netdev;
1471 struct kvaser_usb_net_priv *priv;
1472 int i, err;
1473
1474 netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
1475 if (!netdev) {
1476 dev_err(&intf->dev, "Cannot alloc candev\n");
1477 return -ENOMEM;
1478 }
1479
1480 priv = netdev_priv(netdev);
1481
1482 init_completion(&priv->start_comp);
1483 init_completion(&priv->stop_comp);
1484
1485 init_usb_anchor(&priv->tx_submitted);
1486 atomic_set(&priv->active_tx_urbs, 0);
1487
1488 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++)
1489 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
1490
1491 priv->dev = dev;
1492 priv->netdev = netdev;
1493 priv->channel = channel;
1494
1495 priv->can.state = CAN_STATE_STOPPED;
1496 priv->can.clock.freq = CAN_USB_CLOCK;
1497 priv->can.bittiming_const = &kvaser_usb_bittiming_const;
1498 priv->can.do_set_bittiming = kvaser_usb_set_bittiming;
1499 priv->can.do_set_mode = kvaser_usb_set_mode;
1500 if (id->driver_info & KVASER_HAS_TXRX_ERRORS)
1501 priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter;
1502 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1503 if (id->driver_info & KVASER_HAS_SILENT_MODE)
1504 priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
1505
1506 netdev->flags |= IFF_ECHO;
1507
1508 netdev->netdev_ops = &kvaser_usb_netdev_ops;
1509
1510 SET_NETDEV_DEV(netdev, &intf->dev);
1511
1512 dev->nets[channel] = priv;
1513
1514 err = register_candev(netdev);
1515 if (err) {
1516 dev_err(&intf->dev, "Failed to register can device\n");
1517 free_candev(netdev);
1518 dev->nets[channel] = NULL;
1519 return err;
1520 }
1521
1522 netdev_dbg(netdev, "device registered\n");
1523
1524 return 0;
1525}
1526
1527static void kvaser_usb_get_endpoints(const struct usb_interface *intf,
1528 struct usb_endpoint_descriptor **in,
1529 struct usb_endpoint_descriptor **out)
1530{
1531 const struct usb_host_interface *iface_desc;
1532 struct usb_endpoint_descriptor *endpoint;
1533 int i;
1534
1535 iface_desc = &intf->altsetting[0];
1536
1537 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1538 endpoint = &iface_desc->endpoint[i].desc;
1539
1540 if (usb_endpoint_is_bulk_in(endpoint))
1541 *in = endpoint;
1542
1543 if (usb_endpoint_is_bulk_out(endpoint))
1544 *out = endpoint;
1545 }
1546}
1547
1548static int kvaser_usb_probe(struct usb_interface *intf,
1549 const struct usb_device_id *id)
1550{
1551 struct kvaser_usb *dev;
1552 int err = -ENOMEM;
1553 int i;
1554
1555 dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL);
1556 if (!dev)
1557 return -ENOMEM;
1558
1559 kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out);
1560 if (!dev->bulk_in || !dev->bulk_out) {
1561 dev_err(&intf->dev, "Cannot get usb endpoint(s)");
1562 return err;
1563 }
1564
1565 dev->udev = interface_to_usbdev(intf);
1566
1567 init_usb_anchor(&dev->rx_submitted);
1568
1569 usb_set_intfdata(intf, dev);
1570
1571 for (i = 0; i < MAX_NET_DEVICES; i++)
1572 kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, i);
1573
1574 err = kvaser_usb_get_software_info(dev);
1575 if (err) {
1576 dev_err(&intf->dev,
1577 "Cannot get software infos, error %d\n", err);
1578 return err;
1579 }
1580
1581 err = kvaser_usb_get_card_info(dev);
1582 if (err) {
1583 dev_err(&intf->dev,
1584 "Cannot get card infos, error %d\n", err);
1585 return err;
1586 }
1587
1588 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
1589 ((dev->fw_version >> 24) & 0xff),
1590 ((dev->fw_version >> 16) & 0xff),
1591 (dev->fw_version & 0xffff));
1592
1593 for (i = 0; i < dev->nchannels; i++) {
1594 err = kvaser_usb_init_one(intf, id, i);
1595 if (err) {
1596 kvaser_usb_remove_interfaces(dev);
1597 return err;
1598 }
1599 }
1600
1601 return 0;
1602}
1603
1604static void kvaser_usb_disconnect(struct usb_interface *intf)
1605{
1606 struct kvaser_usb *dev = usb_get_intfdata(intf);
1607
1608 usb_set_intfdata(intf, NULL);
1609
1610 if (!dev)
1611 return;
1612
1613 kvaser_usb_remove_interfaces(dev);
1614}
1615
1616static struct usb_driver kvaser_usb_driver = {
1617 .name = "kvaser_usb",
1618 .probe = kvaser_usb_probe,
1619 .disconnect = kvaser_usb_disconnect,
1620 .id_table = kvaser_usb_table,
1621};
1622
1623module_usb_driver(kvaser_usb_driver);
1624
1625MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
1626MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices");
1627MODULE_LICENSE("GPL v2");
diff --git a/drivers/net/can/usb/peak_usb/Makefile b/drivers/net/can/usb/peak_usb/Makefile
deleted file mode 100644
index 1aefbc88d64..00000000000
--- a/drivers/net/can/usb/peak_usb/Makefile
+++ /dev/null
@@ -1,2 +0,0 @@
1obj-$(CONFIG_CAN_PEAK_USB) += peak_usb.o
2peak_usb-y = pcan_usb_core.o pcan_usb.o pcan_usb_pro.o
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb.c b/drivers/net/can/usb/peak_usb/pcan_usb.c
deleted file mode 100644
index 25723d8ee20..00000000000
--- a/drivers/net/can/usb/peak_usb/pcan_usb.c
+++ /dev/null
@@ -1,903 +0,0 @@
1/*
2 * CAN driver for PEAK System PCAN-USB adapter
3 * Derived from the PCAN project file driver/src/pcan_usb.c
4 *
5 * Copyright (C) 2003-2010 PEAK System-Technik GmbH
6 * Copyright (C) 2011-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/netdevice.h>
20#include <linux/usb.h>
21#include <linux/module.h>
22
23#include <linux/can.h>
24#include <linux/can/dev.h>
25#include <linux/can/error.h>
26
27#include "pcan_usb_core.h"
28
29MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB adapter");
30
31/* PCAN-USB Endpoints */
32#define PCAN_USB_EP_CMDOUT 1
33#define PCAN_USB_EP_CMDIN (PCAN_USB_EP_CMDOUT | USB_DIR_IN)
34#define PCAN_USB_EP_MSGOUT 2
35#define PCAN_USB_EP_MSGIN (PCAN_USB_EP_MSGOUT | USB_DIR_IN)
36
37/* PCAN-USB command struct */
38#define PCAN_USB_CMD_FUNC 0
39#define PCAN_USB_CMD_NUM 1
40#define PCAN_USB_CMD_ARGS 2
41#define PCAN_USB_CMD_ARGS_LEN 14
42#define PCAN_USB_CMD_LEN (PCAN_USB_CMD_ARGS + \
43 PCAN_USB_CMD_ARGS_LEN)
44
45/* PCAN-USB command timeout (ms.) */
46#define PCAN_USB_COMMAND_TIMEOUT 1000
47
48/* PCAN-USB startup timeout (ms.) */
49#define PCAN_USB_STARTUP_TIMEOUT 10
50
51/* PCAN-USB rx/tx buffers size */
52#define PCAN_USB_RX_BUFFER_SIZE 64
53#define PCAN_USB_TX_BUFFER_SIZE 64
54
55#define PCAN_USB_MSG_HEADER_LEN 2
56
57/* PCAN-USB adapter internal clock (MHz) */
58#define PCAN_USB_CRYSTAL_HZ 16000000
59
60/* PCAN-USB USB message record status/len field */
61#define PCAN_USB_STATUSLEN_TIMESTAMP (1 << 7)
62#define PCAN_USB_STATUSLEN_INTERNAL (1 << 6)
63#define PCAN_USB_STATUSLEN_EXT_ID (1 << 5)
64#define PCAN_USB_STATUSLEN_RTR (1 << 4)
65#define PCAN_USB_STATUSLEN_DLC (0xf)
66
67/* PCAN-USB error flags */
68#define PCAN_USB_ERROR_TXFULL 0x01
69#define PCAN_USB_ERROR_RXQOVR 0x02
70#define PCAN_USB_ERROR_BUS_LIGHT 0x04
71#define PCAN_USB_ERROR_BUS_HEAVY 0x08
72#define PCAN_USB_ERROR_BUS_OFF 0x10
73#define PCAN_USB_ERROR_RXQEMPTY 0x20
74#define PCAN_USB_ERROR_QOVR 0x40
75#define PCAN_USB_ERROR_TXQFULL 0x80
76
77/* SJA1000 modes */
78#define SJA1000_MODE_NORMAL 0x00
79#define SJA1000_MODE_INIT 0x01
80
81/*
82 * tick duration = 42.666 us =>
83 * (tick_number * 44739243) >> 20 ~ (tick_number * 42666) / 1000
84 * accuracy = 10^-7
85 */
86#define PCAN_USB_TS_DIV_SHIFTER 20
87#define PCAN_USB_TS_US_PER_TICK 44739243
88
89/* PCAN-USB messages record types */
90#define PCAN_USB_REC_ERROR 1
91#define PCAN_USB_REC_ANALOG 2
92#define PCAN_USB_REC_BUSLOAD 3
93#define PCAN_USB_REC_TS 4
94#define PCAN_USB_REC_BUSEVT 5
95
96/* private to PCAN-USB adapter */
97struct pcan_usb {
98 struct peak_usb_device dev;
99 struct peak_time_ref time_ref;
100 struct timer_list restart_timer;
101};
102
103/* incoming message context for decoding */
104struct pcan_usb_msg_context {
105 u16 ts16;
106 u8 prev_ts8;
107 u8 *ptr;
108 u8 *end;
109 u8 rec_cnt;
110 u8 rec_idx;
111 u8 rec_data_idx;
112 struct net_device *netdev;
113 struct pcan_usb *pdev;
114};
115
116/*
117 * send a command
118 */
119static int pcan_usb_send_cmd(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
120{
121 int err;
122 int actual_length;
123
124 /* usb device unregistered? */
125 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
126 return 0;
127
128 dev->cmd_buf[PCAN_USB_CMD_FUNC] = f;
129 dev->cmd_buf[PCAN_USB_CMD_NUM] = n;
130
131 if (p)
132 memcpy(dev->cmd_buf + PCAN_USB_CMD_ARGS,
133 p, PCAN_USB_CMD_ARGS_LEN);
134
135 err = usb_bulk_msg(dev->udev,
136 usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
137 dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
138 PCAN_USB_COMMAND_TIMEOUT);
139 if (err)
140 netdev_err(dev->netdev,
141 "sending cmd f=0x%x n=0x%x failure: %d\n",
142 f, n, err);
143 return err;
144}
145
146/*
147 * send a command then wait for its response
148 */
149static int pcan_usb_wait_rsp(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
150{
151 int err;
152 int actual_length;
153
154 /* usb device unregistered? */
155 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
156 return 0;
157
158 /* first, send command */
159 err = pcan_usb_send_cmd(dev, f, n, NULL);
160 if (err)
161 return err;
162
163 err = usb_bulk_msg(dev->udev,
164 usb_rcvbulkpipe(dev->udev, PCAN_USB_EP_CMDIN),
165 dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
166 PCAN_USB_COMMAND_TIMEOUT);
167 if (err)
168 netdev_err(dev->netdev,
169 "waiting rsp f=0x%x n=0x%x failure: %d\n", f, n, err);
170 else if (p)
171 memcpy(p, dev->cmd_buf + PCAN_USB_CMD_ARGS,
172 PCAN_USB_CMD_ARGS_LEN);
173
174 return err;
175}
176
177static int pcan_usb_set_sja1000(struct peak_usb_device *dev, u8 mode)
178{
179 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
180 [1] = mode,
181 };
182
183 return pcan_usb_send_cmd(dev, 9, 2, args);
184}
185
186static int pcan_usb_set_bus(struct peak_usb_device *dev, u8 onoff)
187{
188 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
189 [0] = !!onoff,
190 };
191
192 return pcan_usb_send_cmd(dev, 3, 2, args);
193}
194
195static int pcan_usb_set_silent(struct peak_usb_device *dev, u8 onoff)
196{
197 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
198 [0] = !!onoff,
199 };
200
201 return pcan_usb_send_cmd(dev, 3, 3, args);
202}
203
204static int pcan_usb_set_ext_vcc(struct peak_usb_device *dev, u8 onoff)
205{
206 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
207 [0] = !!onoff,
208 };
209
210 return pcan_usb_send_cmd(dev, 10, 2, args);
211}
212
213/*
214 * set bittiming value to can
215 */
216static int pcan_usb_set_bittiming(struct peak_usb_device *dev,
217 struct can_bittiming *bt)
218{
219 u8 args[PCAN_USB_CMD_ARGS_LEN];
220 u8 btr0, btr1;
221
222 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
223 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
224 (((bt->phase_seg2 - 1) & 0x7) << 4);
225 if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
226 btr1 |= 0x80;
227
228 netdev_info(dev->netdev, "setting BTR0=0x%02x BTR1=0x%02x\n",
229 btr0, btr1);
230
231 args[0] = btr1;
232 args[1] = btr0;
233
234 return pcan_usb_send_cmd(dev, 1, 2, args);
235}
236
237/*
238 * init/reset can
239 */
240static int pcan_usb_write_mode(struct peak_usb_device *dev, u8 onoff)
241{
242 int err;
243
244 err = pcan_usb_set_bus(dev, onoff);
245 if (err)
246 return err;
247
248 if (!onoff) {
249 err = pcan_usb_set_sja1000(dev, SJA1000_MODE_INIT);
250 } else {
251 /* the PCAN-USB needs time to init */
252 set_current_state(TASK_INTERRUPTIBLE);
253 schedule_timeout(msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
254 }
255
256 return err;
257}
258
259/*
260 * handle end of waiting for the device to reset
261 */
262static void pcan_usb_restart(unsigned long arg)
263{
264 /* notify candev and netdev */
265 peak_usb_restart_complete((struct peak_usb_device *)arg);
266}
267
268/*
269 * handle the submission of the restart urb
270 */
271static void pcan_usb_restart_pending(struct urb *urb)
272{
273 struct pcan_usb *pdev = urb->context;
274
275 /* the PCAN-USB needs time to restart */
276 mod_timer(&pdev->restart_timer,
277 jiffies + msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
278
279 /* can delete usb resources */
280 peak_usb_async_complete(urb);
281}
282
283/*
284 * handle asynchronous restart
285 */
286static int pcan_usb_restart_async(struct peak_usb_device *dev, struct urb *urb,
287 u8 *buf)
288{
289 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
290
291 if (timer_pending(&pdev->restart_timer))
292 return -EBUSY;
293
294 /* set bus on */
295 buf[PCAN_USB_CMD_FUNC] = 3;
296 buf[PCAN_USB_CMD_NUM] = 2;
297 buf[PCAN_USB_CMD_ARGS] = 1;
298
299 usb_fill_bulk_urb(urb, dev->udev,
300 usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
301 buf, PCAN_USB_CMD_LEN,
302 pcan_usb_restart_pending, pdev);
303
304 return usb_submit_urb(urb, GFP_ATOMIC);
305}
306
307/*
308 * read serial number from device
309 */
310static int pcan_usb_get_serial(struct peak_usb_device *dev, u32 *serial_number)
311{
312 u8 args[PCAN_USB_CMD_ARGS_LEN];
313 int err;
314
315 err = pcan_usb_wait_rsp(dev, 6, 1, args);
316 if (err) {
317 netdev_err(dev->netdev, "getting serial failure: %d\n", err);
318 } else if (serial_number) {
319 u32 tmp32;
320
321 memcpy(&tmp32, args, 4);
322 *serial_number = le32_to_cpu(tmp32);
323 }
324
325 return err;
326}
327
328/*
329 * read device id from device
330 */
331static int pcan_usb_get_device_id(struct peak_usb_device *dev, u32 *device_id)
332{
333 u8 args[PCAN_USB_CMD_ARGS_LEN];
334 int err;
335
336 err = pcan_usb_wait_rsp(dev, 4, 1, args);
337 if (err)
338 netdev_err(dev->netdev, "getting device id failure: %d\n", err);
339 else if (device_id)
340 *device_id = args[0];
341
342 return err;
343}
344
345/*
346 * update current time ref with received timestamp
347 */
348static int pcan_usb_update_ts(struct pcan_usb_msg_context *mc)
349{
350 u16 tmp16;
351
352 if ((mc->ptr+2) > mc->end)
353 return -EINVAL;
354
355 memcpy(&tmp16, mc->ptr, 2);
356
357 mc->ts16 = le16_to_cpu(tmp16);
358
359 if (mc->rec_idx > 0)
360 peak_usb_update_ts_now(&mc->pdev->time_ref, mc->ts16);
361 else
362 peak_usb_set_ts_now(&mc->pdev->time_ref, mc->ts16);
363
364 return 0;
365}
366
367/*
368 * decode received timestamp
369 */
370static int pcan_usb_decode_ts(struct pcan_usb_msg_context *mc, u8 first_packet)
371{
372 /* only 1st packet supplies a word timestamp */
373 if (first_packet) {
374 u16 tmp16;
375
376 if ((mc->ptr + 2) > mc->end)
377 return -EINVAL;
378
379 memcpy(&tmp16, mc->ptr, 2);
380 mc->ptr += 2;
381
382 mc->ts16 = le16_to_cpu(tmp16);
383 mc->prev_ts8 = mc->ts16 & 0x00ff;
384 } else {
385 u8 ts8;
386
387 if ((mc->ptr + 1) > mc->end)
388 return -EINVAL;
389
390 ts8 = *mc->ptr++;
391
392 if (ts8 < mc->prev_ts8)
393 mc->ts16 += 0x100;
394
395 mc->ts16 &= 0xff00;
396 mc->ts16 |= ts8;
397 mc->prev_ts8 = ts8;
398 }
399
400 return 0;
401}
402
403static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
404 u8 status_len)
405{
406 struct sk_buff *skb;
407 struct can_frame *cf;
408 struct timeval tv;
409 enum can_state new_state;
410
411 /* ignore this error until 1st ts received */
412 if (n == PCAN_USB_ERROR_QOVR)
413 if (!mc->pdev->time_ref.tick_count)
414 return 0;
415
416 new_state = mc->pdev->dev.can.state;
417
418 switch (mc->pdev->dev.can.state) {
419 case CAN_STATE_ERROR_ACTIVE:
420 if (n & PCAN_USB_ERROR_BUS_LIGHT) {
421 new_state = CAN_STATE_ERROR_WARNING;
422 break;
423 }
424
425 case CAN_STATE_ERROR_WARNING:
426 if (n & PCAN_USB_ERROR_BUS_HEAVY) {
427 new_state = CAN_STATE_ERROR_PASSIVE;
428 break;
429 }
430 if (n & PCAN_USB_ERROR_BUS_OFF) {
431 new_state = CAN_STATE_BUS_OFF;
432 break;
433 }
434 if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
435 /*
436 * trick to bypass next comparison and process other
437 * errors
438 */
439 new_state = CAN_STATE_MAX;
440 break;
441 }
442 if ((n & PCAN_USB_ERROR_BUS_LIGHT) == 0) {
443 /* no error (back to active state) */
444 mc->pdev->dev.can.state = CAN_STATE_ERROR_ACTIVE;
445 return 0;
446 }
447 break;
448
449 case CAN_STATE_ERROR_PASSIVE:
450 if (n & PCAN_USB_ERROR_BUS_OFF) {
451 new_state = CAN_STATE_BUS_OFF;
452 break;
453 }
454 if (n & PCAN_USB_ERROR_BUS_LIGHT) {
455 new_state = CAN_STATE_ERROR_WARNING;
456 break;
457 }
458 if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
459 /*
460 * trick to bypass next comparison and process other
461 * errors
462 */
463 new_state = CAN_STATE_MAX;
464 break;
465 }
466
467 if ((n & PCAN_USB_ERROR_BUS_HEAVY) == 0) {
468 /* no error (back to active state) */
469 mc->pdev->dev.can.state = CAN_STATE_ERROR_ACTIVE;
470 return 0;
471 }
472 break;
473
474 default:
475 /* do nothing waiting for restart */
476 return 0;
477 }
478
479 /* donot post any error if current state didn't change */
480 if (mc->pdev->dev.can.state == new_state)
481 return 0;
482
483 /* allocate an skb to store the error frame */
484 skb = alloc_can_err_skb(mc->netdev, &cf);
485 if (!skb)
486 return -ENOMEM;
487
488 switch (new_state) {
489 case CAN_STATE_BUS_OFF:
490 cf->can_id |= CAN_ERR_BUSOFF;
491 can_bus_off(mc->netdev);
492 break;
493
494 case CAN_STATE_ERROR_PASSIVE:
495 cf->can_id |= CAN_ERR_CRTL;
496 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE |
497 CAN_ERR_CRTL_RX_PASSIVE;
498 mc->pdev->dev.can.can_stats.error_passive++;
499 break;
500
501 case CAN_STATE_ERROR_WARNING:
502 cf->can_id |= CAN_ERR_CRTL;
503 cf->data[1] |= CAN_ERR_CRTL_TX_WARNING |
504 CAN_ERR_CRTL_RX_WARNING;
505 mc->pdev->dev.can.can_stats.error_warning++;
506 break;
507
508 default:
509 /* CAN_STATE_MAX (trick to handle other errors) */
510 cf->can_id |= CAN_ERR_CRTL;
511 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
512 mc->netdev->stats.rx_over_errors++;
513 mc->netdev->stats.rx_errors++;
514
515 new_state = mc->pdev->dev.can.state;
516 break;
517 }
518
519 mc->pdev->dev.can.state = new_state;
520
521 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
522 struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
523
524 peak_usb_get_ts_tv(&mc->pdev->time_ref, mc->ts16, &tv);
525 hwts->hwtstamp = timeval_to_ktime(tv);
526 }
527
528 netif_rx(skb);
529 mc->netdev->stats.rx_packets++;
530 mc->netdev->stats.rx_bytes += cf->can_dlc;
531
532 return 0;
533}
534
535/*
536 * decode non-data usb message
537 */
538static int pcan_usb_decode_status(struct pcan_usb_msg_context *mc,
539 u8 status_len)
540{
541 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
542 u8 f, n;
543 int err;
544
545 /* check whether function and number can be read */
546 if ((mc->ptr + 2) > mc->end)
547 return -EINVAL;
548
549 f = mc->ptr[PCAN_USB_CMD_FUNC];
550 n = mc->ptr[PCAN_USB_CMD_NUM];
551 mc->ptr += PCAN_USB_CMD_ARGS;
552
553 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
554 int err = pcan_usb_decode_ts(mc, !mc->rec_idx);
555
556 if (err)
557 return err;
558 }
559
560 switch (f) {
561 case PCAN_USB_REC_ERROR:
562 err = pcan_usb_decode_error(mc, n, status_len);
563 if (err)
564 return err;
565 break;
566
567 case PCAN_USB_REC_ANALOG:
568 /* analog values (ignored) */
569 rec_len = 2;
570 break;
571
572 case PCAN_USB_REC_BUSLOAD:
573 /* bus load (ignored) */
574 rec_len = 1;
575 break;
576
577 case PCAN_USB_REC_TS:
578 /* only timestamp */
579 if (pcan_usb_update_ts(mc))
580 return -EINVAL;
581 break;
582
583 case PCAN_USB_REC_BUSEVT:
584 /* error frame/bus event */
585 if (n & PCAN_USB_ERROR_TXQFULL)
586 netdev_dbg(mc->netdev, "device Tx queue full)\n");
587 break;
588 default:
589 netdev_err(mc->netdev, "unexpected function %u\n", f);
590 break;
591 }
592
593 if ((mc->ptr + rec_len) > mc->end)
594 return -EINVAL;
595
596 mc->ptr += rec_len;
597
598 return 0;
599}
600
601/*
602 * decode data usb message
603 */
604static int pcan_usb_decode_data(struct pcan_usb_msg_context *mc, u8 status_len)
605{
606 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
607 struct sk_buff *skb;
608 struct can_frame *cf;
609 struct timeval tv;
610 struct skb_shared_hwtstamps *hwts;
611
612 skb = alloc_can_skb(mc->netdev, &cf);
613 if (!skb)
614 return -ENOMEM;
615
616 if (status_len & PCAN_USB_STATUSLEN_EXT_ID) {
617 u32 tmp32;
618
619 if ((mc->ptr + 4) > mc->end)
620 goto decode_failed;
621
622 memcpy(&tmp32, mc->ptr, 4);
623 mc->ptr += 4;
624
625 cf->can_id = le32_to_cpu(tmp32 >> 3) | CAN_EFF_FLAG;
626 } else {
627 u16 tmp16;
628
629 if ((mc->ptr + 2) > mc->end)
630 goto decode_failed;
631
632 memcpy(&tmp16, mc->ptr, 2);
633 mc->ptr += 2;
634
635 cf->can_id = le16_to_cpu(tmp16 >> 5);
636 }
637
638 cf->can_dlc = get_can_dlc(rec_len);
639
640 /* first data packet timestamp is a word */
641 if (pcan_usb_decode_ts(mc, !mc->rec_data_idx))
642 goto decode_failed;
643
644 /* read data */
645 memset(cf->data, 0x0, sizeof(cf->data));
646 if (status_len & PCAN_USB_STATUSLEN_RTR) {
647 cf->can_id |= CAN_RTR_FLAG;
648 } else {
649 if ((mc->ptr + rec_len) > mc->end)
650 goto decode_failed;
651
652 memcpy(cf->data, mc->ptr, rec_len);
653 mc->ptr += rec_len;
654 }
655
656 /* convert timestamp into kernel time */
657 peak_usb_get_ts_tv(&mc->pdev->time_ref, mc->ts16, &tv);
658 hwts = skb_hwtstamps(skb);
659 hwts->hwtstamp = timeval_to_ktime(tv);
660
661 /* push the skb */
662 netif_rx(skb);
663
664 /* update statistics */
665 mc->netdev->stats.rx_packets++;
666 mc->netdev->stats.rx_bytes += cf->can_dlc;
667
668 return 0;
669
670decode_failed:
671 dev_kfree_skb(skb);
672 return -EINVAL;
673}
674
675/*
676 * process incoming message
677 */
678static int pcan_usb_decode_msg(struct peak_usb_device *dev, u8 *ibuf, u32 lbuf)
679{
680 struct pcan_usb_msg_context mc = {
681 .rec_cnt = ibuf[1],
682 .ptr = ibuf + PCAN_USB_MSG_HEADER_LEN,
683 .end = ibuf + lbuf,
684 .netdev = dev->netdev,
685 .pdev = container_of(dev, struct pcan_usb, dev),
686 };
687 int err;
688
689 for (err = 0; mc.rec_idx < mc.rec_cnt && !err; mc.rec_idx++) {
690 u8 sl = *mc.ptr++;
691
692 /* handle status and error frames here */
693 if (sl & PCAN_USB_STATUSLEN_INTERNAL) {
694 err = pcan_usb_decode_status(&mc, sl);
695 /* handle normal can frames here */
696 } else {
697 err = pcan_usb_decode_data(&mc, sl);
698 mc.rec_data_idx++;
699 }
700 }
701
702 return err;
703}
704
705/*
706 * process any incoming buffer
707 */
708static int pcan_usb_decode_buf(struct peak_usb_device *dev, struct urb *urb)
709{
710 int err = 0;
711
712 if (urb->actual_length > PCAN_USB_MSG_HEADER_LEN) {
713 err = pcan_usb_decode_msg(dev, urb->transfer_buffer,
714 urb->actual_length);
715
716 } else if (urb->actual_length > 0) {
717 netdev_err(dev->netdev, "usb message length error (%u)\n",
718 urb->actual_length);
719 err = -EINVAL;
720 }
721
722 return err;
723}
724
725/*
726 * process outgoing packet
727 */
728static int pcan_usb_encode_msg(struct peak_usb_device *dev, struct sk_buff *skb,
729 u8 *obuf, size_t *size)
730{
731 struct net_device *netdev = dev->netdev;
732 struct net_device_stats *stats = &netdev->stats;
733 struct can_frame *cf = (struct can_frame *)skb->data;
734 u8 *pc;
735
736 obuf[0] = 2;
737 obuf[1] = 1;
738
739 pc = obuf + PCAN_USB_MSG_HEADER_LEN;
740
741 /* status/len byte */
742 *pc = cf->can_dlc;
743 if (cf->can_id & CAN_RTR_FLAG)
744 *pc |= PCAN_USB_STATUSLEN_RTR;
745
746 /* can id */
747 if (cf->can_id & CAN_EFF_FLAG) {
748 __le32 tmp32 = cpu_to_le32((cf->can_id & CAN_ERR_MASK) << 3);
749
750 *pc |= PCAN_USB_STATUSLEN_EXT_ID;
751 memcpy(++pc, &tmp32, 4);
752 pc += 4;
753 } else {
754 __le16 tmp16 = cpu_to_le16((cf->can_id & CAN_ERR_MASK) << 5);
755
756 memcpy(++pc, &tmp16, 2);
757 pc += 2;
758 }
759
760 /* can data */
761 if (!(cf->can_id & CAN_RTR_FLAG)) {
762 memcpy(pc, cf->data, cf->can_dlc);
763 pc += cf->can_dlc;
764 }
765
766 obuf[(*size)-1] = (u8)(stats->tx_packets & 0xff);
767
768 return 0;
769}
770
771/*
772 * start interface
773 */
774static int pcan_usb_start(struct peak_usb_device *dev)
775{
776 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
777
778 /* number of bits used in timestamps read from adapter struct */
779 peak_usb_init_time_ref(&pdev->time_ref, &pcan_usb);
780
781 /* if revision greater than 3, can put silent mode on/off */
782 if (dev->device_rev > 3) {
783 int err;
784
785 err = pcan_usb_set_silent(dev,
786 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
787 if (err)
788 return err;
789 }
790
791 return pcan_usb_set_ext_vcc(dev, 0);
792}
793
794static int pcan_usb_init(struct peak_usb_device *dev)
795{
796 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
797 u32 serial_number;
798 int err;
799
800 /* initialize a timer needed to wait for hardware restart */
801 init_timer(&pdev->restart_timer);
802 pdev->restart_timer.function = pcan_usb_restart;
803 pdev->restart_timer.data = (unsigned long)dev;
804
805 /*
806 * explicit use of dev_xxx() instead of netdev_xxx() here:
807 * information displayed are related to the device itself, not
808 * to the canx netdevice.
809 */
810 err = pcan_usb_get_serial(dev, &serial_number);
811 if (err) {
812 dev_err(dev->netdev->dev.parent,
813 "unable to read %s serial number (err %d)\n",
814 pcan_usb.name, err);
815 return err;
816 }
817
818 dev_info(dev->netdev->dev.parent,
819 "PEAK-System %s adapter hwrev %u serial %08X (%u channel)\n",
820 pcan_usb.name, dev->device_rev, serial_number,
821 pcan_usb.ctrl_count);
822
823 return 0;
824}
825
826/*
827 * probe function for new PCAN-USB usb interface
828 */
829static int pcan_usb_probe(struct usb_interface *intf)
830{
831 struct usb_host_interface *if_desc;
832 int i;
833
834 if_desc = intf->altsetting;
835
836 /* check interface endpoint addresses */
837 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
838 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
839
840 switch (ep->bEndpointAddress) {
841 case PCAN_USB_EP_CMDOUT:
842 case PCAN_USB_EP_CMDIN:
843 case PCAN_USB_EP_MSGOUT:
844 case PCAN_USB_EP_MSGIN:
845 break;
846 default:
847 return -ENODEV;
848 }
849 }
850
851 return 0;
852}
853
854/*
855 * describe the PCAN-USB adapter
856 */
857struct peak_usb_adapter pcan_usb = {
858 .name = "PCAN-USB",
859 .device_id = PCAN_USB_PRODUCT_ID,
860 .ctrl_count = 1,
861 .clock = {
862 .freq = PCAN_USB_CRYSTAL_HZ / 2 ,
863 },
864 .bittiming_const = {
865 .name = "pcan_usb",
866 .tseg1_min = 1,
867 .tseg1_max = 16,
868 .tseg2_min = 1,
869 .tseg2_max = 8,
870 .sjw_max = 4,
871 .brp_min = 1,
872 .brp_max = 64,
873 .brp_inc = 1,
874 },
875
876 /* size of device private data */
877 .sizeof_dev_private = sizeof(struct pcan_usb),
878
879 /* timestamps usage */
880 .ts_used_bits = 16,
881 .ts_period = 24575, /* calibration period in ts. */
882 .us_per_ts_scale = PCAN_USB_TS_US_PER_TICK, /* us=(ts*scale) */
883 .us_per_ts_shift = PCAN_USB_TS_DIV_SHIFTER, /* >> shift */
884
885 /* give here messages in/out endpoints */
886 .ep_msg_in = PCAN_USB_EP_MSGIN,
887 .ep_msg_out = {PCAN_USB_EP_MSGOUT},
888
889 /* size of rx/tx usb buffers */
890 .rx_buffer_size = PCAN_USB_RX_BUFFER_SIZE,
891 .tx_buffer_size = PCAN_USB_TX_BUFFER_SIZE,
892
893 /* device callbacks */
894 .intf_probe = pcan_usb_probe,
895 .dev_init = pcan_usb_init,
896 .dev_set_bus = pcan_usb_write_mode,
897 .dev_set_bittiming = pcan_usb_set_bittiming,
898 .dev_get_device_id = pcan_usb_get_device_id,
899 .dev_decode_buf = pcan_usb_decode_buf,
900 .dev_encode_msg = pcan_usb_encode_msg,
901 .dev_start = pcan_usb_start,
902 .dev_restart_async = pcan_usb_restart_async,
903};
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.c b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
deleted file mode 100644
index d9290ea788e..00000000000
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
+++ /dev/null
@@ -1,946 +0,0 @@
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
32MODULE_AUTHOR("Stephane Grosjean <s.grosjean@peak-system.com>");
33MODULE_DESCRIPTION("CAN driver for PEAK-System USB adapters");
34MODULE_LICENSE("GPL v2");
35
36/* Table of devices that work with this driver */
37static 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 {} /* Terminating entry */
41};
42
43MODULE_DEVICE_TABLE(usb, peak_usb_table);
44
45/* List of supported PCAN-USB adapters (NULL terminated list) */
46static struct peak_usb_adapter *peak_usb_adapters_list[] = {
47 &pcan_usb,
48 &pcan_usb_pro,
49 NULL,
50};
51
52/*
53 * dump memory
54 */
55#define DUMP_WIDTH 16
56void pcan_dump_mem(char *prompt, void *p, int l)
57{
58 pr_info("%s dumping %s (%d bytes):\n",
59 PCAN_USB_DRIVER_NAME, prompt ? prompt : "memory", l);
60 print_hex_dump(KERN_INFO, PCAN_USB_DRIVER_NAME " ", DUMP_PREFIX_NONE,
61 DUMP_WIDTH, 1, p, l, false);
62}
63
64/*
65 * initialize a time_ref object with usb adapter own settings
66 */
67void peak_usb_init_time_ref(struct peak_time_ref *time_ref,
68 struct peak_usb_adapter *adapter)
69{
70 if (time_ref) {
71 memset(time_ref, 0, sizeof(struct peak_time_ref));
72 time_ref->adapter = adapter;
73 }
74}
75
76static void peak_usb_add_us(struct timeval *tv, u32 delta_us)
77{
78 /* number of s. to add to final time */
79 u32 delta_s = delta_us / 1000000;
80
81 delta_us -= delta_s * 1000000;
82
83 tv->tv_usec += delta_us;
84 if (tv->tv_usec >= 1000000) {
85 tv->tv_usec -= 1000000;
86 delta_s++;
87 }
88 tv->tv_sec += delta_s;
89}
90
91/*
92 * sometimes, another now may be more recent than current one...
93 */
94void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
95{
96 time_ref->ts_dev_2 = ts_now;
97
98 /* should wait at least two passes before computing */
99 if (time_ref->tv_host.tv_sec > 0) {
100 u32 delta_ts = time_ref->ts_dev_2 - time_ref->ts_dev_1;
101
102 if (time_ref->ts_dev_2 < time_ref->ts_dev_1)
103 delta_ts &= (1 << time_ref->adapter->ts_used_bits) - 1;
104
105 time_ref->ts_total += delta_ts;
106 }
107}
108
109/*
110 * register device timestamp as now
111 */
112void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
113{
114 if (time_ref->tv_host_0.tv_sec == 0) {
115 /* use monotonic clock to correctly compute further deltas */
116 time_ref->tv_host_0 = ktime_to_timeval(ktime_get());
117 time_ref->tv_host.tv_sec = 0;
118 } else {
119 /*
120 * delta_us should not be >= 2^32 => delta_s should be < 4294
121 * handle 32-bits wrapping here: if count of s. reaches 4200,
122 * reset counters and change time base
123 */
124 if (time_ref->tv_host.tv_sec != 0) {
125 u32 delta_s = time_ref->tv_host.tv_sec
126 - time_ref->tv_host_0.tv_sec;
127 if (delta_s > 4200) {
128 time_ref->tv_host_0 = time_ref->tv_host;
129 time_ref->ts_total = 0;
130 }
131 }
132
133 time_ref->tv_host = ktime_to_timeval(ktime_get());
134 time_ref->tick_count++;
135 }
136
137 time_ref->ts_dev_1 = time_ref->ts_dev_2;
138 peak_usb_update_ts_now(time_ref, ts_now);
139}
140
141/*
142 * compute timeval according to current ts and time_ref data
143 */
144void peak_usb_get_ts_tv(struct peak_time_ref *time_ref, u32 ts,
145 struct timeval *tv)
146{
147 /* protect from getting timeval before setting now */
148 if (time_ref->tv_host.tv_sec > 0) {
149 u64 delta_us;
150
151 delta_us = ts - time_ref->ts_dev_2;
152 if (ts < time_ref->ts_dev_2)
153 delta_us &= (1 << time_ref->adapter->ts_used_bits) - 1;
154
155 delta_us += time_ref->ts_total;
156
157 delta_us *= time_ref->adapter->us_per_ts_scale;
158 delta_us >>= time_ref->adapter->us_per_ts_shift;
159
160 *tv = time_ref->tv_host_0;
161 peak_usb_add_us(tv, (u32)delta_us);
162 } else {
163 *tv = ktime_to_timeval(ktime_get());
164 }
165}
166
167/*
168 * callback for bulk Rx urb
169 */
170static void peak_usb_read_bulk_callback(struct urb *urb)
171{
172 struct peak_usb_device *dev = urb->context;
173 struct net_device *netdev;
174 int err;
175
176 netdev = dev->netdev;
177
178 if (!netif_device_present(netdev))
179 return;
180
181 /* check reception status */
182 switch (urb->status) {
183 case 0:
184 /* success */
185 break;
186
187 case -EILSEQ:
188 case -ENOENT:
189 case -ECONNRESET:
190 case -ESHUTDOWN:
191 return;
192
193 default:
194 if (net_ratelimit())
195 netdev_err(netdev,
196 "Rx urb aborted (%d)\n", urb->status);
197 goto resubmit_urb;
198 }
199
200 /* protect from any incoming empty msgs */
201 if ((urb->actual_length > 0) && (dev->adapter->dev_decode_buf)) {
202 /* handle these kinds of msgs only if _start callback called */
203 if (dev->state & PCAN_USB_STATE_STARTED) {
204 err = dev->adapter->dev_decode_buf(dev, urb);
205 if (err)
206 pcan_dump_mem("received usb message",
207 urb->transfer_buffer,
208 urb->transfer_buffer_length);
209 }
210 }
211
212resubmit_urb:
213 usb_fill_bulk_urb(urb, dev->udev,
214 usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
215 urb->transfer_buffer, dev->adapter->rx_buffer_size,
216 peak_usb_read_bulk_callback, dev);
217
218 usb_anchor_urb(urb, &dev->rx_submitted);
219 err = usb_submit_urb(urb, GFP_ATOMIC);
220 if (!err)
221 return;
222
223 usb_unanchor_urb(urb);
224
225 if (err == -ENODEV)
226 netif_device_detach(netdev);
227 else
228 netdev_err(netdev, "failed resubmitting read bulk urb: %d\n",
229 err);
230}
231
232/*
233 * callback for bulk Tx urb
234 */
235static void peak_usb_write_bulk_callback(struct urb *urb)
236{
237 struct peak_tx_urb_context *context = urb->context;
238 struct peak_usb_device *dev;
239 struct net_device *netdev;
240
241 BUG_ON(!context);
242
243 dev = context->dev;
244 netdev = dev->netdev;
245
246 atomic_dec(&dev->active_tx_urbs);
247
248 if (!netif_device_present(netdev))
249 return;
250
251 /* check tx status */
252 switch (urb->status) {
253 case 0:
254 /* transmission complete */
255 netdev->stats.tx_packets++;
256 netdev->stats.tx_bytes += context->dlc;
257
258 /* prevent tx timeout */
259 netdev->trans_start = jiffies;
260 break;
261
262 default:
263 if (net_ratelimit())
264 netdev_err(netdev, "Tx urb aborted (%d)\n",
265 urb->status);
266 case -EPROTO:
267 case -ENOENT:
268 case -ECONNRESET:
269 case -ESHUTDOWN:
270
271 break;
272 }
273
274 /* should always release echo skb and corresponding context */
275 can_get_echo_skb(netdev, context->echo_index);
276 context->echo_index = PCAN_USB_MAX_TX_URBS;
277
278 /* do wakeup tx queue in case of success only */
279 if (!urb->status)
280 netif_wake_queue(netdev);
281}
282
283/*
284 * called by netdev to send one skb on the CAN interface.
285 */
286static netdev_tx_t peak_usb_ndo_start_xmit(struct sk_buff *skb,
287 struct net_device *netdev)
288{
289 struct peak_usb_device *dev = netdev_priv(netdev);
290 struct peak_tx_urb_context *context = NULL;
291 struct net_device_stats *stats = &netdev->stats;
292 struct can_frame *cf = (struct can_frame *)skb->data;
293 struct urb *urb;
294 u8 *obuf;
295 int i, err;
296 size_t size = dev->adapter->tx_buffer_size;
297
298 if (can_dropped_invalid_skb(netdev, skb))
299 return NETDEV_TX_OK;
300
301 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++)
302 if (dev->tx_contexts[i].echo_index == PCAN_USB_MAX_TX_URBS) {
303 context = dev->tx_contexts + i;
304 break;
305 }
306
307 if (!context) {
308 /* should not occur except during restart */
309 return NETDEV_TX_BUSY;
310 }
311
312 urb = context->urb;
313 obuf = urb->transfer_buffer;
314
315 err = dev->adapter->dev_encode_msg(dev, skb, obuf, &size);
316 if (err) {
317 if (net_ratelimit())
318 netdev_err(netdev, "packet dropped\n");
319 dev_kfree_skb(skb);
320 stats->tx_dropped++;
321 return NETDEV_TX_OK;
322 }
323
324 context->echo_index = i;
325 context->dlc = cf->can_dlc;
326
327 usb_anchor_urb(urb, &dev->tx_submitted);
328
329 can_put_echo_skb(skb, netdev, context->echo_index);
330
331 atomic_inc(&dev->active_tx_urbs);
332
333 err = usb_submit_urb(urb, GFP_ATOMIC);
334 if (err) {
335 can_free_echo_skb(netdev, context->echo_index);
336
337 usb_unanchor_urb(urb);
338
339 /* this context is not used in fact */
340 context->echo_index = PCAN_USB_MAX_TX_URBS;
341
342 atomic_dec(&dev->active_tx_urbs);
343
344 switch (err) {
345 case -ENODEV:
346 netif_device_detach(netdev);
347 break;
348 default:
349 netdev_warn(netdev, "tx urb submitting failed err=%d\n",
350 err);
351 case -ENOENT:
352 /* cable unplugged */
353 stats->tx_dropped++;
354 }
355 } else {
356 netdev->trans_start = jiffies;
357
358 /* slow down tx path */
359 if (atomic_read(&dev->active_tx_urbs) >= PCAN_USB_MAX_TX_URBS)
360 netif_stop_queue(netdev);
361 }
362
363 return NETDEV_TX_OK;
364}
365
366/*
367 * start the CAN interface.
368 * Rx and Tx urbs are allocated here. Rx urbs are submitted here.
369 */
370static int peak_usb_start(struct peak_usb_device *dev)
371{
372 struct net_device *netdev = dev->netdev;
373 int err, i;
374
375 for (i = 0; i < PCAN_USB_MAX_RX_URBS; i++) {
376 struct urb *urb;
377 u8 *buf;
378
379 /* create a URB, and a buffer for it, to receive usb messages */
380 urb = usb_alloc_urb(0, GFP_KERNEL);
381 if (!urb) {
382 netdev_err(netdev, "No memory left for URBs\n");
383 err = -ENOMEM;
384 break;
385 }
386
387 buf = kmalloc(dev->adapter->rx_buffer_size, GFP_KERNEL);
388 if (!buf) {
389 netdev_err(netdev, "No memory left for USB buffer\n");
390 usb_free_urb(urb);
391 err = -ENOMEM;
392 break;
393 }
394
395 usb_fill_bulk_urb(urb, dev->udev,
396 usb_rcvbulkpipe(dev->udev, dev->ep_msg_in),
397 buf, dev->adapter->rx_buffer_size,
398 peak_usb_read_bulk_callback, dev);
399
400 /* ask last usb_free_urb() to also kfree() transfer_buffer */
401 urb->transfer_flags |= URB_FREE_BUFFER;
402 usb_anchor_urb(urb, &dev->rx_submitted);
403
404 err = usb_submit_urb(urb, GFP_KERNEL);
405 if (err) {
406 if (err == -ENODEV)
407 netif_device_detach(dev->netdev);
408
409 usb_unanchor_urb(urb);
410 kfree(buf);
411 usb_free_urb(urb);
412 break;
413 }
414
415 /* drop reference, USB core will take care of freeing it */
416 usb_free_urb(urb);
417 }
418
419 /* did we submit any URBs? Warn if we was not able to submit all urbs */
420 if (i < PCAN_USB_MAX_RX_URBS) {
421 if (i == 0) {
422 netdev_err(netdev, "couldn't setup any rx URB\n");
423 return err;
424 }
425
426 netdev_warn(netdev, "rx performance may be slow\n");
427 }
428
429 /* pre-alloc tx buffers and corresponding urbs */
430 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
431 struct peak_tx_urb_context *context;
432 struct urb *urb;
433 u8 *buf;
434
435 /* create a URB and a buffer for it, to transmit usb messages */
436 urb = usb_alloc_urb(0, GFP_KERNEL);
437 if (!urb) {
438 netdev_err(netdev, "No memory left for URBs\n");
439 err = -ENOMEM;
440 break;
441 }
442
443 buf = kmalloc(dev->adapter->tx_buffer_size, GFP_KERNEL);
444 if (!buf) {
445 netdev_err(netdev, "No memory left for USB buffer\n");
446 usb_free_urb(urb);
447 err = -ENOMEM;
448 break;
449 }
450
451 context = dev->tx_contexts + i;
452 context->dev = dev;
453 context->urb = urb;
454
455 usb_fill_bulk_urb(urb, dev->udev,
456 usb_sndbulkpipe(dev->udev, dev->ep_msg_out),
457 buf, dev->adapter->tx_buffer_size,
458 peak_usb_write_bulk_callback, context);
459
460 /* ask last usb_free_urb() to also kfree() transfer_buffer */
461 urb->transfer_flags |= URB_FREE_BUFFER;
462 }
463
464 /* warn if we were not able to allocate enough tx contexts */
465 if (i < PCAN_USB_MAX_TX_URBS) {
466 if (i == 0) {
467 netdev_err(netdev, "couldn't setup any tx URB\n");
468 return err;
469 }
470
471 netdev_warn(netdev, "tx performance may be slow\n");
472 }
473
474 if (dev->adapter->dev_start) {
475 err = dev->adapter->dev_start(dev);
476 if (err)
477 goto failed;
478 }
479
480 dev->state |= PCAN_USB_STATE_STARTED;
481
482 /* can set bus on now */
483 if (dev->adapter->dev_set_bus) {
484 err = dev->adapter->dev_set_bus(dev, 1);
485 if (err)
486 goto failed;
487 }
488
489 dev->can.state = CAN_STATE_ERROR_ACTIVE;
490
491 return 0;
492
493failed:
494 if (err == -ENODEV)
495 netif_device_detach(dev->netdev);
496
497 netdev_warn(netdev, "couldn't submit control: %d\n", err);
498
499 return err;
500}
501
502/*
503 * called by netdev to open the corresponding CAN interface.
504 */
505static int peak_usb_ndo_open(struct net_device *netdev)
506{
507 struct peak_usb_device *dev = netdev_priv(netdev);
508 int err;
509
510 /* common open */
511 err = open_candev(netdev);
512 if (err)
513 return err;
514
515 /* finally start device */
516 err = peak_usb_start(dev);
517 if (err) {
518 netdev_err(netdev, "couldn't start device: %d\n", err);
519 close_candev(netdev);
520 return err;
521 }
522
523 netif_start_queue(netdev);
524
525 return 0;
526}
527
528/*
529 * unlink in-flight Rx and Tx urbs and free their memory.
530 */
531static void peak_usb_unlink_all_urbs(struct peak_usb_device *dev)
532{
533 int i;
534
535 /* free all Rx (submitted) urbs */
536 usb_kill_anchored_urbs(&dev->rx_submitted);
537
538 /* free unsubmitted Tx urbs first */
539 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
540 struct urb *urb = dev->tx_contexts[i].urb;
541
542 if (!urb ||
543 dev->tx_contexts[i].echo_index != PCAN_USB_MAX_TX_URBS) {
544 /*
545 * this urb is already released or always submitted,
546 * let usb core free by itself
547 */
548 continue;
549 }
550
551 usb_free_urb(urb);
552 dev->tx_contexts[i].urb = NULL;
553 }
554
555 /* then free all submitted Tx urbs */
556 usb_kill_anchored_urbs(&dev->tx_submitted);
557 atomic_set(&dev->active_tx_urbs, 0);
558}
559
560/*
561 * called by netdev to close the corresponding CAN interface.
562 */
563static int peak_usb_ndo_stop(struct net_device *netdev)
564{
565 struct peak_usb_device *dev = netdev_priv(netdev);
566
567 dev->state &= ~PCAN_USB_STATE_STARTED;
568 netif_stop_queue(netdev);
569
570 /* unlink all pending urbs and free used memory */
571 peak_usb_unlink_all_urbs(dev);
572
573 if (dev->adapter->dev_stop)
574 dev->adapter->dev_stop(dev);
575
576 close_candev(netdev);
577
578 dev->can.state = CAN_STATE_STOPPED;
579
580 /* can set bus off now */
581 if (dev->adapter->dev_set_bus) {
582 int err = dev->adapter->dev_set_bus(dev, 0);
583 if (err)
584 return err;
585 }
586
587 return 0;
588}
589
590/*
591 * handle end of waiting for the device to reset
592 */
593void peak_usb_restart_complete(struct peak_usb_device *dev)
594{
595 /* finally MUST update can state */
596 dev->can.state = CAN_STATE_ERROR_ACTIVE;
597
598 /* netdev queue can be awaken now */
599 netif_wake_queue(dev->netdev);
600}
601
602void peak_usb_async_complete(struct urb *urb)
603{
604 kfree(urb->transfer_buffer);
605 usb_free_urb(urb);
606}
607
608/*
609 * device (auto-)restart mechanism runs in a timer context =>
610 * MUST handle restart with asynchronous usb transfers
611 */
612static int peak_usb_restart(struct peak_usb_device *dev)
613{
614 struct urb *urb;
615 int err;
616 u8 *buf;
617
618 /*
619 * if device doesn't define any asynchronous restart handler, simply
620 * wake the netdev queue up
621 */
622 if (!dev->adapter->dev_restart_async) {
623 peak_usb_restart_complete(dev);
624 return 0;
625 }
626
627 /* first allocate a urb to handle the asynchronous steps */
628 urb = usb_alloc_urb(0, GFP_ATOMIC);
629 if (!urb) {
630 netdev_err(dev->netdev, "no memory left for urb\n");
631 return -ENOMEM;
632 }
633
634 /* also allocate enough space for the commands to send */
635 buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_ATOMIC);
636 if (!buf) {
637 netdev_err(dev->netdev, "no memory left for async cmd\n");
638 usb_free_urb(urb);
639 return -ENOMEM;
640 }
641
642 /* call the device specific handler for the restart */
643 err = dev->adapter->dev_restart_async(dev, urb, buf);
644 if (!err)
645 return 0;
646
647 kfree(buf);
648 usb_free_urb(urb);
649
650 return err;
651}
652
653/*
654 * candev callback used to change CAN mode.
655 * Warning: this is called from a timer context!
656 */
657static int peak_usb_set_mode(struct net_device *netdev, enum can_mode mode)
658{
659 struct peak_usb_device *dev = netdev_priv(netdev);
660 int err = 0;
661
662 switch (mode) {
663 case CAN_MODE_START:
664 err = peak_usb_restart(dev);
665 if (err)
666 netdev_err(netdev, "couldn't start device (err %d)\n",
667 err);
668 break;
669
670 default:
671 return -EOPNOTSUPP;
672 }
673
674 return err;
675}
676
677/*
678 * candev callback used to set device bitrate.
679 */
680static int peak_usb_set_bittiming(struct net_device *netdev)
681{
682 struct peak_usb_device *dev = netdev_priv(netdev);
683 struct can_bittiming *bt = &dev->can.bittiming;
684
685 if (dev->adapter->dev_set_bittiming) {
686 int err = dev->adapter->dev_set_bittiming(dev, bt);
687
688 if (err)
689 netdev_info(netdev, "couldn't set bitrate (err %d)\n",
690 err);
691 return err;
692 }
693
694 return 0;
695}
696
697static const struct net_device_ops peak_usb_netdev_ops = {
698 .ndo_open = peak_usb_ndo_open,
699 .ndo_stop = peak_usb_ndo_stop,
700 .ndo_start_xmit = peak_usb_ndo_start_xmit,
701};
702
703/*
704 * create one device which is attached to CAN controller #ctrl_idx of the
705 * usb adapter.
706 */
707static int peak_usb_create_dev(struct peak_usb_adapter *peak_usb_adapter,
708 struct usb_interface *intf, int ctrl_idx)
709{
710 struct usb_device *usb_dev = interface_to_usbdev(intf);
711 int sizeof_candev = peak_usb_adapter->sizeof_dev_private;
712 struct peak_usb_device *dev;
713 struct net_device *netdev;
714 int i, err;
715 u16 tmp16;
716
717 if (sizeof_candev < sizeof(struct peak_usb_device))
718 sizeof_candev = sizeof(struct peak_usb_device);
719
720 netdev = alloc_candev(sizeof_candev, PCAN_USB_MAX_TX_URBS);
721 if (!netdev) {
722 dev_err(&intf->dev, "%s: couldn't alloc candev\n",
723 PCAN_USB_DRIVER_NAME);
724 return -ENOMEM;
725 }
726
727 dev = netdev_priv(netdev);
728
729 /* allocate a buffer large enough to send commands */
730 dev->cmd_buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_KERNEL);
731 if (!dev->cmd_buf) {
732 dev_err(&intf->dev, "%s: couldn't alloc cmd buffer\n",
733 PCAN_USB_DRIVER_NAME);
734 err = -ENOMEM;
735 goto lbl_set_intf_data;
736 }
737
738 dev->udev = usb_dev;
739 dev->netdev = netdev;
740 dev->adapter = peak_usb_adapter;
741 dev->ctrl_idx = ctrl_idx;
742 dev->state = PCAN_USB_STATE_CONNECTED;
743
744 dev->ep_msg_in = peak_usb_adapter->ep_msg_in;
745 dev->ep_msg_out = peak_usb_adapter->ep_msg_out[ctrl_idx];
746
747 dev->can.clock = peak_usb_adapter->clock;
748 dev->can.bittiming_const = &peak_usb_adapter->bittiming_const;
749 dev->can.do_set_bittiming = peak_usb_set_bittiming;
750 dev->can.do_set_mode = peak_usb_set_mode;
751 dev->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES |
752 CAN_CTRLMODE_LISTENONLY;
753
754 netdev->netdev_ops = &peak_usb_netdev_ops;
755
756 netdev->flags |= IFF_ECHO; /* we support local echo */
757
758 init_usb_anchor(&dev->rx_submitted);
759
760 init_usb_anchor(&dev->tx_submitted);
761 atomic_set(&dev->active_tx_urbs, 0);
762
763 for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++)
764 dev->tx_contexts[i].echo_index = PCAN_USB_MAX_TX_URBS;
765
766 dev->prev_siblings = usb_get_intfdata(intf);
767 usb_set_intfdata(intf, dev);
768
769 SET_NETDEV_DEV(netdev, &intf->dev);
770
771 err = register_candev(netdev);
772 if (err) {
773 dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
774 goto lbl_free_cmd_buf;
775 }
776
777 if (dev->prev_siblings)
778 (dev->prev_siblings)->next_siblings = dev;
779
780 /* keep hw revision into the netdevice */
781 tmp16 = le16_to_cpu(usb_dev->descriptor.bcdDevice);
782 dev->device_rev = tmp16 >> 8;
783
784 if (dev->adapter->dev_init) {
785 err = dev->adapter->dev_init(dev);
786 if (err)
787 goto lbl_free_cmd_buf;
788 }
789
790 /* set bus off */
791 if (dev->adapter->dev_set_bus) {
792 err = dev->adapter->dev_set_bus(dev, 0);
793 if (err)
794 goto lbl_free_cmd_buf;
795 }
796
797 /* get device number early */
798 if (dev->adapter->dev_get_device_id)
799 dev->adapter->dev_get_device_id(dev, &dev->device_number);
800
801 netdev_info(netdev, "attached to %s channel %u (device %u)\n",
802 peak_usb_adapter->name, ctrl_idx, dev->device_number);
803
804 return 0;
805
806lbl_free_cmd_buf:
807 kfree(dev->cmd_buf);
808
809lbl_set_intf_data:
810 usb_set_intfdata(intf, dev->prev_siblings);
811 free_candev(netdev);
812
813 return err;
814}
815
816/*
817 * called by the usb core when the device is unplugged from the system
818 */
819static void peak_usb_disconnect(struct usb_interface *intf)
820{
821 struct peak_usb_device *dev;
822
823 /* unregister as many netdev devices as siblings */
824 for (dev = usb_get_intfdata(intf); dev; dev = dev->prev_siblings) {
825 struct net_device *netdev = dev->netdev;
826 char name[IFNAMSIZ];
827
828 dev->state &= ~PCAN_USB_STATE_CONNECTED;
829 strncpy(name, netdev->name, IFNAMSIZ);
830
831 unregister_netdev(netdev);
832 free_candev(netdev);
833
834 kfree(dev->cmd_buf);
835 dev->next_siblings = NULL;
836 if (dev->adapter->dev_free)
837 dev->adapter->dev_free(dev);
838
839 dev_info(&intf->dev, "%s removed\n", name);
840 }
841
842 usb_set_intfdata(intf, NULL);
843}
844
845/*
846 * probe function for new PEAK-System devices
847 */
848static int peak_usb_probe(struct usb_interface *intf,
849 const struct usb_device_id *id)
850{
851 struct usb_device *usb_dev = interface_to_usbdev(intf);
852 struct peak_usb_adapter *peak_usb_adapter, **pp;
853 int i, err = -ENOMEM;
854
855 usb_dev = interface_to_usbdev(intf);
856
857 /* get corresponding PCAN-USB adapter */
858 for (pp = peak_usb_adapters_list; *pp; pp++)
859 if ((*pp)->device_id == usb_dev->descriptor.idProduct)
860 break;
861
862 peak_usb_adapter = *pp;
863 if (!peak_usb_adapter) {
864 /* should never come except device_id bad usage in this file */
865 pr_err("%s: didn't find device id. 0x%x in devices list\n",
866 PCAN_USB_DRIVER_NAME, usb_dev->descriptor.idProduct);
867 return -ENODEV;
868 }
869
870 /* got corresponding adapter: check if it handles current interface */
871 if (peak_usb_adapter->intf_probe) {
872 err = peak_usb_adapter->intf_probe(intf);
873 if (err)
874 return err;
875 }
876
877 for (i = 0; i < peak_usb_adapter->ctrl_count; i++) {
878 err = peak_usb_create_dev(peak_usb_adapter, intf, i);
879 if (err) {
880 /* deregister already created devices */
881 peak_usb_disconnect(intf);
882 break;
883 }
884 }
885
886 return err;
887}
888
889/* usb specific object needed to register this driver with the usb subsystem */
890static struct usb_driver peak_usb_driver = {
891 .name = PCAN_USB_DRIVER_NAME,
892 .disconnect = peak_usb_disconnect,
893 .probe = peak_usb_probe,
894 .id_table = peak_usb_table,
895};
896
897static int __init peak_usb_init(void)
898{
899 int err;
900
901 /* register this driver with the USB subsystem */
902 err = usb_register(&peak_usb_driver);
903 if (err)
904 pr_err("%s: usb_register failed (err %d)\n",
905 PCAN_USB_DRIVER_NAME, err);
906
907 return err;
908}
909
910static int peak_usb_do_device_exit(struct device *d, void *arg)
911{
912 struct usb_interface *intf = to_usb_interface(d);
913 struct peak_usb_device *dev;
914
915 /* stop as many netdev devices as siblings */
916 for (dev = usb_get_intfdata(intf); dev; dev = dev->prev_siblings) {
917 struct net_device *netdev = dev->netdev;
918
919 if (netif_device_present(netdev))
920 if (dev->adapter->dev_exit)
921 dev->adapter->dev_exit(dev);
922 }
923
924 return 0;
925}
926
927static void __exit peak_usb_exit(void)
928{
929 int err;
930
931 /* last chance do send any synchronous commands here */
932 err = driver_for_each_device(&peak_usb_driver.drvwrap.driver, NULL,
933 NULL, peak_usb_do_device_exit);
934 if (err)
935 pr_err("%s: failed to stop all can devices (err %d)\n",
936 PCAN_USB_DRIVER_NAME, err);
937
938 /* deregister this driver with the USB subsystem */
939 usb_deregister(&peak_usb_driver);
940
941 pr_info("%s: PCAN-USB interfaces driver unloaded\n",
942 PCAN_USB_DRIVER_NAME);
943}
944
945module_init(peak_usb_init);
946module_exit(peak_usb_exit);
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.h b/drivers/net/can/usb/peak_usb/pcan_usb_core.h
deleted file mode 100644
index 073b47ff8ee..00000000000
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.h
+++ /dev/null
@@ -1,145 +0,0 @@
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#ifndef PCAN_USB_CORE_H
20#define PCAN_USB_CORE_H
21
22/* PEAK-System vendor id. */
23#define PCAN_USB_VENDOR_ID 0x0c72
24
25/* supported device ids. */
26#define PCAN_USB_PRODUCT_ID 0x000c
27#define PCAN_USBPRO_PRODUCT_ID 0x000d
28
29#define PCAN_USB_DRIVER_NAME "peak_usb"
30
31/* number of urbs that are submitted for rx/tx per channel */
32#define PCAN_USB_MAX_RX_URBS 4
33#define PCAN_USB_MAX_TX_URBS 10
34
35/* usb adapters maximum channels per usb interface */
36#define PCAN_USB_MAX_CHANNEL 2
37
38/* maximum length of the usb commands sent to/received from the devices */
39#define PCAN_USB_MAX_CMD_LEN 32
40
41struct peak_usb_device;
42
43/* PEAK-System USB adapter descriptor */
44struct peak_usb_adapter {
45 char *name;
46 u32 device_id;
47 struct can_clock clock;
48 const struct can_bittiming_const bittiming_const;
49 unsigned int ctrl_count;
50
51 int (*intf_probe)(struct usb_interface *intf);
52
53 int (*dev_init)(struct peak_usb_device *dev);
54 void (*dev_exit)(struct peak_usb_device *dev);
55 void (*dev_free)(struct peak_usb_device *dev);
56 int (*dev_open)(struct peak_usb_device *dev);
57 int (*dev_close)(struct peak_usb_device *dev);
58 int (*dev_set_bittiming)(struct peak_usb_device *dev,
59 struct can_bittiming *bt);
60 int (*dev_set_bus)(struct peak_usb_device *dev, u8 onoff);
61 int (*dev_get_device_id)(struct peak_usb_device *dev, u32 *device_id);
62 int (*dev_decode_buf)(struct peak_usb_device *dev, struct urb *urb);
63 int (*dev_encode_msg)(struct peak_usb_device *dev, struct sk_buff *skb,
64 u8 *obuf, size_t *size);
65 int (*dev_start)(struct peak_usb_device *dev);
66 int (*dev_stop)(struct peak_usb_device *dev);
67 int (*dev_restart_async)(struct peak_usb_device *dev, struct urb *urb,
68 u8 *buf);
69 u8 ep_msg_in;
70 u8 ep_msg_out[PCAN_USB_MAX_CHANNEL];
71 u8 ts_used_bits;
72 u32 ts_period;
73 u8 us_per_ts_shift;
74 u32 us_per_ts_scale;
75
76 int rx_buffer_size;
77 int tx_buffer_size;
78 int sizeof_dev_private;
79};
80
81extern struct peak_usb_adapter pcan_usb;
82extern struct peak_usb_adapter pcan_usb_pro;
83
84struct peak_time_ref {
85 struct timeval tv_host_0, tv_host;
86 u32 ts_dev_1, ts_dev_2;
87 u64 ts_total;
88 u32 tick_count;
89 struct peak_usb_adapter *adapter;
90};
91
92struct peak_tx_urb_context {
93 struct peak_usb_device *dev;
94 u32 echo_index;
95 u8 dlc;
96 struct urb *urb;
97};
98
99#define PCAN_USB_STATE_CONNECTED 0x00000001
100#define PCAN_USB_STATE_STARTED 0x00000002
101
102/* PEAK-System USB device */
103struct peak_usb_device {
104 struct can_priv can;
105 struct peak_usb_adapter *adapter;
106 unsigned int ctrl_idx;
107 u32 state;
108
109 struct sk_buff *echo_skb[PCAN_USB_MAX_TX_URBS];
110
111 struct usb_device *udev;
112 struct net_device *netdev;
113
114 atomic_t active_tx_urbs;
115 struct usb_anchor tx_submitted;
116 struct peak_tx_urb_context tx_contexts[PCAN_USB_MAX_TX_URBS];
117
118 u8 *cmd_buf;
119 struct usb_anchor rx_submitted;
120
121 u32 device_number;
122 u8 device_rev;
123
124 u8 ep_msg_in;
125 u8 ep_msg_out;
126
127 u16 bus_load;
128
129 struct peak_usb_device *prev_siblings;
130 struct peak_usb_device *next_siblings;
131};
132
133void pcan_dump_mem(char *prompt, void *p, int l);
134
135/* common timestamp management */
136void peak_usb_init_time_ref(struct peak_time_ref *time_ref,
137 struct peak_usb_adapter *adapter);
138void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now);
139void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now);
140void peak_usb_get_ts_tv(struct peak_time_ref *time_ref, u32 ts,
141 struct timeval *tv);
142
143void peak_usb_async_complete(struct urb *urb);
144void peak_usb_restart_complete(struct peak_usb_device *dev);
145#endif
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
deleted file mode 100644
index 30d79bfa5b1..00000000000
--- a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c
+++ /dev/null
@@ -1,1042 +0,0 @@
1/*
2 * CAN driver for PEAK System PCAN-USB Pro adapter
3 * Derived from the PCAN project file driver/src/pcan_usbpro.c
4 *
5 * Copyright (C) 2003-2011 PEAK System-Technik GmbH
6 * Copyright (C) 2011-2012 Stephane Grosjean <s.grosjean@peak-system.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 */
17#include <linux/netdevice.h>
18#include <linux/usb.h>
19#include <linux/module.h>
20
21#include <linux/can.h>
22#include <linux/can/dev.h>
23#include <linux/can/error.h>
24
25#include "pcan_usb_core.h"
26#include "pcan_usb_pro.h"
27
28MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB Pro adapter");
29
30/* PCAN-USB Pro Endpoints */
31#define PCAN_USBPRO_EP_CMDOUT 1
32#define PCAN_USBPRO_EP_CMDIN (PCAN_USBPRO_EP_CMDOUT | USB_DIR_IN)
33#define PCAN_USBPRO_EP_MSGOUT_0 2
34#define PCAN_USBPRO_EP_MSGIN (PCAN_USBPRO_EP_MSGOUT_0 | USB_DIR_IN)
35#define PCAN_USBPRO_EP_MSGOUT_1 3
36#define PCAN_USBPRO_EP_UNUSED (PCAN_USBPRO_EP_MSGOUT_1 | USB_DIR_IN)
37
38#define PCAN_USBPRO_CHANNEL_COUNT 2
39
40/* PCAN-USB Pro adapter internal clock (MHz) */
41#define PCAN_USBPRO_CRYSTAL_HZ 56000000
42
43/* PCAN-USB Pro command timeout (ms.) */
44#define PCAN_USBPRO_COMMAND_TIMEOUT 1000
45
46/* PCAN-USB Pro rx/tx buffers size */
47#define PCAN_USBPRO_RX_BUFFER_SIZE 1024
48#define PCAN_USBPRO_TX_BUFFER_SIZE 64
49
50#define PCAN_USBPRO_MSG_HEADER_LEN 4
51
52/* some commands responses need to be re-submitted */
53#define PCAN_USBPRO_RSP_SUBMIT_MAX 2
54
55#define PCAN_USBPRO_RTR 0x01
56#define PCAN_USBPRO_EXT 0x02
57
58#define PCAN_USBPRO_CMD_BUFFER_SIZE 512
59
60/* handle device specific info used by the netdevices */
61struct pcan_usb_pro_interface {
62 struct peak_usb_device *dev[PCAN_USBPRO_CHANNEL_COUNT];
63 struct peak_time_ref time_ref;
64 int cm_ignore_count;
65 int dev_opened_count;
66};
67
68/* device information */
69struct pcan_usb_pro_device {
70 struct peak_usb_device dev;
71 struct pcan_usb_pro_interface *usb_if;
72 u32 cached_ccbt;
73};
74
75/* internal structure used to handle messages sent to bulk urb */
76struct pcan_usb_pro_msg {
77 u8 *rec_ptr;
78 int rec_buffer_size;
79 int rec_buffer_len;
80 union {
81 u16 *rec_cnt_rd;
82 u32 *rec_cnt;
83 u8 *rec_buffer;
84 } u;
85};
86
87/* records sizes table indexed on message id. (8-bits value) */
88static u16 pcan_usb_pro_sizeof_rec[256] = {
89 [PCAN_USBPRO_SETBTR] = sizeof(struct pcan_usb_pro_btr),
90 [PCAN_USBPRO_SETBUSACT] = sizeof(struct pcan_usb_pro_busact),
91 [PCAN_USBPRO_SETSILENT] = sizeof(struct pcan_usb_pro_silent),
92 [PCAN_USBPRO_SETFILTR] = sizeof(struct pcan_usb_pro_filter),
93 [PCAN_USBPRO_SETTS] = sizeof(struct pcan_usb_pro_setts),
94 [PCAN_USBPRO_GETDEVID] = sizeof(struct pcan_usb_pro_devid),
95 [PCAN_USBPRO_SETLED] = sizeof(struct pcan_usb_pro_setled),
96 [PCAN_USBPRO_RXMSG8] = sizeof(struct pcan_usb_pro_rxmsg),
97 [PCAN_USBPRO_RXMSG4] = sizeof(struct pcan_usb_pro_rxmsg) - 4,
98 [PCAN_USBPRO_RXMSG0] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
99 [PCAN_USBPRO_RXRTR] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
100 [PCAN_USBPRO_RXSTATUS] = sizeof(struct pcan_usb_pro_rxstatus),
101 [PCAN_USBPRO_RXTS] = sizeof(struct pcan_usb_pro_rxts),
102 [PCAN_USBPRO_TXMSG8] = sizeof(struct pcan_usb_pro_txmsg),
103 [PCAN_USBPRO_TXMSG4] = sizeof(struct pcan_usb_pro_txmsg) - 4,
104 [PCAN_USBPRO_TXMSG0] = sizeof(struct pcan_usb_pro_txmsg) - 8,
105};
106
107/*
108 * initialize PCAN-USB Pro message data structure
109 */
110static u8 *pcan_msg_init(struct pcan_usb_pro_msg *pm, void *buffer_addr,
111 int buffer_size)
112{
113 if (buffer_size < PCAN_USBPRO_MSG_HEADER_LEN)
114 return NULL;
115
116 pm->u.rec_buffer = (u8 *)buffer_addr;
117 pm->rec_buffer_size = pm->rec_buffer_len = buffer_size;
118 pm->rec_ptr = pm->u.rec_buffer + PCAN_USBPRO_MSG_HEADER_LEN;
119
120 return pm->rec_ptr;
121}
122
123static u8 *pcan_msg_init_empty(struct pcan_usb_pro_msg *pm,
124 void *buffer_addr, int buffer_size)
125{
126 u8 *pr = pcan_msg_init(pm, buffer_addr, buffer_size);
127
128 if (pr) {
129 pm->rec_buffer_len = PCAN_USBPRO_MSG_HEADER_LEN;
130 *pm->u.rec_cnt = 0;
131 }
132 return pr;
133}
134
135/*
136 * add one record to a message being built
137 */
138static int pcan_msg_add_rec(struct pcan_usb_pro_msg *pm, u8 id, ...)
139{
140 int len, i;
141 u8 *pc;
142 va_list ap;
143
144 va_start(ap, id);
145
146 pc = pm->rec_ptr + 1;
147
148 i = 0;
149 switch (id) {
150 case PCAN_USBPRO_TXMSG8:
151 i += 4;
152 case PCAN_USBPRO_TXMSG4:
153 i += 4;
154 case PCAN_USBPRO_TXMSG0:
155 *pc++ = va_arg(ap, int);
156 *pc++ = va_arg(ap, int);
157 *pc++ = va_arg(ap, int);
158 *(u32 *)pc = cpu_to_le32(va_arg(ap, u32));
159 pc += 4;
160 memcpy(pc, va_arg(ap, int *), i);
161 pc += i;
162 break;
163
164 case PCAN_USBPRO_SETBTR:
165 case PCAN_USBPRO_GETDEVID:
166 *pc++ = va_arg(ap, int);
167 pc += 2;
168 *(u32 *)pc = cpu_to_le32(va_arg(ap, u32));
169 pc += 4;
170 break;
171
172 case PCAN_USBPRO_SETFILTR:
173 case PCAN_USBPRO_SETBUSACT:
174 case PCAN_USBPRO_SETSILENT:
175 *pc++ = va_arg(ap, int);
176 *(u16 *)pc = cpu_to_le16(va_arg(ap, int));
177 pc += 2;
178 break;
179
180 case PCAN_USBPRO_SETLED:
181 *pc++ = va_arg(ap, int);
182 *(u16 *)pc = cpu_to_le16(va_arg(ap, int));
183 pc += 2;
184 *(u32 *)pc = cpu_to_le32(va_arg(ap, u32));
185 pc += 4;
186 break;
187
188 case PCAN_USBPRO_SETTS:
189 pc++;
190 *(u16 *)pc = cpu_to_le16(va_arg(ap, int));
191 pc += 2;
192 break;
193
194 default:
195 pr_err("%s: %s(): unknown data type %02Xh (%d)\n",
196 PCAN_USB_DRIVER_NAME, __func__, id, id);
197 pc--;
198 break;
199 }
200
201 len = pc - pm->rec_ptr;
202 if (len > 0) {
203 *pm->u.rec_cnt = cpu_to_le32(*pm->u.rec_cnt+1);
204 *pm->rec_ptr = id;
205
206 pm->rec_ptr = pc;
207 pm->rec_buffer_len += len;
208 }
209
210 va_end(ap);
211
212 return len;
213}
214
215/*
216 * send PCAN-USB Pro command synchronously
217 */
218static int pcan_usb_pro_send_cmd(struct peak_usb_device *dev,
219 struct pcan_usb_pro_msg *pum)
220{
221 int actual_length;
222 int err;
223
224 /* usb device unregistered? */
225 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
226 return 0;
227
228 err = usb_bulk_msg(dev->udev,
229 usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
230 pum->u.rec_buffer, pum->rec_buffer_len,
231 &actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
232 if (err)
233 netdev_err(dev->netdev, "sending command failure: %d\n", err);
234
235 return err;
236}
237
238/*
239 * wait for PCAN-USB Pro command response
240 */
241static int pcan_usb_pro_wait_rsp(struct peak_usb_device *dev,
242 struct pcan_usb_pro_msg *pum)
243{
244 u8 req_data_type, req_channel;
245 int actual_length;
246 int i, err = 0;
247
248 /* usb device unregistered? */
249 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
250 return 0;
251
252 req_data_type = pum->u.rec_buffer[4];
253 req_channel = pum->u.rec_buffer[5];
254
255 *pum->u.rec_cnt = 0;
256 for (i = 0; !err && i < PCAN_USBPRO_RSP_SUBMIT_MAX; i++) {
257 struct pcan_usb_pro_msg rsp;
258 union pcan_usb_pro_rec *pr;
259 u32 r, rec_cnt;
260 u16 rec_len;
261 u8 *pc;
262
263 err = usb_bulk_msg(dev->udev,
264 usb_rcvbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDIN),
265 pum->u.rec_buffer, pum->rec_buffer_len,
266 &actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
267 if (err) {
268 netdev_err(dev->netdev, "waiting rsp error %d\n", err);
269 break;
270 }
271
272 if (actual_length == 0)
273 continue;
274
275 err = -EBADMSG;
276 if (actual_length < PCAN_USBPRO_MSG_HEADER_LEN) {
277 netdev_err(dev->netdev,
278 "got abnormal too small rsp (len=%d)\n",
279 actual_length);
280 break;
281 }
282
283 pc = pcan_msg_init(&rsp, pum->u.rec_buffer,
284 actual_length);
285
286 rec_cnt = le32_to_cpu(*rsp.u.rec_cnt);
287
288 /* loop on records stored into message */
289 for (r = 0; r < rec_cnt; r++) {
290 pr = (union pcan_usb_pro_rec *)pc;
291 rec_len = pcan_usb_pro_sizeof_rec[pr->data_type];
292 if (!rec_len) {
293 netdev_err(dev->netdev,
294 "got unprocessed record in msg\n");
295 pcan_dump_mem("rcvd rsp msg", pum->u.rec_buffer,
296 actual_length);
297 break;
298 }
299
300 /* check if response corresponds to request */
301 if (pr->data_type != req_data_type)
302 netdev_err(dev->netdev,
303 "got unwanted rsp %xh: ignored\n",
304 pr->data_type);
305
306 /* check if channel in response corresponds too */
307 else if ((req_channel != 0xff) && \
308 (pr->bus_act.channel != req_channel))
309 netdev_err(dev->netdev,
310 "got rsp %xh but on chan%u: ignored\n",
311 req_data_type, pr->bus_act.channel);
312
313 /* got the response */
314 else
315 return 0;
316
317 /* otherwise, go on with next record in message */
318 pc += rec_len;
319 }
320 }
321
322 return (i >= PCAN_USBPRO_RSP_SUBMIT_MAX) ? -ERANGE : err;
323}
324
325static int pcan_usb_pro_send_req(struct peak_usb_device *dev, int req_id,
326 int req_value, void *req_addr, int req_size)
327{
328 int err;
329 u8 req_type;
330 unsigned int p;
331
332 /* usb device unregistered? */
333 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
334 return 0;
335
336 memset(req_addr, '\0', req_size);
337
338 req_type = USB_TYPE_VENDOR | USB_RECIP_OTHER;
339
340 switch (req_id) {
341 case PCAN_USBPRO_REQ_FCT:
342 p = usb_sndctrlpipe(dev->udev, 0);
343 break;
344
345 default:
346 p = usb_rcvctrlpipe(dev->udev, 0);
347 req_type |= USB_DIR_IN;
348 break;
349 }
350
351 err = usb_control_msg(dev->udev, p, req_id, req_type, req_value, 0,
352 req_addr, req_size, 2 * USB_CTRL_GET_TIMEOUT);
353 if (err < 0) {
354 netdev_info(dev->netdev,
355 "unable to request usb[type=%d value=%d] err=%d\n",
356 req_id, req_value, err);
357 return err;
358 }
359
360 return 0;
361}
362
363static int pcan_usb_pro_set_ts(struct peak_usb_device *dev, u16 onoff)
364{
365 struct pcan_usb_pro_msg um;
366
367 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
368 pcan_msg_add_rec(&um, PCAN_USBPRO_SETTS, onoff);
369
370 return pcan_usb_pro_send_cmd(dev, &um);
371}
372
373static int pcan_usb_pro_set_bitrate(struct peak_usb_device *dev, u32 ccbt)
374{
375 struct pcan_usb_pro_device *pdev =
376 container_of(dev, struct pcan_usb_pro_device, dev);
377 struct pcan_usb_pro_msg um;
378
379 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
380 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBTR, dev->ctrl_idx, ccbt);
381
382 /* cache the CCBT value to reuse it before next buson */
383 pdev->cached_ccbt = ccbt;
384
385 return pcan_usb_pro_send_cmd(dev, &um);
386}
387
388static int pcan_usb_pro_set_bus(struct peak_usb_device *dev, u8 onoff)
389{
390 struct pcan_usb_pro_msg um;
391
392 /* if bus=on, be sure the bitrate being set before! */
393 if (onoff) {
394 struct pcan_usb_pro_device *pdev =
395 container_of(dev, struct pcan_usb_pro_device, dev);
396
397 pcan_usb_pro_set_bitrate(dev, pdev->cached_ccbt);
398 }
399
400 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
401 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, onoff);
402
403 return pcan_usb_pro_send_cmd(dev, &um);
404}
405
406static int pcan_usb_pro_set_silent(struct peak_usb_device *dev, u8 onoff)
407{
408 struct pcan_usb_pro_msg um;
409
410 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
411 pcan_msg_add_rec(&um, PCAN_USBPRO_SETSILENT, dev->ctrl_idx, onoff);
412
413 return pcan_usb_pro_send_cmd(dev, &um);
414}
415
416static int pcan_usb_pro_set_filter(struct peak_usb_device *dev, u16 filter_mode)
417{
418 struct pcan_usb_pro_msg um;
419
420 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
421 pcan_msg_add_rec(&um, PCAN_USBPRO_SETFILTR, dev->ctrl_idx, filter_mode);
422
423 return pcan_usb_pro_send_cmd(dev, &um);
424}
425
426static int pcan_usb_pro_set_led(struct peak_usb_device *dev, u8 mode,
427 u32 timeout)
428{
429 struct pcan_usb_pro_msg um;
430
431 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
432 pcan_msg_add_rec(&um, PCAN_USBPRO_SETLED, dev->ctrl_idx, mode, timeout);
433
434 return pcan_usb_pro_send_cmd(dev, &um);
435}
436
437static int pcan_usb_pro_get_device_id(struct peak_usb_device *dev,
438 u32 *device_id)
439{
440 struct pcan_usb_pro_devid *pdn;
441 struct pcan_usb_pro_msg um;
442 int err;
443 u8 *pc;
444
445 pc = pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
446 pcan_msg_add_rec(&um, PCAN_USBPRO_GETDEVID, dev->ctrl_idx);
447
448 err = pcan_usb_pro_send_cmd(dev, &um);
449 if (err)
450 return err;
451
452 err = pcan_usb_pro_wait_rsp(dev, &um);
453 if (err)
454 return err;
455
456 pdn = (struct pcan_usb_pro_devid *)pc;
457 if (device_id)
458 *device_id = le32_to_cpu(pdn->serial_num);
459
460 return err;
461}
462
463static int pcan_usb_pro_set_bittiming(struct peak_usb_device *dev,
464 struct can_bittiming *bt)
465{
466 u32 ccbt;
467
468 ccbt = (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 0x00800000 : 0;
469 ccbt |= (bt->sjw - 1) << 24;
470 ccbt |= (bt->phase_seg2 - 1) << 20;
471 ccbt |= (bt->prop_seg + bt->phase_seg1 - 1) << 16; /* = tseg1 */
472 ccbt |= bt->brp - 1;
473
474 netdev_info(dev->netdev, "setting ccbt=0x%08x\n", ccbt);
475
476 return pcan_usb_pro_set_bitrate(dev, ccbt);
477}
478
479static void pcan_usb_pro_restart_complete(struct urb *urb)
480{
481 /* can delete usb resources */
482 peak_usb_async_complete(urb);
483
484 /* notify candev and netdev */
485 peak_usb_restart_complete(urb->context);
486}
487
488/*
489 * handle restart but in asynchronously way
490 */
491static int pcan_usb_pro_restart_async(struct peak_usb_device *dev,
492 struct urb *urb, u8 *buf)
493{
494 struct pcan_usb_pro_msg um;
495
496 pcan_msg_init_empty(&um, buf, PCAN_USB_MAX_CMD_LEN);
497 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, 1);
498
499 usb_fill_bulk_urb(urb, dev->udev,
500 usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
501 buf, PCAN_USB_MAX_CMD_LEN,
502 pcan_usb_pro_restart_complete, dev);
503
504 return usb_submit_urb(urb, GFP_ATOMIC);
505}
506
507static void pcan_usb_pro_drv_loaded(struct peak_usb_device *dev, int loaded)
508{
509 u8 buffer[16];
510
511 buffer[0] = 0;
512 buffer[1] = !!loaded;
513
514 pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_FCT,
515 PCAN_USBPRO_FCT_DRVLD, buffer, sizeof(buffer));
516}
517
518static inline
519struct pcan_usb_pro_interface *pcan_usb_pro_dev_if(struct peak_usb_device *dev)
520{
521 struct pcan_usb_pro_device *pdev =
522 container_of(dev, struct pcan_usb_pro_device, dev);
523 return pdev->usb_if;
524}
525
526static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if,
527 struct pcan_usb_pro_rxmsg *rx)
528{
529 const unsigned int ctrl_idx = (rx->len >> 4) & 0x0f;
530 struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
531 struct net_device *netdev = dev->netdev;
532 struct can_frame *can_frame;
533 struct sk_buff *skb;
534 struct timeval tv;
535 struct skb_shared_hwtstamps *hwts;
536
537 skb = alloc_can_skb(netdev, &can_frame);
538 if (!skb)
539 return -ENOMEM;
540
541 can_frame->can_id = le32_to_cpu(rx->id);
542 can_frame->can_dlc = rx->len & 0x0f;
543
544 if (rx->flags & PCAN_USBPRO_EXT)
545 can_frame->can_id |= CAN_EFF_FLAG;
546
547 if (rx->flags & PCAN_USBPRO_RTR)
548 can_frame->can_id |= CAN_RTR_FLAG;
549 else
550 memcpy(can_frame->data, rx->data, can_frame->can_dlc);
551
552 peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(rx->ts32), &tv);
553 hwts = skb_hwtstamps(skb);
554 hwts->hwtstamp = timeval_to_ktime(tv);
555
556 netif_rx(skb);
557 netdev->stats.rx_packets++;
558 netdev->stats.rx_bytes += can_frame->can_dlc;
559
560 return 0;
561}
562
563static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if,
564 struct pcan_usb_pro_rxstatus *er)
565{
566 const u32 raw_status = le32_to_cpu(er->status);
567 const unsigned int ctrl_idx = (er->channel >> 4) & 0x0f;
568 struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
569 struct net_device *netdev = dev->netdev;
570 struct can_frame *can_frame;
571 enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
572 u8 err_mask = 0;
573 struct sk_buff *skb;
574 struct timeval tv;
575 struct skb_shared_hwtstamps *hwts;
576
577 /* nothing should be sent while in BUS_OFF state */
578 if (dev->can.state == CAN_STATE_BUS_OFF)
579 return 0;
580
581 if (!raw_status) {
582 /* no error bit (back to active state) */
583 dev->can.state = CAN_STATE_ERROR_ACTIVE;
584 return 0;
585 }
586
587 if (raw_status & (PCAN_USBPRO_STATUS_OVERRUN |
588 PCAN_USBPRO_STATUS_QOVERRUN)) {
589 /* trick to bypass next comparison and process other errors */
590 new_state = CAN_STATE_MAX;
591 }
592
593 if (raw_status & PCAN_USBPRO_STATUS_BUS) {
594 new_state = CAN_STATE_BUS_OFF;
595 } else if (raw_status & PCAN_USBPRO_STATUS_ERROR) {
596 u32 rx_err_cnt = (le32_to_cpu(er->err_frm) & 0x00ff0000) >> 16;
597 u32 tx_err_cnt = (le32_to_cpu(er->err_frm) & 0xff000000) >> 24;
598
599 if (rx_err_cnt > 127)
600 err_mask |= CAN_ERR_CRTL_RX_PASSIVE;
601 else if (rx_err_cnt > 96)
602 err_mask |= CAN_ERR_CRTL_RX_WARNING;
603
604 if (tx_err_cnt > 127)
605 err_mask |= CAN_ERR_CRTL_TX_PASSIVE;
606 else if (tx_err_cnt > 96)
607 err_mask |= CAN_ERR_CRTL_TX_WARNING;
608
609 if (err_mask & (CAN_ERR_CRTL_RX_WARNING |
610 CAN_ERR_CRTL_TX_WARNING))
611 new_state = CAN_STATE_ERROR_WARNING;
612 else if (err_mask & (CAN_ERR_CRTL_RX_PASSIVE |
613 CAN_ERR_CRTL_TX_PASSIVE))
614 new_state = CAN_STATE_ERROR_PASSIVE;
615 }
616
617 /* donot post any error if current state didn't change */
618 if (dev->can.state == new_state)
619 return 0;
620
621 /* allocate an skb to store the error frame */
622 skb = alloc_can_err_skb(netdev, &can_frame);
623 if (!skb)
624 return -ENOMEM;
625
626 switch (new_state) {
627 case CAN_STATE_BUS_OFF:
628 can_frame->can_id |= CAN_ERR_BUSOFF;
629 can_bus_off(netdev);
630 break;
631
632 case CAN_STATE_ERROR_PASSIVE:
633 can_frame->can_id |= CAN_ERR_CRTL;
634 can_frame->data[1] |= err_mask;
635 dev->can.can_stats.error_passive++;
636 break;
637
638 case CAN_STATE_ERROR_WARNING:
639 can_frame->can_id |= CAN_ERR_CRTL;
640 can_frame->data[1] |= err_mask;
641 dev->can.can_stats.error_warning++;
642 break;
643
644 case CAN_STATE_ERROR_ACTIVE:
645 break;
646
647 default:
648 /* CAN_STATE_MAX (trick to handle other errors) */
649 if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) {
650 can_frame->can_id |= CAN_ERR_PROT;
651 can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD;
652 netdev->stats.rx_over_errors++;
653 netdev->stats.rx_errors++;
654 }
655
656 if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) {
657 can_frame->can_id |= CAN_ERR_CRTL;
658 can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
659 netdev->stats.rx_over_errors++;
660 netdev->stats.rx_errors++;
661 }
662
663 new_state = CAN_STATE_ERROR_ACTIVE;
664 break;
665 }
666
667 dev->can.state = new_state;
668
669 peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(er->ts32), &tv);
670 hwts = skb_hwtstamps(skb);
671 hwts->hwtstamp = timeval_to_ktime(tv);
672 netif_rx(skb);
673 netdev->stats.rx_packets++;
674 netdev->stats.rx_bytes += can_frame->can_dlc;
675
676 return 0;
677}
678
679static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if,
680 struct pcan_usb_pro_rxts *ts)
681{
682 /* should wait until clock is stabilized */
683 if (usb_if->cm_ignore_count > 0)
684 usb_if->cm_ignore_count--;
685 else
686 peak_usb_set_ts_now(&usb_if->time_ref,
687 le32_to_cpu(ts->ts64[1]));
688}
689
690/*
691 * callback for bulk IN urb
692 */
693static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb)
694{
695 struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev);
696 struct net_device *netdev = dev->netdev;
697 struct pcan_usb_pro_msg usb_msg;
698 u8 *rec_ptr, *msg_end;
699 u16 rec_cnt;
700 int err = 0;
701
702 rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
703 urb->actual_length);
704 if (!rec_ptr) {
705 netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length);
706 return -EINVAL;
707 }
708
709 /* loop reading all the records from the incoming message */
710 msg_end = urb->transfer_buffer + urb->actual_length;
711 rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd);
712 for (; rec_cnt > 0; rec_cnt--) {
713 union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr;
714 u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type];
715
716 if (!sizeof_rec) {
717 netdev_err(netdev,
718 "got unsupported rec in usb msg:\n");
719 err = -ENOTSUPP;
720 break;
721 }
722
723 /* check if the record goes out of current packet */
724 if (rec_ptr + sizeof_rec > msg_end) {
725 netdev_err(netdev,
726 "got frag rec: should inc usb rx buf size\n");
727 err = -EBADMSG;
728 break;
729 }
730
731 switch (pr->data_type) {
732 case PCAN_USBPRO_RXMSG8:
733 case PCAN_USBPRO_RXMSG4:
734 case PCAN_USBPRO_RXMSG0:
735 case PCAN_USBPRO_RXRTR:
736 err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg);
737 if (err < 0)
738 goto fail;
739 break;
740
741 case PCAN_USBPRO_RXSTATUS:
742 err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status);
743 if (err < 0)
744 goto fail;
745 break;
746
747 case PCAN_USBPRO_RXTS:
748 pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts);
749 break;
750
751 default:
752 netdev_err(netdev,
753 "unhandled rec type 0x%02x (%d): ignored\n",
754 pr->data_type, pr->data_type);
755 break;
756 }
757
758 rec_ptr += sizeof_rec;
759 }
760
761fail:
762 if (err)
763 pcan_dump_mem("received msg",
764 urb->transfer_buffer, urb->actual_length);
765
766 return err;
767}
768
769static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev,
770 struct sk_buff *skb, u8 *obuf, size_t *size)
771{
772 struct can_frame *cf = (struct can_frame *)skb->data;
773 u8 data_type, len, flags;
774 struct pcan_usb_pro_msg usb_msg;
775
776 pcan_msg_init_empty(&usb_msg, obuf, *size);
777
778 if ((cf->can_id & CAN_RTR_FLAG) || (cf->can_dlc == 0))
779 data_type = PCAN_USBPRO_TXMSG0;
780 else if (cf->can_dlc <= 4)
781 data_type = PCAN_USBPRO_TXMSG4;
782 else
783 data_type = PCAN_USBPRO_TXMSG8;
784
785 len = (dev->ctrl_idx << 4) | (cf->can_dlc & 0x0f);
786
787 flags = 0;
788 if (cf->can_id & CAN_EFF_FLAG)
789 flags |= 0x02;
790 if (cf->can_id & CAN_RTR_FLAG)
791 flags |= 0x01;
792
793 pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id,
794 cf->data);
795
796 *size = usb_msg.rec_buffer_len;
797
798 return 0;
799}
800
801static int pcan_usb_pro_start(struct peak_usb_device *dev)
802{
803 struct pcan_usb_pro_device *pdev =
804 container_of(dev, struct pcan_usb_pro_device, dev);
805 int err;
806
807 err = pcan_usb_pro_set_silent(dev,
808 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
809 if (err)
810 return err;
811
812 /* filter mode: 0-> All OFF; 1->bypass */
813 err = pcan_usb_pro_set_filter(dev, 1);
814 if (err)
815 return err;
816
817 /* opening first device: */
818 if (pdev->usb_if->dev_opened_count == 0) {
819 /* reset time_ref */
820 peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro);
821
822 /* ask device to send ts messages */
823 err = pcan_usb_pro_set_ts(dev, 1);
824 }
825
826 pdev->usb_if->dev_opened_count++;
827
828 return err;
829}
830
831/*
832 * stop interface
833 * (last chance before set bus off)
834 */
835static int pcan_usb_pro_stop(struct peak_usb_device *dev)
836{
837 struct pcan_usb_pro_device *pdev =
838 container_of(dev, struct pcan_usb_pro_device, dev);
839
840 /* turn off ts msgs for that interface if no other dev opened */
841 if (pdev->usb_if->dev_opened_count == 1)
842 pcan_usb_pro_set_ts(dev, 0);
843
844 pdev->usb_if->dev_opened_count--;
845
846 return 0;
847}
848
849/*
850 * called when probing to initialize a device object.
851 */
852static int pcan_usb_pro_init(struct peak_usb_device *dev)
853{
854 struct pcan_usb_pro_interface *usb_if;
855 struct pcan_usb_pro_device *pdev =
856 container_of(dev, struct pcan_usb_pro_device, dev);
857
858 /* do this for 1st channel only */
859 if (!dev->prev_siblings) {
860 struct pcan_usb_pro_fwinfo fi;
861 struct pcan_usb_pro_blinfo bi;
862 int err;
863
864 /* allocate netdevices common structure attached to first one */
865 usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
866 GFP_KERNEL);
867 if (!usb_if)
868 return -ENOMEM;
869
870 /* number of ts msgs to ignore before taking one into account */
871 usb_if->cm_ignore_count = 5;
872
873 /*
874 * explicit use of dev_xxx() instead of netdev_xxx() here:
875 * information displayed are related to the device itself, not
876 * to the canx netdevices.
877 */
878 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
879 PCAN_USBPRO_INFO_FW,
880 &fi, sizeof(fi));
881 if (err) {
882 kfree(usb_if);
883 dev_err(dev->netdev->dev.parent,
884 "unable to read %s firmware info (err %d)\n",
885 pcan_usb_pro.name, err);
886 return err;
887 }
888
889 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
890 PCAN_USBPRO_INFO_BL,
891 &bi, sizeof(bi));
892 if (err) {
893 kfree(usb_if);
894 dev_err(dev->netdev->dev.parent,
895 "unable to read %s bootloader info (err %d)\n",
896 pcan_usb_pro.name, err);
897 return err;
898 }
899
900 dev_info(dev->netdev->dev.parent,
901 "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
902 pcan_usb_pro.name,
903 bi.hw_rev, bi.serial_num_hi, bi.serial_num_lo,
904 pcan_usb_pro.ctrl_count);
905
906 /* tell the device the can driver is running */
907 pcan_usb_pro_drv_loaded(dev, 1);
908 } else {
909 usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
910 }
911
912 pdev->usb_if = usb_if;
913 usb_if->dev[dev->ctrl_idx] = dev;
914
915 /* set LED in default state (end of init phase) */
916 pcan_usb_pro_set_led(dev, 0, 1);
917
918 return 0;
919}
920
921static void pcan_usb_pro_exit(struct peak_usb_device *dev)
922{
923 struct pcan_usb_pro_device *pdev =
924 container_of(dev, struct pcan_usb_pro_device, dev);
925
926 /*
927 * when rmmod called before unplug and if down, should reset things
928 * before leaving
929 */
930 if (dev->can.state != CAN_STATE_STOPPED) {
931 /* set bus off on the corresponding channel */
932 pcan_usb_pro_set_bus(dev, 0);
933 }
934
935 /* if channel #0 (only) */
936 if (dev->ctrl_idx == 0) {
937 /* turn off calibration message if any device were opened */
938 if (pdev->usb_if->dev_opened_count > 0)
939 pcan_usb_pro_set_ts(dev, 0);
940
941 /* tell the PCAN-USB Pro device the driver is being unloaded */
942 pcan_usb_pro_drv_loaded(dev, 0);
943 }
944}
945
946/*
947 * called when PCAN-USB Pro adapter is unplugged
948 */
949static void pcan_usb_pro_free(struct peak_usb_device *dev)
950{
951 /* last device: can free pcan_usb_pro_interface object now */
952 if (!dev->prev_siblings && !dev->next_siblings)
953 kfree(pcan_usb_pro_dev_if(dev));
954}
955
956/*
957 * probe function for new PCAN-USB Pro usb interface
958 */
959static int pcan_usb_pro_probe(struct usb_interface *intf)
960{
961 struct usb_host_interface *if_desc;
962 int i;
963
964 if_desc = intf->altsetting;
965
966 /* check interface endpoint addresses */
967 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
968 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
969
970 /*
971 * below is the list of valid ep addreses. Any other ep address
972 * is considered as not-CAN interface address => no dev created
973 */
974 switch (ep->bEndpointAddress) {
975 case PCAN_USBPRO_EP_CMDOUT:
976 case PCAN_USBPRO_EP_CMDIN:
977 case PCAN_USBPRO_EP_MSGOUT_0:
978 case PCAN_USBPRO_EP_MSGOUT_1:
979 case PCAN_USBPRO_EP_MSGIN:
980 case PCAN_USBPRO_EP_UNUSED:
981 break;
982 default:
983 return -ENODEV;
984 }
985 }
986
987 return 0;
988}
989
990/*
991 * describe the PCAN-USB Pro adapter
992 */
993struct peak_usb_adapter pcan_usb_pro = {
994 .name = "PCAN-USB Pro",
995 .device_id = PCAN_USBPRO_PRODUCT_ID,
996 .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
997 .clock = {
998 .freq = PCAN_USBPRO_CRYSTAL_HZ,
999 },
1000 .bittiming_const = {
1001 .name = "pcan_usb_pro",
1002 .tseg1_min = 1,
1003 .tseg1_max = 16,
1004 .tseg2_min = 1,
1005 .tseg2_max = 8,
1006 .sjw_max = 4,
1007 .brp_min = 1,
1008 .brp_max = 1024,
1009 .brp_inc = 1,
1010 },
1011
1012 /* size of device private data */
1013 .sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
1014
1015 /* timestamps usage */
1016 .ts_used_bits = 32,
1017 .ts_period = 1000000, /* calibration period in ts. */
1018 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
1019 .us_per_ts_shift = 0,
1020
1021 /* give here messages in/out endpoints */
1022 .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
1023 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
1024
1025 /* size of rx/tx usb buffers */
1026 .rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
1027 .tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
1028
1029 /* device callbacks */
1030 .intf_probe = pcan_usb_pro_probe,
1031 .dev_init = pcan_usb_pro_init,
1032 .dev_exit = pcan_usb_pro_exit,
1033 .dev_free = pcan_usb_pro_free,
1034 .dev_set_bus = pcan_usb_pro_set_bus,
1035 .dev_set_bittiming = pcan_usb_pro_set_bittiming,
1036 .dev_get_device_id = pcan_usb_pro_get_device_id,
1037 .dev_decode_buf = pcan_usb_pro_decode_buf,
1038 .dev_encode_msg = pcan_usb_pro_encode_msg,
1039 .dev_start = pcan_usb_pro_start,
1040 .dev_stop = pcan_usb_pro_stop,
1041 .dev_restart_async = pcan_usb_pro_restart_async,
1042};
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_pro.h b/drivers/net/can/usb/peak_usb/pcan_usb_pro.h
deleted file mode 100644
index a869918c562..00000000000
--- a/drivers/net/can/usb/peak_usb/pcan_usb_pro.h
+++ /dev/null
@@ -1,178 +0,0 @@
1/*
2 * CAN driver for PEAK System PCAN-USB Pro adapter
3 * Derived from the PCAN project file driver/src/pcan_usbpro_fw.h
4 *
5 * Copyright (C) 2003-2011 PEAK System-Technik GmbH
6 * Copyright (C) 2011-2012 Stephane Grosjean <s.grosjean@peak-system.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 */
17#ifndef PCAN_USB_PRO_H
18#define PCAN_USB_PRO_H
19
20/*
21 * USB Vendor request data types
22 */
23#define PCAN_USBPRO_REQ_INFO 0
24#define PCAN_USBPRO_REQ_FCT 2
25
26/* Vendor Request value for XXX_INFO */
27#define PCAN_USBPRO_INFO_BL 0
28#define PCAN_USBPRO_INFO_FW 1
29
30/* Vendor Request value for XXX_FCT */
31#define PCAN_USBPRO_FCT_DRVLD 5 /* tell device driver is loaded */
32
33/* PCAN_USBPRO_INFO_BL vendor request record type */
34struct __packed pcan_usb_pro_blinfo {
35 u32 ctrl_type;
36 u8 version[4];
37 u8 day;
38 u8 month;
39 u8 year;
40 u8 dummy;
41 u32 serial_num_hi;
42 u32 serial_num_lo;
43 u32 hw_type;
44 u32 hw_rev;
45};
46
47/* PCAN_USBPRO_INFO_FW vendor request record type */
48struct __packed pcan_usb_pro_fwinfo {
49 u32 ctrl_type;
50 u8 version[4];
51 u8 day;
52 u8 month;
53 u8 year;
54 u8 dummy;
55 u32 fw_type;
56};
57
58/*
59 * USB Command record types
60 */
61#define PCAN_USBPRO_SETBTR 0x02
62#define PCAN_USBPRO_SETBUSACT 0x04
63#define PCAN_USBPRO_SETSILENT 0x05
64#define PCAN_USBPRO_SETFILTR 0x0a
65#define PCAN_USBPRO_SETTS 0x10
66#define PCAN_USBPRO_GETDEVID 0x12
67#define PCAN_USBPRO_SETLED 0x1C
68#define PCAN_USBPRO_RXMSG8 0x80
69#define PCAN_USBPRO_RXMSG4 0x81
70#define PCAN_USBPRO_RXMSG0 0x82
71#define PCAN_USBPRO_RXRTR 0x83
72#define PCAN_USBPRO_RXSTATUS 0x84
73#define PCAN_USBPRO_RXTS 0x85
74#define PCAN_USBPRO_TXMSG8 0x41
75#define PCAN_USBPRO_TXMSG4 0x42
76#define PCAN_USBPRO_TXMSG0 0x43
77
78/* record structures */
79struct __packed pcan_usb_pro_btr {
80 u8 data_type;
81 u8 channel;
82 u16 dummy;
83 u32 CCBT;
84};
85
86struct __packed pcan_usb_pro_busact {
87 u8 data_type;
88 u8 channel;
89 u16 onoff;
90};
91
92struct __packed pcan_usb_pro_silent {
93 u8 data_type;
94 u8 channel;
95 u16 onoff;
96};
97
98struct __packed pcan_usb_pro_filter {
99 u8 data_type;
100 u8 dummy;
101 u16 filter_mode;
102};
103
104struct __packed pcan_usb_pro_setts {
105 u8 data_type;
106 u8 dummy;
107 u16 mode;
108};
109
110struct __packed pcan_usb_pro_devid {
111 u8 data_type;
112 u8 channel;
113 u16 dummy;
114 u32 serial_num;
115};
116
117struct __packed pcan_usb_pro_setled {
118 u8 data_type;
119 u8 channel;
120 u16 mode;
121 u32 timeout;
122};
123
124struct __packed pcan_usb_pro_rxmsg {
125 u8 data_type;
126 u8 client;
127 u8 flags;
128 u8 len;
129 u32 ts32;
130 u32 id;
131
132 u8 data[8];
133};
134
135#define PCAN_USBPRO_STATUS_ERROR 0x0001
136#define PCAN_USBPRO_STATUS_BUS 0x0002
137#define PCAN_USBPRO_STATUS_OVERRUN 0x0004
138#define PCAN_USBPRO_STATUS_QOVERRUN 0x0008
139
140struct __packed pcan_usb_pro_rxstatus {
141 u8 data_type;
142 u8 channel;
143 u16 status;
144 u32 ts32;
145 u32 err_frm;
146};
147
148struct __packed pcan_usb_pro_rxts {
149 u8 data_type;
150 u8 dummy[3];
151 u32 ts64[2];
152};
153
154struct __packed pcan_usb_pro_txmsg {
155 u8 data_type;
156 u8 client;
157 u8 flags;
158 u8 len;
159 u32 id;
160 u8 data[8];
161};
162
163union pcan_usb_pro_rec {
164 u8 data_type;
165 struct pcan_usb_pro_btr btr;
166 struct pcan_usb_pro_busact bus_act;
167 struct pcan_usb_pro_silent silent_mode;
168 struct pcan_usb_pro_filter filter_mode;
169 struct pcan_usb_pro_setts ts;
170 struct pcan_usb_pro_devid dev_id;
171 struct pcan_usb_pro_setled set_led;
172 struct pcan_usb_pro_rxmsg rx_msg;
173 struct pcan_usb_pro_rxstatus rx_status;
174 struct pcan_usb_pro_rxts rx_ts;
175 struct pcan_usb_pro_txmsg tx_msg;
176};
177
178#endif