aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/can
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-02-20 21:58:50 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2013-02-20 21:58:50 -0500
commita0b1c42951dd06ec83cc1bc2c9788131d9fefcd8 (patch)
treea572f1523cf904c93020c9cdb32f3bc84ec3ac16 /drivers/net/can
parent8ec4942212a6d337982967778a3dc3b60aea782e (diff)
parentecd9883724b78cc72ed92c98bcb1a46c764fff21 (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
Pull networking update from David Miller: 1) Checkpoint/restarted TCP sockets now can properly propagate the TCP timestamp offset. From Andrey Vagin. 2) VMWARE VM VSOCK layer, from Andy King. 3) Much improved support for virtual functions and SR-IOV in bnx2x, from Ariel ELior. 4) All protocols on ipv4 and ipv6 are now network namespace aware, and all the compatability checks for initial-namespace-only protocols is removed. Thanks to Tom Parkin for helping deal with the last major holdout, L2TP. 5) IPV6 support in netpoll and network namespace support in pktgen, from Cong Wang. 6) Multiple Registration Protocol (MRP) and Multiple VLAN Registration Protocol (MVRP) support, from David Ward. 7) Compute packet lengths more accurately in the packet scheduler, from Eric Dumazet. 8) Use per-task page fragment allocator in skb_append_datato_frags(), also from Eric Dumazet. 9) Add support for connection tracking labels in netfilter, from Florian Westphal. 10) Fix default multicast group joining on ipv6, and add anti-spoofing checks to 6to4 and 6rd. From Hannes Frederic Sowa. 11) Make ipv4/ipv6 fragmentation memory limits more reasonable in modern times, rearrange inet frag datastructures for better cacheline locality, and move more operations outside of locking. From Jesper Dangaard Brouer. 12) Instead of strict master <--> slave relationships, allow arbitrary scenerios with "upper device lists". From Jiri Pirko. 13) Improve rate limiting accuracy in TBF and act_police, also from Jiri Pirko. 14) Add a BPF filter netfilter match target, from Willem de Bruijn. 15) Orphan and delete a bunch of pre-historic networking drivers from Paul Gortmaker. 16) Add TSO support for GRE tunnels, from Pravin B SHelar. Although this still needs some minor bug fixing before it's %100 correct in all cases. 17) Handle unresolved IPSEC states like ARP, with a resolution packet queue. From Steffen Klassert. 18) Remove TCP Appropriate Byte Count support (ABC), from Stephen Hemminger. This was long overdue. 19) Support SO_REUSEPORT, from Tom Herbert. 20) Allow locking a socket BPF filter, so that it cannot change after a process drops capabilities. 21) Add VLAN filtering to bridge, from Vlad Yasevich. 22) Bring ipv6 on-par with ipv4 and do not cache neighbour entries in the ipv6 routes, from YOSHIFUJI Hideaki. * git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next: (1538 commits) ipv6: fix race condition regarding dst->expires and dst->from. net: fix a wrong assignment in skb_split() ip_gre: remove an extra dst_release() ppp: set qdisc_tx_busylock to avoid LOCKDEP splat atl1c: restore buffer state net: fix a build failure when !CONFIG_PROC_FS net: ipv4: fix waring -Wunused-variable net: proc: fix build failed when procfs is not configured Revert "xen: netback: remove redundant xenvif_put" net: move procfs code to net/core/net-procfs.c qmi_wwan, cdc-ether: add ADU960S bonding: set sysfs device_type to 'bond' bonding: fix bond_release_all inconsistencies b44: use netdev_alloc_skb_ip_align() xen: netback: remove redundant xenvif_put net: fec: Do a sanity check on the gpio number ip_gre: propogate target device GSO capability to the tunnel device ip_gre: allow CSUM capable devices to handle packets bonding: Fix initialize after use for 3ad machine state spinlock bonding: Fix race condition between bond_enslave() and bond_3ad_update_lacp_rate() ...
Diffstat (limited to 'drivers/net/can')
-rw-r--r--drivers/net/can/Kconfig37
-rw-r--r--drivers/net/can/Makefile2
-rw-r--r--drivers/net/can/at91_can.c10
-rw-r--r--drivers/net/can/c_can/Kconfig2
-rw-r--r--drivers/net/can/c_can/c_can.c10
-rw-r--r--drivers/net/can/cc770/Kconfig2
-rw-r--r--drivers/net/can/dev.c26
-rw-r--r--drivers/net/can/flexcan.c11
-rw-r--r--drivers/net/can/led.c124
-rw-r--r--drivers/net/can/mcp251x.c23
-rw-r--r--drivers/net/can/mscan/Kconfig2
-rw-r--r--drivers/net/can/sja1000/Kconfig14
-rw-r--r--drivers/net/can/sja1000/ems_pci.c1
-rw-r--r--drivers/net/can/sja1000/peak_pci.c5
-rw-r--r--drivers/net/can/sja1000/peak_pcmcia.c1
-rw-r--r--drivers/net/can/sja1000/plx_pci.c1
-rw-r--r--drivers/net/can/sja1000/sja1000.c17
-rw-r--r--drivers/net/can/slcan.c8
-rw-r--r--drivers/net/can/softing/Kconfig2
-rw-r--r--drivers/net/can/ti_hecc.c10
-rw-r--r--drivers/net/can/usb/Kconfig8
-rw-r--r--drivers/net/can/usb/Makefile1
-rw-r--r--drivers/net/can/usb/ems_usb.c8
-rw-r--r--drivers/net/can/usb/kvaser_usb.c2
-rw-r--r--drivers/net/can/usb/peak_usb/pcan_usb_core.c5
-rw-r--r--drivers/net/can/usb/usb_8dev.c1031
26 files changed, 1310 insertions, 53 deletions
diff --git a/drivers/net/can/Kconfig b/drivers/net/can/Kconfig
index b56bd9e80957..1cca19f1c490 100644
--- a/drivers/net/can/Kconfig
+++ b/drivers/net/can/Kconfig
@@ -1,9 +1,7 @@
1menu "CAN Device Drivers" 1menu "CAN Device Drivers"
2 depends on CAN
3 2
4config CAN_VCAN 3config CAN_VCAN
5 tristate "Virtual Local CAN Interface (vcan)" 4 tristate "Virtual Local CAN Interface (vcan)"
6 depends on CAN
7 ---help--- 5 ---help---
8 Similar to the network loopback devices, vcan offers a 6 Similar to the network loopback devices, vcan offers a
9 virtual local CAN interface. 7 virtual local CAN interface.
@@ -13,7 +11,6 @@ config CAN_VCAN
13 11
14config CAN_SLCAN 12config CAN_SLCAN
15 tristate "Serial / USB serial CAN Adaptors (slcan)" 13 tristate "Serial / USB serial CAN Adaptors (slcan)"
16 depends on CAN
17 ---help--- 14 ---help---
18 CAN driver for several 'low cost' CAN interfaces that are attached 15 CAN driver for several 'low cost' CAN interfaces that are attached
19 via serial lines or via USB-to-serial adapters using the LAWICEL 16 via serial lines or via USB-to-serial adapters using the LAWICEL
@@ -33,16 +30,16 @@ config CAN_SLCAN
33 30
34config CAN_DEV 31config CAN_DEV
35 tristate "Platform CAN drivers with Netlink support" 32 tristate "Platform CAN drivers with Netlink support"
36 depends on CAN
37 default y 33 default y
38 ---help--- 34 ---help---
39 Enables the common framework for platform CAN drivers with Netlink 35 Enables the common framework for platform CAN drivers with Netlink
40 support. This is the standard library for CAN drivers. 36 support. This is the standard library for CAN drivers.
41 If unsure, say Y. 37 If unsure, say Y.
42 38
39if CAN_DEV
40
43config CAN_CALC_BITTIMING 41config CAN_CALC_BITTIMING
44 bool "CAN bit-timing calculation" 42 bool "CAN bit-timing calculation"
45 depends on CAN_DEV
46 default y 43 default y
47 ---help--- 44 ---help---
48 If enabled, CAN bit-timing parameters will be calculated for the 45 If enabled, CAN bit-timing parameters will be calculated for the
@@ -54,15 +51,26 @@ config CAN_CALC_BITTIMING
54 arguments "tq", "prop_seg", "phase_seg1", "phase_seg2" and "sjw". 51 arguments "tq", "prop_seg", "phase_seg1", "phase_seg2" and "sjw".
55 If unsure, say Y. 52 If unsure, say Y.
56 53
54config CAN_LEDS
55 bool "Enable LED triggers for Netlink based drivers"
56 depends on LEDS_CLASS
57 select LEDS_TRIGGERS
58 ---help---
59 This option adds two LED triggers for packet receive and transmit
60 events on each supported CAN device.
61
62 Say Y here if you are working on a system with led-class supported
63 LEDs and you want to use them as canbus activity indicators.
64
57config CAN_AT91 65config CAN_AT91
58 tristate "Atmel AT91 onchip CAN controller" 66 tristate "Atmel AT91 onchip CAN controller"
59 depends on CAN_DEV && (ARCH_AT91SAM9263 || ARCH_AT91SAM9X5) 67 depends on ARCH_AT91SAM9263 || ARCH_AT91SAM9X5
60 ---help--- 68 ---help---
61 This is a driver for the SoC CAN controller in Atmel's AT91SAM9263 69 This is a driver for the SoC CAN controller in Atmel's AT91SAM9263
62 and AT91SAM9X5 processors. 70 and AT91SAM9X5 processors.
63 71
64config CAN_TI_HECC 72config CAN_TI_HECC
65 depends on CAN_DEV && ARCH_OMAP3 73 depends on ARCH_OMAP3
66 tristate "TI High End CAN Controller" 74 tristate "TI High End CAN Controller"
67 ---help--- 75 ---help---
68 Driver for TI HECC (High End CAN Controller) module found on many 76 Driver for TI HECC (High End CAN Controller) module found on many
@@ -70,12 +78,12 @@ config CAN_TI_HECC
70 78
71config CAN_MCP251X 79config CAN_MCP251X
72 tristate "Microchip MCP251x SPI CAN controllers" 80 tristate "Microchip MCP251x SPI CAN controllers"
73 depends on CAN_DEV && SPI && HAS_DMA 81 depends on SPI && HAS_DMA
74 ---help--- 82 ---help---
75 Driver for the Microchip MCP251x SPI CAN controllers. 83 Driver for the Microchip MCP251x SPI CAN controllers.
76 84
77config CAN_BFIN 85config CAN_BFIN
78 depends on CAN_DEV && (BF534 || BF536 || BF537 || BF538 || BF539 || BF54x) 86 depends on BF534 || BF536 || BF537 || BF538 || BF539 || BF54x
79 tristate "Analog Devices Blackfin on-chip CAN" 87 tristate "Analog Devices Blackfin on-chip CAN"
80 ---help--- 88 ---help---
81 Driver for the Analog Devices Blackfin on-chip CAN controllers 89 Driver for the Analog Devices Blackfin on-chip CAN controllers
@@ -85,7 +93,7 @@ config CAN_BFIN
85 93
86config CAN_JANZ_ICAN3 94config CAN_JANZ_ICAN3
87 tristate "Janz VMOD-ICAN3 Intelligent CAN controller" 95 tristate "Janz VMOD-ICAN3 Intelligent CAN controller"
88 depends on CAN_DEV && MFD_JANZ_CMODIO 96 depends on MFD_JANZ_CMODIO
89 ---help--- 97 ---help---
90 Driver for Janz VMOD-ICAN3 Intelligent CAN controller module, which 98 Driver for Janz VMOD-ICAN3 Intelligent CAN controller module, which
91 connects to a MODULbus carrier board. 99 connects to a MODULbus carrier board.
@@ -98,13 +106,13 @@ config HAVE_CAN_FLEXCAN
98 106
99config CAN_FLEXCAN 107config CAN_FLEXCAN
100 tristate "Support for Freescale FLEXCAN based chips" 108 tristate "Support for Freescale FLEXCAN based chips"
101 depends on CAN_DEV && HAVE_CAN_FLEXCAN 109 depends on HAVE_CAN_FLEXCAN
102 ---help--- 110 ---help---
103 Say Y here if you want to support for Freescale FlexCAN. 111 Say Y here if you want to support for Freescale FlexCAN.
104 112
105config PCH_CAN 113config PCH_CAN
106 tristate "Intel EG20T PCH CAN controller" 114 tristate "Intel EG20T PCH CAN controller"
107 depends on CAN_DEV && PCI 115 depends on PCI
108 ---help--- 116 ---help---
109 This driver is for PCH CAN of Topcliff (Intel EG20T PCH) which 117 This driver is for PCH CAN of Topcliff (Intel EG20T PCH) which
110 is an IOH for x86 embedded processor (Intel Atom E6xx series). 118 is an IOH for x86 embedded processor (Intel Atom E6xx series).
@@ -112,7 +120,7 @@ config PCH_CAN
112 120
113config CAN_GRCAN 121config CAN_GRCAN
114 tristate "Aeroflex Gaisler GRCAN and GRHCAN CAN devices" 122 tristate "Aeroflex Gaisler GRCAN and GRHCAN CAN devices"
115 depends on CAN_DEV && OF 123 depends on OF
116 ---help--- 124 ---help---
117 Say Y here if you want to use Aeroflex Gaisler GRCAN or GRHCAN. 125 Say Y here if you want to use Aeroflex Gaisler GRCAN or GRHCAN.
118 Note that the driver supports little endian, even though little 126 Note that the driver supports little endian, even though little
@@ -131,9 +139,10 @@ source "drivers/net/can/usb/Kconfig"
131 139
132source "drivers/net/can/softing/Kconfig" 140source "drivers/net/can/softing/Kconfig"
133 141
142endif
143
134config CAN_DEBUG_DEVICES 144config CAN_DEBUG_DEVICES
135 bool "CAN devices debugging messages" 145 bool "CAN devices debugging messages"
136 depends on CAN
137 ---help--- 146 ---help---
138 Say Y here if you want the CAN device drivers to produce a bunch of 147 Say Y here if you want the CAN device drivers to produce a bunch of
139 debug messages to the system log. Select this if you are having 148 debug messages to the system log. Select this if you are having
diff --git a/drivers/net/can/Makefile b/drivers/net/can/Makefile
index 7de59862bbe9..c7440392adbb 100644
--- a/drivers/net/can/Makefile
+++ b/drivers/net/can/Makefile
@@ -8,6 +8,8 @@ obj-$(CONFIG_CAN_SLCAN) += slcan.o
8obj-$(CONFIG_CAN_DEV) += can-dev.o 8obj-$(CONFIG_CAN_DEV) += can-dev.o
9can-dev-y := dev.o 9can-dev-y := dev.o
10 10
11can-dev-$(CONFIG_CAN_LEDS) += led.o
12
11obj-y += usb/ 13obj-y += usb/
12obj-y += softing/ 14obj-y += softing/
13 15
diff --git a/drivers/net/can/at91_can.c b/drivers/net/can/at91_can.c
index 81baefda037b..44f363792b59 100644
--- a/drivers/net/can/at91_can.c
+++ b/drivers/net/can/at91_can.c
@@ -37,6 +37,7 @@
37 37
38#include <linux/can/dev.h> 38#include <linux/can/dev.h>
39#include <linux/can/error.h> 39#include <linux/can/error.h>
40#include <linux/can/led.h>
40 41
41#define AT91_MB_MASK(i) ((1 << (i)) - 1) 42#define AT91_MB_MASK(i) ((1 << (i)) - 1)
42 43
@@ -641,6 +642,8 @@ static void at91_read_msg(struct net_device *dev, unsigned int mb)
641 642
642 stats->rx_packets++; 643 stats->rx_packets++;
643 stats->rx_bytes += cf->can_dlc; 644 stats->rx_bytes += cf->can_dlc;
645
646 can_led_event(dev, CAN_LED_EVENT_RX);
644} 647}
645 648
646/** 649/**
@@ -875,6 +878,7 @@ static void at91_irq_tx(struct net_device *dev, u32 reg_sr)
875 /* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */ 878 /* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */
876 can_get_echo_skb(dev, mb - get_mb_tx_first(priv)); 879 can_get_echo_skb(dev, mb - get_mb_tx_first(priv));
877 dev->stats.tx_packets++; 880 dev->stats.tx_packets++;
881 can_led_event(dev, CAN_LED_EVENT_TX);
878 } 882 }
879 } 883 }
880 884
@@ -1128,6 +1132,8 @@ static int at91_open(struct net_device *dev)
1128 goto out_close; 1132 goto out_close;
1129 } 1133 }
1130 1134
1135 can_led_event(dev, CAN_LED_EVENT_OPEN);
1136
1131 /* start chip and queuing */ 1137 /* start chip and queuing */
1132 at91_chip_start(dev); 1138 at91_chip_start(dev);
1133 napi_enable(&priv->napi); 1139 napi_enable(&priv->napi);
@@ -1159,6 +1165,8 @@ static int at91_close(struct net_device *dev)
1159 1165
1160 close_candev(dev); 1166 close_candev(dev);
1161 1167
1168 can_led_event(dev, CAN_LED_EVENT_STOP);
1169
1162 return 0; 1170 return 0;
1163} 1171}
1164 1172
@@ -1321,6 +1329,8 @@ static int at91_can_probe(struct platform_device *pdev)
1321 goto exit_free; 1329 goto exit_free;
1322 } 1330 }
1323 1331
1332 devm_can_led_init(dev);
1333
1324 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n", 1334 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
1325 priv->reg_base, dev->irq); 1335 priv->reg_base, dev->irq);
1326 1336
diff --git a/drivers/net/can/c_can/Kconfig b/drivers/net/can/c_can/Kconfig
index 3b83bafcd947..61ffc12d8fd8 100644
--- a/drivers/net/can/c_can/Kconfig
+++ b/drivers/net/can/c_can/Kconfig
@@ -1,6 +1,6 @@
1menuconfig CAN_C_CAN 1menuconfig CAN_C_CAN
2 tristate "Bosch C_CAN/D_CAN devices" 2 tristate "Bosch C_CAN/D_CAN devices"
3 depends on CAN_DEV && HAS_IOMEM 3 depends on HAS_IOMEM
4 4
5if CAN_C_CAN 5if CAN_C_CAN
6 6
diff --git a/drivers/net/can/c_can/c_can.c b/drivers/net/can/c_can/c_can.c
index 2282b1ae9765..a668cd491cb3 100644
--- a/drivers/net/can/c_can/c_can.c
+++ b/drivers/net/can/c_can/c_can.c
@@ -39,6 +39,7 @@
39#include <linux/can.h> 39#include <linux/can.h>
40#include <linux/can/dev.h> 40#include <linux/can/dev.h>
41#include <linux/can/error.h> 41#include <linux/can/error.h>
42#include <linux/can/led.h>
42 43
43#include "c_can.h" 44#include "c_can.h"
44 45
@@ -477,6 +478,8 @@ static int c_can_read_msg_object(struct net_device *dev, int iface, int ctrl)
477 stats->rx_packets++; 478 stats->rx_packets++;
478 stats->rx_bytes += frame->can_dlc; 479 stats->rx_bytes += frame->can_dlc;
479 480
481 can_led_event(dev, CAN_LED_EVENT_RX);
482
480 return 0; 483 return 0;
481} 484}
482 485
@@ -755,6 +758,7 @@ static void c_can_do_tx(struct net_device *dev)
755 C_CAN_IFACE(MSGCTRL_REG, 0)) 758 C_CAN_IFACE(MSGCTRL_REG, 0))
756 & IF_MCONT_DLC_MASK; 759 & IF_MCONT_DLC_MASK;
757 stats->tx_packets++; 760 stats->tx_packets++;
761 can_led_event(dev, CAN_LED_EVENT_TX);
758 c_can_inval_msg_object(dev, 0, msg_obj_no); 762 c_can_inval_msg_object(dev, 0, msg_obj_no);
759 } else { 763 } else {
760 break; 764 break;
@@ -1119,6 +1123,8 @@ static int c_can_open(struct net_device *dev)
1119 1123
1120 napi_enable(&priv->napi); 1124 napi_enable(&priv->napi);
1121 1125
1126 can_led_event(dev, CAN_LED_EVENT_OPEN);
1127
1122 /* start the c_can controller */ 1128 /* start the c_can controller */
1123 c_can_start(dev); 1129 c_can_start(dev);
1124 1130
@@ -1147,6 +1153,8 @@ static int c_can_close(struct net_device *dev)
1147 c_can_reset_ram(priv, false); 1153 c_can_reset_ram(priv, false);
1148 c_can_pm_runtime_put_sync(priv); 1154 c_can_pm_runtime_put_sync(priv);
1149 1155
1156 can_led_event(dev, CAN_LED_EVENT_STOP);
1157
1150 return 0; 1158 return 0;
1151} 1159}
1152 1160
@@ -1272,6 +1280,8 @@ int register_c_can_dev(struct net_device *dev)
1272 err = register_candev(dev); 1280 err = register_candev(dev);
1273 if (err) 1281 if (err)
1274 c_can_pm_runtime_disable(priv); 1282 c_can_pm_runtime_disable(priv);
1283 else
1284 devm_can_led_init(dev);
1275 1285
1276 return err; 1286 return err;
1277} 1287}
diff --git a/drivers/net/can/cc770/Kconfig b/drivers/net/can/cc770/Kconfig
index 22c07a8c8b43..6a9a5ba79220 100644
--- a/drivers/net/can/cc770/Kconfig
+++ b/drivers/net/can/cc770/Kconfig
@@ -1,6 +1,6 @@
1menuconfig CAN_CC770 1menuconfig CAN_CC770
2 tristate "Bosch CC770 and Intel AN82527 devices" 2 tristate "Bosch CC770 and Intel AN82527 devices"
3 depends on CAN_DEV && HAS_IOMEM 3 depends on HAS_IOMEM
4 4
5if CAN_CC770 5if CAN_CC770
6 6
diff --git a/drivers/net/can/dev.c b/drivers/net/can/dev.c
index 8233e5ed2939..f9cba4123c66 100644
--- a/drivers/net/can/dev.c
+++ b/drivers/net/can/dev.c
@@ -24,7 +24,9 @@
24#include <linux/if_arp.h> 24#include <linux/if_arp.h>
25#include <linux/can.h> 25#include <linux/can.h>
26#include <linux/can/dev.h> 26#include <linux/can/dev.h>
27#include <linux/can/skb.h>
27#include <linux/can/netlink.h> 28#include <linux/can/netlink.h>
29#include <linux/can/led.h>
28#include <net/rtnetlink.h> 30#include <net/rtnetlink.h>
29 31
30#define MOD_DESC "CAN device driver interface" 32#define MOD_DESC "CAN device driver interface"
@@ -501,13 +503,18 @@ struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
501{ 503{
502 struct sk_buff *skb; 504 struct sk_buff *skb;
503 505
504 skb = netdev_alloc_skb(dev, sizeof(struct can_frame)); 506 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
507 sizeof(struct can_frame));
505 if (unlikely(!skb)) 508 if (unlikely(!skb))
506 return NULL; 509 return NULL;
507 510
508 skb->protocol = htons(ETH_P_CAN); 511 skb->protocol = htons(ETH_P_CAN);
509 skb->pkt_type = PACKET_BROADCAST; 512 skb->pkt_type = PACKET_BROADCAST;
510 skb->ip_summed = CHECKSUM_UNNECESSARY; 513 skb->ip_summed = CHECKSUM_UNNECESSARY;
514
515 can_skb_reserve(skb);
516 can_skb_prv(skb)->ifindex = dev->ifindex;
517
511 *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame)); 518 *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
512 memset(*cf, 0, sizeof(struct can_frame)); 519 memset(*cf, 0, sizeof(struct can_frame));
513 520
@@ -794,10 +801,25 @@ void unregister_candev(struct net_device *dev)
794} 801}
795EXPORT_SYMBOL_GPL(unregister_candev); 802EXPORT_SYMBOL_GPL(unregister_candev);
796 803
804/*
805 * Test if a network device is a candev based device
806 * and return the can_priv* if so.
807 */
808struct can_priv *safe_candev_priv(struct net_device *dev)
809{
810 if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
811 return NULL;
812
813 return netdev_priv(dev);
814}
815EXPORT_SYMBOL_GPL(safe_candev_priv);
816
797static __init int can_dev_init(void) 817static __init int can_dev_init(void)
798{ 818{
799 int err; 819 int err;
800 820
821 can_led_notifier_init();
822
801 err = rtnl_link_register(&can_link_ops); 823 err = rtnl_link_register(&can_link_ops);
802 if (!err) 824 if (!err)
803 printk(KERN_INFO MOD_DESC "\n"); 825 printk(KERN_INFO MOD_DESC "\n");
@@ -809,6 +831,8 @@ module_init(can_dev_init);
809static __exit void can_dev_exit(void) 831static __exit void can_dev_exit(void)
810{ 832{
811 rtnl_link_unregister(&can_link_ops); 833 rtnl_link_unregister(&can_link_ops);
834
835 can_led_notifier_exit();
812} 836}
813module_exit(can_dev_exit); 837module_exit(can_dev_exit);
814 838
diff --git a/drivers/net/can/flexcan.c b/drivers/net/can/flexcan.c
index 0289a6d86f66..769d29ed106d 100644
--- a/drivers/net/can/flexcan.c
+++ b/drivers/net/can/flexcan.c
@@ -23,6 +23,7 @@
23#include <linux/can.h> 23#include <linux/can.h>
24#include <linux/can/dev.h> 24#include <linux/can/dev.h>
25#include <linux/can/error.h> 25#include <linux/can/error.h>
26#include <linux/can/led.h>
26#include <linux/can/platform/flexcan.h> 27#include <linux/can/platform/flexcan.h>
27#include <linux/clk.h> 28#include <linux/clk.h>
28#include <linux/delay.h> 29#include <linux/delay.h>
@@ -564,6 +565,8 @@ static int flexcan_read_frame(struct net_device *dev)
564 stats->rx_packets++; 565 stats->rx_packets++;
565 stats->rx_bytes += cf->can_dlc; 566 stats->rx_bytes += cf->can_dlc;
566 567
568 can_led_event(dev, CAN_LED_EVENT_RX);
569
567 return 1; 570 return 1;
568} 571}
569 572
@@ -652,6 +655,7 @@ static irqreturn_t flexcan_irq(int irq, void *dev_id)
652 if (reg_iflag1 & (1 << FLEXCAN_TX_BUF_ID)) { 655 if (reg_iflag1 & (1 << FLEXCAN_TX_BUF_ID)) {
653 stats->tx_bytes += can_get_echo_skb(dev, 0); 656 stats->tx_bytes += can_get_echo_skb(dev, 0);
654 stats->tx_packets++; 657 stats->tx_packets++;
658 can_led_event(dev, CAN_LED_EVENT_TX);
655 flexcan_write((1 << FLEXCAN_TX_BUF_ID), &regs->iflag1); 659 flexcan_write((1 << FLEXCAN_TX_BUF_ID), &regs->iflag1);
656 netif_wake_queue(dev); 660 netif_wake_queue(dev);
657 } 661 }
@@ -865,6 +869,9 @@ static int flexcan_open(struct net_device *dev)
865 err = flexcan_chip_start(dev); 869 err = flexcan_chip_start(dev);
866 if (err) 870 if (err)
867 goto out_close; 871 goto out_close;
872
873 can_led_event(dev, CAN_LED_EVENT_OPEN);
874
868 napi_enable(&priv->napi); 875 napi_enable(&priv->napi);
869 netif_start_queue(dev); 876 netif_start_queue(dev);
870 877
@@ -893,6 +900,8 @@ static int flexcan_close(struct net_device *dev)
893 900
894 close_candev(dev); 901 close_candev(dev);
895 902
903 can_led_event(dev, CAN_LED_EVENT_STOP);
904
896 return 0; 905 return 0;
897} 906}
898 907
@@ -1092,6 +1101,8 @@ static int flexcan_probe(struct platform_device *pdev)
1092 goto failed_register; 1101 goto failed_register;
1093 } 1102 }
1094 1103
1104 devm_can_led_init(dev);
1105
1095 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n", 1106 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
1096 priv->base, dev->irq); 1107 priv->base, dev->irq);
1097 1108
diff --git a/drivers/net/can/led.c b/drivers/net/can/led.c
new file mode 100644
index 000000000000..f27fca65dc4a
--- /dev/null
+++ b/drivers/net/can/led.c
@@ -0,0 +1,124 @@
1/*
2 * Copyright 2012, Fabio Baltieri <fabio.baltieri@gmail.com>
3 * Copyright 2012, Kurt Van Dijck <kurt.van.dijck@eia.be>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/module.h>
11#include <linux/device.h>
12#include <linux/kernel.h>
13#include <linux/slab.h>
14#include <linux/netdevice.h>
15#include <linux/can/dev.h>
16
17#include <linux/can/led.h>
18
19static unsigned long led_delay = 50;
20module_param(led_delay, ulong, 0644);
21MODULE_PARM_DESC(led_delay,
22 "blink delay time for activity leds (msecs, default: 50).");
23
24/* Trigger a LED event in response to a CAN device event */
25void can_led_event(struct net_device *netdev, enum can_led_event event)
26{
27 struct can_priv *priv = netdev_priv(netdev);
28
29 switch (event) {
30 case CAN_LED_EVENT_OPEN:
31 led_trigger_event(priv->tx_led_trig, LED_FULL);
32 led_trigger_event(priv->rx_led_trig, LED_FULL);
33 break;
34 case CAN_LED_EVENT_STOP:
35 led_trigger_event(priv->tx_led_trig, LED_OFF);
36 led_trigger_event(priv->rx_led_trig, LED_OFF);
37 break;
38 case CAN_LED_EVENT_TX:
39 if (led_delay)
40 led_trigger_blink_oneshot(priv->tx_led_trig,
41 &led_delay, &led_delay, 1);
42 break;
43 case CAN_LED_EVENT_RX:
44 if (led_delay)
45 led_trigger_blink_oneshot(priv->rx_led_trig,
46 &led_delay, &led_delay, 1);
47 break;
48 }
49}
50EXPORT_SYMBOL_GPL(can_led_event);
51
52static void can_led_release(struct device *gendev, void *res)
53{
54 struct can_priv *priv = netdev_priv(to_net_dev(gendev));
55
56 led_trigger_unregister_simple(priv->tx_led_trig);
57 led_trigger_unregister_simple(priv->rx_led_trig);
58}
59
60/* Register CAN LED triggers for a CAN device
61 *
62 * This is normally called from a driver's probe function
63 */
64void devm_can_led_init(struct net_device *netdev)
65{
66 struct can_priv *priv = netdev_priv(netdev);
67 void *res;
68
69 res = devres_alloc(can_led_release, 0, GFP_KERNEL);
70 if (!res) {
71 netdev_err(netdev, "cannot register LED triggers\n");
72 return;
73 }
74
75 snprintf(priv->tx_led_trig_name, sizeof(priv->tx_led_trig_name),
76 "%s-tx", netdev->name);
77 snprintf(priv->rx_led_trig_name, sizeof(priv->rx_led_trig_name),
78 "%s-rx", netdev->name);
79
80 led_trigger_register_simple(priv->tx_led_trig_name,
81 &priv->tx_led_trig);
82 led_trigger_register_simple(priv->rx_led_trig_name,
83 &priv->rx_led_trig);
84
85 devres_add(&netdev->dev, res);
86}
87EXPORT_SYMBOL_GPL(devm_can_led_init);
88
89/* NETDEV rename notifier to rename the associated led triggers too */
90static int can_led_notifier(struct notifier_block *nb, unsigned long msg,
91 void *data)
92{
93 struct net_device *netdev = data;
94 struct can_priv *priv = safe_candev_priv(netdev);
95 char name[CAN_LED_NAME_SZ];
96
97 if (!priv)
98 return NOTIFY_DONE;
99
100 if (msg == NETDEV_CHANGENAME) {
101 snprintf(name, sizeof(name), "%s-tx", netdev->name);
102 led_trigger_rename_static(name, priv->tx_led_trig);
103
104 snprintf(name, sizeof(name), "%s-rx", netdev->name);
105 led_trigger_rename_static(name, priv->rx_led_trig);
106 }
107
108 return NOTIFY_DONE;
109}
110
111/* notifier block for netdevice event */
112static struct notifier_block can_netdev_notifier __read_mostly = {
113 .notifier_call = can_led_notifier,
114};
115
116int __init can_led_notifier_init(void)
117{
118 return register_netdevice_notifier(&can_netdev_notifier);
119}
120
121void __exit can_led_notifier_exit(void)
122{
123 unregister_netdevice_notifier(&can_netdev_notifier);
124}
diff --git a/drivers/net/can/mcp251x.c b/drivers/net/can/mcp251x.c
index 5eaf47b8e37b..f32b9fc6a983 100644
--- a/drivers/net/can/mcp251x.c
+++ b/drivers/net/can/mcp251x.c
@@ -60,6 +60,7 @@
60 60
61#include <linux/can/core.h> 61#include <linux/can/core.h>
62#include <linux/can/dev.h> 62#include <linux/can/dev.h>
63#include <linux/can/led.h>
63#include <linux/can/platform/mcp251x.h> 64#include <linux/can/platform/mcp251x.h>
64#include <linux/completion.h> 65#include <linux/completion.h>
65#include <linux/delay.h> 66#include <linux/delay.h>
@@ -494,6 +495,9 @@ static void mcp251x_hw_rx(struct spi_device *spi, int buf_idx)
494 495
495 priv->net->stats.rx_packets++; 496 priv->net->stats.rx_packets++;
496 priv->net->stats.rx_bytes += frame->can_dlc; 497 priv->net->stats.rx_bytes += frame->can_dlc;
498
499 can_led_event(priv->net, CAN_LED_EVENT_RX);
500
497 netif_rx_ni(skb); 501 netif_rx_ni(skb);
498} 502}
499 503
@@ -707,6 +711,8 @@ static int mcp251x_stop(struct net_device *net)
707 711
708 mutex_unlock(&priv->mcp_lock); 712 mutex_unlock(&priv->mcp_lock);
709 713
714 can_led_event(net, CAN_LED_EVENT_STOP);
715
710 return 0; 716 return 0;
711} 717}
712 718
@@ -905,6 +911,7 @@ static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
905 if (intf & CANINTF_TX) { 911 if (intf & CANINTF_TX) {
906 net->stats.tx_packets++; 912 net->stats.tx_packets++;
907 net->stats.tx_bytes += priv->tx_len - 1; 913 net->stats.tx_bytes += priv->tx_len - 1;
914 can_led_event(net, CAN_LED_EVENT_TX);
908 if (priv->tx_len) { 915 if (priv->tx_len) {
909 can_get_echo_skb(net, 0); 916 can_get_echo_skb(net, 0);
910 priv->tx_len = 0; 917 priv->tx_len = 0;
@@ -968,6 +975,9 @@ static int mcp251x_open(struct net_device *net)
968 mcp251x_open_clean(net); 975 mcp251x_open_clean(net);
969 goto open_unlock; 976 goto open_unlock;
970 } 977 }
978
979 can_led_event(net, CAN_LED_EVENT_OPEN);
980
971 netif_wake_queue(net); 981 netif_wake_queue(net);
972 982
973open_unlock: 983open_unlock:
@@ -1077,10 +1087,15 @@ static int mcp251x_can_probe(struct spi_device *spi)
1077 pdata->transceiver_enable(0); 1087 pdata->transceiver_enable(0);
1078 1088
1079 ret = register_candev(net); 1089 ret = register_candev(net);
1080 if (!ret) { 1090 if (ret)
1081 dev_info(&spi->dev, "probed\n"); 1091 goto error_probe;
1082 return ret; 1092
1083 } 1093 devm_can_led_init(net);
1094
1095 dev_info(&spi->dev, "probed\n");
1096
1097 return ret;
1098
1084error_probe: 1099error_probe:
1085 if (!mcp251x_enable_dma) 1100 if (!mcp251x_enable_dma)
1086 kfree(priv->spi_rx_buf); 1101 kfree(priv->spi_rx_buf);
diff --git a/drivers/net/can/mscan/Kconfig b/drivers/net/can/mscan/Kconfig
index d38706958af6..f19be5269e7b 100644
--- a/drivers/net/can/mscan/Kconfig
+++ b/drivers/net/can/mscan/Kconfig
@@ -1,5 +1,5 @@
1config CAN_MSCAN 1config CAN_MSCAN
2 depends on CAN_DEV && (PPC || M68K) 2 depends on PPC || M68K
3 tristate "Support for Freescale MSCAN based chips" 3 tristate "Support for Freescale MSCAN based chips"
4 ---help--- 4 ---help---
5 The Motorola Scalable Controller Area Network (MSCAN) definition 5 The Motorola Scalable Controller Area Network (MSCAN) definition
diff --git a/drivers/net/can/sja1000/Kconfig b/drivers/net/can/sja1000/Kconfig
index 92f73c708a3d..b39ca5b3ea7f 100644
--- a/drivers/net/can/sja1000/Kconfig
+++ b/drivers/net/can/sja1000/Kconfig
@@ -1,6 +1,6 @@
1menuconfig CAN_SJA1000 1menuconfig CAN_SJA1000
2 tristate "Philips/NXP SJA1000 devices" 2 tristate "Philips/NXP SJA1000 devices"
3 depends on CAN_DEV && HAS_IOMEM 3 depends on HAS_IOMEM
4 4
5if CAN_SJA1000 5if CAN_SJA1000
6 6
@@ -99,11 +99,11 @@ config CAN_TSCAN1
99 tristate "TS-CAN1 PC104 boards" 99 tristate "TS-CAN1 PC104 boards"
100 depends on ISA 100 depends on ISA
101 help 101 help
102 This driver is for Technologic Systems' TSCAN-1 PC104 boards. 102 This driver is for Technologic Systems' TSCAN-1 PC104 boards.
103 http://www.embeddedarm.com/products/board-detail.php?product=TS-CAN1 103 http://www.embeddedarm.com/products/board-detail.php?product=TS-CAN1
104 The driver supports multiple boards and automatically configures them: 104 The driver supports multiple boards and automatically configures them:
105 PLD IO base addresses are read from jumpers JP1 and JP2, 105 PLD IO base addresses are read from jumpers JP1 and JP2,
106 IRQ numbers are read from jumpers JP4 and JP5, 106 IRQ numbers are read from jumpers JP4 and JP5,
107 SJA1000 IO base addresses are chosen heuristically (first that works). 107 SJA1000 IO base addresses are chosen heuristically (first that works).
108 108
109endif 109endif
diff --git a/drivers/net/can/sja1000/ems_pci.c b/drivers/net/can/sja1000/ems_pci.c
index 036a326836b2..36d298da2af6 100644
--- a/drivers/net/can/sja1000/ems_pci.c
+++ b/drivers/net/can/sja1000/ems_pci.c
@@ -238,7 +238,6 @@ static int ems_pci_add_card(struct pci_dev *pdev,
238 /* Allocating card structures to hold addresses, ... */ 238 /* Allocating card structures to hold addresses, ... */
239 card = kzalloc(sizeof(struct ems_pci_card), GFP_KERNEL); 239 card = kzalloc(sizeof(struct ems_pci_card), GFP_KERNEL);
240 if (card == NULL) { 240 if (card == NULL) {
241 dev_err(&pdev->dev, "Unable to allocate memory\n");
242 pci_disable_device(pdev); 241 pci_disable_device(pdev);
243 return -ENOMEM; 242 return -ENOMEM;
244 } 243 }
diff --git a/drivers/net/can/sja1000/peak_pci.c b/drivers/net/can/sja1000/peak_pci.c
index 600ac7226e5c..d1e7f1006ddd 100644
--- a/drivers/net/can/sja1000/peak_pci.c
+++ b/drivers/net/can/sja1000/peak_pci.c
@@ -450,11 +450,8 @@ static int peak_pciec_probe(struct pci_dev *pdev, struct net_device *dev)
450 } else { 450 } else {
451 /* create the bit banging I2C adapter structure */ 451 /* create the bit banging I2C adapter structure */
452 card = kzalloc(sizeof(struct peak_pciec_card), GFP_KERNEL); 452 card = kzalloc(sizeof(struct peak_pciec_card), GFP_KERNEL);
453 if (!card) { 453 if (!card)
454 dev_err(&pdev->dev,
455 "failed allocating memory for i2c chip\n");
456 return -ENOMEM; 454 return -ENOMEM;
457 }
458 455
459 card->cfg_base = chan->cfg_base; 456 card->cfg_base = chan->cfg_base;
460 card->reg_base = priv->reg_base; 457 card->reg_base = priv->reg_base;
diff --git a/drivers/net/can/sja1000/peak_pcmcia.c b/drivers/net/can/sja1000/peak_pcmcia.c
index f1175142b0a0..1a7020ba37f5 100644
--- a/drivers/net/can/sja1000/peak_pcmcia.c
+++ b/drivers/net/can/sja1000/peak_pcmcia.c
@@ -660,7 +660,6 @@ static int pcan_probe(struct pcmcia_device *pdev)
660 660
661 card = kzalloc(sizeof(struct pcan_pccard), GFP_KERNEL); 661 card = kzalloc(sizeof(struct pcan_pccard), GFP_KERNEL);
662 if (!card) { 662 if (!card) {
663 dev_err(&pdev->dev, "couldn't allocate card memory\n");
664 err = -ENOMEM; 663 err = -ENOMEM;
665 goto probe_err_2; 664 goto probe_err_2;
666 } 665 }
diff --git a/drivers/net/can/sja1000/plx_pci.c b/drivers/net/can/sja1000/plx_pci.c
index 11d1062a9449..a042cdc260dc 100644
--- a/drivers/net/can/sja1000/plx_pci.c
+++ b/drivers/net/can/sja1000/plx_pci.c
@@ -508,7 +508,6 @@ static int plx_pci_add_card(struct pci_dev *pdev,
508 /* Allocate card structures to hold addresses, ... */ 508 /* Allocate card structures to hold addresses, ... */
509 card = kzalloc(sizeof(*card), GFP_KERNEL); 509 card = kzalloc(sizeof(*card), GFP_KERNEL);
510 if (!card) { 510 if (!card) {
511 dev_err(&pdev->dev, "Unable to allocate memory\n");
512 pci_disable_device(pdev); 511 pci_disable_device(pdev);
513 return -ENOMEM; 512 return -ENOMEM;
514 } 513 }
diff --git a/drivers/net/can/sja1000/sja1000.c b/drivers/net/can/sja1000/sja1000.c
index 83ee11eca0e2..daf4013a8fc7 100644
--- a/drivers/net/can/sja1000/sja1000.c
+++ b/drivers/net/can/sja1000/sja1000.c
@@ -60,6 +60,7 @@
60 60
61#include <linux/can/dev.h> 61#include <linux/can/dev.h>
62#include <linux/can/error.h> 62#include <linux/can/error.h>
63#include <linux/can/led.h>
63 64
64#include "sja1000.h" 65#include "sja1000.h"
65 66
@@ -368,6 +369,8 @@ static void sja1000_rx(struct net_device *dev)
368 369
369 stats->rx_packets++; 370 stats->rx_packets++;
370 stats->rx_bytes += cf->can_dlc; 371 stats->rx_bytes += cf->can_dlc;
372
373 can_led_event(dev, CAN_LED_EVENT_RX);
371} 374}
372 375
373static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status) 376static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status)
@@ -521,6 +524,7 @@ irqreturn_t sja1000_interrupt(int irq, void *dev_id)
521 can_get_echo_skb(dev, 0); 524 can_get_echo_skb(dev, 0);
522 } 525 }
523 netif_wake_queue(dev); 526 netif_wake_queue(dev);
527 can_led_event(dev, CAN_LED_EVENT_TX);
524 } 528 }
525 if (isrc & IRQ_RI) { 529 if (isrc & IRQ_RI) {
526 /* receive interrupt */ 530 /* receive interrupt */
@@ -575,6 +579,8 @@ static int sja1000_open(struct net_device *dev)
575 /* init and start chi */ 579 /* init and start chi */
576 sja1000_start(dev); 580 sja1000_start(dev);
577 581
582 can_led_event(dev, CAN_LED_EVENT_OPEN);
583
578 netif_start_queue(dev); 584 netif_start_queue(dev);
579 585
580 return 0; 586 return 0;
@@ -592,6 +598,8 @@ static int sja1000_close(struct net_device *dev)
592 598
593 close_candev(dev); 599 close_candev(dev);
594 600
601 can_led_event(dev, CAN_LED_EVENT_STOP);
602
595 return 0; 603 return 0;
596} 604}
597 605
@@ -639,6 +647,8 @@ static const struct net_device_ops sja1000_netdev_ops = {
639 647
640int register_sja1000dev(struct net_device *dev) 648int register_sja1000dev(struct net_device *dev)
641{ 649{
650 int ret;
651
642 if (!sja1000_probe_chip(dev)) 652 if (!sja1000_probe_chip(dev))
643 return -ENODEV; 653 return -ENODEV;
644 654
@@ -648,7 +658,12 @@ int register_sja1000dev(struct net_device *dev)
648 set_reset_mode(dev); 658 set_reset_mode(dev);
649 chipset_init(dev); 659 chipset_init(dev);
650 660
651 return register_candev(dev); 661 ret = register_candev(dev);
662
663 if (!ret)
664 devm_can_led_init(dev);
665
666 return ret;
652} 667}
653EXPORT_SYMBOL_GPL(register_sja1000dev); 668EXPORT_SYMBOL_GPL(register_sja1000dev);
654 669
diff --git a/drivers/net/can/slcan.c b/drivers/net/can/slcan.c
index adc3708d8829..06b7e097d36e 100644
--- a/drivers/net/can/slcan.c
+++ b/drivers/net/can/slcan.c
@@ -55,6 +55,7 @@
55#include <linux/init.h> 55#include <linux/init.h>
56#include <linux/kernel.h> 56#include <linux/kernel.h>
57#include <linux/can.h> 57#include <linux/can.h>
58#include <linux/can/skb.h>
58 59
59static __initconst const char banner[] = 60static __initconst const char banner[] =
60 KERN_INFO "slcan: serial line CAN interface driver\n"; 61 KERN_INFO "slcan: serial line CAN interface driver\n";
@@ -184,7 +185,8 @@ static void slc_bump(struct slcan *sl)
184 cf.data[i] |= tmp; 185 cf.data[i] |= tmp;
185 } 186 }
186 187
187 skb = dev_alloc_skb(sizeof(struct can_frame)); 188 skb = dev_alloc_skb(sizeof(struct can_frame) +
189 sizeof(struct can_skb_priv));
188 if (!skb) 190 if (!skb)
189 return; 191 return;
190 192
@@ -192,6 +194,10 @@ static void slc_bump(struct slcan *sl)
192 skb->protocol = htons(ETH_P_CAN); 194 skb->protocol = htons(ETH_P_CAN);
193 skb->pkt_type = PACKET_BROADCAST; 195 skb->pkt_type = PACKET_BROADCAST;
194 skb->ip_summed = CHECKSUM_UNNECESSARY; 196 skb->ip_summed = CHECKSUM_UNNECESSARY;
197
198 can_skb_reserve(skb);
199 can_skb_prv(skb)->ifindex = sl->dev->ifindex;
200
195 memcpy(skb_put(skb, sizeof(struct can_frame)), 201 memcpy(skb_put(skb, sizeof(struct can_frame)),
196 &cf, sizeof(struct can_frame)); 202 &cf, sizeof(struct can_frame));
197 netif_rx_ni(skb); 203 netif_rx_ni(skb);
diff --git a/drivers/net/can/softing/Kconfig b/drivers/net/can/softing/Kconfig
index 5de46a9a77bb..96b6fe158b5b 100644
--- a/drivers/net/can/softing/Kconfig
+++ b/drivers/net/can/softing/Kconfig
@@ -1,6 +1,6 @@
1config CAN_SOFTING 1config CAN_SOFTING
2 tristate "Softing Gmbh CAN generic support" 2 tristate "Softing Gmbh CAN generic support"
3 depends on CAN_DEV && HAS_IOMEM 3 depends on HAS_IOMEM
4 ---help--- 4 ---help---
5 Support for CAN cards from Softing Gmbh & some cards 5 Support for CAN cards from Softing Gmbh & some cards
6 from Vector Gmbh. 6 from Vector Gmbh.
diff --git a/drivers/net/can/ti_hecc.c b/drivers/net/can/ti_hecc.c
index 300581b24ff3..f21fc37ec578 100644
--- a/drivers/net/can/ti_hecc.c
+++ b/drivers/net/can/ti_hecc.c
@@ -50,6 +50,7 @@
50 50
51#include <linux/can/dev.h> 51#include <linux/can/dev.h>
52#include <linux/can/error.h> 52#include <linux/can/error.h>
53#include <linux/can/led.h>
53#include <linux/can/platform/ti_hecc.h> 54#include <linux/can/platform/ti_hecc.h>
54 55
55#define DRV_NAME "ti_hecc" 56#define DRV_NAME "ti_hecc"
@@ -593,6 +594,7 @@ static int ti_hecc_rx_pkt(struct ti_hecc_priv *priv, int mbxno)
593 spin_unlock_irqrestore(&priv->mbx_lock, flags); 594 spin_unlock_irqrestore(&priv->mbx_lock, flags);
594 595
595 stats->rx_bytes += cf->can_dlc; 596 stats->rx_bytes += cf->can_dlc;
597 can_led_event(priv->ndev, CAN_LED_EVENT_RX);
596 netif_receive_skb(skb); 598 netif_receive_skb(skb);
597 stats->rx_packets++; 599 stats->rx_packets++;
598 600
@@ -796,6 +798,7 @@ static irqreturn_t ti_hecc_interrupt(int irq, void *dev_id)
796 stats->tx_bytes += hecc_read_mbx(priv, mbxno, 798 stats->tx_bytes += hecc_read_mbx(priv, mbxno,
797 HECC_CANMCF) & 0xF; 799 HECC_CANMCF) & 0xF;
798 stats->tx_packets++; 800 stats->tx_packets++;
801 can_led_event(ndev, CAN_LED_EVENT_TX);
799 can_get_echo_skb(ndev, mbxno); 802 can_get_echo_skb(ndev, mbxno);
800 --priv->tx_tail; 803 --priv->tx_tail;
801 } 804 }
@@ -851,6 +854,8 @@ static int ti_hecc_open(struct net_device *ndev)
851 return err; 854 return err;
852 } 855 }
853 856
857 can_led_event(ndev, CAN_LED_EVENT_OPEN);
858
854 ti_hecc_start(ndev); 859 ti_hecc_start(ndev);
855 napi_enable(&priv->napi); 860 napi_enable(&priv->napi);
856 netif_start_queue(ndev); 861 netif_start_queue(ndev);
@@ -869,6 +874,8 @@ static int ti_hecc_close(struct net_device *ndev)
869 close_candev(ndev); 874 close_candev(ndev);
870 ti_hecc_transceiver_switch(priv, 0); 875 ti_hecc_transceiver_switch(priv, 0);
871 876
877 can_led_event(ndev, CAN_LED_EVENT_STOP);
878
872 return 0; 879 return 0;
873} 880}
874 881
@@ -961,6 +968,9 @@ static int ti_hecc_probe(struct platform_device *pdev)
961 dev_err(&pdev->dev, "register_candev() failed\n"); 968 dev_err(&pdev->dev, "register_candev() failed\n");
962 goto probe_exit_clk; 969 goto probe_exit_clk;
963 } 970 }
971
972 devm_can_led_init(ndev);
973
964 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%u)\n", 974 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%u)\n",
965 priv->base, (u32) ndev->irq); 975 priv->base, (u32) ndev->irq);
966 976
diff --git a/drivers/net/can/usb/Kconfig b/drivers/net/can/usb/Kconfig
index a4e4bee35710..fc96a3d83ebe 100644
--- a/drivers/net/can/usb/Kconfig
+++ b/drivers/net/can/usb/Kconfig
@@ -1,5 +1,5 @@
1menu "CAN USB interfaces" 1menu "CAN USB interfaces"
2 depends on USB && CAN_DEV 2 depends on USB
3 3
4config CAN_EMS_USB 4config CAN_EMS_USB
5 tristate "EMS CPC-USB/ARM7 CAN/USB interface" 5 tristate "EMS CPC-USB/ARM7 CAN/USB interface"
@@ -48,4 +48,10 @@ config CAN_PEAK_USB
48 This driver supports the PCAN-USB and PCAN-USB Pro adapters 48 This driver supports the PCAN-USB and PCAN-USB Pro adapters
49 from PEAK-System Technik (http://www.peak-system.com). 49 from PEAK-System Technik (http://www.peak-system.com).
50 50
51config CAN_8DEV_USB
52 tristate "8 devices USB2CAN interface"
53 ---help---
54 This driver supports the USB2CAN interface
55 from 8 devices (http://www.8devices.com).
56
51endmenu 57endmenu
diff --git a/drivers/net/can/usb/Makefile b/drivers/net/can/usb/Makefile
index 80a2ee41fd61..becef460a91a 100644
--- a/drivers/net/can/usb/Makefile
+++ b/drivers/net/can/usb/Makefile
@@ -6,5 +6,6 @@ obj-$(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 7obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb.o
8obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/ 8obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/
9obj-$(CONFIG_CAN_8DEV_USB) += usb_8dev.o
9 10
10ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG 11ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG
diff --git a/drivers/net/can/usb/ems_usb.c b/drivers/net/can/usb/ems_usb.c
index c69f0b72b352..5f9a7ad9b964 100644
--- a/drivers/net/can/usb/ems_usb.c
+++ b/drivers/net/can/usb/ems_usb.c
@@ -1014,17 +1014,13 @@ static int ems_usb_probe(struct usb_interface *intf,
1014 } 1014 }
1015 1015
1016 dev->intr_in_buffer = kzalloc(INTR_IN_BUFFER_SIZE, GFP_KERNEL); 1016 dev->intr_in_buffer = kzalloc(INTR_IN_BUFFER_SIZE, GFP_KERNEL);
1017 if (!dev->intr_in_buffer) { 1017 if (!dev->intr_in_buffer)
1018 dev_err(&intf->dev, "Couldn't alloc Intr buffer\n");
1019 goto cleanup_intr_urb; 1018 goto cleanup_intr_urb;
1020 }
1021 1019
1022 dev->tx_msg_buffer = kzalloc(CPC_HEADER_SIZE + 1020 dev->tx_msg_buffer = kzalloc(CPC_HEADER_SIZE +
1023 sizeof(struct ems_cpc_msg), GFP_KERNEL); 1021 sizeof(struct ems_cpc_msg), GFP_KERNEL);
1024 if (!dev->tx_msg_buffer) { 1022 if (!dev->tx_msg_buffer)
1025 dev_err(&intf->dev, "Couldn't alloc Tx buffer\n");
1026 goto cleanup_intr_in_buffer; 1023 goto cleanup_intr_in_buffer;
1027 }
1028 1024
1029 usb_set_intfdata(intf, dev); 1025 usb_set_intfdata(intf, dev);
1030 1026
diff --git a/drivers/net/can/usb/kvaser_usb.c b/drivers/net/can/usb/kvaser_usb.c
index 5b58a4d87397..45cb9f3c1324 100644
--- a/drivers/net/can/usb/kvaser_usb.c
+++ b/drivers/net/can/usb/kvaser_usb.c
@@ -561,7 +561,6 @@ static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
561 561
562 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC); 562 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
563 if (!buf) { 563 if (!buf) {
564 netdev_err(netdev, "No memory left for USB buffer\n");
565 usb_free_urb(urb); 564 usb_free_urb(urb);
566 return -ENOMEM; 565 return -ENOMEM;
567 } 566 }
@@ -1268,7 +1267,6 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1268 1267
1269 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC); 1268 buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC);
1270 if (!buf) { 1269 if (!buf) {
1271 netdev_err(netdev, "No memory left for USB buffer\n");
1272 stats->tx_dropped++; 1270 stats->tx_dropped++;
1273 goto nobufmem; 1271 goto nobufmem;
1274 } 1272 }
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.c b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
index d9290ea788e0..a0f647f92bf5 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
@@ -386,7 +386,6 @@ static int peak_usb_start(struct peak_usb_device *dev)
386 386
387 buf = kmalloc(dev->adapter->rx_buffer_size, GFP_KERNEL); 387 buf = kmalloc(dev->adapter->rx_buffer_size, GFP_KERNEL);
388 if (!buf) { 388 if (!buf) {
389 netdev_err(netdev, "No memory left for USB buffer\n");
390 usb_free_urb(urb); 389 usb_free_urb(urb);
391 err = -ENOMEM; 390 err = -ENOMEM;
392 break; 391 break;
@@ -442,7 +441,6 @@ static int peak_usb_start(struct peak_usb_device *dev)
442 441
443 buf = kmalloc(dev->adapter->tx_buffer_size, GFP_KERNEL); 442 buf = kmalloc(dev->adapter->tx_buffer_size, GFP_KERNEL);
444 if (!buf) { 443 if (!buf) {
445 netdev_err(netdev, "No memory left for USB buffer\n");
446 usb_free_urb(urb); 444 usb_free_urb(urb);
447 err = -ENOMEM; 445 err = -ENOMEM;
448 break; 446 break;
@@ -634,7 +632,6 @@ static int peak_usb_restart(struct peak_usb_device *dev)
634 /* also allocate enough space for the commands to send */ 632 /* also allocate enough space for the commands to send */
635 buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_ATOMIC); 633 buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_ATOMIC);
636 if (!buf) { 634 if (!buf) {
637 netdev_err(dev->netdev, "no memory left for async cmd\n");
638 usb_free_urb(urb); 635 usb_free_urb(urb);
639 return -ENOMEM; 636 return -ENOMEM;
640 } 637 }
@@ -729,8 +726,6 @@ static int peak_usb_create_dev(struct peak_usb_adapter *peak_usb_adapter,
729 /* allocate a buffer large enough to send commands */ 726 /* allocate a buffer large enough to send commands */
730 dev->cmd_buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_KERNEL); 727 dev->cmd_buf = kmalloc(PCAN_USB_MAX_CMD_LEN, GFP_KERNEL);
731 if (!dev->cmd_buf) { 728 if (!dev->cmd_buf) {
732 dev_err(&intf->dev, "%s: couldn't alloc cmd buffer\n",
733 PCAN_USB_DRIVER_NAME);
734 err = -ENOMEM; 729 err = -ENOMEM;
735 goto lbl_set_intf_data; 730 goto lbl_set_intf_data;
736 } 731 }
diff --git a/drivers/net/can/usb/usb_8dev.c b/drivers/net/can/usb/usb_8dev.c
new file mode 100644
index 000000000000..6e15ef08f301
--- /dev/null
+++ b/drivers/net/can/usb/usb_8dev.c
@@ -0,0 +1,1031 @@
1/*
2 * CAN driver for "8 devices" USB2CAN converter
3 *
4 * Copyright (C) 2012 Bernd Krumboeck (krumboeck@universalnet.at)
5 *
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
8 * by the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program.
17 *
18 * This driver is inspired by the 3.2.0 version of drivers/net/can/usb/ems_usb.c
19 * and drivers/net/can/usb/esd_usb2.c
20 *
21 * Many thanks to Gerhard Bertelsmann (info@gerhard-bertelsmann.de)
22 * for testing and fixing this driver. Also many thanks to "8 devices",
23 * who were very cooperative and answered my questions.
24 */
25
26#include <linux/init.h>
27#include <linux/signal.h>
28#include <linux/slab.h>
29#include <linux/module.h>
30#include <linux/netdevice.h>
31#include <linux/usb.h>
32
33#include <linux/can.h>
34#include <linux/can/dev.h>
35#include <linux/can/error.h>
36#include <linux/can/led.h>
37
38/* driver constants */
39#define MAX_RX_URBS 20
40#define MAX_TX_URBS 20
41#define RX_BUFFER_SIZE 64
42
43/* vendor and product id */
44#define USB_8DEV_VENDOR_ID 0x0483
45#define USB_8DEV_PRODUCT_ID 0x1234
46
47/* endpoints */
48enum usb_8dev_endpoint {
49 USB_8DEV_ENDP_DATA_RX = 1,
50 USB_8DEV_ENDP_DATA_TX,
51 USB_8DEV_ENDP_CMD_RX,
52 USB_8DEV_ENDP_CMD_TX
53};
54
55/* device CAN clock */
56#define USB_8DEV_ABP_CLOCK 32000000
57
58/* setup flags */
59#define USB_8DEV_SILENT 0x01
60#define USB_8DEV_LOOPBACK 0x02
61#define USB_8DEV_DISABLE_AUTO_RESTRANS 0x04
62#define USB_8DEV_STATUS_FRAME 0x08
63
64/* commands */
65enum usb_8dev_cmd {
66 USB_8DEV_RESET = 1,
67 USB_8DEV_OPEN,
68 USB_8DEV_CLOSE,
69 USB_8DEV_SET_SPEED,
70 USB_8DEV_SET_MASK_FILTER,
71 USB_8DEV_GET_STATUS,
72 USB_8DEV_GET_STATISTICS,
73 USB_8DEV_GET_SERIAL,
74 USB_8DEV_GET_SOFTW_VER,
75 USB_8DEV_GET_HARDW_VER,
76 USB_8DEV_RESET_TIMESTAMP,
77 USB_8DEV_GET_SOFTW_HARDW_VER
78};
79
80/* command options */
81#define USB_8DEV_BAUD_MANUAL 0x09
82#define USB_8DEV_CMD_START 0x11
83#define USB_8DEV_CMD_END 0x22
84
85#define USB_8DEV_CMD_SUCCESS 0
86#define USB_8DEV_CMD_ERROR 255
87
88#define USB_8DEV_CMD_TIMEOUT 1000
89
90/* frames */
91#define USB_8DEV_DATA_START 0x55
92#define USB_8DEV_DATA_END 0xAA
93
94#define USB_8DEV_TYPE_CAN_FRAME 0
95#define USB_8DEV_TYPE_ERROR_FRAME 3
96
97#define USB_8DEV_EXTID 0x01
98#define USB_8DEV_RTR 0x02
99#define USB_8DEV_ERR_FLAG 0x04
100
101/* status */
102#define USB_8DEV_STATUSMSG_OK 0x00 /* Normal condition. */
103#define USB_8DEV_STATUSMSG_OVERRUN 0x01 /* Overrun occured when sending */
104#define USB_8DEV_STATUSMSG_BUSLIGHT 0x02 /* Error counter has reached 96 */
105#define USB_8DEV_STATUSMSG_BUSHEAVY 0x03 /* Error count. has reached 128 */
106#define USB_8DEV_STATUSMSG_BUSOFF 0x04 /* Device is in BUSOFF */
107#define USB_8DEV_STATUSMSG_STUFF 0x20 /* Stuff Error */
108#define USB_8DEV_STATUSMSG_FORM 0x21 /* Form Error */
109#define USB_8DEV_STATUSMSG_ACK 0x23 /* Ack Error */
110#define USB_8DEV_STATUSMSG_BIT0 0x24 /* Bit1 Error */
111#define USB_8DEV_STATUSMSG_BIT1 0x25 /* Bit0 Error */
112#define USB_8DEV_STATUSMSG_CRC 0x27 /* CRC Error */
113
114#define USB_8DEV_RP_MASK 0x7F /* Mask for Receive Error Bit */
115
116
117/* table of devices that work with this driver */
118static const struct usb_device_id usb_8dev_table[] = {
119 { USB_DEVICE(USB_8DEV_VENDOR_ID, USB_8DEV_PRODUCT_ID) },
120 { } /* Terminating entry */
121};
122
123MODULE_DEVICE_TABLE(usb, usb_8dev_table);
124
125struct usb_8dev_tx_urb_context {
126 struct usb_8dev_priv *priv;
127
128 u32 echo_index;
129 u8 dlc;
130};
131
132/* Structure to hold all of our device specific stuff */
133struct usb_8dev_priv {
134 struct can_priv can; /* must be the first member */
135
136 struct sk_buff *echo_skb[MAX_TX_URBS];
137
138 struct usb_device *udev;
139 struct net_device *netdev;
140
141 atomic_t active_tx_urbs;
142 struct usb_anchor tx_submitted;
143 struct usb_8dev_tx_urb_context tx_contexts[MAX_TX_URBS];
144
145 struct usb_anchor rx_submitted;
146
147 struct can_berr_counter bec;
148
149 u8 *cmd_msg_buffer;
150
151 struct mutex usb_8dev_cmd_lock;
152
153};
154
155/* tx frame */
156struct __packed usb_8dev_tx_msg {
157 u8 begin;
158 u8 flags; /* RTR and EXT_ID flag */
159 __be32 id; /* upper 3 bits not used */
160 u8 dlc; /* data length code 0-8 bytes */
161 u8 data[8]; /* 64-bit data */
162 u8 end;
163};
164
165/* rx frame */
166struct __packed usb_8dev_rx_msg {
167 u8 begin;
168 u8 type; /* frame type */
169 u8 flags; /* RTR and EXT_ID flag */
170 __be32 id; /* upper 3 bits not used */
171 u8 dlc; /* data length code 0-8 bytes */
172 u8 data[8]; /* 64-bit data */
173 __be32 timestamp; /* 32-bit timestamp */
174 u8 end;
175};
176
177/* command frame */
178struct __packed usb_8dev_cmd_msg {
179 u8 begin;
180 u8 channel; /* unkown - always 0 */
181 u8 command; /* command to execute */
182 u8 opt1; /* optional parameter / return value */
183 u8 opt2; /* optional parameter 2 */
184 u8 data[10]; /* optional parameter and data */
185 u8 end;
186};
187
188static int usb_8dev_send_cmd_msg(struct usb_8dev_priv *priv, u8 *msg, int size)
189{
190 int actual_length;
191
192 return usb_bulk_msg(priv->udev,
193 usb_sndbulkpipe(priv->udev, USB_8DEV_ENDP_CMD_TX),
194 msg, size, &actual_length, USB_8DEV_CMD_TIMEOUT);
195}
196
197static int usb_8dev_wait_cmd_msg(struct usb_8dev_priv *priv, u8 *msg, int size,
198 int *actual_length)
199{
200 return usb_bulk_msg(priv->udev,
201 usb_rcvbulkpipe(priv->udev, USB_8DEV_ENDP_CMD_RX),
202 msg, size, actual_length, USB_8DEV_CMD_TIMEOUT);
203}
204
205/* Send command to device and receive result.
206 * Command was successful when opt1 = 0.
207 */
208static int usb_8dev_send_cmd(struct usb_8dev_priv *priv,
209 struct usb_8dev_cmd_msg *out,
210 struct usb_8dev_cmd_msg *in)
211{
212 int err;
213 int num_bytes_read;
214 struct net_device *netdev;
215
216 netdev = priv->netdev;
217
218 out->begin = USB_8DEV_CMD_START;
219 out->end = USB_8DEV_CMD_END;
220
221 mutex_lock(&priv->usb_8dev_cmd_lock);
222
223 memcpy(priv->cmd_msg_buffer, out,
224 sizeof(struct usb_8dev_cmd_msg));
225
226 err = usb_8dev_send_cmd_msg(priv, priv->cmd_msg_buffer,
227 sizeof(struct usb_8dev_cmd_msg));
228 if (err < 0) {
229 netdev_err(netdev, "sending command message failed\n");
230 goto failed;
231 }
232
233 err = usb_8dev_wait_cmd_msg(priv, priv->cmd_msg_buffer,
234 sizeof(struct usb_8dev_cmd_msg),
235 &num_bytes_read);
236 if (err < 0) {
237 netdev_err(netdev, "no command message answer\n");
238 goto failed;
239 }
240
241 memcpy(in, priv->cmd_msg_buffer, sizeof(struct usb_8dev_cmd_msg));
242
243 if (in->begin != USB_8DEV_CMD_START || in->end != USB_8DEV_CMD_END ||
244 num_bytes_read != 16 || in->opt1 != 0)
245 err = -EPROTO;
246
247failed:
248 mutex_unlock(&priv->usb_8dev_cmd_lock);
249 return err;
250}
251
252/* Send open command to device */
253static int usb_8dev_cmd_open(struct usb_8dev_priv *priv)
254{
255 struct can_bittiming *bt = &priv->can.bittiming;
256 struct usb_8dev_cmd_msg outmsg;
257 struct usb_8dev_cmd_msg inmsg;
258 u32 ctrlmode = priv->can.ctrlmode;
259 u32 flags = USB_8DEV_STATUS_FRAME;
260 __be32 beflags;
261 __be16 bebrp;
262
263 memset(&outmsg, 0, sizeof(outmsg));
264 outmsg.command = USB_8DEV_OPEN;
265 outmsg.opt1 = USB_8DEV_BAUD_MANUAL;
266 outmsg.data[0] = bt->prop_seg + bt->phase_seg1;
267 outmsg.data[1] = bt->phase_seg2;
268 outmsg.data[2] = bt->sjw;
269
270 /* BRP */
271 bebrp = cpu_to_be16((u16)bt->brp);
272 memcpy(&outmsg.data[3], &bebrp, sizeof(bebrp));
273
274 /* flags */
275 if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
276 flags |= USB_8DEV_LOOPBACK;
277 if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
278 flags |= USB_8DEV_SILENT;
279 if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
280 flags |= USB_8DEV_DISABLE_AUTO_RESTRANS;
281
282 beflags = cpu_to_be32(flags);
283 memcpy(&outmsg.data[5], &beflags, sizeof(beflags));
284
285 return usb_8dev_send_cmd(priv, &outmsg, &inmsg);
286}
287
288/* Send close command to device */
289static int usb_8dev_cmd_close(struct usb_8dev_priv *priv)
290{
291 struct usb_8dev_cmd_msg inmsg;
292 struct usb_8dev_cmd_msg outmsg = {
293 .channel = 0,
294 .command = USB_8DEV_CLOSE,
295 .opt1 = 0,
296 .opt2 = 0
297 };
298
299 return usb_8dev_send_cmd(priv, &outmsg, &inmsg);
300}
301
302/* Get firmware and hardware version */
303static int usb_8dev_cmd_version(struct usb_8dev_priv *priv, u32 *res)
304{
305 struct usb_8dev_cmd_msg inmsg;
306 struct usb_8dev_cmd_msg outmsg = {
307 .channel = 0,
308 .command = USB_8DEV_GET_SOFTW_HARDW_VER,
309 .opt1 = 0,
310 .opt2 = 0
311 };
312
313 int err = usb_8dev_send_cmd(priv, &outmsg, &inmsg);
314 if (err)
315 return err;
316
317 *res = be32_to_cpup((__be32 *)inmsg.data);
318
319 return err;
320}
321
322/* Set network device mode
323 *
324 * Maybe we should leave this function empty, because the device
325 * set mode variable with open command.
326 */
327static int usb_8dev_set_mode(struct net_device *netdev, enum can_mode mode)
328{
329 struct usb_8dev_priv *priv = netdev_priv(netdev);
330 int err = 0;
331
332 switch (mode) {
333 case CAN_MODE_START:
334 err = usb_8dev_cmd_open(priv);
335 if (err)
336 netdev_warn(netdev, "couldn't start device");
337 break;
338
339 default:
340 return -EOPNOTSUPP;
341 }
342
343 return err;
344}
345
346/* Read error/status frames */
347static void usb_8dev_rx_err_msg(struct usb_8dev_priv *priv,
348 struct usb_8dev_rx_msg *msg)
349{
350 struct can_frame *cf;
351 struct sk_buff *skb;
352 struct net_device_stats *stats = &priv->netdev->stats;
353
354 /* Error message:
355 * byte 0: Status
356 * byte 1: bit 7: Receive Passive
357 * byte 1: bit 0-6: Receive Error Counter
358 * byte 2: Transmit Error Counter
359 * byte 3: Always 0 (maybe reserved for future use)
360 */
361
362 u8 state = msg->data[0];
363 u8 rxerr = msg->data[1] & USB_8DEV_RP_MASK;
364 u8 txerr = msg->data[2];
365 int rx_errors = 0;
366 int tx_errors = 0;
367
368 skb = alloc_can_err_skb(priv->netdev, &cf);
369 if (!skb)
370 return;
371
372 switch (state) {
373 case USB_8DEV_STATUSMSG_OK:
374 priv->can.state = CAN_STATE_ERROR_ACTIVE;
375 cf->can_id |= CAN_ERR_PROT;
376 cf->data[2] = CAN_ERR_PROT_ACTIVE;
377 break;
378 case USB_8DEV_STATUSMSG_BUSOFF:
379 priv->can.state = CAN_STATE_BUS_OFF;
380 cf->can_id |= CAN_ERR_BUSOFF;
381 can_bus_off(priv->netdev);
382 break;
383 case USB_8DEV_STATUSMSG_OVERRUN:
384 case USB_8DEV_STATUSMSG_BUSLIGHT:
385 case USB_8DEV_STATUSMSG_BUSHEAVY:
386 cf->can_id |= CAN_ERR_CRTL;
387 break;
388 default:
389 priv->can.state = CAN_STATE_ERROR_WARNING;
390 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
391 priv->can.can_stats.bus_error++;
392 break;
393 }
394
395 switch (state) {
396 case USB_8DEV_STATUSMSG_OK:
397 case USB_8DEV_STATUSMSG_BUSOFF:
398 break;
399 case USB_8DEV_STATUSMSG_ACK:
400 cf->can_id |= CAN_ERR_ACK;
401 tx_errors = 1;
402 break;
403 case USB_8DEV_STATUSMSG_CRC:
404 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
405 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ |
406 CAN_ERR_PROT_LOC_CRC_DEL;
407 rx_errors = 1;
408 break;
409 case USB_8DEV_STATUSMSG_BIT0:
410 cf->data[2] |= CAN_ERR_PROT_BIT0;
411 tx_errors = 1;
412 break;
413 case USB_8DEV_STATUSMSG_BIT1:
414 cf->data[2] |= CAN_ERR_PROT_BIT1;
415 tx_errors = 1;
416 break;
417 case USB_8DEV_STATUSMSG_FORM:
418 cf->data[2] |= CAN_ERR_PROT_FORM;
419 rx_errors = 1;
420 break;
421 case USB_8DEV_STATUSMSG_STUFF:
422 cf->data[2] |= CAN_ERR_PROT_STUFF;
423 rx_errors = 1;
424 break;
425 case USB_8DEV_STATUSMSG_OVERRUN:
426 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
427 stats->rx_over_errors++;
428 rx_errors = 1;
429 break;
430 case USB_8DEV_STATUSMSG_BUSLIGHT:
431 priv->can.state = CAN_STATE_ERROR_WARNING;
432 cf->data[1] = (txerr > rxerr) ?
433 CAN_ERR_CRTL_TX_WARNING :
434 CAN_ERR_CRTL_RX_WARNING;
435 priv->can.can_stats.error_warning++;
436 break;
437 case USB_8DEV_STATUSMSG_BUSHEAVY:
438 priv->can.state = CAN_STATE_ERROR_PASSIVE;
439 cf->data[1] = (txerr > rxerr) ?
440 CAN_ERR_CRTL_TX_PASSIVE :
441 CAN_ERR_CRTL_RX_PASSIVE;
442 priv->can.can_stats.error_passive++;
443 break;
444 default:
445 netdev_warn(priv->netdev,
446 "Unknown status/error message (%d)\n", state);
447 break;
448 }
449
450 if (tx_errors) {
451 cf->data[2] |= CAN_ERR_PROT_TX;
452 stats->tx_errors++;
453 }
454
455 if (rx_errors)
456 stats->rx_errors++;
457
458 cf->data[6] = txerr;
459 cf->data[7] = rxerr;
460
461 priv->bec.txerr = txerr;
462 priv->bec.rxerr = rxerr;
463
464 netif_rx(skb);
465
466 stats->rx_packets++;
467 stats->rx_bytes += cf->can_dlc;
468}
469
470/* Read data and status frames */
471static void usb_8dev_rx_can_msg(struct usb_8dev_priv *priv,
472 struct usb_8dev_rx_msg *msg)
473{
474 struct can_frame *cf;
475 struct sk_buff *skb;
476 struct net_device_stats *stats = &priv->netdev->stats;
477
478 if (msg->type == USB_8DEV_TYPE_ERROR_FRAME &&
479 msg->flags == USB_8DEV_ERR_FLAG) {
480 usb_8dev_rx_err_msg(priv, msg);
481 } else if (msg->type == USB_8DEV_TYPE_CAN_FRAME) {
482 skb = alloc_can_skb(priv->netdev, &cf);
483 if (!skb)
484 return;
485
486 cf->can_id = be32_to_cpu(msg->id);
487 cf->can_dlc = get_can_dlc(msg->dlc & 0xF);
488
489 if (msg->flags & USB_8DEV_EXTID)
490 cf->can_id |= CAN_EFF_FLAG;
491
492 if (msg->flags & USB_8DEV_RTR)
493 cf->can_id |= CAN_RTR_FLAG;
494 else
495 memcpy(cf->data, msg->data, cf->can_dlc);
496
497 netif_rx(skb);
498
499 stats->rx_packets++;
500 stats->rx_bytes += cf->can_dlc;
501
502 can_led_event(priv->netdev, CAN_LED_EVENT_RX);
503 } else {
504 netdev_warn(priv->netdev, "frame type %d unknown",
505 msg->type);
506 }
507
508}
509
510/* Callback for reading data from device
511 *
512 * Check urb status, call read function and resubmit urb read operation.
513 */
514static void usb_8dev_read_bulk_callback(struct urb *urb)
515{
516 struct usb_8dev_priv *priv = urb->context;
517 struct net_device *netdev;
518 int retval;
519 int pos = 0;
520
521 netdev = priv->netdev;
522
523 if (!netif_device_present(netdev))
524 return;
525
526 switch (urb->status) {
527 case 0: /* success */
528 break;
529
530 case -ENOENT:
531 case -ESHUTDOWN:
532 return;
533
534 default:
535 netdev_info(netdev, "Rx URB aborted (%d)\n",
536 urb->status);
537 goto resubmit_urb;
538 }
539
540 while (pos < urb->actual_length) {
541 struct usb_8dev_rx_msg *msg;
542
543 if (pos + sizeof(struct usb_8dev_rx_msg) > urb->actual_length) {
544 netdev_err(priv->netdev, "format error\n");
545 break;
546 }
547
548 msg = (struct usb_8dev_rx_msg *)(urb->transfer_buffer + pos);
549 usb_8dev_rx_can_msg(priv, msg);
550
551 pos += sizeof(struct usb_8dev_rx_msg);
552 }
553
554resubmit_urb:
555 usb_fill_bulk_urb(urb, priv->udev,
556 usb_rcvbulkpipe(priv->udev, USB_8DEV_ENDP_DATA_RX),
557 urb->transfer_buffer, RX_BUFFER_SIZE,
558 usb_8dev_read_bulk_callback, priv);
559
560 retval = usb_submit_urb(urb, GFP_ATOMIC);
561
562 if (retval == -ENODEV)
563 netif_device_detach(netdev);
564 else if (retval)
565 netdev_err(netdev,
566 "failed resubmitting read bulk urb: %d\n", retval);
567}
568
569/* Callback handler for write operations
570 *
571 * Free allocated buffers, check transmit status and
572 * calculate statistic.
573 */
574static void usb_8dev_write_bulk_callback(struct urb *urb)
575{
576 struct usb_8dev_tx_urb_context *context = urb->context;
577 struct usb_8dev_priv *priv;
578 struct net_device *netdev;
579
580 BUG_ON(!context);
581
582 priv = context->priv;
583 netdev = priv->netdev;
584
585 /* free up our allocated buffer */
586 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
587 urb->transfer_buffer, urb->transfer_dma);
588
589 atomic_dec(&priv->active_tx_urbs);
590
591 if (!netif_device_present(netdev))
592 return;
593
594 if (urb->status)
595 netdev_info(netdev, "Tx URB aborted (%d)\n",
596 urb->status);
597
598 netdev->stats.tx_packets++;
599 netdev->stats.tx_bytes += context->dlc;
600
601 can_get_echo_skb(netdev, context->echo_index);
602
603 can_led_event(netdev, CAN_LED_EVENT_TX);
604
605 /* Release context */
606 context->echo_index = MAX_TX_URBS;
607
608 netif_wake_queue(netdev);
609}
610
611/* Send data to device */
612static netdev_tx_t usb_8dev_start_xmit(struct sk_buff *skb,
613 struct net_device *netdev)
614{
615 struct usb_8dev_priv *priv = netdev_priv(netdev);
616 struct net_device_stats *stats = &netdev->stats;
617 struct can_frame *cf = (struct can_frame *) skb->data;
618 struct usb_8dev_tx_msg *msg;
619 struct urb *urb;
620 struct usb_8dev_tx_urb_context *context = NULL;
621 u8 *buf;
622 int i, err;
623 size_t size = sizeof(struct usb_8dev_tx_msg);
624
625 if (can_dropped_invalid_skb(netdev, skb))
626 return NETDEV_TX_OK;
627
628 /* create a URB, and a buffer for it, and copy the data to the URB */
629 urb = usb_alloc_urb(0, GFP_ATOMIC);
630 if (!urb) {
631 netdev_err(netdev, "No memory left for URBs\n");
632 goto nomem;
633 }
634
635 buf = usb_alloc_coherent(priv->udev, size, GFP_ATOMIC,
636 &urb->transfer_dma);
637 if (!buf) {
638 netdev_err(netdev, "No memory left for USB buffer\n");
639 goto nomembuf;
640 }
641
642 memset(buf, 0, size);
643
644 msg = (struct usb_8dev_tx_msg *)buf;
645 msg->begin = USB_8DEV_DATA_START;
646 msg->flags = 0x00;
647
648 if (cf->can_id & CAN_RTR_FLAG)
649 msg->flags |= USB_8DEV_RTR;
650
651 if (cf->can_id & CAN_EFF_FLAG)
652 msg->flags |= USB_8DEV_EXTID;
653
654 msg->id = cpu_to_be32(cf->can_id & CAN_ERR_MASK);
655 msg->dlc = cf->can_dlc;
656 memcpy(msg->data, cf->data, cf->can_dlc);
657 msg->end = USB_8DEV_DATA_END;
658
659 for (i = 0; i < MAX_TX_URBS; i++) {
660 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
661 context = &priv->tx_contexts[i];
662 break;
663 }
664 }
665
666 /* May never happen! When this happens we'd more URBs in flight as
667 * allowed (MAX_TX_URBS).
668 */
669 if (!context)
670 goto nofreecontext;
671
672 context->priv = priv;
673 context->echo_index = i;
674 context->dlc = cf->can_dlc;
675
676 usb_fill_bulk_urb(urb, priv->udev,
677 usb_sndbulkpipe(priv->udev, USB_8DEV_ENDP_DATA_TX),
678 buf, size, usb_8dev_write_bulk_callback, context);
679 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
680 usb_anchor_urb(urb, &priv->tx_submitted);
681
682 can_put_echo_skb(skb, netdev, context->echo_index);
683
684 atomic_inc(&priv->active_tx_urbs);
685
686 err = usb_submit_urb(urb, GFP_ATOMIC);
687 if (unlikely(err))
688 goto failed;
689 else if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
690 /* Slow down tx path */
691 netif_stop_queue(netdev);
692
693 /* Release our reference to this URB, the USB core will eventually free
694 * it entirely.
695 */
696 usb_free_urb(urb);
697
698 return NETDEV_TX_OK;
699
700nofreecontext:
701 usb_unanchor_urb(urb);
702 usb_free_coherent(priv->udev, size, buf, urb->transfer_dma);
703
704 netdev_warn(netdev, "couldn't find free context");
705
706 return NETDEV_TX_BUSY;
707
708failed:
709 can_free_echo_skb(netdev, context->echo_index);
710
711 usb_unanchor_urb(urb);
712 usb_free_coherent(priv->udev, size, buf, urb->transfer_dma);
713
714 atomic_dec(&priv->active_tx_urbs);
715
716 if (err == -ENODEV)
717 netif_device_detach(netdev);
718 else
719 netdev_warn(netdev, "failed tx_urb %d\n", err);
720
721nomembuf:
722 usb_free_urb(urb);
723
724nomem:
725 dev_kfree_skb(skb);
726 stats->tx_dropped++;
727
728 return NETDEV_TX_OK;
729}
730
731static int usb_8dev_get_berr_counter(const struct net_device *netdev,
732 struct can_berr_counter *bec)
733{
734 struct usb_8dev_priv *priv = netdev_priv(netdev);
735
736 bec->txerr = priv->bec.txerr;
737 bec->rxerr = priv->bec.rxerr;
738
739 return 0;
740}
741
742/* Start USB device */
743static int usb_8dev_start(struct usb_8dev_priv *priv)
744{
745 struct net_device *netdev = priv->netdev;
746 int err, i;
747
748 for (i = 0; i < MAX_RX_URBS; i++) {
749 struct urb *urb = NULL;
750 u8 *buf;
751
752 /* create a URB, and a buffer for it */
753 urb = usb_alloc_urb(0, GFP_KERNEL);
754 if (!urb) {
755 netdev_err(netdev, "No memory left for URBs\n");
756 err = -ENOMEM;
757 break;
758 }
759
760 buf = usb_alloc_coherent(priv->udev, RX_BUFFER_SIZE, GFP_KERNEL,
761 &urb->transfer_dma);
762 if (!buf) {
763 netdev_err(netdev, "No memory left for USB buffer\n");
764 usb_free_urb(urb);
765 err = -ENOMEM;
766 break;
767 }
768
769 usb_fill_bulk_urb(urb, priv->udev,
770 usb_rcvbulkpipe(priv->udev,
771 USB_8DEV_ENDP_DATA_RX),
772 buf, RX_BUFFER_SIZE,
773 usb_8dev_read_bulk_callback, priv);
774 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
775 usb_anchor_urb(urb, &priv->rx_submitted);
776
777 err = usb_submit_urb(urb, GFP_KERNEL);
778 if (err) {
779 usb_unanchor_urb(urb);
780 usb_free_coherent(priv->udev, RX_BUFFER_SIZE, buf,
781 urb->transfer_dma);
782 break;
783 }
784
785 /* Drop reference, USB core will take care of freeing it */
786 usb_free_urb(urb);
787 }
788
789 /* Did we submit any URBs */
790 if (i == 0) {
791 netdev_warn(netdev, "couldn't setup read URBs\n");
792 return err;
793 }
794
795 /* Warn if we've couldn't transmit all the URBs */
796 if (i < MAX_RX_URBS)
797 netdev_warn(netdev, "rx performance may be slow\n");
798
799 err = usb_8dev_cmd_open(priv);
800 if (err)
801 goto failed;
802
803 priv->can.state = CAN_STATE_ERROR_ACTIVE;
804
805 return 0;
806
807failed:
808 if (err == -ENODEV)
809 netif_device_detach(priv->netdev);
810
811 netdev_warn(netdev, "couldn't submit control: %d\n", err);
812
813 return err;
814}
815
816/* Open USB device */
817static int usb_8dev_open(struct net_device *netdev)
818{
819 struct usb_8dev_priv *priv = netdev_priv(netdev);
820 int err;
821
822 /* common open */
823 err = open_candev(netdev);
824 if (err)
825 return err;
826
827 can_led_event(netdev, CAN_LED_EVENT_OPEN);
828
829 /* finally start device */
830 err = usb_8dev_start(priv);
831 if (err) {
832 if (err == -ENODEV)
833 netif_device_detach(priv->netdev);
834
835 netdev_warn(netdev, "couldn't start device: %d\n",
836 err);
837
838 close_candev(netdev);
839
840 return err;
841 }
842
843 netif_start_queue(netdev);
844
845 return 0;
846}
847
848static void unlink_all_urbs(struct usb_8dev_priv *priv)
849{
850 int i;
851
852 usb_kill_anchored_urbs(&priv->rx_submitted);
853
854 usb_kill_anchored_urbs(&priv->tx_submitted);
855 atomic_set(&priv->active_tx_urbs, 0);
856
857 for (i = 0; i < MAX_TX_URBS; i++)
858 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
859}
860
861/* Close USB device */
862static int usb_8dev_close(struct net_device *netdev)
863{
864 struct usb_8dev_priv *priv = netdev_priv(netdev);
865 int err = 0;
866
867 /* Send CLOSE command to CAN controller */
868 err = usb_8dev_cmd_close(priv);
869 if (err)
870 netdev_warn(netdev, "couldn't stop device");
871
872 priv->can.state = CAN_STATE_STOPPED;
873
874 netif_stop_queue(netdev);
875
876 /* Stop polling */
877 unlink_all_urbs(priv);
878
879 close_candev(netdev);
880
881 can_led_event(netdev, CAN_LED_EVENT_STOP);
882
883 return err;
884}
885
886static const struct net_device_ops usb_8dev_netdev_ops = {
887 .ndo_open = usb_8dev_open,
888 .ndo_stop = usb_8dev_close,
889 .ndo_start_xmit = usb_8dev_start_xmit,
890};
891
892static const struct can_bittiming_const usb_8dev_bittiming_const = {
893 .name = "usb_8dev",
894 .tseg1_min = 1,
895 .tseg1_max = 16,
896 .tseg2_min = 1,
897 .tseg2_max = 8,
898 .sjw_max = 4,
899 .brp_min = 1,
900 .brp_max = 1024,
901 .brp_inc = 1,
902};
903
904/* Probe USB device
905 *
906 * Check device and firmware.
907 * Set supported modes and bittiming constants.
908 * Allocate some memory.
909 */
910static int usb_8dev_probe(struct usb_interface *intf,
911 const struct usb_device_id *id)
912{
913 struct net_device *netdev;
914 struct usb_8dev_priv *priv;
915 int i, err = -ENOMEM;
916 u32 version;
917 char buf[18];
918 struct usb_device *usbdev = interface_to_usbdev(intf);
919
920 /* product id looks strange, better we also check iProduct string */
921 if (usb_string(usbdev, usbdev->descriptor.iProduct, buf,
922 sizeof(buf)) > 0 && strcmp(buf, "USB2CAN converter")) {
923 dev_info(&usbdev->dev, "ignoring: not an USB2CAN converter\n");
924 return -ENODEV;
925 }
926
927 netdev = alloc_candev(sizeof(struct usb_8dev_priv), MAX_TX_URBS);
928 if (!netdev) {
929 dev_err(&intf->dev, "Couldn't alloc candev\n");
930 return -ENOMEM;
931 }
932
933 priv = netdev_priv(netdev);
934
935 priv->udev = usbdev;
936 priv->netdev = netdev;
937
938 priv->can.state = CAN_STATE_STOPPED;
939 priv->can.clock.freq = USB_8DEV_ABP_CLOCK;
940 priv->can.bittiming_const = &usb_8dev_bittiming_const;
941 priv->can.do_set_mode = usb_8dev_set_mode;
942 priv->can.do_get_berr_counter = usb_8dev_get_berr_counter;
943 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
944 CAN_CTRLMODE_LISTENONLY |
945 CAN_CTRLMODE_ONE_SHOT;
946
947 netdev->netdev_ops = &usb_8dev_netdev_ops;
948
949 netdev->flags |= IFF_ECHO; /* we support local echo */
950
951 init_usb_anchor(&priv->rx_submitted);
952
953 init_usb_anchor(&priv->tx_submitted);
954 atomic_set(&priv->active_tx_urbs, 0);
955
956 for (i = 0; i < MAX_TX_URBS; i++)
957 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
958
959 priv->cmd_msg_buffer = kzalloc(sizeof(struct usb_8dev_cmd_msg),
960 GFP_KERNEL);
961 if (!priv->cmd_msg_buffer)
962 goto cleanup_candev;
963
964 usb_set_intfdata(intf, priv);
965
966 SET_NETDEV_DEV(netdev, &intf->dev);
967
968 mutex_init(&priv->usb_8dev_cmd_lock);
969
970 err = register_candev(netdev);
971 if (err) {
972 netdev_err(netdev,
973 "couldn't register CAN device: %d\n", err);
974 goto cleanup_cmd_msg_buffer;
975 }
976
977 err = usb_8dev_cmd_version(priv, &version);
978 if (err) {
979 netdev_err(netdev, "can't get firmware version\n");
980 goto cleanup_cmd_msg_buffer;
981 } else {
982 netdev_info(netdev,
983 "firmware: %d.%d, hardware: %d.%d\n",
984 (version>>24) & 0xff, (version>>16) & 0xff,
985 (version>>8) & 0xff, version & 0xff);
986 }
987
988 devm_can_led_init(netdev);
989
990 return 0;
991
992cleanup_cmd_msg_buffer:
993 kfree(priv->cmd_msg_buffer);
994
995cleanup_candev:
996 free_candev(netdev);
997
998 return err;
999
1000}
1001
1002/* Called by the usb core when driver is unloaded or device is removed */
1003static void usb_8dev_disconnect(struct usb_interface *intf)
1004{
1005 struct usb_8dev_priv *priv = usb_get_intfdata(intf);
1006
1007 usb_set_intfdata(intf, NULL);
1008
1009 if (priv) {
1010 netdev_info(priv->netdev, "device disconnected\n");
1011
1012 unregister_netdev(priv->netdev);
1013 free_candev(priv->netdev);
1014
1015 unlink_all_urbs(priv);
1016 }
1017
1018}
1019
1020static struct usb_driver usb_8dev_driver = {
1021 .name = "usb_8dev",
1022 .probe = usb_8dev_probe,
1023 .disconnect = usb_8dev_disconnect,
1024 .id_table = usb_8dev_table,
1025};
1026
1027module_usb_driver(usb_8dev_driver);
1028
1029MODULE_AUTHOR("Bernd Krumboeck <krumboeck@universalnet.at>");
1030MODULE_DESCRIPTION("CAN driver for 8 devices USB2CAN interfaces");
1031MODULE_LICENSE("GPL v2");