2 * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
3 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
4 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the version 2 of the GNU General Public License
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/netdevice.h>
23 #include <linux/if_arp.h>
24 #include <linux/can.h>
25 #include <linux/can/dev.h>
26 #include <linux/can/netlink.h>
27 #include <net/rtnetlink.h>
29 #define MOD_DESC "CAN device driver interface"
31 MODULE_DESCRIPTION(MOD_DESC
);
32 MODULE_LICENSE("GPL v2");
33 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
35 #ifdef CONFIG_CAN_CALC_BITTIMING
36 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
39 * Bit-timing calculation derived from:
41 * Code based on LinCAN sources and H8S2638 project
42 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
43 * Copyright 2005 Stanislav Marek
44 * email: pisa@cmp.felk.cvut.cz
46 * Calculates proper bit-timing parameters for a specified bit-rate
47 * and sample-point, which can then be used to set the bit-timing
48 * registers of the CAN controller. You can find more information
49 * in the header file linux/can/netlink.h.
51 static int can_update_spt(const struct can_bittiming_const
*btc
,
52 int sampl_pt
, int tseg
, int *tseg1
, int *tseg2
)
54 *tseg2
= tseg
+ 1 - (sampl_pt
* (tseg
+ 1)) / 1000;
55 if (*tseg2
< btc
->tseg2_min
)
56 *tseg2
= btc
->tseg2_min
;
57 if (*tseg2
> btc
->tseg2_max
)
58 *tseg2
= btc
->tseg2_max
;
59 *tseg1
= tseg
- *tseg2
;
60 if (*tseg1
> btc
->tseg1_max
) {
61 *tseg1
= btc
->tseg1_max
;
62 *tseg2
= tseg
- *tseg1
;
64 return 1000 * (tseg
+ 1 - *tseg2
) / (tseg
+ 1);
67 static int can_calc_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
)
69 struct can_priv
*priv
= netdev_priv(dev
);
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
= priv
->clock
.freq
/ (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
= priv
->clock
.freq
/ (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(dev
->dev
.parent
,
133 "bitrate error %ld.%ld%% too high\n",
134 error
/ 10, error
% 10);
137 dev_warn(dev
->dev
.parent
, "bitrate error %ld.%ld%%\n",
138 error
/ 10, error
% 10);
142 /* real sample point */
143 bt
->sample_point
= can_update_spt(btc
, sampl_pt
, best_tseg
,
146 v64
= (u64
)best_brp
* 1000000000UL;
147 do_div(v64
, priv
->clock
.freq
);
149 bt
->prop_seg
= tseg1
/ 2;
150 bt
->phase_seg1
= tseg1
- bt
->prop_seg
;
151 bt
->phase_seg2
= tseg2
;
155 bt
->bitrate
= priv
->clock
.freq
/ (bt
->brp
* (tseg1
+ tseg2
+ 1));
159 #else /* !CONFIG_CAN_CALC_BITTIMING */
160 static int can_calc_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
)
162 dev_err(dev
->dev
.parent
, "bit-timing calculation not available\n");
165 #endif /* CONFIG_CAN_CALC_BITTIMING */
168 * Checks the validity of the specified bit-timing parameters prop_seg,
169 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
170 * prescaler value brp. You can find more information in the header
171 * file linux/can/netlink.h.
173 static int can_fixup_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
)
175 struct can_priv
*priv
= netdev_priv(dev
);
176 const struct can_bittiming_const
*btc
= priv
->bittiming_const
;
180 if (!priv
->bittiming_const
)
183 tseg1
= bt
->prop_seg
+ bt
->phase_seg1
;
186 if (bt
->sjw
> btc
->sjw_max
||
187 tseg1
< btc
->tseg1_min
|| tseg1
> btc
->tseg1_max
||
188 bt
->phase_seg2
< btc
->tseg2_min
|| bt
->phase_seg2
> btc
->tseg2_max
)
191 brp64
= (u64
)priv
->clock
.freq
* (u64
)bt
->tq
;
192 if (btc
->brp_inc
> 1)
193 do_div(brp64
, btc
->brp_inc
);
194 brp64
+= 500000000UL - 1;
195 do_div(brp64
, 1000000000UL); /* the practicable BRP */
196 if (btc
->brp_inc
> 1)
197 brp64
*= btc
->brp_inc
;
198 bt
->brp
= (u32
)brp64
;
200 if (bt
->brp
< btc
->brp_min
|| bt
->brp
> btc
->brp_max
)
203 alltseg
= bt
->prop_seg
+ bt
->phase_seg1
+ bt
->phase_seg2
+ 1;
204 bt
->bitrate
= priv
->clock
.freq
/ (bt
->brp
* alltseg
);
205 bt
->sample_point
= ((tseg1
+ 1) * 1000) / alltseg
;
210 int can_get_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
)
212 struct can_priv
*priv
= netdev_priv(dev
);
215 /* Check if the CAN device has bit-timing parameters */
216 if (priv
->bittiming_const
) {
218 /* Non-expert mode? Check if the bitrate has been pre-defined */
220 /* Determine bit-timing parameters */
221 err
= can_calc_bittiming(dev
, bt
);
223 /* Check bit-timing params and calculate proper brp */
224 err
= can_fixup_bittiming(dev
, bt
);
233 * Local echo of CAN messages
235 * CAN network devices *should* support a local echo functionality
236 * (see Documentation/networking/can.txt). To test the handling of CAN
237 * interfaces that do not support the local echo both driver types are
238 * implemented. In the case that the driver does not support the echo
239 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
240 * to perform the echo as a fallback solution.
242 static void can_flush_echo_skb(struct net_device
*dev
)
244 struct can_priv
*priv
= netdev_priv(dev
);
245 struct net_device_stats
*stats
= &dev
->stats
;
248 for (i
= 0; i
< CAN_ECHO_SKB_MAX
; i
++) {
249 if (priv
->echo_skb
[i
]) {
250 kfree_skb(priv
->echo_skb
[i
]);
251 priv
->echo_skb
[i
] = NULL
;
253 stats
->tx_aborted_errors
++;
259 * Put the skb on the stack to be looped backed locally lateron
261 * The function is typically called in the start_xmit function
262 * of the device driver. The driver must protect access to
263 * priv->echo_skb, if necessary.
265 void can_put_echo_skb(struct sk_buff
*skb
, struct net_device
*dev
, int idx
)
267 struct can_priv
*priv
= netdev_priv(dev
);
269 /* check flag whether this packet has to be looped back */
270 if (!(dev
->flags
& IFF_ECHO
) || skb
->pkt_type
!= PACKET_LOOPBACK
) {
275 if (!priv
->echo_skb
[idx
]) {
276 struct sock
*srcsk
= skb
->sk
;
278 if (atomic_read(&skb
->users
) != 1) {
279 struct sk_buff
*old_skb
= skb
;
281 skb
= skb_clone(old_skb
, GFP_ATOMIC
);
290 /* make settings for echo to reduce code in irq context */
291 skb
->protocol
= htons(ETH_P_CAN
);
292 skb
->pkt_type
= PACKET_BROADCAST
;
293 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
296 /* save this skb for tx interrupt echo handling */
297 priv
->echo_skb
[idx
] = skb
;
299 /* locking problem with netif_stop_queue() ?? */
300 dev_err(dev
->dev
.parent
, "%s: BUG! echo_skb is occupied!\n",
305 EXPORT_SYMBOL_GPL(can_put_echo_skb
);
308 * Get the skb from the stack and loop it back locally
310 * The function is typically called when the TX done interrupt
311 * is handled in the device driver. The driver must protect
312 * access to priv->echo_skb, if necessary.
314 void can_get_echo_skb(struct net_device
*dev
, int idx
)
316 struct can_priv
*priv
= netdev_priv(dev
);
318 if (priv
->echo_skb
[idx
]) {
319 netif_rx(priv
->echo_skb
[idx
]);
320 priv
->echo_skb
[idx
] = NULL
;
323 EXPORT_SYMBOL_GPL(can_get_echo_skb
);
326 * Remove the skb from the stack and free it.
328 * The function is typically called when TX failed.
330 void can_free_echo_skb(struct net_device
*dev
, int idx
)
332 struct can_priv
*priv
= netdev_priv(dev
);
334 if (priv
->echo_skb
[idx
]) {
335 kfree_skb(priv
->echo_skb
[idx
]);
336 priv
->echo_skb
[idx
] = NULL
;
339 EXPORT_SYMBOL_GPL(can_free_echo_skb
);
342 * CAN device restart for bus-off recovery
344 void can_restart(unsigned long data
)
346 struct net_device
*dev
= (struct net_device
*)data
;
347 struct can_priv
*priv
= netdev_priv(dev
);
348 struct net_device_stats
*stats
= &dev
->stats
;
350 struct can_frame
*cf
;
353 BUG_ON(netif_carrier_ok(dev
));
356 * No synchronization needed because the device is bus-off and
357 * no messages can come in or go out.
359 can_flush_echo_skb(dev
);
361 /* send restart message upstream */
362 skb
= dev_alloc_skb(sizeof(struct can_frame
));
368 skb
->protocol
= htons(ETH_P_CAN
);
369 cf
= (struct can_frame
*)skb_put(skb
, sizeof(struct can_frame
));
370 memset(cf
, 0, sizeof(struct can_frame
));
371 cf
->can_id
= CAN_ERR_FLAG
| CAN_ERR_RESTARTED
;
372 cf
->can_dlc
= CAN_ERR_DLC
;
377 stats
->rx_bytes
+= cf
->can_dlc
;
380 dev_dbg(dev
->dev
.parent
, "restarted\n");
381 priv
->can_stats
.restarts
++;
383 /* Now restart the device */
384 err
= priv
->do_set_mode(dev
, CAN_MODE_START
);
386 netif_carrier_on(dev
);
388 dev_err(dev
->dev
.parent
, "Error %d during restart", err
);
391 int can_restart_now(struct net_device
*dev
)
393 struct can_priv
*priv
= netdev_priv(dev
);
396 * A manual restart is only permitted if automatic restart is
397 * disabled and the device is in the bus-off state
399 if (priv
->restart_ms
)
401 if (priv
->state
!= CAN_STATE_BUS_OFF
)
404 /* Runs as soon as possible in the timer context */
405 mod_timer(&priv
->restart_timer
, jiffies
);
413 * This functions should be called when the device goes bus-off to
414 * tell the netif layer that no more packets can be sent or received.
415 * If enabled, a timer is started to trigger bus-off recovery.
417 void can_bus_off(struct net_device
*dev
)
419 struct can_priv
*priv
= netdev_priv(dev
);
421 dev_dbg(dev
->dev
.parent
, "bus-off\n");
423 netif_carrier_off(dev
);
424 priv
->can_stats
.bus_off
++;
426 if (priv
->restart_ms
)
427 mod_timer(&priv
->restart_timer
,
428 jiffies
+ (priv
->restart_ms
* HZ
) / 1000);
430 EXPORT_SYMBOL_GPL(can_bus_off
);
432 static void can_setup(struct net_device
*dev
)
434 dev
->type
= ARPHRD_CAN
;
435 dev
->mtu
= sizeof(struct can_frame
);
436 dev
->hard_header_len
= 0;
438 dev
->tx_queue_len
= 10;
440 /* New-style flags. */
441 dev
->flags
= IFF_NOARP
;
442 dev
->features
= NETIF_F_NO_CSUM
;
446 * Allocate and setup space for the CAN network device
448 struct net_device
*alloc_candev(int sizeof_priv
)
450 struct net_device
*dev
;
451 struct can_priv
*priv
;
453 dev
= alloc_netdev(sizeof_priv
, "can%d", can_setup
);
457 priv
= netdev_priv(dev
);
459 priv
->state
= CAN_STATE_STOPPED
;
461 init_timer(&priv
->restart_timer
);
465 EXPORT_SYMBOL_GPL(alloc_candev
);
468 * Free space of the CAN network device
470 void free_candev(struct net_device
*dev
)
474 EXPORT_SYMBOL_GPL(free_candev
);
477 * Common open function when the device gets opened.
479 * This function should be called in the open function of the device
482 int open_candev(struct net_device
*dev
)
484 struct can_priv
*priv
= netdev_priv(dev
);
486 if (!priv
->bittiming
.tq
&& !priv
->bittiming
.bitrate
) {
487 dev_err(dev
->dev
.parent
, "bit-timing not yet defined\n");
491 /* Switch carrier on if device was stopped while in bus-off state */
492 if (!netif_carrier_ok(dev
))
493 netif_carrier_on(dev
);
495 setup_timer(&priv
->restart_timer
, can_restart
, (unsigned long)dev
);
499 EXPORT_SYMBOL_GPL(open_candev
);
502 * Common close function for cleanup before the device gets closed.
504 * This function should be called in the close function of the device
507 void close_candev(struct net_device
*dev
)
509 struct can_priv
*priv
= netdev_priv(dev
);
511 if (del_timer_sync(&priv
->restart_timer
))
513 can_flush_echo_skb(dev
);
515 EXPORT_SYMBOL_GPL(close_candev
);
518 * CAN netlink interface
520 static const struct nla_policy can_policy
[IFLA_CAN_MAX
+ 1] = {
521 [IFLA_CAN_STATE
] = { .type
= NLA_U32
},
522 [IFLA_CAN_CTRLMODE
] = { .len
= sizeof(struct can_ctrlmode
) },
523 [IFLA_CAN_RESTART_MS
] = { .type
= NLA_U32
},
524 [IFLA_CAN_RESTART
] = { .type
= NLA_U32
},
525 [IFLA_CAN_BITTIMING
] = { .len
= sizeof(struct can_bittiming
) },
526 [IFLA_CAN_BITTIMING_CONST
]
527 = { .len
= sizeof(struct can_bittiming_const
) },
528 [IFLA_CAN_CLOCK
] = { .len
= sizeof(struct can_clock
) },
531 static int can_changelink(struct net_device
*dev
,
532 struct nlattr
*tb
[], struct nlattr
*data
[])
534 struct can_priv
*priv
= netdev_priv(dev
);
537 /* We need synchronization with dev->stop() */
540 if (data
[IFLA_CAN_CTRLMODE
]) {
541 struct can_ctrlmode
*cm
;
543 /* Do not allow changing controller mode while running */
544 if (dev
->flags
& IFF_UP
)
546 cm
= nla_data(data
[IFLA_CAN_CTRLMODE
]);
547 priv
->ctrlmode
&= ~cm
->mask
;
548 priv
->ctrlmode
|= cm
->flags
;
551 if (data
[IFLA_CAN_BITTIMING
]) {
552 struct can_bittiming bt
;
554 /* Do not allow changing bittiming while running */
555 if (dev
->flags
& IFF_UP
)
557 memcpy(&bt
, nla_data(data
[IFLA_CAN_BITTIMING
]), sizeof(bt
));
558 if ((!bt
.bitrate
&& !bt
.tq
) || (bt
.bitrate
&& bt
.tq
))
560 err
= can_get_bittiming(dev
, &bt
);
563 memcpy(&priv
->bittiming
, &bt
, sizeof(bt
));
565 if (priv
->do_set_bittiming
) {
566 /* Finally, set the bit-timing registers */
567 err
= priv
->do_set_bittiming(dev
);
573 if (data
[IFLA_CAN_RESTART_MS
]) {
574 /* Do not allow changing restart delay while running */
575 if (dev
->flags
& IFF_UP
)
577 priv
->restart_ms
= nla_get_u32(data
[IFLA_CAN_RESTART_MS
]);
580 if (data
[IFLA_CAN_RESTART
]) {
581 /* Do not allow a restart while not running */
582 if (!(dev
->flags
& IFF_UP
))
584 err
= can_restart_now(dev
);
592 static int can_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
594 struct can_priv
*priv
= netdev_priv(dev
);
595 struct can_ctrlmode cm
= {.flags
= priv
->ctrlmode
};
596 enum can_state state
= priv
->state
;
598 if (priv
->do_get_state
)
599 priv
->do_get_state(dev
, &state
);
600 NLA_PUT_U32(skb
, IFLA_CAN_STATE
, state
);
601 NLA_PUT(skb
, IFLA_CAN_CTRLMODE
, sizeof(cm
), &cm
);
602 NLA_PUT_U32(skb
, IFLA_CAN_RESTART_MS
, priv
->restart_ms
);
603 NLA_PUT(skb
, IFLA_CAN_BITTIMING
,
604 sizeof(priv
->bittiming
), &priv
->bittiming
);
605 NLA_PUT(skb
, IFLA_CAN_CLOCK
, sizeof(cm
), &priv
->clock
);
606 if (priv
->bittiming_const
)
607 NLA_PUT(skb
, IFLA_CAN_BITTIMING_CONST
,
608 sizeof(*priv
->bittiming_const
), priv
->bittiming_const
);
616 static int can_fill_xstats(struct sk_buff
*skb
, const struct net_device
*dev
)
618 struct can_priv
*priv
= netdev_priv(dev
);
620 NLA_PUT(skb
, IFLA_INFO_XSTATS
,
621 sizeof(priv
->can_stats
), &priv
->can_stats
);
629 static int can_newlink(struct net_device
*dev
,
630 struct nlattr
*tb
[], struct nlattr
*data
[])
635 static struct rtnl_link_ops can_link_ops __read_mostly
= {
637 .maxtype
= IFLA_CAN_MAX
,
638 .policy
= can_policy
,
640 .newlink
= can_newlink
,
641 .changelink
= can_changelink
,
642 .fill_info
= can_fill_info
,
643 .fill_xstats
= can_fill_xstats
,
647 * Register the CAN network device
649 int register_candev(struct net_device
*dev
)
651 dev
->rtnl_link_ops
= &can_link_ops
;
652 return register_netdev(dev
);
654 EXPORT_SYMBOL_GPL(register_candev
);
657 * Unregister the CAN network device
659 void unregister_candev(struct net_device
*dev
)
661 unregister_netdev(dev
);
663 EXPORT_SYMBOL_GPL(unregister_candev
);
665 static __init
int can_dev_init(void)
669 err
= rtnl_link_register(&can_link_ops
);
671 printk(KERN_INFO MOD_DESC
"\n");
675 module_init(can_dev_init
);
677 static __exit
void can_dev_exit(void)
679 rtnl_link_unregister(&can_link_ops
);
681 module_exit(can_dev_exit
);
683 MODULE_ALIAS_RTNL_LINK("can");