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 device driver interface"
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)
157 dev_err(ND2D(dev), "bit-timing calculation not available\n");
160 #endif /* CONFIG_CAN_CALC_BITTIMING */
162 int can_sample_point(struct can_bittiming *bt)
164 return ((bt->prop_seg + bt->phase_seg1 + 1) * 1000) /
165 (bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1);
168 int can_fixup_bittiming(struct net_device *dev)
170 struct can_priv *priv = netdev_priv(dev);
171 struct can_bittiming *bt = &priv->bittiming;
172 const struct can_bittiming_const *btc = priv->bittiming_const;
177 if (!priv->bittiming_const)
180 tseg1 = bt->prop_seg + bt->phase_seg1;
181 if (bt->sjw > btc->sjw_max ||
182 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
183 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
186 brp64 = (u64)bt->clock * (u64)bt->tq;
187 if (btc->brp_inc > 1)
188 do_div(brp64, btc->brp_inc);
189 brp64 += 500000000UL - 1;
190 do_div(brp64, 1000000000UL); /* the practicable BRP */
191 if (btc->brp_inc > 1)
192 brp64 *= btc->brp_inc;
193 bt->brp = (u32)brp64;
195 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
198 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
199 bitrate = bt->clock / (bt->brp * alltseg);
200 bt->bitrate = bitrate;
206 * Set CAN bit-timing for the device
208 * This functions should be called in the open function of the device
209 * driver to determine, check and set appropriate bit-timing parameters.
211 int can_set_bittiming(struct net_device *dev)
213 struct can_priv *priv = netdev_priv(dev);
216 /* Check if bit-timing parameters have been pre-defined */
217 if (!priv->bittiming.tq && !priv->bittiming.bitrate) {
218 dev_err(ND2D(dev), "bit-timing not yet defined\n");
222 /* Check if the CAN device has bit-timing parameters */
223 if (priv->bittiming_const) {
225 /* Check if bit-timing parameters have already been set */
226 if (priv->bittiming.tq && priv->bittiming.bitrate)
229 /* Non-expert mode? Check if the bitrate has been pre-defined */
230 if (!priv->bittiming.tq)
231 /* Determine bit-timing parameters */
232 err = can_calc_bittiming(dev);
234 /* Check bit-timing params and calculate proper brp */
235 err = can_fixup_bittiming(dev);
240 if (priv->do_set_bittiming) {
241 /* Finally, set the bit-timing registers */
242 err = priv->do_set_bittiming(dev);
249 EXPORT_SYMBOL(can_set_bittiming);
251 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
252 struct net_device_stats *can_get_stats(struct net_device *dev)
254 struct can_priv *priv = netdev_priv(dev);
256 return &priv->net_stats;
258 EXPORT_SYMBOL(can_get_stats);
261 static void can_setup(struct net_device *dev)
263 dev->type = ARPHRD_CAN;
264 dev->mtu = sizeof(struct can_frame);
265 dev->hard_header_len = 0;
267 dev->tx_queue_len = 10;
269 /* New-style flags. */
270 dev->flags = IFF_NOARP;
271 dev->features = NETIF_F_NO_CSUM;
272 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
273 dev->get_stats = can_get_stats;
278 * Allocate and setup space for the CAN network device
280 struct net_device *alloc_candev(int sizeof_priv)
282 struct net_device *dev;
283 struct can_priv *priv;
285 dev = alloc_netdev(sizeof_priv, "can%d", can_setup);
289 priv = netdev_priv(dev);
291 priv->state = CAN_STATE_STOPPED;
292 spin_lock_init(&priv->irq_lock);
294 init_timer(&priv->timer);
295 priv->timer.expires = 0;
299 EXPORT_SYMBOL(alloc_candev);
302 * Allocate space of the CAN network device
304 void free_candev(struct net_device *dev)
308 EXPORT_SYMBOL(free_candev);
311 * Register the CAN network device
313 int register_candev(struct net_device *dev)
317 err = register_netdev(dev);
322 can_create_sysfs(dev);
326 EXPORT_SYMBOL(register_candev);
329 * Unregister the CAN network device
331 void unregister_candev(struct net_device *dev)
334 can_remove_sysfs(dev);
336 unregister_netdev(dev);
338 EXPORT_SYMBOL(unregister_candev);
341 * Local echo of CAN messages
343 * CAN network devices *should* support a local echo functionality
344 * (see Documentation/networking/can.txt). To test the handling of CAN
345 * interfaces that do not support the local echo both driver types are
346 * implemented. In the case that the driver does not support the echo
347 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
348 * to perform the echo as a fallback solution.
351 void can_flush_echo_skb(struct net_device *dev)
353 struct can_priv *priv = netdev_priv(dev);
354 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
355 struct net_device_stats *stats = can_get_stats(dev);
357 struct net_device_stats *stats = &dev->stats;
361 for (i = 0; i < CAN_ECHO_SKB_MAX; i++) {
362 if (priv->echo_skb[i]) {
363 kfree_skb(priv->echo_skb[i]);
364 priv->echo_skb[i] = NULL;
366 stats->tx_aborted_errors++;
372 * Put the skb on the stack to be looped backed locally lateron
374 * The function is typically called in the start_xmit function
375 * of the device driver.
377 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx)
379 struct can_priv *priv = netdev_priv(dev);
381 /* set flag whether this packet has to be looped back */
382 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
387 if (!priv->echo_skb[idx]) {
388 struct sock *srcsk = skb->sk;
390 if (atomic_read(&skb->users) != 1) {
391 struct sk_buff *old_skb = skb;
393 skb = skb_clone(old_skb, GFP_ATOMIC);
402 /* make settings for echo to reduce code in irq context */
403 skb->protocol = htons(ETH_P_CAN);
404 skb->pkt_type = PACKET_BROADCAST;
405 skb->ip_summed = CHECKSUM_UNNECESSARY;
408 /* save this skb for tx interrupt echo handling */
409 priv->echo_skb[idx] = skb;
411 /* locking problem with netif_stop_queue() ?? */
412 printk(KERN_ERR "%s: %s: BUG! echo_skb is occupied!\n",
413 dev->name, __func__);
417 EXPORT_SYMBOL(can_put_echo_skb);
420 * Get the skb from the stack and loop it back locally
422 * The function is typically called when the TX done interrupt
423 * is handled in the device driver.
425 void can_get_echo_skb(struct net_device *dev, int idx)
427 struct can_priv *priv = netdev_priv(dev);
429 if ((dev->flags & IFF_ECHO) && priv->echo_skb[idx]) {
430 netif_rx(priv->echo_skb[idx]);
431 priv->echo_skb[idx] = NULL;
434 EXPORT_SYMBOL(can_get_echo_skb);
437 * CAN device restart for bus-off recovery
439 int can_restart_now(struct net_device *dev)
441 struct can_priv *priv = netdev_priv(dev);
442 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
443 struct net_device_stats *stats = can_get_stats(dev);
445 struct net_device_stats *stats = &dev->stats;
448 struct can_frame *cf;
451 if (netif_carrier_ok(dev))
452 netif_carrier_off(dev);
454 /* Cancel restart in progress */
455 if (priv->timer.expires) {
456 del_timer(&priv->timer);
457 priv->timer.expires = 0; /* mark inactive timer */
460 can_flush_echo_skb(dev);
462 err = priv->do_set_mode(dev, CAN_MODE_START);
466 netif_carrier_on(dev);
468 dev_dbg(ND2D(dev), "restarted\n");
469 priv->can_stats.restarts++;
471 /* send restart message upstream */
472 skb = dev_alloc_skb(sizeof(struct can_frame));
476 skb->protocol = htons(ETH_P_CAN);
477 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
478 memset(cf, 0, sizeof(struct can_frame));
479 cf->can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED;
480 cf->can_dlc = CAN_ERR_DLC;
484 dev->last_rx = jiffies;
486 stats->rx_bytes += cf->can_dlc;
491 static void can_restart_after(unsigned long data)
493 struct net_device *dev = (struct net_device *)data;
494 struct can_priv *priv = netdev_priv(dev);
496 priv->timer.expires = 0; /* mark inactive timer */
497 can_restart_now(dev);
503 * This functions should be called when the device goes bus-off to
504 * tell the netif layer that no more packets can be sent or received.
505 * If enabled, a timer is started to trigger bus-off recovery.
507 void can_bus_off(struct net_device *dev)
509 struct can_priv *priv = netdev_priv(dev);
511 dev_dbg(ND2D(dev), "bus-off\n");
513 netif_carrier_off(dev);
515 if (priv->restart_ms > 0 && !priv->timer.expires) {
517 priv->timer.function = can_restart_after;
518 priv->timer.data = (unsigned long)dev;
519 priv->timer.expires =
520 jiffies + (priv->restart_ms * HZ) / 1000;
521 add_timer(&priv->timer);
524 EXPORT_SYMBOL(can_bus_off);
527 * Cleanup function before the device gets closed.
529 * This functions should be called in the close function of the device
532 void can_close_cleanup(struct net_device *dev)
534 struct can_priv *priv = netdev_priv(dev);
536 if (priv->timer.expires) {
537 del_timer(&priv->timer);
538 priv->timer.expires = 0;
541 can_flush_echo_skb(dev);
543 EXPORT_SYMBOL(can_close_cleanup);
545 static __init int can_dev_init(void)
547 printk(KERN_INFO MOD_DESC "\n");
551 module_init(can_dev_init);
553 static __exit void can_dev_exit(void)
556 module_exit(can_dev_exit);