2 * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
3 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the version 2 of the GNU General Public License
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/netdevice.h>
24 #include <linux/if_arp.h>
25 #include <linux/can.h>
26 #include <linux/can/dev.h>
27 #include <linux/can/netlink.h>
28 #include <net/rtnetlink.h>
30 #define MOD_DESC "CAN device driver interface"
32 MODULE_DESCRIPTION(MOD_DESC);
33 MODULE_LICENSE("GPL v2");
36 /* CAN DLC to real data length conversion helpers */
38 static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
39 8, 12, 16, 20, 24, 32, 48, 64};
41 /* get data length from can_dlc with sanitized can_dlc */
42 u8 can_dlc2len(u8 can_dlc)
44 return dlc2len[can_dlc & 0x0F];
46 EXPORT_SYMBOL_GPL(can_dlc2len);
48 static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
49 9, 9, 9, 9, /* 9 - 12 */
50 10, 10, 10, 10, /* 13 - 16 */
51 11, 11, 11, 11, /* 17 - 20 */
52 12, 12, 12, 12, /* 21 - 24 */
53 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
54 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
55 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
56 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
57 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
59 /* map the sanitized data length to an appropriate data length code */
60 u8 can_len2dlc(u8 len)
62 if (unlikely(len > 64))
67 EXPORT_SYMBOL_GPL(can_len2dlc);
69 #ifdef CONFIG_CAN_CALC_BITTIMING
70 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
73 * Bit-timing calculation derived from:
75 * Code based on LinCAN sources and H8S2638 project
76 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
77 * Copyright 2005 Stanislav Marek
80 * Calculates proper bit-timing parameters for a specified bit-rate
81 * and sample-point, which can then be used to set the bit-timing
82 * registers of the CAN controller. You can find more information
83 * in the header file linux/can/netlink.h.
85 static int can_update_spt(const struct can_bittiming_const *btc,
86 int sampl_pt, int tseg, int *tseg1, int *tseg2)
88 *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
89 if (*tseg2 < btc->tseg2_min)
90 *tseg2 = btc->tseg2_min;
91 if (*tseg2 > btc->tseg2_max)
92 *tseg2 = btc->tseg2_max;
93 *tseg1 = tseg - *tseg2;
94 if (*tseg1 > btc->tseg1_max) {
95 *tseg1 = btc->tseg1_max;
96 *tseg2 = tseg - *tseg1;
98 return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
101 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
103 struct can_priv *priv = netdev_priv(dev);
104 const struct can_bittiming_const *btc = priv->bittiming_const;
105 long rate, best_rate = 0;
106 long best_error = 1000000000, error = 0;
107 int best_tseg = 0, best_brp = 0, brp = 0;
108 int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
109 int spt_error = 1000, spt = 0, sampl_pt;
112 if (!priv->bittiming_const)
115 /* Use CIA recommended sample points */
116 if (bt->sample_point) {
117 sampl_pt = bt->sample_point;
119 if (bt->bitrate > 800000)
121 else if (bt->bitrate > 500000)
127 /* tseg even = round down, odd = round up */
128 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
129 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
130 tsegall = 1 + tseg / 2;
131 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
132 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
133 /* chose brp step which is possible in system */
134 brp = (brp / btc->brp_inc) * btc->brp_inc;
135 if ((brp < btc->brp_min) || (brp > btc->brp_max))
137 rate = priv->clock.freq / (brp * tsegall);
138 error = bt->bitrate - rate;
139 /* tseg brp biterror */
142 if (error > best_error)
146 spt = can_update_spt(btc, sampl_pt, tseg / 2,
148 error = sampl_pt - spt;
151 if (error > spt_error)
155 best_tseg = tseg / 2;
163 /* Error in one-tenth of a percent */
164 error = (best_error * 1000) / bt->bitrate;
165 if (error > CAN_CALC_MAX_ERROR) {
167 "bitrate error %ld.%ld%% too high\n",
168 error / 10, error % 10);
171 netdev_warn(dev, "bitrate error %ld.%ld%%\n",
172 error / 10, error % 10);
176 /* real sample point */
177 bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
180 v64 = (u64)best_brp * 1000000000UL;
181 do_div(v64, priv->clock.freq);
183 bt->prop_seg = tseg1 / 2;
184 bt->phase_seg1 = tseg1 - bt->prop_seg;
185 bt->phase_seg2 = tseg2;
187 /* check for sjw user settings */
188 if (!bt->sjw || !btc->sjw_max)
191 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
192 if (bt->sjw > btc->sjw_max)
193 bt->sjw = btc->sjw_max;
194 /* bt->sjw must not be higher than tseg2 */
201 bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
205 #else /* !CONFIG_CAN_CALC_BITTIMING */
206 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
208 netdev_err(dev, "bit-timing calculation not available\n");
211 #endif /* CONFIG_CAN_CALC_BITTIMING */
214 * Checks the validity of the specified bit-timing parameters prop_seg,
215 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
216 * prescaler value brp. You can find more information in the header
217 * file linux/can/netlink.h.
219 static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt)
221 struct can_priv *priv = netdev_priv(dev);
222 const struct can_bittiming_const *btc = priv->bittiming_const;
226 if (!priv->bittiming_const)
229 tseg1 = bt->prop_seg + bt->phase_seg1;
232 if (bt->sjw > btc->sjw_max ||
233 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
234 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
237 brp64 = (u64)priv->clock.freq * (u64)bt->tq;
238 if (btc->brp_inc > 1)
239 do_div(brp64, btc->brp_inc);
240 brp64 += 500000000UL - 1;
241 do_div(brp64, 1000000000UL); /* the practicable BRP */
242 if (btc->brp_inc > 1)
243 brp64 *= btc->brp_inc;
244 bt->brp = (u32)brp64;
246 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
249 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
250 bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
251 bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
256 static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt)
258 struct can_priv *priv = netdev_priv(dev);
261 /* Check if the CAN device has bit-timing parameters */
262 if (priv->bittiming_const) {
264 /* Non-expert mode? Check if the bitrate has been pre-defined */
266 /* Determine bit-timing parameters */
267 err = can_calc_bittiming(dev, bt);
269 /* Check bit-timing params and calculate proper brp */
270 err = can_fixup_bittiming(dev, bt);
279 * Local echo of CAN messages
281 * CAN network devices *should* support a local echo functionality
282 * (see Documentation/networking/can.txt). To test the handling of CAN
283 * interfaces that do not support the local echo both driver types are
284 * implemented. In the case that the driver does not support the echo
285 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
286 * to perform the echo as a fallback solution.
288 static void can_flush_echo_skb(struct net_device *dev)
290 struct can_priv *priv = netdev_priv(dev);
291 struct net_device_stats *stats = &dev->stats;
294 for (i = 0; i < priv->echo_skb_max; i++) {
295 if (priv->echo_skb[i]) {
296 kfree_skb(priv->echo_skb[i]);
297 priv->echo_skb[i] = NULL;
299 stats->tx_aborted_errors++;
305 * Put the skb on the stack to be looped backed locally lateron
307 * The function is typically called in the start_xmit function
308 * of the device driver. The driver must protect access to
309 * priv->echo_skb, if necessary.
311 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
314 struct can_priv *priv = netdev_priv(dev);
316 BUG_ON(idx >= priv->echo_skb_max);
318 /* check flag whether this packet has to be looped back */
319 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
324 if (!priv->echo_skb[idx]) {
325 struct sock *srcsk = skb->sk;
327 if (atomic_read(&skb->users) != 1) {
328 struct sk_buff *old_skb = skb;
330 skb = skb_clone(old_skb, GFP_ATOMIC);
339 /* make settings for echo to reduce code in irq context */
340 skb->protocol = htons(ETH_P_CAN);
341 skb->pkt_type = PACKET_BROADCAST;
342 skb->ip_summed = CHECKSUM_UNNECESSARY;
345 /* save this skb for tx interrupt echo handling */
346 priv->echo_skb[idx] = skb;
348 /* locking problem with netif_stop_queue() ?? */
349 netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
353 EXPORT_SYMBOL_GPL(can_put_echo_skb);
356 * Get the skb from the stack and loop it back locally
358 * The function is typically called when the TX done interrupt
359 * is handled in the device driver. The driver must protect
360 * access to priv->echo_skb, if necessary.
362 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
364 struct can_priv *priv = netdev_priv(dev);
366 BUG_ON(idx >= priv->echo_skb_max);
368 if (priv->echo_skb[idx]) {
369 struct sk_buff *skb = priv->echo_skb[idx];
370 struct can_frame *cf = (struct can_frame *)skb->data;
371 u8 dlc = cf->can_dlc;
373 netif_rx(priv->echo_skb[idx]);
374 priv->echo_skb[idx] = NULL;
381 EXPORT_SYMBOL_GPL(can_get_echo_skb);
384 * Remove the skb from the stack and free it.
386 * The function is typically called when TX failed.
388 void can_free_echo_skb(struct net_device *dev, unsigned int idx)
390 struct can_priv *priv = netdev_priv(dev);
392 BUG_ON(idx >= priv->echo_skb_max);
394 if (priv->echo_skb[idx]) {
395 kfree_skb(priv->echo_skb[idx]);
396 priv->echo_skb[idx] = NULL;
399 EXPORT_SYMBOL_GPL(can_free_echo_skb);
402 * CAN device restart for bus-off recovery
404 static void can_restart(unsigned long data)
406 struct net_device *dev = (struct net_device *)data;
407 struct can_priv *priv = netdev_priv(dev);
408 struct net_device_stats *stats = &dev->stats;
410 struct can_frame *cf;
413 BUG_ON(netif_carrier_ok(dev));
416 * No synchronization needed because the device is bus-off and
417 * no messages can come in or go out.
419 can_flush_echo_skb(dev);
421 /* send restart message upstream */
422 skb = alloc_can_err_skb(dev, &cf);
427 cf->can_id |= CAN_ERR_RESTARTED;
432 stats->rx_bytes += cf->can_dlc;
435 netdev_dbg(dev, "restarted\n");
436 priv->can_stats.restarts++;
438 /* Now restart the device */
439 err = priv->do_set_mode(dev, CAN_MODE_START);
441 netif_carrier_on(dev);
443 netdev_err(dev, "Error %d during restart", err);
446 int can_restart_now(struct net_device *dev)
448 struct can_priv *priv = netdev_priv(dev);
451 * A manual restart is only permitted if automatic restart is
452 * disabled and the device is in the bus-off state
454 if (priv->restart_ms)
456 if (priv->state != CAN_STATE_BUS_OFF)
459 /* Runs as soon as possible in the timer context */
460 mod_timer(&priv->restart_timer, jiffies);
468 * This functions should be called when the device goes bus-off to
469 * tell the netif layer that no more packets can be sent or received.
470 * If enabled, a timer is started to trigger bus-off recovery.
472 void can_bus_off(struct net_device *dev)
474 struct can_priv *priv = netdev_priv(dev);
476 netdev_dbg(dev, "bus-off\n");
478 netif_carrier_off(dev);
479 priv->can_stats.bus_off++;
481 if (priv->restart_ms)
482 mod_timer(&priv->restart_timer,
483 jiffies + (priv->restart_ms * HZ) / 1000);
485 EXPORT_SYMBOL_GPL(can_bus_off);
487 static void can_setup(struct net_device *dev)
489 dev->type = ARPHRD_CAN;
491 dev->hard_header_len = 0;
493 dev->tx_queue_len = 10;
495 /* New-style flags. */
496 dev->flags = IFF_NOARP;
497 dev->features = NETIF_F_HW_CSUM;
500 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
504 skb = netdev_alloc_skb(dev, sizeof(struct can_frame));
508 skb->protocol = htons(ETH_P_CAN);
509 skb->pkt_type = PACKET_BROADCAST;
510 skb->ip_summed = CHECKSUM_UNNECESSARY;
511 *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
512 memset(*cf, 0, sizeof(struct can_frame));
516 EXPORT_SYMBOL_GPL(alloc_can_skb);
518 struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
522 skb = alloc_can_skb(dev, cf);
526 (*cf)->can_id = CAN_ERR_FLAG;
527 (*cf)->can_dlc = CAN_ERR_DLC;
531 EXPORT_SYMBOL_GPL(alloc_can_err_skb);
534 * Allocate and setup space for the CAN network device
536 struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
538 struct net_device *dev;
539 struct can_priv *priv;
543 size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
544 echo_skb_max * sizeof(struct sk_buff *);
548 dev = alloc_netdev(size, "can%d", can_setup);
552 priv = netdev_priv(dev);
555 priv->echo_skb_max = echo_skb_max;
556 priv->echo_skb = (void *)priv +
557 ALIGN(sizeof_priv, sizeof(struct sk_buff *));
560 priv->state = CAN_STATE_STOPPED;
562 init_timer(&priv->restart_timer);
566 EXPORT_SYMBOL_GPL(alloc_candev);
569 * Free space of the CAN network device
571 void free_candev(struct net_device *dev)
575 EXPORT_SYMBOL_GPL(free_candev);
578 * Common open function when the device gets opened.
580 * This function should be called in the open function of the device
583 int open_candev(struct net_device *dev)
585 struct can_priv *priv = netdev_priv(dev);
587 if (!priv->bittiming.tq && !priv->bittiming.bitrate) {
588 netdev_err(dev, "bit-timing not yet defined\n");
592 /* Switch carrier on if device was stopped while in bus-off state */
593 if (!netif_carrier_ok(dev))
594 netif_carrier_on(dev);
596 setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev);
600 EXPORT_SYMBOL_GPL(open_candev);
603 * Common close function for cleanup before the device gets closed.
605 * This function should be called in the close function of the device
608 void close_candev(struct net_device *dev)
610 struct can_priv *priv = netdev_priv(dev);
612 del_timer_sync(&priv->restart_timer);
613 can_flush_echo_skb(dev);
615 EXPORT_SYMBOL_GPL(close_candev);
618 * CAN netlink interface
620 static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
621 [IFLA_CAN_STATE] = { .type = NLA_U32 },
622 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
623 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
624 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
625 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
626 [IFLA_CAN_BITTIMING_CONST]
627 = { .len = sizeof(struct can_bittiming_const) },
628 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
629 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
632 static int can_changelink(struct net_device *dev,
633 struct nlattr *tb[], struct nlattr *data[])
635 struct can_priv *priv = netdev_priv(dev);
638 /* We need synchronization with dev->stop() */
641 if (data[IFLA_CAN_CTRLMODE]) {
642 struct can_ctrlmode *cm;
644 /* Do not allow changing controller mode while running */
645 if (dev->flags & IFF_UP)
647 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
648 if (cm->flags & ~priv->ctrlmode_supported)
650 priv->ctrlmode &= ~cm->mask;
651 priv->ctrlmode |= cm->flags;
654 if (data[IFLA_CAN_BITTIMING]) {
655 struct can_bittiming bt;
657 /* Do not allow changing bittiming while running */
658 if (dev->flags & IFF_UP)
660 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
661 if ((!bt.bitrate && !bt.tq) || (bt.bitrate && bt.tq))
663 err = can_get_bittiming(dev, &bt);
666 memcpy(&priv->bittiming, &bt, sizeof(bt));
668 if (priv->do_set_bittiming) {
669 /* Finally, set the bit-timing registers */
670 err = priv->do_set_bittiming(dev);
676 if (data[IFLA_CAN_RESTART_MS]) {
677 /* Do not allow changing restart delay while running */
678 if (dev->flags & IFF_UP)
680 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
683 if (data[IFLA_CAN_RESTART]) {
684 /* Do not allow a restart while not running */
685 if (!(dev->flags & IFF_UP))
687 err = can_restart_now(dev);
695 static size_t can_get_size(const struct net_device *dev)
697 struct can_priv *priv = netdev_priv(dev);
700 size = nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
701 size += sizeof(struct can_ctrlmode); /* IFLA_CAN_CTRLMODE */
702 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
703 size += sizeof(struct can_bittiming); /* IFLA_CAN_BITTIMING */
704 size += sizeof(struct can_clock); /* IFLA_CAN_CLOCK */
705 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
706 size += sizeof(struct can_berr_counter);
707 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
708 size += sizeof(struct can_bittiming_const);
713 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
715 struct can_priv *priv = netdev_priv(dev);
716 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
717 struct can_berr_counter bec;
718 enum can_state state = priv->state;
720 if (priv->do_get_state)
721 priv->do_get_state(dev, &state);
722 if (nla_put_u32(skb, IFLA_CAN_STATE, state) ||
723 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
724 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
725 nla_put(skb, IFLA_CAN_BITTIMING,
726 sizeof(priv->bittiming), &priv->bittiming) ||
727 nla_put(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock) ||
728 (priv->do_get_berr_counter &&
729 !priv->do_get_berr_counter(dev, &bec) &&
730 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
731 (priv->bittiming_const &&
732 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
733 sizeof(*priv->bittiming_const), priv->bittiming_const)))
734 goto nla_put_failure;
741 static size_t can_get_xstats_size(const struct net_device *dev)
743 return sizeof(struct can_device_stats);
746 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
748 struct can_priv *priv = netdev_priv(dev);
750 if (nla_put(skb, IFLA_INFO_XSTATS,
751 sizeof(priv->can_stats), &priv->can_stats))
752 goto nla_put_failure;
759 static int can_newlink(struct net *src_net, struct net_device *dev,
760 struct nlattr *tb[], struct nlattr *data[])
765 static struct rtnl_link_ops can_link_ops __read_mostly = {
767 .maxtype = IFLA_CAN_MAX,
768 .policy = can_policy,
770 .newlink = can_newlink,
771 .changelink = can_changelink,
772 .get_size = can_get_size,
773 .fill_info = can_fill_info,
774 .get_xstats_size = can_get_xstats_size,
775 .fill_xstats = can_fill_xstats,
779 * Register the CAN network device
781 int register_candev(struct net_device *dev)
783 dev->rtnl_link_ops = &can_link_ops;
784 return register_netdev(dev);
786 EXPORT_SYMBOL_GPL(register_candev);
789 * Unregister the CAN network device
791 void unregister_candev(struct net_device *dev)
793 unregister_netdev(dev);
795 EXPORT_SYMBOL_GPL(unregister_candev);
797 static __init int can_dev_init(void)
801 err = rtnl_link_register(&can_link_ops);
803 printk(KERN_INFO MOD_DESC "\n");
807 module_init(can_dev_init);
809 static __exit void can_dev_exit(void)
811 rtnl_link_unregister(&can_link_ops);
813 module_exit(can_dev_exit);
815 MODULE_ALIAS_RTNL_LINK("can");