1 [PATCH 3/9] Socket-CAN: add network device driver interface
3 The CAN network device driver interface provides a generic interface to
4 setup, configure and monitor CAN network devices. It exports a set of
5 common data structures and functions, which all real CAN network device
6 drivers should use. Please have a look to the SJA1000 or MSCAN driver
7 to understand how to use them. The name of the module is can-dev.ko.
9 For further information please check "Documentation/networking/can.txt"
10 provided by the first patch of this series.
12 Signed-off-by: Wolfgang Grandegger <wg@grandegger.com>
14 drivers/net/can/Kconfig | 23 ++
15 drivers/net/can/Makefile | 5
16 drivers/net/can/dev.c | 523 +++++++++++++++++++++++++++++++++++++++++++++++
17 include/linux/can/dev.h | 136 ++++++++++++
18 4 files changed, 687 insertions(+)
20 Index: net-next-2.6/drivers/net/can/Makefile
21 ===================================================================
22 --- net-next-2.6.orig/drivers/net/can/Makefile
23 +++ net-next-2.6/drivers/net/can/Makefile
27 obj-$(CONFIG_CAN_VCAN) += vcan.o
29 +obj-$(CONFIG_CAN_DEV) += can-dev.o
32 +ccflags-$(CONFIG_CAN_DEBUG_DEVICES) := -DDEBUG
33 Index: net-next-2.6/drivers/net/can/dev.c
34 ===================================================================
36 +++ net-next-2.6/drivers/net/can/dev.c
39 + * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
40 + * Copyright (C) 2006 Andrey Volkov, Varma Electronics
41 + * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
43 + * This program is free software; you can redistribute it and/or modify
44 + * it under the terms of the version 2 of the GNU General Public License
45 + * as published by the Free Software Foundation
47 + * This program is distributed in the hope that it will be useful,
48 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 + * GNU General Public License for more details.
52 + * You should have received a copy of the GNU General Public License
53 + * along with this program; if not, write to the Free Software
54 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
57 +#include <linux/module.h>
58 +#include <linux/netdevice.h>
59 +#include <linux/if_arp.h>
60 +#include <linux/can.h>
61 +#include <linux/can/dev.h>
62 +#include <net/rtnetlink.h>
64 +#define MOD_DESC "CAN device driver interface"
66 +MODULE_DESCRIPTION(MOD_DESC);
67 +MODULE_LICENSE("GPL v2");
68 +MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
70 +#ifdef CONFIG_CAN_CALC_BITTIMING
71 +#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
74 + * Bit-timing calculation derived from:
76 + * Code based on LinCAN sources and H8S2638 project
77 + * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
78 + * Copyright 2005 Stanislav Marek
79 + * email: pisa@cmp.felk.cvut.cz
81 +static int can_update_spt(const struct can_bittiming_const *btc,
82 + int sampl_pt, int tseg, int *tseg1, int *tseg2)
84 + *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
85 + if (*tseg2 < btc->tseg2_min)
86 + *tseg2 = btc->tseg2_min;
87 + if (*tseg2 > btc->tseg2_max)
88 + *tseg2 = btc->tseg2_max;
89 + *tseg1 = tseg - *tseg2;
90 + if (*tseg1 > btc->tseg1_max) {
91 + *tseg1 = btc->tseg1_max;
92 + *tseg2 = tseg - *tseg1;
94 + return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
97 +static int can_calc_bittiming(struct net_device *dev)
99 + struct can_priv *priv = netdev_priv(dev);
100 + struct can_bittiming *bt = &priv->bittiming;
101 + const struct can_bittiming_const *btc = priv->bittiming_const;
102 + long rate, best_rate = 0;
103 + long best_error = 1000000000, error = 0;
104 + int best_tseg = 0, best_brp = 0, brp = 0;
105 + int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
106 + int spt_error = 1000, spt = 0, sampl_pt;
109 + if (!priv->bittiming_const)
112 + /* Use CIA recommended sample points */
113 + if (bt->sample_point) {
114 + sampl_pt = bt->sample_point;
116 + if (bt->bitrate > 800000)
118 + else if (bt->bitrate > 500000)
124 + /* tseg even = round down, odd = round up */
125 + for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
126 + tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
127 + tsegall = 1 + tseg / 2;
128 + /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
129 + brp = bt->clock / (tsegall * bt->bitrate) + tseg % 2;
130 + /* chose brp step which is possible in system */
131 + brp = (brp / btc->brp_inc) * btc->brp_inc;
132 + if ((brp < btc->brp_min) || (brp > btc->brp_max))
134 + rate = bt->clock / (brp * tsegall);
135 + error = bt->bitrate - rate;
136 + /* tseg brp biterror */
139 + if (error > best_error)
141 + best_error = error;
143 + spt = can_update_spt(btc, sampl_pt, tseg / 2,
145 + error = sampl_pt - spt;
148 + if (error > spt_error)
152 + best_tseg = tseg / 2;
160 + /* Error in one-tenth of a percent */
161 + error = (best_error * 1000) / bt->bitrate;
162 + if (error > CAN_CALC_MAX_ERROR) {
163 + dev_err(ND2D(dev), "bitrate error %ld.%ld%% too high\n",
164 + error / 10, error % 10);
167 + dev_warn(ND2D(dev), "bitrate error %ld.%ld%%\n",
168 + error / 10, error % 10);
172 + spt = can_update_spt(btc, sampl_pt, best_tseg, &tseg1, &tseg2);
174 + v64 = (u64)best_brp * 1000000000UL;
175 + do_div(v64, bt->clock);
177 + bt->prop_seg = tseg1 / 2;
178 + bt->phase_seg1 = tseg1 - bt->prop_seg;
179 + bt->phase_seg2 = tseg2;
181 + bt->brp = best_brp;
185 +#else /* !CONFIG_CAN_CALC_BITTIMING */
186 +static int can_calc_bittiming(struct net_device *dev)
188 + dev_err(ND2D(dev), "bit-timing calculation not available\n");
191 +#endif /* CONFIG_CAN_CALC_BITTIMING */
193 +int can_sample_point(struct can_bittiming *bt)
195 + return ((bt->prop_seg + bt->phase_seg1 + 1) * 1000) /
196 + (bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1);
199 +int can_fixup_bittiming(struct net_device *dev)
201 + struct can_priv *priv = netdev_priv(dev);
202 + struct can_bittiming *bt = &priv->bittiming;
203 + const struct can_bittiming_const *btc = priv->bittiming_const;
204 + int tseg1, alltseg;
208 + if (!priv->bittiming_const)
211 + tseg1 = bt->prop_seg + bt->phase_seg1;
212 + if (bt->sjw > btc->sjw_max ||
213 + tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
214 + bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
217 + brp64 = (u64)bt->clock * (u64)bt->tq;
218 + if (btc->brp_inc > 1)
219 + do_div(brp64, btc->brp_inc);
220 + brp64 += 500000000UL - 1;
221 + do_div(brp64, 1000000000UL); /* the practicable BRP */
222 + if (btc->brp_inc > 1)
223 + brp64 *= btc->brp_inc;
224 + bt->brp = (u32)brp64;
226 + if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
229 + alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
230 + bitrate = bt->clock / (bt->brp * alltseg);
231 + bt->bitrate = bitrate;
237 + * Set CAN bit-timing for the device
239 + * This functions should be called in the open function of the device
240 + * driver to determine, check and set appropriate bit-timing parameters.
242 +int can_set_bittiming(struct net_device *dev)
244 + struct can_priv *priv = netdev_priv(dev);
247 + /* Check if bit-timing parameters have been pre-defined */
248 + if (!priv->bittiming.tq && !priv->bittiming.bitrate) {
249 + dev_err(ND2D(dev), "bit-timing not yet defined\n");
253 + /* Check if the CAN device has bit-timing parameters */
254 + if (priv->bittiming_const) {
256 + /* Check if bit-timing parameters have already been set */
257 + if (priv->bittiming.tq && priv->bittiming.bitrate)
260 + /* Non-expert mode? Check if the bitrate has been pre-defined */
261 + if (!priv->bittiming.tq)
262 + /* Determine bit-timing parameters */
263 + err = can_calc_bittiming(dev);
265 + /* Check bit-timing params and calculate proper brp */
266 + err = can_fixup_bittiming(dev);
271 + if (priv->do_set_bittiming) {
272 + /* Finally, set the bit-timing registers */
273 + err = priv->do_set_bittiming(dev);
280 +EXPORT_SYMBOL(can_set_bittiming);
282 +static void can_setup(struct net_device *dev)
284 + dev->type = ARPHRD_CAN;
285 + dev->mtu = sizeof(struct can_frame);
286 + dev->hard_header_len = 0;
288 + dev->tx_queue_len = 10;
290 + /* New-style flags. */
291 + dev->flags = IFF_NOARP;
292 + dev->features = NETIF_F_NO_CSUM;
296 + * Allocate and setup space for the CAN network device
298 +struct net_device *alloc_candev(int sizeof_priv)
300 + struct net_device *dev;
301 + struct can_priv *priv;
303 + dev = alloc_netdev(sizeof_priv, "can%d", can_setup);
307 + priv = netdev_priv(dev);
309 + priv->state = CAN_STATE_STOPPED;
310 + spin_lock_init(&priv->irq_lock);
312 + init_timer(&priv->timer);
313 + priv->timer.expires = 0;
317 +EXPORT_SYMBOL(alloc_candev);
320 + * Allocate space of the CAN network device
322 +void free_candev(struct net_device *dev)
326 +EXPORT_SYMBOL(free_candev);
329 + * Register the CAN network device
331 +int register_candev(struct net_device *dev)
335 + err = register_netdev(dev);
341 +EXPORT_SYMBOL(register_candev);
344 + * Unregister the CAN network device
346 +void unregister_candev(struct net_device *dev)
348 + unregister_netdev(dev);
350 +EXPORT_SYMBOL(unregister_candev);
353 + * Local echo of CAN messages
355 + * CAN network devices *should* support a local echo functionality
356 + * (see Documentation/networking/can.txt). To test the handling of CAN
357 + * interfaces that do not support the local echo both driver types are
358 + * implemented. In the case that the driver does not support the echo
359 + * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
360 + * to perform the echo as a fallback solution.
363 +void can_flush_echo_skb(struct net_device *dev)
365 + struct can_priv *priv = netdev_priv(dev);
366 + struct net_device_stats *stats = &dev->stats;
369 + for (i = 0; i < CAN_ECHO_SKB_MAX; i++) {
370 + if (priv->echo_skb[i]) {
371 + kfree_skb(priv->echo_skb[i]);
372 + priv->echo_skb[i] = NULL;
373 + stats->tx_dropped++;
374 + stats->tx_aborted_errors++;
380 + * Put the skb on the stack to be looped backed locally lateron
382 + * The function is typically called in the start_xmit function
383 + * of the device driver.
385 +void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx)
387 + struct can_priv *priv = netdev_priv(dev);
389 + /* set flag whether this packet has to be looped back */
390 + if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
395 + if (!priv->echo_skb[idx]) {
396 + struct sock *srcsk = skb->sk;
398 + if (atomic_read(&skb->users) != 1) {
399 + struct sk_buff *old_skb = skb;
401 + skb = skb_clone(old_skb, GFP_ATOMIC);
402 + kfree_skb(old_skb);
410 + /* make settings for echo to reduce code in irq context */
411 + skb->protocol = htons(ETH_P_CAN);
412 + skb->pkt_type = PACKET_BROADCAST;
413 + skb->ip_summed = CHECKSUM_UNNECESSARY;
416 + /* save this skb for tx interrupt echo handling */
417 + priv->echo_skb[idx] = skb;
419 + /* locking problem with netif_stop_queue() ?? */
420 + printk(KERN_ERR "%s: %s: BUG! echo_skb is occupied!\n",
421 + dev->name, __func__);
425 +EXPORT_SYMBOL(can_put_echo_skb);
428 + * Get the skb from the stack and loop it back locally
430 + * The function is typically called when the TX done interrupt
431 + * is handled in the device driver.
433 +void can_get_echo_skb(struct net_device *dev, int idx)
435 + struct can_priv *priv = netdev_priv(dev);
437 + if ((dev->flags & IFF_ECHO) && priv->echo_skb[idx]) {
438 + netif_rx(priv->echo_skb[idx]);
439 + priv->echo_skb[idx] = NULL;
442 +EXPORT_SYMBOL(can_get_echo_skb);
445 + * CAN device restart for bus-off recovery
447 +int can_restart_now(struct net_device *dev)
449 + struct can_priv *priv = netdev_priv(dev);
450 + struct net_device_stats *stats = &dev->stats;
451 + struct sk_buff *skb;
452 + struct can_frame *cf;
455 + if (netif_carrier_ok(dev))
456 + netif_carrier_off(dev);
458 + /* Cancel restart in progress */
459 + if (priv->timer.expires) {
460 + del_timer(&priv->timer);
461 + priv->timer.expires = 0; /* mark inactive timer */
464 + can_flush_echo_skb(dev);
466 + err = priv->do_set_mode(dev, CAN_MODE_START);
470 + netif_carrier_on(dev);
472 + dev_dbg(ND2D(dev), "restarted\n");
473 + priv->can_stats.restarts++;
475 + /* send restart message upstream */
476 + skb = dev_alloc_skb(sizeof(struct can_frame));
480 + skb->protocol = htons(ETH_P_CAN);
481 + cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
482 + memset(cf, 0, sizeof(struct can_frame));
483 + cf->can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED;
484 + cf->can_dlc = CAN_ERR_DLC;
488 + dev->last_rx = jiffies;
489 + stats->rx_packets++;
490 + stats->rx_bytes += cf->can_dlc;
495 +static void can_restart_after(unsigned long data)
497 + struct net_device *dev = (struct net_device *)data;
498 + struct can_priv *priv = netdev_priv(dev);
500 + priv->timer.expires = 0; /* mark inactive timer */
501 + can_restart_now(dev);
507 + * This functions should be called when the device goes bus-off to
508 + * tell the netif layer that no more packets can be sent or received.
509 + * If enabled, a timer is started to trigger bus-off recovery.
511 +void can_bus_off(struct net_device *dev)
513 + struct can_priv *priv = netdev_priv(dev);
515 + dev_dbg(ND2D(dev), "bus-off\n");
517 + netif_carrier_off(dev);
519 + if (priv->restart_ms > 0 && !priv->timer.expires) {
521 + priv->timer.function = can_restart_after;
522 + priv->timer.data = (unsigned long)dev;
523 + priv->timer.expires =
524 + jiffies + (priv->restart_ms * HZ) / 1000;
525 + add_timer(&priv->timer);
528 +EXPORT_SYMBOL(can_bus_off);
531 + * Cleanup function before the device gets closed.
533 + * This functions should be called in the close function of the device
536 +void can_close_cleanup(struct net_device *dev)
538 + struct can_priv *priv = netdev_priv(dev);
540 + if (priv->timer.expires) {
541 + del_timer(&priv->timer);
542 + priv->timer.expires = 0;
545 + can_flush_echo_skb(dev);
547 +EXPORT_SYMBOL(can_close_cleanup);
549 +static __init int can_dev_init(void)
551 + printk(KERN_INFO MOD_DESC "\n");
555 +module_init(can_dev_init);
557 +static __exit void can_dev_exit(void)
560 +module_exit(can_dev_exit);
561 Index: net-next-2.6/include/linux/can/dev.h
562 ===================================================================
564 +++ net-next-2.6/include/linux/can/dev.h
569 + * Definitions for the CAN network device driver interface
571 + * Copyright (C) 2006 Andrey Volkov <avolkov@varma-el.com>
572 + * Varma Electronics Oy
574 + * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
576 + * Send feedback to <socketcan-users@lists.berlios.de>
582 +#include <linux/can/error.h>
585 + * CAN bitrate and bit-timing
587 +struct can_bittiming {
599 +struct can_bittiming_const {
620 + * CAN controller mode
622 +#define CAN_CTRLMODE_LOOPBACK 0x1
623 +#define CAN_CTRLMODE_LISTENONLY 0x2
624 +#define CAN_CTRLMODE_3_SAMPLES 0x4 /* Triple sampling mode */
627 + * CAN operational and error states
630 + CAN_STATE_ACTIVE = 0,
631 + CAN_STATE_BUS_WARNING,
632 + CAN_STATE_BUS_PASSIVE,
639 + * CAN device statistics
641 +struct can_device_stats {
642 + unsigned long error_warning;
643 + unsigned long data_overrun;
644 + unsigned long wakeup;
645 + unsigned long bus_error;
646 + unsigned long error_passive;
647 + unsigned long arbitration_lost;
648 + unsigned long restarts;
649 + unsigned long bus_error_at_init;
653 + * CAN common private data
655 +#define CAN_ECHO_SKB_MAX 4
658 + struct can_device_stats can_stats;
660 + struct can_bittiming bittiming;
661 + struct can_bittiming_const *bittiming_const;
663 + spinlock_t irq_lock;
665 + enum can_state state;
669 + struct timer_list timer;
671 + struct sk_buff *echo_skb[CAN_ECHO_SKB_MAX];
673 + int (*do_set_bittiming)(struct net_device *dev);
674 + int (*do_get_state)(struct net_device *dev, enum can_state *state);
675 + int (*do_set_mode)(struct net_device *dev, enum can_mode mode);
676 + int (*do_set_ctrlmode)(struct net_device *dev, u32 ctrlmode);
677 + int (*do_get_ctrlmode)(struct net_device *dev, u32 *ctrlmode);
680 +#define ND2D(_ndev) (_ndev->dev.parent)
683 +struct net_device *alloc_candev(int sizeof_priv);
684 +void free_candev(struct net_device *dev);
685 +int register_candev(struct net_device *dev);
686 +void unregister_candev(struct net_device *dev);
688 +int can_set_bittiming(struct net_device *dev);
690 +int can_restart_now(struct net_device *dev);
692 +void can_bus_off(struct net_device *dev);
694 +void can_close_cleanup(struct net_device *dev);
696 +void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx);
697 +void can_get_echo_skb(struct net_device *dev, int idx);
699 +int can_sample_point(struct can_bittiming *bt);
701 +#endif /* CAN_DEV_H */
702 Index: net-next-2.6/drivers/net/can/Kconfig
703 ===================================================================
704 --- net-next-2.6.orig/drivers/net/can/Kconfig
705 +++ net-next-2.6/drivers/net/can/Kconfig
706 @@ -12,6 +12,29 @@ config CAN_VCAN
707 This driver can also be built as a module. If so, the module
711 + tristate "Platform CAN drivers with SYSFS support"
715 + Enables the common framework for platform CAN drivers with SYSFS
716 + support. This is the standard library for CAN drivers.
719 +config CAN_CALC_BITTIMING
720 + bool "CAN bit-timing calculation"
724 + If enabled, CAN bit-timing parameters will be calculated for the
725 + bit-rate specified via SYSFS file "bitrate" when the device gets
726 + started. This works fine for the most common CAN controllers
727 + with standard bit-rates but may fail for exotic bit-rates or CAN
728 + source clock frequencies. Disabling saves some space, but then the
729 + bit-timing parameters must be specified directly using the SYSFS
730 + files "tq", "prop_seg", "phase_seg1", "phase_seg2" and "sjw".
733 config CAN_DEBUG_DEVICES
734 bool "CAN devices debugging messages"