4 * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
5 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
6 * Copyright (C) 2008 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/netdevice.h>
24 #include <linux/if_arp.h>
25 #include <linux/can.h>
26 #include <linux/can/dev.h>
27 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
28 #include <net/rtnetlink.h>
33 #define MOD_DESC "CAN netdevice library"
35 MODULE_DESCRIPTION(MOD_DESC);
36 MODULE_LICENSE("GPL v2");
37 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
39 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
42 * Bit-timing calculation derived from:
44 * Code based on LinCAN sources and H8S2638 project
45 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
46 * Copyright 2005 Stanislav Marek
47 * email: pisa@cmp.felk.cvut.cz
49 static int can_update_spt(const struct can_bittiming_const *btc,
50 int sampl_pt, int tseg, int *tseg1, int *tseg2)
52 *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
53 if (*tseg2 < btc->tseg2_min)
54 *tseg2 = btc->tseg2_min;
55 if (*tseg2 > btc->tseg2_max)
56 *tseg2 = btc->tseg2_max;
57 *tseg1 = tseg - *tseg2;
58 if (*tseg1 > btc->tseg1_max) {
59 *tseg1 = btc->tseg1_max;
60 *tseg2 = tseg - *tseg1;
62 return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
65 static int can_calc_bittiming(struct net_device *dev)
67 struct can_priv *priv = netdev_priv(dev);
68 struct can_bittiming *bt = &priv->bittiming;
69 const struct can_bittiming_const *btc = priv->bittiming_const;
70 long rate, best_rate = 0;
71 long best_error = 1000000000, error = 0;
72 int best_tseg = 0, best_brp = 0, brp = 0;
73 int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
74 int spt_error = 1000, spt = 0, sampl_pt;
77 if (!priv->bittiming_const)
80 /* Use CIA recommended sample points */
81 if (bt->sample_point) {
82 sampl_pt = bt->sample_point;
84 if (bt->bitrate > 800000)
86 else if (bt->bitrate > 500000)
92 /* tseg even = round down, odd = round up */
93 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
94 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
95 tsegall = 1 + tseg / 2;
96 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
97 brp = bt->clock / (tsegall * bt->bitrate) + tseg % 2;
98 /* chose brp step which is possible in system */
99 brp = (brp / btc->brp_inc) * btc->brp_inc;
100 if ((brp < btc->brp_min) || (brp > btc->brp_max))
102 rate = bt->clock / (brp * tsegall);
103 error = bt->bitrate - rate;
104 /* tseg brp biterror */
107 if (error > best_error)
111 spt = can_update_spt(btc, sampl_pt, tseg / 2,
113 error = sampl_pt - spt;
116 if (error > spt_error)
120 best_tseg = tseg / 2;
128 /* Error in one-tenth of a percent */
129 error = (best_error * 1000) / bt->bitrate;
130 if (error > CAN_CALC_MAX_ERROR) {
131 dev_err(ND2D(dev), "bitrate error %ld.%ld%% too high\n",
132 error / 10, error % 10);
135 dev_warn(ND2D(dev), "bitrate error %ld.%ld%%\n",
136 error / 10, error % 10);
140 spt = can_update_spt(btc, sampl_pt, best_tseg, &tseg1, &tseg2);
142 v64 = (u64)best_brp * 1000000000UL;
143 do_div(v64, bt->clock);
145 bt->prop_seg = tseg1 / 2;
146 bt->phase_seg1 = tseg1 - bt->prop_seg;
147 bt->phase_seg2 = tseg2;
154 int can_sample_point(struct can_bittiming *bt)
156 return ((bt->prop_seg + bt->phase_seg1 + 1) * 1000) /
157 (bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1);
160 int can_fixup_bittiming(struct net_device *dev)
162 struct can_priv *priv = netdev_priv(dev);
163 struct can_bittiming *bt = &priv->bittiming;
164 const struct can_bittiming_const *btc = priv->bittiming_const;
169 if (!priv->bittiming_const)
172 tseg1 = bt->prop_seg + bt->phase_seg1;
173 if (bt->sjw > btc->sjw_max ||
174 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
175 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
178 brp64 = (u64)bt->clock * (u64)bt->tq;
179 if (btc->brp_inc > 1)
180 do_div(brp64, btc->brp_inc);
181 brp64 += 500000000UL - 1;
182 do_div(brp64, 1000000000UL); /* the practicable BRP */
183 if (btc->brp_inc > 1)
184 brp64 *= btc->brp_inc;
185 bt->brp = (u32)brp64;
187 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
190 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
191 bitrate = bt->clock / (bt->brp * alltseg);
192 bt->bitrate = bitrate;
197 int can_set_bittiming(struct net_device *dev)
199 struct can_priv *priv = netdev_priv(dev);
202 /* Check if bit-timing parameters have been pre-defined */
203 if (!priv->bittiming.tq && !priv->bittiming.bitrate)
206 /* Check if the CAN device has bit-timing parameters */
207 if (priv->bittiming_const) {
209 /* Check if bit-timing parameters have already been set */
210 if (priv->bittiming.tq && priv->bittiming.bitrate)
213 /* Non-expert mode? Check if the bitrate has been pre-defined */
214 if (!priv->bittiming.tq)
215 /* Determine bit-timing parameters */
216 err = can_calc_bittiming(dev);
218 /* Check bit-timing params and calculate proper brp */
219 err = can_fixup_bittiming(dev);
224 if (priv->do_set_bittiming) {
225 /* Finally, set the bit-timing registers */
226 err = priv->do_set_bittiming(dev);
233 EXPORT_SYMBOL(can_set_bittiming);
235 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
236 static struct net_device_stats *can_get_stats(struct net_device *dev)
238 struct can_priv *priv = netdev_priv(dev);
240 return &priv->net_stats;
244 static void can_setup(struct net_device *dev)
246 dev->type = ARPHRD_CAN;
247 dev->mtu = sizeof(struct can_frame);
248 dev->hard_header_len = 0;
250 dev->tx_queue_len = 10;
252 /* New-style flags. */
253 dev->flags = IFF_NOARP;
254 dev->features = NETIF_F_NO_CSUM;
255 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
256 dev->get_stats = can_get_stats;
261 * Function alloc_candev
262 * Allocates and sets up an CAN device
264 struct net_device *alloc_candev(int sizeof_priv)
266 struct net_device *dev;
267 struct can_priv *priv;
269 dev = alloc_netdev(sizeof_priv, "can%d", can_setup);
273 priv = netdev_priv(dev);
275 priv->state = CAN_STATE_STOPPED;
276 spin_lock_init(&priv->irq_lock);
278 init_timer(&priv->timer);
279 priv->timer.expires = 0;
283 EXPORT_SYMBOL(alloc_candev);
285 void free_candev(struct net_device *dev)
289 EXPORT_SYMBOL(free_candev);
292 * Local echo of CAN messages
294 * CAN network devices *should* support a local echo functionality
295 * (see Documentation/networking/can.txt). To test the handling of CAN
296 * interfaces that do not support the local echo both driver types are
297 * implemented. In the case that the driver does not support the echo
298 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
299 * to perform the echo as a fallback solution.
302 void can_flush_echo_skb(struct net_device *dev)
304 struct can_priv *priv = netdev_priv(dev);
306 struct net_device_stats *stats = dev->get_stats(dev);
310 for (i = 0; i < CAN_ECHO_SKB_MAX; i++) {
311 if (priv->echo_skb[i]) {
312 kfree_skb(priv->echo_skb[i]);
313 priv->echo_skb[i] = NULL;
316 stats->tx_aborted_errors++;
322 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx)
324 struct can_priv *priv = netdev_priv(dev);
326 /* set flag whether this packet has to be looped back */
327 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
332 if (!priv->echo_skb[idx]) {
333 struct sock *srcsk = skb->sk;
335 if (atomic_read(&skb->users) != 1) {
336 struct sk_buff *old_skb = skb;
338 skb = skb_clone(old_skb, GFP_ATOMIC);
347 /* make settings for echo to reduce code in irq context */
348 skb->protocol = htons(ETH_P_CAN);
349 skb->pkt_type = PACKET_BROADCAST;
350 skb->ip_summed = CHECKSUM_UNNECESSARY;
353 /* save this skb for tx interrupt echo handling */
354 priv->echo_skb[idx] = skb;
356 /* locking problem with netif_stop_queue() ?? */
357 printk(KERN_ERR "%s: %s: BUG! echo_skb is occupied!\n",
358 dev->name, __func__);
362 EXPORT_SYMBOL(can_put_echo_skb);
364 void can_get_echo_skb(struct net_device *dev, int idx)
366 struct can_priv *priv = netdev_priv(dev);
368 if ((dev->flags & IFF_ECHO) && priv->echo_skb[idx]) {
369 netif_rx(priv->echo_skb[idx]);
370 priv->echo_skb[idx] = NULL;
373 EXPORT_SYMBOL(can_get_echo_skb);
376 * CAN bus-off handling
377 * FIXME: we need some synchronization
379 int can_restart_now(struct net_device *dev)
381 struct can_priv *priv = netdev_priv(dev);
382 struct net_device_stats *stats = dev->get_stats(dev);
384 struct can_frame *cf;
387 if (netif_carrier_ok(dev))
388 netif_carrier_off(dev);
390 /* Cancel restart in progress */
391 if (priv->timer.expires) {
392 del_timer(&priv->timer);
393 priv->timer.expires = 0; /* mark inactive timer */
396 can_flush_echo_skb(dev);
398 err = priv->do_set_mode(dev, CAN_MODE_START);
402 netif_carrier_on(dev);
404 priv->can_stats.restarts++;
406 /* send restart message upstream */
407 skb = dev_alloc_skb(sizeof(struct can_frame));
411 skb->protocol = htons(ETH_P_CAN);
412 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
413 memset(cf, 0, sizeof(struct can_frame));
414 cf->can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED;
415 cf->can_dlc = CAN_ERR_DLC;
419 dev->last_rx = jiffies;
421 stats->rx_bytes += cf->can_dlc;
426 static void can_restart_after(unsigned long data)
428 struct net_device *dev = (struct net_device *)data;
429 struct can_priv *priv = netdev_priv(dev);
431 priv->timer.expires = 0; /* mark inactive timer */
432 can_restart_now(dev);
435 void can_bus_off(struct net_device *dev)
437 struct can_priv *priv = netdev_priv(dev);
439 netif_carrier_off(dev);
441 if (priv->restart_ms > 0 && !priv->timer.expires) {
443 priv->timer.function = can_restart_after;
444 priv->timer.data = (unsigned long)dev;
445 priv->timer.expires =
446 jiffies + (priv->restart_ms * HZ) / 1000;
447 add_timer(&priv->timer);
450 EXPORT_SYMBOL(can_bus_off);
452 void can_close_cleanup(struct net_device *dev)
454 struct can_priv *priv = netdev_priv(dev);
456 if (priv->timer.expires) {
457 del_timer(&priv->timer);
458 priv->timer.expires = 0;
461 can_flush_echo_skb(dev);
463 EXPORT_SYMBOL(can_close_cleanup);
465 static int can_netdev_notifier_call(struct notifier_block *nb,
469 struct net_device *dev = ndev;
471 if (dev->type != ARPHRD_CAN)
474 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
475 /* omit virtual CAN software network devices */
476 if (dev->rtnl_link_ops) {
477 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
478 if (!strcmp(ops->kind, "vcan"))
482 /* software CAN devices like 'vcan' do not have private data */
488 case NETDEV_REGISTER:
490 can_create_sysfs(dev);
493 case NETDEV_UNREGISTER:
495 can_remove_sysfs(dev);
502 static struct notifier_block can_netdev_notifier = {
503 .notifier_call = can_netdev_notifier_call,
506 static __init int can_dev_init(void)
508 printk(KERN_INFO MOD_DESC "\n");
510 return register_netdevice_notifier(&can_netdev_notifier);
512 module_init(can_dev_init);
514 static __exit void can_dev_exit(void)
516 unregister_netdevice_notifier(&can_netdev_notifier);
518 module_exit(can_dev_exit);