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/rtnetlink.h>
26 #include <linux/can.h>
27 #include <linux/can/dev.h>
31 #define MOD_DESC "CAN netdevice library"
33 MODULE_DESCRIPTION(MOD_DESC);
34 MODULE_LICENSE("GPL v2");
35 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
38 * Bit-timing calculation derived from:
40 * Code based on LinCAN sources and H8S2638 project
41 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
42 * Copyright 2005 Stanislav Marek
43 * email: pisa@cmp.felk.cvut.cz
45 static int can_update_spt(const struct can_bittiming_const *btc,
46 int sampl_pt, int tseg, int *tseg1, int *tseg2)
48 *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
49 if (*tseg2 < btc->tseg2_min)
50 *tseg2 = btc->tseg2_min;
51 if (*tseg2 > btc->tseg2_max)
52 *tseg2 = btc->tseg2_max;
53 *tseg1 = tseg - *tseg2;
54 if (*tseg1 > btc->tseg1_max) {
55 *tseg1 = btc->tseg1_max;
56 *tseg2 = tseg - *tseg1;
58 return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
61 static int can_calc_bittiming(struct net_device *dev)
63 struct can_priv *priv = netdev_priv(dev);
64 struct can_bittiming *bt = &priv->bittiming;
65 const struct can_bittiming_const *btc = priv->bittiming_const;
66 long rate, best_rate = 0;
67 long best_error = 1000000000, error = 0;
68 int best_tseg = 0, best_brp = 0, brp = 0;
69 int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
70 int spt_error = 1000, spt = 0, sampl_pt;
73 if (!priv->bittiming_const)
76 /* Use CIA recommended sample points */
77 if (bt->sample_point) {
78 sampl_pt = bt->sample_point;
80 if (bt->bitrate > 800000)
82 else if (bt->bitrate > 500000)
88 /* tseg even = round down, odd = round up */
89 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
90 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
91 tsegall = 1 + tseg / 2;
92 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
93 brp = bt->clock / (tsegall * bt->bitrate) + tseg % 2;
94 /* chose brp step which is possible in system */
95 brp = (brp / btc->brp_inc) * btc->brp_inc;
96 if ((brp < btc->brp_min) || (brp > btc->brp_max))
98 rate = bt->clock / (brp * tsegall);
99 error = bt->bitrate - rate;
100 /* tseg brp biterror */
103 if (error > best_error)
107 spt = can_update_spt(btc, sampl_pt, tseg / 2,
109 error = sampl_pt - spt;
112 if (error > spt_error)
116 best_tseg = tseg / 2;
124 spt = can_update_spt(btc, sampl_pt, best_tseg, &tseg1, &tseg2);
126 v64 = (u64)best_brp * 1000000000UL;
127 do_div(v64, bt->clock);
130 bt->phase_seg1 = tseg1;
131 bt->phase_seg2 = tseg2;
136 error = best_error * 1000;
137 error /= bt->bitrate;
138 dev_warn(ND2D(dev), "bitrate error %ld.%ld%%\n",
139 error / 10, error % 10);
145 int can_sample_point(struct can_bittiming *bt)
147 return ((bt->prop_seg + bt->phase_seg1 + 1) * 1000) /
148 (bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1);
151 int can_fixup_bittiming(struct net_device *dev)
153 struct can_priv *priv = netdev_priv(dev);
154 struct can_bittiming *bt = &priv->bittiming;
155 const struct can_bittiming_const *btc = priv->bittiming_const;
160 if (!priv->bittiming_const)
163 tseg1 = bt->prop_seg + bt->phase_seg1;
164 if (bt->sjw > btc->sjw_max ||
165 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
166 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
169 brp64 = (u64)bt->clock * (u64)bt->tq;
170 if (btc->brp_inc > 1)
171 do_div(brp64, btc->brp_inc);
172 brp64 += 500000000UL - 1;
173 do_div(brp64, 1000000000UL); /* the practicable BRP */
174 if (btc->brp_inc > 1)
175 brp64 *= btc->brp_inc;
176 bt->brp = (u32)brp64;
178 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
181 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
182 bitrate = bt->clock / (bt->brp * alltseg);
183 bt->bitrate = bitrate;
188 int can_set_bittiming(struct net_device *dev)
190 struct can_priv *priv = netdev_priv(dev);
193 /* Check if bit-timing parameters have been pre-defined */
194 if (!priv->bittiming.tq && !priv->bittiming.bitrate)
197 /* Check if the CAN device has bit-timing parameters */
198 if (priv->bittiming_const) {
200 /* Check if bit-timing parameters have already been set */
201 if (priv->bittiming.tq && priv->bittiming.bitrate)
204 /* Non-expert mode? Check if the bitrate has been pre-defined */
205 if (!priv->bittiming.tq)
206 /* Determine bit-timing parameters */
207 err = can_calc_bittiming(dev);
209 /* Check bit-timing params and calculate proper brp */
210 err = can_fixup_bittiming(dev);
215 if (priv->do_set_bittiming) {
216 /* Finally, set the bit-timing registers */
217 err = priv->do_set_bittiming(dev);
224 EXPORT_SYMBOL(can_set_bittiming);
226 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
227 static struct net_device_stats *can_get_stats(struct net_device *dev)
229 struct can_priv *priv = netdev_priv(dev);
231 return &priv->net_stats;
235 static void can_setup(struct net_device *dev)
237 dev->type = ARPHRD_CAN;
238 dev->mtu = sizeof(struct can_frame);
239 dev->hard_header_len = 0;
241 dev->tx_queue_len = 10;
243 /* New-style flags. */
244 dev->flags = IFF_NOARP;
245 dev->features = NETIF_F_NO_CSUM;
246 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
247 dev->get_stats = can_get_stats;
252 * Function alloc_candev
253 * Allocates and sets up an CAN device
255 struct net_device *alloc_candev(int sizeof_priv)
257 struct net_device *dev;
258 struct can_priv *priv;
260 dev = alloc_netdev(sizeof_priv, "can%d", can_setup);
264 priv = netdev_priv(dev);
266 priv->state = CAN_STATE_STOPPED;
267 spin_lock_init(&priv->irq_lock);
269 init_timer(&priv->timer);
270 priv->timer.expires = 0;
274 EXPORT_SYMBOL(alloc_candev);
276 void free_candev(struct net_device *dev)
280 EXPORT_SYMBOL(free_candev);
283 * Local echo of CAN messages
285 * CAN network devices *should* support a local echo functionality
286 * (see Documentation/networking/can.txt). To test the handling of CAN
287 * interfaces that do not support the local echo both driver types are
288 * implemented. In the case that the driver does not support the echo
289 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
290 * to perform the echo as a fallback solution.
293 void can_flush_echo_skb(struct net_device *dev)
295 struct can_priv *priv = netdev_priv(dev);
297 struct net_device_stats *stats = dev->get_stats(dev);
301 for (i = 0; i < CAN_ECHO_SKB_MAX; i++) {
302 if (priv->echo_skb[i]) {
303 kfree_skb(priv->echo_skb[i]);
304 priv->echo_skb[i] = NULL;
307 stats->tx_aborted_errors++;
313 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx)
315 struct can_priv *priv = netdev_priv(dev);
317 /* set flag whether this packet has to be looped back */
318 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
323 if (!priv->echo_skb[idx]) {
324 struct sock *srcsk = skb->sk;
326 if (atomic_read(&skb->users) != 1) {
327 struct sk_buff *old_skb = skb;
329 skb = skb_clone(old_skb, GFP_ATOMIC);
338 /* make settings for echo to reduce code in irq context */
339 skb->protocol = htons(ETH_P_CAN);
340 skb->pkt_type = PACKET_BROADCAST;
341 skb->ip_summed = CHECKSUM_UNNECESSARY;
344 /* save this skb for tx interrupt echo handling */
345 priv->echo_skb[idx] = skb;
347 /* locking problem with netif_stop_queue() ?? */
348 printk(KERN_ERR "%s: %s: BUG! echo_skb is occupied!\n",
349 dev->name, __func__);
353 EXPORT_SYMBOL(can_put_echo_skb);
355 void can_get_echo_skb(struct net_device *dev, int idx)
357 struct can_priv *priv = netdev_priv(dev);
359 if ((dev->flags & IFF_ECHO) && priv->echo_skb[idx]) {
360 netif_rx(priv->echo_skb[idx]);
361 priv->echo_skb[idx] = NULL;
364 EXPORT_SYMBOL(can_get_echo_skb);
367 * CAN bus-off handling
368 * FIXME: we need some synchronization
370 int can_restart_now(struct net_device *dev)
372 struct can_priv *priv = netdev_priv(dev);
373 struct net_device_stats *stats = dev->get_stats(dev);
375 struct can_frame *cf;
378 if (netif_carrier_ok(dev))
379 netif_carrier_off(dev);
381 /* Cancel restart in progress */
382 if (priv->timer.expires) {
383 del_timer(&priv->timer);
384 priv->timer.expires = 0; /* mark inactive timer */
387 can_flush_echo_skb(dev);
389 err = priv->do_set_mode(dev, CAN_MODE_START);
393 netif_carrier_on(dev);
395 priv->can_stats.restarts++;
397 /* send restart message upstream */
398 skb = dev_alloc_skb(sizeof(struct can_frame));
402 skb->protocol = htons(ETH_P_CAN);
403 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
404 memset(cf, 0, sizeof(struct can_frame));
405 cf->can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED;
406 cf->can_dlc = CAN_ERR_DLC;
410 dev->last_rx = jiffies;
412 stats->rx_bytes += cf->can_dlc;
417 static void can_restart_after(unsigned long data)
419 struct net_device *dev = (struct net_device *)data;
420 struct can_priv *priv = netdev_priv(dev);
422 priv->timer.expires = 0; /* mark inactive timer */
423 can_restart_now(dev);
426 void can_bus_off(struct net_device *dev)
428 struct can_priv *priv = netdev_priv(dev);
430 netif_carrier_off(dev);
432 if (priv->restart_ms > 0 && !priv->timer.expires) {
434 priv->timer.function = can_restart_after;
435 priv->timer.data = (unsigned long)dev;
436 priv->timer.expires =
437 jiffies + (priv->restart_ms * HZ) / 1000;
438 add_timer(&priv->timer);
441 EXPORT_SYMBOL(can_bus_off);
443 void can_close_cleanup(struct net_device *dev)
445 struct can_priv *priv = netdev_priv(dev);
447 if (priv->timer.expires) {
448 del_timer(&priv->timer);
449 priv->timer.expires = 0;
452 can_flush_echo_skb(dev);
454 EXPORT_SYMBOL(can_close_cleanup);
456 static int can_netdev_notifier_call(struct notifier_block *nb,
460 struct net_device *dev = ndev;
461 struct can_priv *priv;
463 if (dev->type != ARPHRD_CAN)
466 priv = netdev_priv(dev);
468 /* software CAN devices like 'vcan' do not have private data */
473 case NETDEV_REGISTER:
475 can_create_sysfs(dev);
478 case NETDEV_UNREGISTER:
480 can_remove_sysfs(dev);
487 static struct notifier_block can_netdev_notifier = {
488 .notifier_call = can_netdev_notifier_call,
491 static __init int can_dev_init(void)
493 printk(KERN_INFO MOD_DESC "\n");
495 return register_netdevice_notifier(&can_netdev_notifier);
497 module_init(can_dev_init);
499 static __exit void can_dev_exit(void)
501 unregister_netdevice_notifier(&can_netdev_notifier);
503 module_exit(can_dev_exit);