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"
32 MODULE_DESCRIPTION(MOD_DESC);
33 MODULE_LICENSE("GPL v2");
34 MODULE_AUTHOR("Marc Kleine-Budde <mkl@pengutronix.de>, "
35 "Andrey Volkov <avolkov@varma-el.com>");
37 static int restart_ms;
39 module_param(restart_ms, int, S_IRUGO | S_IWUSR);
41 MODULE_PARM_DESC(restart_ms, "Restart time after bus-off in ms (default 0)");
46 * Bit rate calculated with next formula:
47 * bitrate = frq/(brp*(1 + prop_seg+ phase_seg1 + phase_seg2))
49 * This calc function based on work of Florian Hartwich and Armin Bassemi
50 * "The Configuration of the CAN Bit Timing"
51 * (http://www.semiconductors.bosch.de/pdf/CiA99Paper.pdf)
55 * bittime_nsec - expected bit time in nanosecs
58 * bittime - calculated time segments, for meaning of
59 * each field read CAN standard.
62 #define DEFAULT_MAX_BRP 64U
63 #define DEFAULT_MAX_SJW 4U
65 /* All below values in tq units */
66 #define MAX_BITTIME 25U
67 #define MIN_BITTIME 8U
68 #define MAX_PROP_SEG 8U
69 #define MAX_PHASE_SEG1 8U
70 #define MAX_PHASE_SEG2 8U
72 int can_calc_bittime(struct can_priv *can, u32 bitrate,
73 struct can_bittime_std *bittime)
75 int best_error = -1; /* Ariphmetic error */
76 int df, best_df = -1; /* oscillator's tolerance range,
78 u32 quanta; /* in tq units */
79 u32 brp, phase_seg1, phase_seg2, sjw, prop_seg;
80 u32 brp_min, brp_max, brp_expected;
83 /* bitrate range [1baud,1MiB/s] */
84 if (bitrate == 0 || bitrate > 1000000UL)
87 tmp = (u64) can->can_sys_clock * 1000;
89 brp_expected = (u32) tmp;
91 brp_min = brp_expected / (1000 * MAX_BITTIME);
94 if (brp_min > can->max_brp)
97 brp_max = (brp_expected + 500 * MIN_BITTIME) / (1000 * MIN_BITTIME);
100 if (brp_max > can->max_brp)
101 brp_max = can->max_brp;
103 for (brp = brp_min; brp <= brp_max; brp++) {
104 quanta = brp_expected / (brp * 1000);
105 if (quanta < MAX_BITTIME
106 && quanta * brp * 1000 != brp_expected)
108 if (quanta < MIN_BITTIME || quanta > MAX_BITTIME)
111 phase_seg2 = min((quanta - 3) / 2, MAX_PHASE_SEG2);
112 for (sjw = can->max_sjw; sjw > 0; sjw--) {
113 for (; phase_seg2 > sjw; phase_seg2--) {
116 phase_seg2 % 2 ? phase_seg2 -
118 prop_seg = quanta - 1 - phase_seg2 - phase_seg1;
120 * FIXME: support of longer lines (i.e. bigger
121 * prop_seg) is more prefered than support of
122 * cheap oscillators (i.e. bigger
123 * df/phase_seg1/phase_seg2)
125 if (prop_seg < phase_seg1)
127 if (prop_seg > MAX_PROP_SEG)
130 err1 = phase_seg1 * brp * 500 * 1000 /
132 phase_seg2 * brp * 1000);
133 err2 = sjw * brp * 50 * 1000 / brp_expected;
135 df = min(err1, err2);
138 abs(brp_expected * 10 /
139 (brp * (1 + prop_seg +
144 if (error > 10 || error > best_error)
147 if (error == best_error
148 && prop_seg < bittime->prop_seg)
154 bittime->prop_seg = prop_seg;
155 bittime->phase_seg1 = phase_seg1;
156 bittime->phase_seg2 = phase_seg2;
159 (bittime->phase_seg1 > 3);
171 int can_set_bitrate(struct net_device *dev, u32 bitrate)
173 struct can_priv *priv = netdev_priv(dev);
176 if (priv->state != CAN_STATE_STOPPED)
179 if (priv->do_set_bittime) {
180 if (priv->do_set_bittime) {
181 struct can_bittime bittime;
182 err = can_calc_bittime(priv, bitrate, &bittime.std);
185 bittime.type = CAN_BITTIME_STD;
186 err = priv->do_set_bittime(dev, &bittime);
188 priv->bitrate = bitrate;
189 priv->bittime = bittime;
196 EXPORT_SYMBOL(can_set_bitrate);
198 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
199 static struct net_device_stats *can_get_stats(struct net_device *dev)
201 struct can_priv *priv = netdev_priv(dev);
203 return &priv->net_stats;
207 static void can_setup(struct net_device *dev)
209 dev->type = ARPHRD_CAN;
210 dev->mtu = sizeof(struct can_frame);
211 dev->hard_header_len = 0;
213 dev->tx_queue_len = 10;
215 /* New-style flags. */
216 dev->flags = IFF_NOARP;
217 dev->features = NETIF_F_NO_CSUM;
218 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
219 dev->get_stats = can_get_stats;
224 * Function alloc_candev
225 * Allocates and sets up an CAN device
227 struct net_device *alloc_candev(int sizeof_priv)
229 struct net_device *dev;
230 struct can_priv *priv;
232 dev = alloc_netdev(sizeof_priv, "can%d", can_setup);
236 priv = netdev_priv(dev);
238 /* Default values can be overwritten by the device driver */
239 priv->restart_ms = restart_ms;
240 priv->bitrate = CAN_BITRATE_UNCONFIGURED;
241 priv->state = CAN_STATE_STOPPED;
242 priv->max_brp = DEFAULT_MAX_BRP;
243 priv->max_sjw = DEFAULT_MAX_SJW;
244 spin_lock_init(&priv->irq_lock);
246 init_timer(&priv->timer);
247 priv->timer.expires = 0;
251 EXPORT_SYMBOL(alloc_candev);
253 void free_candev(struct net_device *dev)
257 EXPORT_SYMBOL(free_candev);
260 * Local echo of CAN messages
262 * CAN network devices *should* support a local echo functionality
263 * (see Documentation/networking/can.txt). To test the handling of CAN
264 * interfaces that do not support the local echo both driver types are
265 * implemented. In the case that the driver does not support the echo
266 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
267 * to perform the echo as a fallback solution.
270 void can_flush_echo_skb(struct net_device *dev)
272 struct can_priv *priv = netdev_priv(dev);
274 struct net_device_stats *stats = dev->get_stats(dev);
278 for (i = 0; i < CAN_ECHO_SKB_MAX; i++) {
279 if (priv->echo_skb[i]) {
280 kfree_skb(priv->echo_skb[i]);
281 priv->echo_skb[i] = NULL;
284 stats->tx_aborted_errors++;
290 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx)
292 struct can_priv *priv = netdev_priv(dev);
294 /* set flag whether this packet has to be looped back */
295 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
300 if (!priv->echo_skb[idx]) {
301 struct sock *srcsk = skb->sk;
303 if (atomic_read(&skb->users) != 1) {
304 struct sk_buff *old_skb = skb;
306 skb = skb_clone(old_skb, GFP_ATOMIC);
315 /* make settings for echo to reduce code in irq context */
316 skb->protocol = htons(ETH_P_CAN);
317 skb->pkt_type = PACKET_BROADCAST;
318 skb->ip_summed = CHECKSUM_UNNECESSARY;
321 /* save this skb for tx interrupt echo handling */
322 priv->echo_skb[idx] = skb;
324 /* locking problem with netif_stop_queue() ?? */
325 printk(KERN_ERR "%s: %s: BUG! echo_skb is occupied!\n",
326 dev->name, __func__);
330 EXPORT_SYMBOL(can_put_echo_skb);
332 void can_get_echo_skb(struct net_device *dev, int idx)
334 struct can_priv *priv = netdev_priv(dev);
336 if ((dev->flags & IFF_ECHO) && priv->echo_skb[idx]) {
337 netif_rx(priv->echo_skb[idx]);
338 priv->echo_skb[idx] = NULL;
341 EXPORT_SYMBOL(can_get_echo_skb);
344 * CAN bus-off handling
345 * FIXME: we need some synchronization
347 int can_restart_now(struct net_device *dev)
349 struct can_priv *priv = netdev_priv(dev);
350 struct net_device_stats *stats = dev->get_stats(dev);
352 struct can_frame *cf;
355 if (netif_carrier_ok(dev))
356 netif_carrier_off(dev);
358 /* Cancel restart in progress */
359 if (priv->timer.expires) {
360 del_timer(&priv->timer);
361 priv->timer.expires = 0; /* mark inactive timer */
364 can_flush_echo_skb(dev);
366 err = priv->do_set_mode(dev, CAN_MODE_START);
370 netif_carrier_on(dev);
372 priv->can_stats.restarts++;
374 /* send restart message upstream */
375 skb = dev_alloc_skb(sizeof(struct can_frame));
379 skb->protocol = htons(ETH_P_CAN);
380 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
381 memset(cf, 0, sizeof(struct can_frame));
382 cf->can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED;
383 cf->can_dlc = CAN_ERR_DLC;
387 dev->last_rx = jiffies;
389 stats->rx_bytes += cf->can_dlc;
394 static void can_restart_after(unsigned long data)
396 struct net_device *dev = (struct net_device *)data;
397 struct can_priv *priv = netdev_priv(dev);
399 priv->timer.expires = 0; /* mark inactive timer */
400 can_restart_now(dev);
403 void can_bus_off(struct net_device *dev)
405 struct can_priv *priv = netdev_priv(dev);
407 netif_carrier_off(dev);
409 if (priv->restart_ms > 0 && !priv->timer.expires) {
411 priv->timer.function = can_restart_after;
412 priv->timer.data = (unsigned long)dev;
413 priv->timer.expires =
414 jiffies + (priv->restart_ms * HZ) / 1000;
415 add_timer(&priv->timer);
418 EXPORT_SYMBOL(can_bus_off);
420 void can_close_cleanup(struct net_device *dev)
422 struct can_priv *priv = netdev_priv(dev);
424 if (priv->timer.expires) {
425 del_timer(&priv->timer);
426 priv->timer.expires = 0;
429 can_flush_echo_skb(dev);
431 EXPORT_SYMBOL(can_close_cleanup);
433 static int can_netdev_notifier_call(struct notifier_block *nb,
437 struct net_device *dev = ndev;
438 struct can_priv *priv;
440 if (dev->type != ARPHRD_CAN)
443 priv = netdev_priv(dev);
445 /* software CAN devices like 'vcan' do not have private data */
450 case NETDEV_REGISTER:
451 /* set default bit timing */
452 if (priv->do_set_bittime &&
453 priv->bitrate == CAN_BITRATE_UNCONFIGURED) {
454 if (can_set_bitrate(dev, CAN_BITRATE_DEFAULT))
455 dev_err(ND2D(dev), "failed to set bitrate\n");
458 can_create_sysfs(dev);
461 case NETDEV_UNREGISTER:
463 can_remove_sysfs(dev);
470 static struct notifier_block can_netdev_notifier = {
471 .notifier_call = can_netdev_notifier_call,
474 static __init int can_dev_init(void)
476 printk(KERN_INFO MOD_DESC "\n");
478 return register_netdevice_notifier(&can_netdev_notifier);
480 module_init(can_dev_init);
482 static __exit void can_dev_exit(void)
484 unregister_netdevice_notifier(&can_netdev_notifier);
486 module_exit(can_dev_exit);