4 * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
5 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the version 2 of the GNU General Public License
9 * as published by the Free Software Foundation
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/module.h>
22 #include <linux/netdevice.h>
23 #include <linux/if_arp.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/can.h>
26 #include <linux/can/dev.h>
30 #define MOD_DESC "CAN netdevice library"
31 #define MOD_REV "20080620"
33 MODULE_DESCRIPTION(MOD_DESC " (rev " MOD_REV ")");
34 MODULE_LICENSE("GPL v2");
35 MODULE_AUTHOR("Marc Kleine-Budde <mkl@pengutronix.de>, "
36 "Andrey Volkov <avolkov@varma-el.com>");
38 static int restart_ms;
40 module_param(restart_ms, int, S_IRUGO | S_IWUSR);
42 MODULE_PARM_DESC(restart_ms, "Restart time after bus-off in ms (default 0)");
47 * Bit rate calculated with next formula:
48 * bitrate = frq/(brp*(1 + prop_seg+ phase_seg1 + phase_seg2))
50 * This calc function based on work of Florian Hartwich and Armin Bassemi
51 * "The Configuration of the CAN Bit Timing"
52 * (http://www.semiconductors.bosch.de/pdf/CiA99Paper.pdf)
56 * bittime_nsec - expected bit time in nanosecs
59 * bittime - calculated time segments, for meaning of
60 * each field read CAN standard.
63 #define DEFAULT_MAX_BRP 64U
64 #define DEFAULT_MAX_SJW 4U
66 /* All below values in tq units */
67 #define MAX_BITTIME 25U
68 #define MIN_BITTIME 8U
69 #define MAX_PROP_SEG 8U
70 #define MAX_PHASE_SEG1 8U
71 #define MAX_PHASE_SEG2 8U
73 int can_calc_bittime(struct can_priv *can, u32 bitrate,
74 struct can_bittime_std *bittime)
76 int best_error = -1; /* Ariphmetic error */
77 int df, best_df = -1; /* oscillator's tolerance range,
79 u32 quanta; /* in tq units */
80 u32 brp, phase_seg1, phase_seg2, sjw, prop_seg;
81 u32 brp_min, brp_max, brp_expected;
84 /* bitrate range [1baud,1MiB/s] */
85 if (bitrate == 0 || bitrate > 1000000UL)
88 tmp = (u64) can->can_sys_clock * 1000;
90 brp_expected = (u32) tmp;
92 brp_min = brp_expected / (1000 * MAX_BITTIME);
95 if (brp_min > can->max_brp)
98 brp_max = (brp_expected + 500 * MIN_BITTIME) / (1000 * MIN_BITTIME);
101 if (brp_max > can->max_brp)
102 brp_max = can->max_brp;
104 for (brp = brp_min; brp <= brp_max; brp++) {
105 quanta = brp_expected / (brp * 1000);
106 if (quanta < MAX_BITTIME
107 && quanta * brp * 1000 != brp_expected)
109 if (quanta < MIN_BITTIME || quanta > MAX_BITTIME)
112 phase_seg2 = min((quanta - 3) / 2, MAX_PHASE_SEG2);
113 for (sjw = can->max_sjw; sjw > 0; sjw--) {
114 for (; phase_seg2 > sjw; phase_seg2--) {
117 phase_seg2 % 2 ? phase_seg2 -
119 prop_seg = quanta - 1 - phase_seg2 - phase_seg1;
121 * FIXME: support of longer lines (i.e. bigger
122 * prop_seg) is more prefered than support of
123 * cheap oscillators (i.e. bigger
124 * df/phase_seg1/phase_seg2)
126 if (prop_seg < phase_seg1)
128 if (prop_seg > MAX_PROP_SEG)
131 err1 = phase_seg1 * brp * 500 * 1000 /
133 phase_seg2 * brp * 1000);
134 err2 = sjw * brp * 50 * 1000 / brp_expected;
136 df = min(err1, err2);
139 abs(brp_expected * 10 /
140 (brp * (1 + prop_seg +
145 if (error > 10 || error > best_error)
148 if (error == best_error
149 && prop_seg < bittime->prop_seg)
155 bittime->prop_seg = prop_seg;
156 bittime->phase_seg1 = phase_seg1;
157 bittime->phase_seg2 = phase_seg2;
160 (bittime->phase_seg1 > 3);
172 int can_set_bitrate(struct net_device *dev, u32 bitrate)
174 struct can_priv *priv = netdev_priv(dev);
177 if (priv->state != CAN_STATE_STOPPED)
180 if (priv->do_set_bittime) {
181 if (priv->do_set_bittime) {
182 struct can_bittime bittime;
183 err = can_calc_bittime(priv, bitrate, &bittime.std);
186 bittime.type = CAN_BITTIME_STD;
187 err = priv->do_set_bittime(dev, &bittime);
189 priv->bitrate = bitrate;
190 priv->bittime = bittime;
197 EXPORT_SYMBOL(can_set_bitrate);
199 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
200 static struct net_device_stats *can_get_stats(struct net_device *dev)
202 struct can_priv *priv = netdev_priv(dev);
204 return &priv->net_stats;
208 static void can_setup(struct net_device *dev)
210 dev->type = ARPHRD_CAN;
211 dev->mtu = sizeof(struct can_frame);
212 dev->hard_header_len = 0;
214 dev->tx_queue_len = 10;
216 /* New-style flags. */
217 dev->flags = IFF_NOARP;
218 dev->features = NETIF_F_NO_CSUM;
219 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
220 dev->get_stats = can_get_stats;
225 * Function alloc_candev
226 * Allocates and sets up an CAN device
228 struct net_device *alloc_candev(int sizeof_priv)
230 struct net_device *dev;
231 struct can_priv *priv;
233 dev = alloc_netdev(sizeof_priv, "can%d", can_setup);
237 priv = netdev_priv(dev);
239 /* Default values can be overwritten by the device driver */
240 priv->restart_ms = restart_ms;
241 priv->bitrate = CAN_BITRATE_UNCONFIGURED;
242 priv->state = CAN_STATE_STOPPED;
243 priv->max_brp = DEFAULT_MAX_BRP;
244 priv->max_sjw = DEFAULT_MAX_SJW;
245 spin_lock_init(&priv->irq_lock);
247 init_timer(&priv->timer);
248 priv->timer.expires = 0;
252 EXPORT_SYMBOL(alloc_candev);
254 void free_candev(struct net_device *dev)
258 EXPORT_SYMBOL(free_candev);
261 * Local echo of CAN messages
263 * CAN network devices *should* support a local echo functionality
264 * (see Documentation/networking/can.txt). To test the handling of CAN
265 * interfaces that do not support the local echo both driver types are
266 * implemented. In the case that the driver does not support the echo
267 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
268 * to perform the echo as a fallback solution.
271 void can_flush_echo_skb(struct net_device *dev)
273 struct can_priv *priv = netdev_priv(dev);
275 struct net_device_stats *stats = dev->get_stats(dev);
279 for (i = 0; i < CAN_ECHO_SKB_MAX; i++) {
280 if (priv->echo_skb[i]) {
281 kfree_skb(priv->echo_skb[i]);
282 priv->echo_skb[i] = NULL;
285 stats->tx_aborted_errors++;
291 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx)
293 struct can_priv *priv = netdev_priv(dev);
295 /* set flag whether this packet has to be looped back */
296 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
301 if (!priv->echo_skb[idx]) {
302 struct sock *srcsk = skb->sk;
304 if (atomic_read(&skb->users) != 1) {
305 struct sk_buff *old_skb = skb;
307 skb = skb_clone(old_skb, GFP_ATOMIC);
316 /* make settings for echo to reduce code in irq context */
317 skb->protocol = htons(ETH_P_CAN);
318 skb->pkt_type = PACKET_BROADCAST;
319 skb->ip_summed = CHECKSUM_UNNECESSARY;
322 /* save this skb for tx interrupt echo handling */
323 priv->echo_skb[idx] = skb;
325 /* locking problem with netif_stop_queue() ?? */
326 printk(KERN_ERR "%s: %s: BUG! echo_skb is occupied!\n",
327 dev->name, __func__);
331 EXPORT_SYMBOL(can_put_echo_skb);
333 void can_get_echo_skb(struct net_device *dev, int idx)
335 struct can_priv *priv = netdev_priv(dev);
337 if ((dev->flags & IFF_ECHO) && priv->echo_skb[idx]) {
338 netif_rx(priv->echo_skb[idx]);
339 priv->echo_skb[idx] = NULL;
342 EXPORT_SYMBOL(can_get_echo_skb);
345 * CAN bus-off handling
346 * FIXME: we need some synchronization
348 int can_restart_now(struct net_device *dev)
350 struct can_priv *priv = netdev_priv(dev);
351 struct net_device_stats *stats = dev->get_stats(dev);
353 struct can_frame *cf;
356 if (netif_carrier_ok(dev))
357 netif_carrier_off(dev);
359 /* Cancel restart in progress */
360 if (priv->timer.expires) {
361 del_timer(&priv->timer);
362 priv->timer.expires = 0; /* mark inactive timer */
365 can_flush_echo_skb(dev);
367 err = priv->do_set_mode(dev, CAN_MODE_START);
371 netif_carrier_on(dev);
373 priv->can_stats.restarts++;
375 /* send restart message upstream */
376 skb = dev_alloc_skb(sizeof(struct can_frame));
380 skb->protocol = htons(ETH_P_CAN);
381 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
382 memset(cf, 0, sizeof(struct can_frame));
383 cf->can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED;
384 cf->can_dlc = CAN_ERR_DLC;
388 dev->last_rx = jiffies;
390 stats->rx_bytes += cf->can_dlc;
395 static void can_restart_after(unsigned long data)
397 struct net_device *dev = (struct net_device *)data;
398 struct can_priv *priv = netdev_priv(dev);
400 priv->timer.expires = 0; /* mark inactive timer */
401 can_restart_now(dev);
404 void can_bus_off(struct net_device *dev)
406 struct can_priv *priv = netdev_priv(dev);
408 netif_carrier_off(dev);
410 if (priv->restart_ms > 0 && !priv->timer.expires) {
412 priv->timer.function = can_restart_after;
413 priv->timer.data = (unsigned long)dev;
414 priv->timer.expires =
415 jiffies + (priv->restart_ms * HZ) / 1000;
416 add_timer(&priv->timer);
419 EXPORT_SYMBOL(can_bus_off);
421 void can_close_cleanup(struct net_device *dev)
423 struct can_priv *priv = netdev_priv(dev);
425 if (priv->timer.expires) {
426 del_timer(&priv->timer);
427 priv->timer.expires = 0;
430 can_flush_echo_skb(dev);
432 EXPORT_SYMBOL(can_close_cleanup);
434 static int can_netdev_notifier_call(struct notifier_block *nb,
438 struct net_device *dev = ndev;
439 struct can_priv *priv;
441 if (dev->type != ARPHRD_CAN)
444 priv = netdev_priv(dev);
446 /* software CAN devices like 'vcan' do not have private data */
451 case NETDEV_REGISTER:
452 /* set default bit timing */
453 if (priv->do_set_bittime &&
454 priv->bitrate == CAN_BITRATE_UNCONFIGURED) {
455 if (can_set_bitrate(dev, CAN_BITRATE_DEFAULT))
456 dev_err(ND2D(dev), "failed to set bitrate\n");
459 can_create_sysfs(dev);
462 case NETDEV_UNREGISTER:
464 can_remove_sysfs(dev);
471 static struct notifier_block can_netdev_notifier = {
472 .notifier_call = can_netdev_notifier_call,
475 static __init int can_dev_init(void)
477 printk(KERN_INFO MOD_DESC " (rev " MOD_REV ")\n");
479 return register_netdevice_notifier(&can_netdev_notifier);
481 module_init(can_dev_init);
483 static __exit void can_dev_exit(void)
485 unregister_netdevice_notifier(&can_netdev_notifier);
487 module_exit(can_dev_exit);