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;
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;
249 EXPORT_SYMBOL(alloc_candev);
251 void free_candev(struct net_device *dev)
255 EXPORT_SYMBOL(free_candev);
258 * Local echo of CAN messages
260 * CAN network devices *should* support a local echo functionality
261 * (see Documentation/networking/can.txt). To test the handling of CAN
262 * interfaces that do not support the local echo both driver types are
263 * implemented. In the case that the driver does not support the echo
264 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
265 * to perform the echo as a fallback solution.
268 void can_flush_echo_skb(struct net_device *dev)
270 struct can_priv *priv = netdev_priv(dev);
272 struct net_device_stats *stats = dev->get_stats(dev);
276 for (i = 0; i < CAN_ECHO_SKB_MAX; i++) {
277 if (priv->echo_skb[i]) {
278 kfree_skb(priv->echo_skb[i]);
279 priv->echo_skb[i] = NULL;
282 stats->tx_aborted_errors++;
288 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx)
290 struct can_priv *priv = netdev_priv(dev);
292 /* set flag whether this packet has to be looped back */
293 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
298 if (!priv->echo_skb[idx]) {
299 struct sock *srcsk = skb->sk;
301 if (atomic_read(&skb->users) != 1) {
302 struct sk_buff *old_skb = skb;
304 skb = skb_clone(old_skb, GFP_ATOMIC);
313 /* make settings for echo to reduce code in irq context */
314 skb->protocol = htons(ETH_P_CAN);
315 skb->pkt_type = PACKET_BROADCAST;
316 skb->ip_summed = CHECKSUM_UNNECESSARY;
319 /* save this skb for tx interrupt echo handling */
320 priv->echo_skb[idx] = skb;
322 /* locking problem with netif_stop_queue() ?? */
323 printk(KERN_ERR "%s: %s: BUG! echo_skb is occupied!\n",
324 dev->name, __func__);
328 EXPORT_SYMBOL(can_put_echo_skb);
330 void can_get_echo_skb(struct net_device *dev, int idx)
332 struct can_priv *priv = netdev_priv(dev);
334 if ((dev->flags & IFF_ECHO) && priv->echo_skb[idx]) {
335 netif_rx(priv->echo_skb[idx]);
336 priv->echo_skb[idx] = NULL;
339 EXPORT_SYMBOL(can_get_echo_skb);
342 * CAN bus-off handling
343 * FIXME: we need some synchronization
345 int can_restart_now(struct net_device *dev)
347 struct can_priv *priv = netdev_priv(dev);
348 struct net_device_stats *stats = dev->get_stats(dev);
350 struct can_frame *cf;
353 if (netif_carrier_ok(dev))
354 netif_carrier_off(dev);
356 /* Cancel restart in progress */
357 if (priv->timer.expires) {
358 del_timer(&priv->timer);
359 priv->timer.expires = 0; /* mark inactive timer */
362 can_flush_echo_skb(dev);
364 err = priv->do_set_mode(dev, CAN_MODE_START);
368 netif_carrier_on(dev);
370 priv->can_stats.restarts++;
372 /* send restart message upstream */
373 skb = dev_alloc_skb(sizeof(struct can_frame));
377 skb->protocol = htons(ETH_P_CAN);
378 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
379 memset(cf, 0, sizeof(struct can_frame));
380 cf->can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED;
381 cf->can_dlc = CAN_ERR_DLC;
385 dev->last_rx = jiffies;
387 stats->rx_bytes += cf->can_dlc;
392 static void can_restart_after(unsigned long data)
394 struct net_device *dev = (struct net_device *)data;
395 struct can_priv *priv = netdev_priv(dev);
397 priv->timer.expires = 0; /* mark inactive timer */
398 can_restart_now(dev);
401 void can_bus_off(struct net_device *dev)
403 struct can_priv *priv = netdev_priv(dev);
405 netif_carrier_off(dev);
407 if (priv->restart_ms > 0 && !priv->timer.expires) {
409 priv->timer.function = can_restart_after;
410 priv->timer.data = (unsigned long)dev;
411 priv->timer.expires =
412 jiffies + (priv->restart_ms * HZ) / 1000;
413 add_timer(&priv->timer);
416 EXPORT_SYMBOL(can_bus_off);
418 void can_close_cleanup(struct net_device *dev)
420 struct can_priv *priv = netdev_priv(dev);
422 if (priv->timer.expires) {
423 del_timer(&priv->timer);
424 priv->timer.expires = 0;
427 can_flush_echo_skb(dev);
429 EXPORT_SYMBOL(can_close_cleanup);
431 static int can_netdev_notifier_call(struct notifier_block *nb,
435 struct net_device *dev = ndev;
436 struct can_priv *priv;
438 if (dev->type != ARPHRD_CAN)
441 priv = netdev_priv(dev);
443 /* software CAN devices like 'vcan' do not have private data */
448 case NETDEV_REGISTER:
449 /* set default bit timing */
450 if (priv->do_set_bittime &&
451 priv->bitrate == CAN_BITRATE_UNCONFIGURED) {
452 if (can_set_bitrate(dev, CAN_BITRATE_DEFAULT))
453 dev_err(ND2D(dev), "failed to set bitrate\n");
456 can_create_sysfs(dev);
459 case NETDEV_UNREGISTER:
461 can_remove_sysfs(dev);
468 static struct notifier_block can_netdev_notifier = {
469 .notifier_call = can_netdev_notifier_call,
472 static __init int can_dev_init(void)
474 return register_netdevice_notifier(&can_netdev_notifier);
476 module_init(can_dev_init);
478 static __exit void can_dev_exit(void)
480 unregister_netdevice_notifier(&can_netdev_notifier);
482 module_exit(can_dev_exit);