4 * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
5 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
6 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the version 2 of the GNU General Public License
10 * as published by the Free Software Foundation
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/module.h>
23 #include <linux/version.h>
24 #include <linux/kernel.h>
25 #include <linux/netdevice.h>
26 #include <linux/if_arp.h>
27 #include <socketcan/can.h>
28 #include <socketcan/can/dev.h>
30 #ifndef CONFIG_CAN_DEV_SYSFS
31 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
32 #error "CAN netlink interface not support by this kernel version"
34 #include <socketcan/can/netlink.h>
35 #include <net/rtnetlink.h>
37 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)
38 #error "CAN sysfs interface not support by this kernel version"
43 #define MOD_DESC "CAN device driver interface"
45 MODULE_DESCRIPTION(MOD_DESC);
46 MODULE_LICENSE("GPL v2");
47 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
49 #ifdef CONFIG_CAN_CALC_BITTIMING
50 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
53 * Bit-timing calculation derived from:
55 * Code based on LinCAN sources and H8S2638 project
56 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
57 * Copyright 2005 Stanislav Marek
58 * email: pisa@cmp.felk.cvut.cz
60 * Calculates proper bit-timing parameters for a specified bit-rate
61 * and sample-point, which can then be used to set the bit-timing
62 * registers of the CAN controller. You can find more information
63 * in the header file socketcan/can/netlink.h.
65 static int can_update_spt(const struct can_bittiming_const *btc,
66 int sampl_pt, int tseg, int *tseg1, int *tseg2)
68 *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
69 if (*tseg2 < btc->tseg2_min)
70 *tseg2 = btc->tseg2_min;
71 if (*tseg2 > btc->tseg2_max)
72 *tseg2 = btc->tseg2_max;
73 *tseg1 = tseg - *tseg2;
74 if (*tseg1 > btc->tseg1_max) {
75 *tseg1 = btc->tseg1_max;
76 *tseg2 = tseg - *tseg1;
78 return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
81 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
83 struct can_priv *priv = netdev_priv(dev);
84 const struct can_bittiming_const *btc = priv->bittiming_const;
85 long rate, best_rate = 0;
86 long best_error = 1000000000, error = 0;
87 int best_tseg = 0, best_brp = 0, brp = 0;
88 int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
89 int spt_error = 1000, spt = 0, sampl_pt;
92 if (!priv->bittiming_const)
95 /* Use CIA recommended sample points */
96 if (bt->sample_point) {
97 sampl_pt = bt->sample_point;
99 if (bt->bitrate > 800000)
101 else if (bt->bitrate > 500000)
107 /* tseg even = round down, odd = round up */
108 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
109 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
110 tsegall = 1 + tseg / 2;
111 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
112 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
113 /* chose brp step which is possible in system */
114 brp = (brp / btc->brp_inc) * btc->brp_inc;
115 if ((brp < btc->brp_min) || (brp > btc->brp_max))
117 rate = priv->clock.freq / (brp * tsegall);
118 error = bt->bitrate - rate;
119 /* tseg brp biterror */
122 if (error > best_error)
126 spt = can_update_spt(btc, sampl_pt, tseg / 2,
128 error = sampl_pt - spt;
131 if (error > spt_error)
135 best_tseg = tseg / 2;
143 /* Error in one-tenth of a percent */
144 error = (best_error * 1000) / bt->bitrate;
145 if (error > CAN_CALC_MAX_ERROR) {
147 "bitrate error %ld.%ld%% too high\n",
148 error / 10, error % 10);
151 dev_warn(ND2D(dev), "bitrate error %ld.%ld%%\n",
152 error / 10, error % 10);
156 spt = can_update_spt(btc, sampl_pt, best_tseg, &tseg1, &tseg2);
158 v64 = (u64)best_brp * 1000000000UL;
159 do_div(v64, priv->clock.freq);
161 bt->prop_seg = tseg1 / 2;
162 bt->phase_seg1 = tseg1 - bt->prop_seg;
163 bt->phase_seg2 = tseg2;
166 #ifndef CONFIG_CAN_DEV_SYSFS
168 bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
169 /* real sample point */
170 bt->sample_point = spt;
174 #else /* !CONFIG_CAN_CALC_BITTIMING */
175 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
177 dev_err(ND2D(dev), "bit-timing calculation not available\n");
180 #endif /* CONFIG_CAN_CALC_BITTIMING */
183 #ifdef CONFIG_CAN_DEV_SYSFS
184 int can_sample_point(struct can_bittiming *bt)
186 return ((bt->prop_seg + bt->phase_seg1 + 1) * 1000) /
187 (bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1);
192 * Checks the validity of the specified bit-timing parameters prop_seg,
193 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
194 * prescaler value brp. You can find more information in the header
195 * file socketcan/can/netlink.h.
197 static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt)
199 struct can_priv *priv = netdev_priv(dev);
200 const struct can_bittiming_const *btc = priv->bittiming_const;
204 if (!priv->bittiming_const)
207 tseg1 = bt->prop_seg + bt->phase_seg1;
210 if (bt->sjw > btc->sjw_max ||
211 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
212 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
215 brp64 = (u64)priv->clock.freq * (u64)bt->tq;
216 if (btc->brp_inc > 1)
217 do_div(brp64, btc->brp_inc);
218 brp64 += 500000000UL - 1;
219 do_div(brp64, 1000000000UL); /* the practicable BRP */
220 if (btc->brp_inc > 1)
221 brp64 *= btc->brp_inc;
222 bt->brp = (u32)brp64;
224 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
227 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
228 bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
229 bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
234 int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt)
236 struct can_priv *priv = netdev_priv(dev);
239 /* Check if the CAN device has bit-timing parameters */
240 if (priv->bittiming_const) {
242 /* Non-expert mode? Check if the bitrate has been pre-defined */
244 /* Determine bit-timing parameters */
245 err = can_calc_bittiming(dev, bt);
247 /* Check bit-timing params and calculate proper brp */
248 err = can_fixup_bittiming(dev, bt);
257 * Local echo of CAN messages
259 * CAN network devices *should* support a local echo functionality
260 * (see Documentation/networking/can.txt). To test the handling of CAN
261 * interfaces that do not support the local echo both driver types are
262 * implemented. In the case that the driver does not support the echo
263 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
264 * to perform the echo as a fallback solution.
266 static void can_flush_echo_skb(struct net_device *dev)
268 struct can_priv *priv = netdev_priv(dev);
269 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
270 struct net_device_stats *stats = can_get_stats(dev);
272 struct net_device_stats *stats = &dev->stats;
276 for (i = 0; i < CAN_ECHO_SKB_MAX; i++) {
277 if (priv->echo_skb[i]) {
278 kfree_skb(priv->echo_skb[i]);
279 priv->echo_skb[i] = NULL;
281 stats->tx_aborted_errors++;
287 * Put the skb on the stack to be looped backed locally lateron
289 * The function is typically called in the start_xmit function
290 * of the device driver. The driver must protect access to
291 * priv->echo_skb, if necessary.
293 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx)
295 struct can_priv *priv = netdev_priv(dev);
297 /* check flag whether this packet has to be looped back */
298 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
303 if (!priv->echo_skb[idx]) {
304 struct sock *srcsk = skb->sk;
306 if (atomic_read(&skb->users) != 1) {
307 struct sk_buff *old_skb = skb;
309 skb = skb_clone(old_skb, GFP_ATOMIC);
318 /* make settings for echo to reduce code in irq context */
319 skb->protocol = htons(ETH_P_CAN);
320 skb->pkt_type = PACKET_BROADCAST;
321 skb->ip_summed = CHECKSUM_UNNECESSARY;
324 /* save this skb for tx interrupt echo handling */
325 priv->echo_skb[idx] = skb;
327 /* locking problem with netif_stop_queue() ?? */
328 dev_err(ND2D(dev), "%s: BUG! echo_skb is occupied!\n",
333 EXPORT_SYMBOL_GPL(can_put_echo_skb);
336 * Get the skb from the stack and loop it back locally
338 * The function is typically called when the TX done interrupt
339 * is handled in the device driver. The driver must protect
340 * access to priv->echo_skb, if necessary.
342 void can_get_echo_skb(struct net_device *dev, int idx)
344 struct can_priv *priv = netdev_priv(dev);
346 if (priv->echo_skb[idx]) {
347 netif_rx(priv->echo_skb[idx]);
348 priv->echo_skb[idx] = NULL;
351 EXPORT_SYMBOL_GPL(can_get_echo_skb);
354 * Remove the skb from the stack and free it.
356 * The function is typically called when TX failed.
358 void can_free_echo_skb(struct net_device *dev, int idx)
360 struct can_priv *priv = netdev_priv(dev);
362 if (priv->echo_skb[idx]) {
363 kfree_skb(priv->echo_skb[idx]);
364 priv->echo_skb[idx] = NULL;
367 EXPORT_SYMBOL_GPL(can_free_echo_skb);
370 * CAN device restart for bus-off recovery
372 void can_restart(unsigned long data)
374 struct net_device *dev = (struct net_device *)data;
375 struct can_priv *priv = netdev_priv(dev);
376 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
377 struct net_device_stats *stats = can_get_stats(dev);
379 struct net_device_stats *stats = &dev->stats;
382 struct can_frame *cf;
385 BUG_ON(netif_carrier_ok(dev));
388 * No synchronization needed because the device is bus-off and
389 * no messages can come in or go out.
391 can_flush_echo_skb(dev);
393 /* send restart message upstream */
394 skb = dev_alloc_skb(sizeof(struct can_frame));
400 skb->protocol = htons(ETH_P_CAN);
401 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
402 memset(cf, 0, sizeof(struct can_frame));
403 cf->can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED;
404 cf->can_dlc = CAN_ERR_DLC;
408 dev->last_rx = jiffies;
410 stats->rx_bytes += cf->can_dlc;
413 dev_dbg(ND2D(dev), "restarted\n");
414 priv->can_stats.restarts++;
416 /* Now restart the device */
417 err = priv->do_set_mode(dev, CAN_MODE_START);
419 netif_carrier_on(dev);
421 dev_err(ND2D(dev), "Error %d during restart", err);
424 int can_restart_now(struct net_device *dev)
426 struct can_priv *priv = netdev_priv(dev);
429 * A manual restart is only permitted if automatic restart is
430 * disabled and the device is in the bus-off state
432 if (priv->restart_ms)
434 if (priv->state != CAN_STATE_BUS_OFF)
437 /* Runs as soon as possible in the timer context */
438 mod_timer(&priv->restart_timer, jiffies);
446 * This functions should be called when the device goes bus-off to
447 * tell the netif layer that no more packets can be sent or received.
448 * If enabled, a timer is started to trigger bus-off recovery.
450 void can_bus_off(struct net_device *dev)
452 struct can_priv *priv = netdev_priv(dev);
454 dev_dbg(ND2D(dev), "bus-off\n");
456 netif_carrier_off(dev);
457 priv->can_stats.bus_off++;
459 if (priv->restart_ms)
460 mod_timer(&priv->restart_timer,
461 jiffies + (priv->restart_ms * HZ) / 1000);
463 EXPORT_SYMBOL_GPL(can_bus_off);
465 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
466 struct net_device_stats *can_get_stats(struct net_device *dev)
468 struct can_priv *priv = netdev_priv(dev);
470 return &priv->net_stats;
472 EXPORT_SYMBOL_GPL(can_get_stats);
475 static void can_setup(struct net_device *dev)
477 dev->type = ARPHRD_CAN;
478 dev->mtu = sizeof(struct can_frame);
479 dev->hard_header_len = 0;
481 dev->tx_queue_len = 10;
483 /* New-style flags. */
484 dev->flags = IFF_NOARP;
485 dev->features = NETIF_F_NO_CSUM;
486 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
487 dev->get_stats = can_get_stats;
492 * Allocate and setup space for the CAN network device
494 struct net_device *alloc_candev(int sizeof_priv)
496 struct net_device *dev;
497 struct can_priv *priv;
499 dev = alloc_netdev(sizeof_priv, "can%d", can_setup);
503 priv = netdev_priv(dev);
505 priv->state = CAN_STATE_STOPPED;
507 init_timer(&priv->restart_timer);
511 EXPORT_SYMBOL_GPL(alloc_candev);
514 * Free space of the CAN network device
516 void free_candev(struct net_device *dev)
520 EXPORT_SYMBOL_GPL(free_candev);
522 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
523 static inline void setup_timer(struct timer_list * timer,
524 void (*function)(unsigned long),
527 timer->function = function;
534 * Common open function when the device gets opened.
536 * This function should be called in the open function of the device
539 int open_candev(struct net_device *dev)
541 struct can_priv *priv = netdev_priv(dev);
542 #ifdef CONFIG_CAN_DEV_SYSFS
546 if (!priv->bittiming.tq && !priv->bittiming.bitrate) {
547 dev_err(ND2D(dev), "bit-timing not yet defined\n");
551 #ifdef CONFIG_CAN_DEV_SYSFS
552 err = can_get_bittiming(dev, &priv->bittiming);
556 if (priv->do_set_bittiming) {
557 /* Finally, set the bit-timing registers */
558 err = priv->do_set_bittiming(dev);
564 setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev);
568 EXPORT_SYMBOL_GPL(open_candev);
571 * Common close function for cleanup before the device gets closed.
573 * This function should be called in the close function of the device
576 void close_candev(struct net_device *dev)
578 struct can_priv *priv = netdev_priv(dev);
580 if (del_timer_sync(&priv->restart_timer))
582 can_flush_echo_skb(dev);
584 EXPORT_SYMBOL_GPL(close_candev);
586 #ifndef CONFIG_CAN_DEV_SYSFS
588 * CAN netlink interface
590 static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
591 [IFLA_CAN_STATE] = { .type = NLA_U32 },
592 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
593 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
594 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
595 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
596 [IFLA_CAN_BITTIMING_CONST]
597 = { .len = sizeof(struct can_bittiming_const) },
598 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
601 static int can_changelink(struct net_device *dev,
602 struct nlattr *tb[], struct nlattr *data[])
604 struct can_priv *priv = netdev_priv(dev);
607 /* We need synchronization with dev->stop() */
610 if (data[IFLA_CAN_CTRLMODE]) {
611 struct can_ctrlmode *cm;
613 /* Do not allow changing controller mode while running */
614 if (dev->flags & IFF_UP)
616 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
617 priv->ctrlmode &= ~cm->mask;
618 priv->ctrlmode |= cm->flags;
621 if (data[IFLA_CAN_BITTIMING]) {
622 struct can_bittiming bt;
624 /* Do not allow changing bittiming while running */
625 if (dev->flags & IFF_UP)
627 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
628 if ((!bt.bitrate && !bt.tq) || (bt.bitrate && bt.tq))
630 err = can_get_bittiming(dev, &bt);
633 memcpy(&priv->bittiming, &bt, sizeof(bt));
635 if (priv->do_set_bittiming) {
636 /* Finally, set the bit-timing registers */
637 err = priv->do_set_bittiming(dev);
643 if (data[IFLA_CAN_RESTART_MS]) {
644 /* Do not allow changing restart delay while running */
645 if (dev->flags & IFF_UP)
647 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
650 if (data[IFLA_CAN_RESTART]) {
651 /* Do not allow a restart while not running */
652 if (!(dev->flags & IFF_UP))
654 err = can_restart_now(dev);
662 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
664 struct can_priv *priv = netdev_priv(dev);
665 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
666 enum can_state state = priv->state;
668 if (priv->do_get_state)
669 priv->do_get_state(dev, &state);
670 NLA_PUT_U32(skb, IFLA_CAN_STATE, state);
671 NLA_PUT(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm);
672 NLA_PUT_U32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms);
673 NLA_PUT(skb, IFLA_CAN_BITTIMING,
674 sizeof(priv->bittiming), &priv->bittiming);
675 NLA_PUT(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock);
676 if (priv->bittiming_const)
677 NLA_PUT(skb, IFLA_CAN_BITTIMING_CONST,
678 sizeof(*priv->bittiming_const), priv->bittiming_const);
686 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
688 struct can_priv *priv = netdev_priv(dev);
690 NLA_PUT(skb, IFLA_INFO_XSTATS,
691 sizeof(priv->can_stats), &priv->can_stats);
699 static int can_newlink(struct net_device *dev,
700 struct nlattr *tb[], struct nlattr *data[])
705 static struct rtnl_link_ops can_link_ops __read_mostly = {
707 .maxtype = IFLA_CAN_MAX,
708 .policy = can_policy,
710 .newlink = can_newlink,
711 .changelink = can_changelink,
712 .fill_info = can_fill_info,
713 .fill_xstats = can_fill_xstats,
716 #endif /* !CONFIG_CAN_DEV_SYSFS */
719 * Register the CAN network device
721 int register_candev(struct net_device *dev)
723 #ifdef CONFIG_CAN_DEV_SYSFS
726 err = register_netdev(dev);
728 can_create_sysfs(dev);
732 dev->rtnl_link_ops = &can_link_ops;
733 return register_netdev(dev);
736 EXPORT_SYMBOL_GPL(register_candev);
739 * Unregister the CAN network device
741 void unregister_candev(struct net_device *dev)
743 #ifdef CONFIG_CAN_DEV_SYSFS
744 can_remove_sysfs(dev);
746 unregister_netdev(dev);
748 EXPORT_SYMBOL_GPL(unregister_candev);
750 static __init int can_dev_init(void)
752 #ifndef CONFIG_CAN_DEV_SYSFS
755 err = rtnl_link_register(&can_link_ops);
757 printk(KERN_INFO MOD_DESC "\n");
761 printk(KERN_INFO MOD_DESC " using the deprecated SYSFS interface\n");
766 module_init(can_dev_init);
768 static __exit void can_dev_exit(void)
770 #ifndef CONFIG_CAN_DEV_SYSFS
771 rtnl_link_unregister(&can_link_ops);
774 module_exit(can_dev_exit);
776 #ifndef CONFIG_CAN_DEV_SYSFS
777 MODULE_ALIAS_RTNL_LINK("can");