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 MODULE_DESCRIPTION("CAN netdevice library");
31 MODULE_LICENSE("GPL v2");
32 MODULE_AUTHOR("Marc Kleine-Budde <mkl@pengutronix.de>, "
33 "Andrey Volkov <avolkov@varma-el.com>");
35 static int restart_ms = 0;
37 module_param(restart_ms, int, S_IRUGO | S_IWUSR);
39 MODULE_PARM_DESC(restart_ms, "Restart time after bus-off in ms (default 0)");
44 * Bit rate calculated with next formula:
45 * bitrate = frq/(brp*(1 + prop_seg+ phase_seg1 + phase_seg2))
47 * This calc function based on work of Florian Hartwich and Armin Bassemi
48 * "The Configuration of the CAN Bit Timing"
49 * (http://www.semiconductors.bosch.de/pdf/CiA99Paper.pdf)
53 * bittime_nsec - expected bit time in nanosecs
56 * bittime - calculated time segments, for meaning of
57 * each field read CAN standard.
60 #define DEFAULT_MAX_BRP 64U
61 #define DEFAULT_MAX_SJW 4U
63 /* All below values in tq units */
64 #define MAX_BITTIME 25U
65 #define MIN_BITTIME 8U
66 #define MAX_PROP_SEG 8U
67 #define MAX_PHASE_SEG1 8U
68 #define MAX_PHASE_SEG2 8U
70 int can_calc_bittime(struct can_priv *can, u32 bitrate,
71 struct can_bittime_std *bittime)
73 int best_error = -1; /* Ariphmetic error */
74 int df, best_df = -1; /* oscillator's tolerance range,
76 u32 quanta; /* in tq units */
77 u32 brp, phase_seg1, phase_seg2, sjw, prop_seg;
78 u32 brp_min, brp_max, brp_expected;
81 /* bitrate range [1baud,1MiB/s] */
82 if (bitrate == 0 || bitrate > 1000000UL)
85 tmp = (u64) can->can_sys_clock * 1000;
87 brp_expected = (u32) tmp;
89 brp_min = brp_expected / (1000 * MAX_BITTIME);
92 if (brp_min > can->max_brp)
95 brp_max = (brp_expected + 500 * MIN_BITTIME) / (1000 * MIN_BITTIME);
98 if (brp_max > can->max_brp)
99 brp_max = can->max_brp;
101 for (brp = brp_min; brp <= brp_max; brp++) {
102 quanta = brp_expected / (brp * 1000);
103 if (quanta < MAX_BITTIME
104 && quanta * brp * 1000 != brp_expected)
106 if (quanta < MIN_BITTIME || quanta > MAX_BITTIME)
109 phase_seg2 = min((quanta - 3) / 2, MAX_PHASE_SEG2);
110 for (sjw = can->max_sjw; sjw > 0; sjw--) {
111 for (; phase_seg2 > sjw; phase_seg2--) {
114 phase_seg2 % 2 ? phase_seg2 -
116 prop_seg = quanta - 1 - phase_seg2 - phase_seg1;
118 * FIXME: support of longer lines (i.e. bigger
119 * prop_seg) is more prefered than support of
120 * cheap oscillators (i.e. bigger
121 * df/phase_seg1/phase_seg2)
123 if (prop_seg < phase_seg1)
125 if (prop_seg > MAX_PROP_SEG)
128 err1 = phase_seg1 * brp * 500 * 1000 /
130 phase_seg2 * brp * 1000);
131 err2 = sjw * brp * 50 * 1000 / brp_expected;
133 df = min(err1, err2);
136 abs(brp_expected * 10 /
137 (brp * (1 + prop_seg +
142 if (error > 10 || error > best_error)
145 if (error == best_error
146 && prop_seg < bittime->prop_seg)
152 bittime->prop_seg = prop_seg;
153 bittime->phase_seg1 = phase_seg1;
154 bittime->phase_seg2 = phase_seg2;
157 (bittime->phase_seg1 > 3);
169 int can_set_bitrate(struct net_device *dev, u32 bitrate)
171 struct can_priv *priv = netdev_priv(dev);
174 if (priv->state != CAN_STATE_STOPPED)
177 if (priv->do_set_bittime) {
178 if (priv->do_set_bittime) {
179 struct can_bittime bittime;
180 err = can_calc_bittime(priv, bitrate, &bittime.std);
183 bittime.type = CAN_BITTIME_STD;
184 err = priv->do_set_bittime(dev, &bittime);
186 priv->bitrate = bitrate;
187 priv->bittime = bittime;
194 EXPORT_SYMBOL(can_set_bitrate);
196 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
197 static struct net_device_stats *can_get_stats(struct net_device *dev)
199 struct can_priv *priv = netdev_priv(dev);
201 return &priv->net_stats;
205 static void can_setup(struct net_device *dev)
207 dev->type = ARPHRD_CAN;
208 dev->mtu = sizeof(struct can_frame);
209 dev->hard_header_len = 0;
211 dev->tx_queue_len = 10;
213 /* New-style flags. */
214 dev->flags = IFF_NOARP;
215 dev->features = NETIF_F_NO_CSUM;
216 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
217 dev->get_stats = can_get_stats;
222 * Function alloc_candev
223 * Allocates and sets up an CAN device
225 struct net_device *alloc_candev(int sizeof_priv)
227 struct net_device *dev;
228 struct can_priv *priv;
230 dev = alloc_netdev(sizeof_priv, "can%d", can_setup);
234 priv = netdev_priv(dev);
236 /* Default values can be overwritten by the device driver */
237 priv->restart_ms = restart_ms;
238 priv->bitrate = CAN_BITRATE_UNCONFIGURED;
239 priv->state = CAN_STATE_STOPPED;
240 priv->max_brp = DEFAULT_MAX_BRP;
241 priv->max_sjw = DEFAULT_MAX_SJW;
242 spin_lock_init(&priv->irq_lock);
244 init_timer(&priv->timer);
245 priv->timer.expires = 0;
250 EXPORT_SYMBOL(alloc_candev);
252 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);
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, __FUNCTION__);
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 if ((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 return register_netdevice_notifier(&can_netdev_notifier);
478 module_init(can_dev_init);
480 static __exit void can_dev_exit(void)
482 unregister_netdevice_notifier(&can_netdev_notifier);
484 module_exit(can_dev_exit);