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 #ifdef CONFIG_CAN_CALC_BITTIMING
40 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
43 * Bit-timing calculation derived from:
45 * Code based on LinCAN sources and H8S2638 project
46 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
47 * Copyright 2005 Stanislav Marek
48 * email: pisa@cmp.felk.cvut.cz
50 static int can_update_spt(const struct can_bittiming_const *btc,
51 int sampl_pt, int tseg, int *tseg1, int *tseg2)
53 *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
54 if (*tseg2 < btc->tseg2_min)
55 *tseg2 = btc->tseg2_min;
56 if (*tseg2 > btc->tseg2_max)
57 *tseg2 = btc->tseg2_max;
58 *tseg1 = tseg - *tseg2;
59 if (*tseg1 > btc->tseg1_max) {
60 *tseg1 = btc->tseg1_max;
61 *tseg2 = tseg - *tseg1;
63 return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
66 static int can_calc_bittiming(struct net_device *dev)
68 struct can_priv *priv = netdev_priv(dev);
69 struct can_bittiming *bt = &priv->bittiming;
70 const struct can_bittiming_const *btc = priv->bittiming_const;
71 long rate, best_rate = 0;
72 long best_error = 1000000000, error = 0;
73 int best_tseg = 0, best_brp = 0, brp = 0;
74 int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
75 int spt_error = 1000, spt = 0, sampl_pt;
78 if (!priv->bittiming_const)
81 /* Use CIA recommended sample points */
82 if (bt->sample_point) {
83 sampl_pt = bt->sample_point;
85 if (bt->bitrate > 800000)
87 else if (bt->bitrate > 500000)
93 /* tseg even = round down, odd = round up */
94 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
95 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
96 tsegall = 1 + tseg / 2;
97 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
98 brp = bt->clock / (tsegall * bt->bitrate) + tseg % 2;
99 /* chose brp step which is possible in system */
100 brp = (brp / btc->brp_inc) * btc->brp_inc;
101 if ((brp < btc->brp_min) || (brp > btc->brp_max))
103 rate = bt->clock / (brp * tsegall);
104 error = bt->bitrate - rate;
105 /* tseg brp biterror */
108 if (error > best_error)
112 spt = can_update_spt(btc, sampl_pt, tseg / 2,
114 error = sampl_pt - spt;
117 if (error > spt_error)
121 best_tseg = tseg / 2;
129 /* Error in one-tenth of a percent */
130 error = (best_error * 1000) / bt->bitrate;
131 if (error > CAN_CALC_MAX_ERROR) {
132 dev_err(ND2D(dev), "bitrate error %ld.%ld%% too high\n",
133 error / 10, error % 10);
136 dev_warn(ND2D(dev), "bitrate error %ld.%ld%%\n",
137 error / 10, error % 10);
141 spt = can_update_spt(btc, sampl_pt, best_tseg, &tseg1, &tseg2);
143 v64 = (u64)best_brp * 1000000000UL;
144 do_div(v64, bt->clock);
146 bt->prop_seg = tseg1 / 2;
147 bt->phase_seg1 = tseg1 - bt->prop_seg;
148 bt->phase_seg2 = tseg2;
154 #else /* !CONFIG_CAN_CALC_BITTIMING */
155 static int can_calc_bittiming(struct net_device *dev)
159 #endif /* CONFIG_CAN_CALC_BITTIMING */
161 int can_sample_point(struct can_bittiming *bt)
163 return ((bt->prop_seg + bt->phase_seg1 + 1) * 1000) /
164 (bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1);
167 int can_fixup_bittiming(struct net_device *dev)
169 struct can_priv *priv = netdev_priv(dev);
170 struct can_bittiming *bt = &priv->bittiming;
171 const struct can_bittiming_const *btc = priv->bittiming_const;
176 if (!priv->bittiming_const)
179 tseg1 = bt->prop_seg + bt->phase_seg1;
180 if (bt->sjw > btc->sjw_max ||
181 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
182 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
185 brp64 = (u64)bt->clock * (u64)bt->tq;
186 if (btc->brp_inc > 1)
187 do_div(brp64, btc->brp_inc);
188 brp64 += 500000000UL - 1;
189 do_div(brp64, 1000000000UL); /* the practicable BRP */
190 if (btc->brp_inc > 1)
191 brp64 *= btc->brp_inc;
192 bt->brp = (u32)brp64;
194 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
197 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
198 bitrate = bt->clock / (bt->brp * alltseg);
199 bt->bitrate = bitrate;
204 int can_set_bittiming(struct net_device *dev)
206 struct can_priv *priv = netdev_priv(dev);
209 /* Check if bit-timing parameters have been pre-defined */
210 if (!priv->bittiming.tq && !priv->bittiming.bitrate)
213 /* Check if the CAN device has bit-timing parameters */
214 if (priv->bittiming_const) {
216 /* Check if bit-timing parameters have already been set */
217 if (priv->bittiming.tq && priv->bittiming.bitrate)
220 /* Non-expert mode? Check if the bitrate has been pre-defined */
221 if (!priv->bittiming.tq)
222 /* Determine bit-timing parameters */
223 err = can_calc_bittiming(dev);
225 /* Check bit-timing params and calculate proper brp */
226 err = can_fixup_bittiming(dev);
231 if (priv->do_set_bittiming) {
232 /* Finally, set the bit-timing registers */
233 err = priv->do_set_bittiming(dev);
240 EXPORT_SYMBOL(can_set_bittiming);
242 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
243 struct net_device_stats *can_get_stats(struct net_device *dev)
245 struct can_priv *priv = netdev_priv(dev);
247 return &priv->net_stats;
249 EXPORT_SYMBOL(can_get_stats);
252 static void can_setup(struct net_device *dev)
254 dev->type = ARPHRD_CAN;
255 dev->mtu = sizeof(struct can_frame);
256 dev->hard_header_len = 0;
258 dev->tx_queue_len = 10;
260 /* New-style flags. */
261 dev->flags = IFF_NOARP;
262 dev->features = NETIF_F_NO_CSUM;
263 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
264 dev->get_stats = can_get_stats;
269 * Function alloc_candev
270 * Allocates and sets up an CAN device
272 struct net_device *alloc_candev(int sizeof_priv)
274 struct net_device *dev;
275 struct can_priv *priv;
277 dev = alloc_netdev(sizeof_priv, "can%d", can_setup);
281 priv = netdev_priv(dev);
283 priv->state = CAN_STATE_STOPPED;
284 spin_lock_init(&priv->irq_lock);
286 init_timer(&priv->timer);
287 priv->timer.expires = 0;
291 EXPORT_SYMBOL(alloc_candev);
293 void free_candev(struct net_device *dev)
297 EXPORT_SYMBOL(free_candev);
299 int register_candev(struct net_device *dev)
303 err = register_netdev(dev);
308 can_create_sysfs(dev);
312 EXPORT_SYMBOL(register_candev);
314 void unregister_candev(struct net_device *dev)
317 can_remove_sysfs(dev);
319 unregister_netdev(dev);
321 EXPORT_SYMBOL(unregister_candev);
324 * Local echo of CAN messages
326 * CAN network devices *should* support a local echo functionality
327 * (see Documentation/networking/can.txt). To test the handling of CAN
328 * interfaces that do not support the local echo both driver types are
329 * implemented. In the case that the driver does not support the echo
330 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
331 * to perform the echo as a fallback solution.
334 void can_flush_echo_skb(struct net_device *dev)
336 struct can_priv *priv = netdev_priv(dev);
338 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
339 struct net_device_stats *stats = can_get_stats(dev);
341 struct net_device_stats *stats = &dev->stats;
346 for (i = 0; i < CAN_ECHO_SKB_MAX; i++) {
347 if (priv->echo_skb[i]) {
348 kfree_skb(priv->echo_skb[i]);
349 priv->echo_skb[i] = NULL;
352 stats->tx_aborted_errors++;
358 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx)
360 struct can_priv *priv = netdev_priv(dev);
362 /* set flag whether this packet has to be looped back */
363 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
368 if (!priv->echo_skb[idx]) {
369 struct sock *srcsk = skb->sk;
371 if (atomic_read(&skb->users) != 1) {
372 struct sk_buff *old_skb = skb;
374 skb = skb_clone(old_skb, GFP_ATOMIC);
383 /* make settings for echo to reduce code in irq context */
384 skb->protocol = htons(ETH_P_CAN);
385 skb->pkt_type = PACKET_BROADCAST;
386 skb->ip_summed = CHECKSUM_UNNECESSARY;
389 /* save this skb for tx interrupt echo handling */
390 priv->echo_skb[idx] = skb;
392 /* locking problem with netif_stop_queue() ?? */
393 printk(KERN_ERR "%s: %s: BUG! echo_skb is occupied!\n",
394 dev->name, __func__);
398 EXPORT_SYMBOL(can_put_echo_skb);
400 void can_get_echo_skb(struct net_device *dev, int idx)
402 struct can_priv *priv = netdev_priv(dev);
404 if ((dev->flags & IFF_ECHO) && priv->echo_skb[idx]) {
405 netif_rx(priv->echo_skb[idx]);
406 priv->echo_skb[idx] = NULL;
409 EXPORT_SYMBOL(can_get_echo_skb);
412 * CAN bus-off handling
413 * FIXME: we need some synchronization
415 int can_restart_now(struct net_device *dev)
417 struct can_priv *priv = netdev_priv(dev);
418 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
419 struct net_device_stats *stats = can_get_stats(dev);
421 struct net_device_stats *stats = &dev->stats;
424 struct can_frame *cf;
427 if (netif_carrier_ok(dev))
428 netif_carrier_off(dev);
430 /* Cancel restart in progress */
431 if (priv->timer.expires) {
432 del_timer(&priv->timer);
433 priv->timer.expires = 0; /* mark inactive timer */
436 can_flush_echo_skb(dev);
438 err = priv->do_set_mode(dev, CAN_MODE_START);
442 netif_carrier_on(dev);
444 priv->can_stats.restarts++;
446 /* send restart message upstream */
447 skb = dev_alloc_skb(sizeof(struct can_frame));
451 skb->protocol = htons(ETH_P_CAN);
452 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
453 memset(cf, 0, sizeof(struct can_frame));
454 cf->can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED;
455 cf->can_dlc = CAN_ERR_DLC;
459 dev->last_rx = jiffies;
461 stats->rx_bytes += cf->can_dlc;
466 static void can_restart_after(unsigned long data)
468 struct net_device *dev = (struct net_device *)data;
469 struct can_priv *priv = netdev_priv(dev);
471 priv->timer.expires = 0; /* mark inactive timer */
472 can_restart_now(dev);
475 void can_bus_off(struct net_device *dev)
477 struct can_priv *priv = netdev_priv(dev);
479 netif_carrier_off(dev);
481 if (priv->restart_ms > 0 && !priv->timer.expires) {
483 priv->timer.function = can_restart_after;
484 priv->timer.data = (unsigned long)dev;
485 priv->timer.expires =
486 jiffies + (priv->restart_ms * HZ) / 1000;
487 add_timer(&priv->timer);
490 EXPORT_SYMBOL(can_bus_off);
492 void can_close_cleanup(struct net_device *dev)
494 struct can_priv *priv = netdev_priv(dev);
496 if (priv->timer.expires) {
497 del_timer(&priv->timer);
498 priv->timer.expires = 0;
501 can_flush_echo_skb(dev);
503 EXPORT_SYMBOL(can_close_cleanup);
505 static __init int can_dev_init(void)
507 printk(KERN_INFO MOD_DESC "\n");
511 module_init(can_dev_init);
513 static __exit void can_dev_exit(void)
516 module_exit(can_dev_exit);