20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/netdevice.h>
24 #include <linux/if_arp.h>
30 #define MOD_DESC "CAN device driver interface"
38 static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
39 8, 12, 16, 20, 24, 32, 48, 64};
44 return dlc2len[can_dlc & 0x0F];
48 static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
53 13, 13, 13, 13, 13, 13, 13, 13,
54 14, 14, 14, 14, 14, 14, 14, 14,
55 14, 14, 14, 14, 14, 14, 14, 14,
56 15, 15, 15, 15, 15, 15, 15, 15,
57 15, 15, 15, 15, 15, 15, 15, 15};
69 #ifdef CONFIG_CAN_CALC_BITTIMING
70 #define CAN_CALC_MAX_ERROR 50
86 int sampl_pt,
int tseg,
int *tseg1,
int *tseg2)
88 *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
89 if (*tseg2 < btc->tseg2_min)
90 *tseg2 = btc->tseg2_min;
91 if (*tseg2 > btc->tseg2_max)
92 *tseg2 = btc->tseg2_max;
93 *tseg1 = tseg - *tseg2;
94 if (*tseg1 > btc->tseg1_max) {
95 *tseg1 = btc->tseg1_max;
96 *tseg2 = tseg - *tseg1;
98 return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
105 long rate, best_rate = 0;
106 long best_error = 1000000000,
error = 0;
107 int best_tseg = 0, best_brp = 0, brp = 0;
108 int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
109 int spt_error = 1000, spt = 0, sampl_pt;
130 tsegall = 1 + tseg / 2;
132 brp = priv->
clock.freq / (tsegall * bt->
bitrate) + tseg % 2;
137 rate = priv->
clock.freq / (brp * tsegall);
142 if (
error > best_error)
146 spt = can_update_spt(btc, sampl_pt, tseg / 2,
148 error = sampl_pt - spt;
151 if (
error > spt_error)
155 best_tseg = tseg / 2;
165 if (
error > CAN_CALC_MAX_ERROR) {
167 "bitrate error %ld.%ld%% too high\n",
171 netdev_warn(dev,
"bitrate error %ld.%ld%%\n",
177 bt->
sample_point = can_update_spt(btc, sampl_pt, best_tseg,
180 v64 = (
u64)best_brp * 1000000000
UL;
208 netdev_err(dev,
"bit-timing calculation not available\n");
221 struct can_priv *priv = netdev_priv(dev);
240 brp64 += 500000000
UL - 1;
258 struct can_priv *priv = netdev_priv(dev);
267 err = can_calc_bittiming(dev, bt);
270 err = can_fixup_bittiming(dev, bt);
288 static void can_flush_echo_skb(
struct net_device *dev)
290 struct can_priv *priv = netdev_priv(dev);
314 struct can_priv *priv = netdev_priv(dev);
325 struct sock *srcsk = skb->
sk;
349 netdev_err(dev,
"%s: BUG! echo_skb is occupied!\n", __func__);
364 struct can_priv *priv = netdev_priv(dev);
390 struct can_priv *priv = netdev_priv(dev);
404 static void can_restart(
unsigned long data)
407 struct can_priv *priv = netdev_priv(dev);
413 BUG_ON(netif_carrier_ok(dev));
419 can_flush_echo_skb(dev);
443 netdev_err(dev,
"Error %d during restart", err);
448 struct can_priv *priv = netdev_priv(dev);
474 struct can_priv *priv = netdev_priv(dev);
504 skb = netdev_alloc_skb(dev,
sizeof(
struct can_frame));
544 echo_skb_max *
sizeof(
struct sk_buff *);
552 priv = netdev_priv(dev);
585 struct can_priv *priv = netdev_priv(dev);
588 netdev_err(dev,
"bit-timing not yet defined\n");
593 if (!netif_carrier_ok(dev))
610 struct can_priv *priv = netdev_priv(dev);
614 can_flush_echo_skb(dev);
633 static int can_changelink(
struct net_device *dev,
636 struct can_priv *priv = netdev_priv(dev);
648 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
661 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]),
sizeof(bt));
664 err = can_get_bittiming(dev, &bt);
681 priv->
restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
696 static size_t can_get_size(
const struct net_device *dev)
698 struct can_priv *priv = netdev_priv(dev);
701 size = nla_total_size(
sizeof(
u32));
703 size += nla_total_size(
sizeof(
u32));
716 struct can_priv *priv = netdev_priv(dev);
724 nla_put(skb, IFLA_CAN_CTRLMODE,
sizeof(cm), &cm) ||
725 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->
restart_ms) ||
726 nla_put(skb, IFLA_CAN_BITTIMING,
735 goto nla_put_failure;
742 static size_t can_get_xstats_size(
const struct net_device *dev)
749 struct can_priv *priv = netdev_priv(dev);
753 goto nla_put_failure;
760 static int can_newlink(
struct net *src_net,
struct net_device *dev,
769 .policy = can_policy,
771 .newlink = can_newlink,
772 .changelink = can_changelink,
773 .get_size = can_get_size,
774 .fill_info = can_fill_info,
775 .get_xstats_size = can_get_xstats_size,
776 .fill_xstats = can_fill_xstats,
798 static __init int can_dev_init(
void)
810 static __exit void can_dev_exit(
void)