blob: e5f1faf36d8ffe56bd283a4c951cc2f5ff94a32b [file] [log] [blame]
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001/*
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>
5 *
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
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
Jeff Kirsher05780d92013-12-06 06:28:45 -080016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000017 */
18
19#include <linux/module.h>
20#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000022#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/can.h>
25#include <linux/can/dev.h>
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +010026#include <linux/can/skb.h>
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000027#include <linux/can/netlink.h>
Kurt Van Dijcka1ef7bd2012-12-18 18:50:57 +010028#include <linux/can/led.h>
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000029#include <net/rtnetlink.h>
30
31#define MOD_DESC "CAN device driver interface"
32
33MODULE_DESCRIPTION(MOD_DESC);
34MODULE_LICENSE("GPL v2");
35MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
36
Oliver Hartkopp1e0625f2012-06-13 20:48:21 +020037/* CAN DLC to real data length conversion helpers */
38
39static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
40 8, 12, 16, 20, 24, 32, 48, 64};
41
42/* get data length from can_dlc with sanitized can_dlc */
43u8 can_dlc2len(u8 can_dlc)
44{
45 return dlc2len[can_dlc & 0x0F];
46}
47EXPORT_SYMBOL_GPL(can_dlc2len);
48
49static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
50 9, 9, 9, 9, /* 9 - 12 */
51 10, 10, 10, 10, /* 13 - 16 */
52 11, 11, 11, 11, /* 17 - 20 */
53 12, 12, 12, 12, /* 21 - 24 */
54 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
55 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
56 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
57 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
58 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
59
60/* map the sanitized data length to an appropriate data length code */
61u8 can_len2dlc(u8 len)
62{
63 if (unlikely(len > 64))
64 return 0xF;
65
66 return len2dlc[len];
67}
68EXPORT_SYMBOL_GPL(can_len2dlc);
69
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000070#ifdef CONFIG_CAN_CALC_BITTIMING
71#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
72
73/*
74 * Bit-timing calculation derived from:
75 *
76 * Code based on LinCAN sources and H8S2638 project
77 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
78 * Copyright 2005 Stanislav Marek
79 * email: pisa@cmp.felk.cvut.cz
80 *
81 * Calculates proper bit-timing parameters for a specified bit-rate
82 * and sample-point, which can then be used to set the bit-timing
83 * registers of the CAN controller. You can find more information
84 * in the header file linux/can/netlink.h.
85 */
86static int can_update_spt(const struct can_bittiming_const *btc,
87 int sampl_pt, int tseg, int *tseg1, int *tseg2)
88{
89 *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
90 if (*tseg2 < btc->tseg2_min)
91 *tseg2 = btc->tseg2_min;
92 if (*tseg2 > btc->tseg2_max)
93 *tseg2 = btc->tseg2_max;
94 *tseg1 = tseg - *tseg2;
95 if (*tseg1 > btc->tseg1_max) {
96 *tseg1 = btc->tseg1_max;
97 *tseg2 = tseg - *tseg1;
98 }
99 return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
100}
101
102static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
103{
104 struct can_priv *priv = netdev_priv(dev);
105 const struct can_bittiming_const *btc = priv->bittiming_const;
106 long rate, best_rate = 0;
107 long best_error = 1000000000, error = 0;
108 int best_tseg = 0, best_brp = 0, brp = 0;
109 int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
110 int spt_error = 1000, spt = 0, sampl_pt;
111 u64 v64;
112
113 if (!priv->bittiming_const)
114 return -ENOTSUPP;
115
116 /* Use CIA recommended sample points */
117 if (bt->sample_point) {
118 sampl_pt = bt->sample_point;
119 } else {
120 if (bt->bitrate > 800000)
121 sampl_pt = 750;
122 else if (bt->bitrate > 500000)
123 sampl_pt = 800;
124 else
125 sampl_pt = 875;
126 }
127
128 /* tseg even = round down, odd = round up */
129 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
130 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
131 tsegall = 1 + tseg / 2;
132 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
133 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
134 /* chose brp step which is possible in system */
135 brp = (brp / btc->brp_inc) * btc->brp_inc;
136 if ((brp < btc->brp_min) || (brp > btc->brp_max))
137 continue;
138 rate = priv->clock.freq / (brp * tsegall);
139 error = bt->bitrate - rate;
140 /* tseg brp biterror */
141 if (error < 0)
142 error = -error;
143 if (error > best_error)
144 continue;
145 best_error = error;
146 if (error == 0) {
147 spt = can_update_spt(btc, sampl_pt, tseg / 2,
148 &tseg1, &tseg2);
149 error = sampl_pt - spt;
150 if (error < 0)
151 error = -error;
152 if (error > spt_error)
153 continue;
154 spt_error = error;
155 }
156 best_tseg = tseg / 2;
157 best_brp = brp;
158 best_rate = rate;
159 if (error == 0)
160 break;
161 }
162
163 if (best_error) {
164 /* Error in one-tenth of a percent */
165 error = (best_error * 1000) / bt->bitrate;
166 if (error > CAN_CALC_MAX_ERROR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100167 netdev_err(dev,
168 "bitrate error %ld.%ld%% too high\n",
169 error / 10, error % 10);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000170 return -EDOM;
171 } else {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100172 netdev_warn(dev, "bitrate error %ld.%ld%%\n",
173 error / 10, error % 10);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000174 }
175 }
176
177 /* real sample point */
178 bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
179 &tseg1, &tseg2);
180
181 v64 = (u64)best_brp * 1000000000UL;
182 do_div(v64, priv->clock.freq);
183 bt->tq = (u32)v64;
184 bt->prop_seg = tseg1 / 2;
185 bt->phase_seg1 = tseg1 - bt->prop_seg;
186 bt->phase_seg2 = tseg2;
Oliver Hartkopp2e114372011-09-28 02:50:11 +0000187
188 /* check for sjw user settings */
189 if (!bt->sjw || !btc->sjw_max)
190 bt->sjw = 1;
191 else {
192 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
193 if (bt->sjw > btc->sjw_max)
194 bt->sjw = btc->sjw_max;
195 /* bt->sjw must not be higher than tseg2 */
196 if (tseg2 < bt->sjw)
197 bt->sjw = tseg2;
198 }
199
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000200 bt->brp = best_brp;
201 /* real bit-rate */
202 bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
203
204 return 0;
205}
206#else /* !CONFIG_CAN_CALC_BITTIMING */
207static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
208{
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100209 netdev_err(dev, "bit-timing calculation not available\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000210 return -EINVAL;
211}
212#endif /* CONFIG_CAN_CALC_BITTIMING */
213
214/*
215 * Checks the validity of the specified bit-timing parameters prop_seg,
216 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
217 * prescaler value brp. You can find more information in the header
218 * file linux/can/netlink.h.
219 */
220static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt)
221{
222 struct can_priv *priv = netdev_priv(dev);
223 const struct can_bittiming_const *btc = priv->bittiming_const;
224 int tseg1, alltseg;
225 u64 brp64;
226
227 if (!priv->bittiming_const)
228 return -ENOTSUPP;
229
230 tseg1 = bt->prop_seg + bt->phase_seg1;
231 if (!bt->sjw)
232 bt->sjw = 1;
233 if (bt->sjw > btc->sjw_max ||
234 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
235 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
236 return -ERANGE;
237
238 brp64 = (u64)priv->clock.freq * (u64)bt->tq;
239 if (btc->brp_inc > 1)
240 do_div(brp64, btc->brp_inc);
241 brp64 += 500000000UL - 1;
242 do_div(brp64, 1000000000UL); /* the practicable BRP */
243 if (btc->brp_inc > 1)
244 brp64 *= btc->brp_inc;
245 bt->brp = (u32)brp64;
246
247 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
248 return -EINVAL;
249
250 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
251 bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
252 bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
253
254 return 0;
255}
256
Thadeu Lima de Souza Cascardo61463a32011-07-21 16:22:31 +0000257static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000258{
259 struct can_priv *priv = netdev_priv(dev);
260 int err;
261
262 /* Check if the CAN device has bit-timing parameters */
Oliver Hartkoppd5298df2014-02-28 16:36:21 +0100263 if (!priv->bittiming_const)
264 return 0;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000265
Oliver Hartkoppd5298df2014-02-28 16:36:21 +0100266 /*
267 * Depending on the given can_bittiming parameter structure the CAN
268 * timing parameters are calculated based on the provided bitrate OR
269 * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
270 * provided directly which are then checked and fixed up.
271 */
272 if (!bt->tq && bt->bitrate)
273 err = can_calc_bittiming(dev, bt);
274 else if (bt->tq && !bt->bitrate)
275 err = can_fixup_bittiming(dev, bt);
276 else
277 err = -EINVAL;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000278
Oliver Hartkoppd5298df2014-02-28 16:36:21 +0100279 return err;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000280}
281
282/*
283 * Local echo of CAN messages
284 *
285 * CAN network devices *should* support a local echo functionality
286 * (see Documentation/networking/can.txt). To test the handling of CAN
287 * interfaces that do not support the local echo both driver types are
288 * implemented. In the case that the driver does not support the echo
289 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
290 * to perform the echo as a fallback solution.
291 */
292static void can_flush_echo_skb(struct net_device *dev)
293{
294 struct can_priv *priv = netdev_priv(dev);
295 struct net_device_stats *stats = &dev->stats;
296 int i;
297
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000298 for (i = 0; i < priv->echo_skb_max; i++) {
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000299 if (priv->echo_skb[i]) {
300 kfree_skb(priv->echo_skb[i]);
301 priv->echo_skb[i] = NULL;
302 stats->tx_dropped++;
303 stats->tx_aborted_errors++;
304 }
305 }
306}
307
308/*
309 * Put the skb on the stack to be looped backed locally lateron
310 *
311 * The function is typically called in the start_xmit function
312 * of the device driver. The driver must protect access to
313 * priv->echo_skb, if necessary.
314 */
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000315void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
316 unsigned int idx)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000317{
318 struct can_priv *priv = netdev_priv(dev);
319
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000320 BUG_ON(idx >= priv->echo_skb_max);
321
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000322 /* check flag whether this packet has to be looped back */
Oliver Hartkoppa94bc9c2014-02-28 16:36:19 +0100323 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
324 (skb->protocol != htons(ETH_P_CAN) &&
325 skb->protocol != htons(ETH_P_CANFD))) {
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000326 kfree_skb(skb);
327 return;
328 }
329
330 if (!priv->echo_skb[idx]) {
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000331
Oliver Hartkopp0ae89be2014-01-30 10:11:28 +0100332 skb = can_create_echo_skb(skb);
333 if (!skb)
334 return;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000335
336 /* make settings for echo to reduce code in irq context */
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000337 skb->pkt_type = PACKET_BROADCAST;
338 skb->ip_summed = CHECKSUM_UNNECESSARY;
339 skb->dev = dev;
340
341 /* save this skb for tx interrupt echo handling */
342 priv->echo_skb[idx] = skb;
343 } else {
344 /* locking problem with netif_stop_queue() ?? */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100345 netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000346 kfree_skb(skb);
347 }
348}
349EXPORT_SYMBOL_GPL(can_put_echo_skb);
350
351/*
352 * Get the skb from the stack and loop it back locally
353 *
354 * The function is typically called when the TX done interrupt
355 * is handled in the device driver. The driver must protect
356 * access to priv->echo_skb, if necessary.
357 */
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200358unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000359{
360 struct can_priv *priv = netdev_priv(dev);
361
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000362 BUG_ON(idx >= priv->echo_skb_max);
363
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000364 if (priv->echo_skb[idx]) {
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200365 struct sk_buff *skb = priv->echo_skb[idx];
366 struct can_frame *cf = (struct can_frame *)skb->data;
367 u8 dlc = cf->can_dlc;
368
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000369 netif_rx(priv->echo_skb[idx]);
370 priv->echo_skb[idx] = NULL;
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200371
372 return dlc;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000373 }
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200374
375 return 0;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000376}
377EXPORT_SYMBOL_GPL(can_get_echo_skb);
378
379/*
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000380 * Remove the skb from the stack and free it.
381 *
382 * The function is typically called when TX failed.
383 */
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000384void can_free_echo_skb(struct net_device *dev, unsigned int idx)
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000385{
386 struct can_priv *priv = netdev_priv(dev);
387
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000388 BUG_ON(idx >= priv->echo_skb_max);
389
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000390 if (priv->echo_skb[idx]) {
391 kfree_skb(priv->echo_skb[idx]);
392 priv->echo_skb[idx] = NULL;
393 }
394}
395EXPORT_SYMBOL_GPL(can_free_echo_skb);
396
397/*
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000398 * CAN device restart for bus-off recovery
399 */
Marc Kleine-Budde77fc95a2012-02-06 22:21:13 +0100400static void can_restart(unsigned long data)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000401{
402 struct net_device *dev = (struct net_device *)data;
403 struct can_priv *priv = netdev_priv(dev);
404 struct net_device_stats *stats = &dev->stats;
405 struct sk_buff *skb;
406 struct can_frame *cf;
407 int err;
408
409 BUG_ON(netif_carrier_ok(dev));
410
411 /*
412 * No synchronization needed because the device is bus-off and
413 * no messages can come in or go out.
414 */
415 can_flush_echo_skb(dev);
416
417 /* send restart message upstream */
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700418 skb = alloc_can_err_skb(dev, &cf);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000419 if (skb == NULL) {
420 err = -ENOMEM;
Wolfgang Grandeggerb3d0df72009-07-20 04:06:40 +0000421 goto restart;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000422 }
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700423 cf->can_id |= CAN_ERR_RESTARTED;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000424
425 netif_rx(skb);
426
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000427 stats->rx_packets++;
428 stats->rx_bytes += cf->can_dlc;
429
Wolfgang Grandeggerb3d0df72009-07-20 04:06:40 +0000430restart:
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100431 netdev_dbg(dev, "restarted\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000432 priv->can_stats.restarts++;
433
434 /* Now restart the device */
435 err = priv->do_set_mode(dev, CAN_MODE_START);
436
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000437 netif_carrier_on(dev);
438 if (err)
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100439 netdev_err(dev, "Error %d during restart", err);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000440}
441
442int can_restart_now(struct net_device *dev)
443{
444 struct can_priv *priv = netdev_priv(dev);
445
446 /*
447 * A manual restart is only permitted if automatic restart is
448 * disabled and the device is in the bus-off state
449 */
450 if (priv->restart_ms)
451 return -EINVAL;
452 if (priv->state != CAN_STATE_BUS_OFF)
453 return -EBUSY;
454
455 /* Runs as soon as possible in the timer context */
456 mod_timer(&priv->restart_timer, jiffies);
457
458 return 0;
459}
460
461/*
462 * CAN bus-off
463 *
464 * This functions should be called when the device goes bus-off to
465 * tell the netif layer that no more packets can be sent or received.
466 * If enabled, a timer is started to trigger bus-off recovery.
467 */
468void can_bus_off(struct net_device *dev)
469{
470 struct can_priv *priv = netdev_priv(dev);
471
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100472 netdev_dbg(dev, "bus-off\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000473
474 netif_carrier_off(dev);
475 priv->can_stats.bus_off++;
476
477 if (priv->restart_ms)
478 mod_timer(&priv->restart_timer,
479 jiffies + (priv->restart_ms * HZ) / 1000);
480}
481EXPORT_SYMBOL_GPL(can_bus_off);
482
483static void can_setup(struct net_device *dev)
484{
485 dev->type = ARPHRD_CAN;
Oliver Hartkopp1e0625f2012-06-13 20:48:21 +0200486 dev->mtu = CAN_MTU;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000487 dev->hard_header_len = 0;
488 dev->addr_len = 0;
489 dev->tx_queue_len = 10;
490
491 /* New-style flags. */
492 dev->flags = IFF_NOARP;
Michał Mirosław34324dc2011-11-15 15:29:55 +0000493 dev->features = NETIF_F_HW_CSUM;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000494}
495
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700496struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
497{
498 struct sk_buff *skb;
499
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +0100500 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
501 sizeof(struct can_frame));
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700502 if (unlikely(!skb))
503 return NULL;
504
505 skb->protocol = htons(ETH_P_CAN);
506 skb->pkt_type = PACKET_BROADCAST;
507 skb->ip_summed = CHECKSUM_UNNECESSARY;
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +0100508
Oliver Hartkopp2bf34402013-01-28 08:33:33 +0000509 can_skb_reserve(skb);
510 can_skb_prv(skb)->ifindex = dev->ifindex;
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +0100511
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700512 *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
513 memset(*cf, 0, sizeof(struct can_frame));
514
515 return skb;
516}
517EXPORT_SYMBOL_GPL(alloc_can_skb);
518
Stephane Grosjeancb2518c2014-01-15 09:50:13 +0100519struct sk_buff *alloc_canfd_skb(struct net_device *dev,
520 struct canfd_frame **cfd)
521{
522 struct sk_buff *skb;
523
524 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
525 sizeof(struct canfd_frame));
526 if (unlikely(!skb))
527 return NULL;
528
529 skb->protocol = htons(ETH_P_CANFD);
530 skb->pkt_type = PACKET_BROADCAST;
531 skb->ip_summed = CHECKSUM_UNNECESSARY;
532
533 can_skb_reserve(skb);
534 can_skb_prv(skb)->ifindex = dev->ifindex;
535
536 *cfd = (struct canfd_frame *)skb_put(skb, sizeof(struct canfd_frame));
537 memset(*cfd, 0, sizeof(struct canfd_frame));
538
539 return skb;
540}
541EXPORT_SYMBOL_GPL(alloc_canfd_skb);
542
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700543struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
544{
545 struct sk_buff *skb;
546
547 skb = alloc_can_skb(dev, cf);
548 if (unlikely(!skb))
549 return NULL;
550
551 (*cf)->can_id = CAN_ERR_FLAG;
552 (*cf)->can_dlc = CAN_ERR_DLC;
553
554 return skb;
555}
556EXPORT_SYMBOL_GPL(alloc_can_err_skb);
557
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000558/*
559 * Allocate and setup space for the CAN network device
560 */
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000561struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000562{
563 struct net_device *dev;
564 struct can_priv *priv;
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000565 int size;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000566
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000567 if (echo_skb_max)
568 size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
569 echo_skb_max * sizeof(struct sk_buff *);
570 else
571 size = sizeof_priv;
572
573 dev = alloc_netdev(size, "can%d", can_setup);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000574 if (!dev)
575 return NULL;
576
577 priv = netdev_priv(dev);
578
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000579 if (echo_skb_max) {
580 priv->echo_skb_max = echo_skb_max;
581 priv->echo_skb = (void *)priv +
582 ALIGN(sizeof_priv, sizeof(struct sk_buff *));
583 }
584
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000585 priv->state = CAN_STATE_STOPPED;
586
587 init_timer(&priv->restart_timer);
588
589 return dev;
590}
591EXPORT_SYMBOL_GPL(alloc_candev);
592
593/*
594 * Free space of the CAN network device
595 */
596void free_candev(struct net_device *dev)
597{
598 free_netdev(dev);
599}
600EXPORT_SYMBOL_GPL(free_candev);
601
602/*
603 * Common open function when the device gets opened.
604 *
605 * This function should be called in the open function of the device
606 * driver.
607 */
608int open_candev(struct net_device *dev)
609{
610 struct can_priv *priv = netdev_priv(dev);
611
Oliver Hartkoppb30749f2014-02-28 16:36:20 +0100612 if (!priv->bittiming.bitrate) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100613 netdev_err(dev, "bit-timing not yet defined\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000614 return -EINVAL;
615 }
616
Wolfgang Grandegger1b0d9222009-07-20 04:06:41 +0000617 /* Switch carrier on if device was stopped while in bus-off state */
618 if (!netif_carrier_ok(dev))
619 netif_carrier_on(dev);
620
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000621 setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev);
622
623 return 0;
624}
Wolfgang Grandegger128ced82009-05-30 07:55:48 +0000625EXPORT_SYMBOL_GPL(open_candev);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000626
627/*
628 * Common close function for cleanup before the device gets closed.
629 *
630 * This function should be called in the close function of the device
631 * driver.
632 */
633void close_candev(struct net_device *dev)
634{
635 struct can_priv *priv = netdev_priv(dev);
636
Alexander Steinab48b032012-11-27 08:52:34 +0100637 del_timer_sync(&priv->restart_timer);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000638 can_flush_echo_skb(dev);
639}
640EXPORT_SYMBOL_GPL(close_candev);
641
642/*
643 * CAN netlink interface
644 */
645static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
646 [IFLA_CAN_STATE] = { .type = NLA_U32 },
647 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
648 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
649 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
650 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
651 [IFLA_CAN_BITTIMING_CONST]
652 = { .len = sizeof(struct can_bittiming_const) },
653 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
Wolfgang Grandegger52c793f2010-02-22 22:21:17 +0000654 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000655};
656
657static int can_changelink(struct net_device *dev,
658 struct nlattr *tb[], struct nlattr *data[])
659{
660 struct can_priv *priv = netdev_priv(dev);
661 int err;
662
663 /* We need synchronization with dev->stop() */
664 ASSERT_RTNL();
665
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000666 if (data[IFLA_CAN_BITTIMING]) {
667 struct can_bittiming bt;
668
669 /* Do not allow changing bittiming while running */
670 if (dev->flags & IFF_UP)
671 return -EBUSY;
672 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000673 err = can_get_bittiming(dev, &bt);
674 if (err)
675 return err;
676 memcpy(&priv->bittiming, &bt, sizeof(bt));
677
678 if (priv->do_set_bittiming) {
679 /* Finally, set the bit-timing registers */
680 err = priv->do_set_bittiming(dev);
681 if (err)
682 return err;
683 }
684 }
685
Marc Kleine-Budde49cb5c02013-10-04 15:40:22 +0200686 if (data[IFLA_CAN_CTRLMODE]) {
687 struct can_ctrlmode *cm;
688
689 /* Do not allow changing controller mode while running */
690 if (dev->flags & IFF_UP)
691 return -EBUSY;
692 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
693 if (cm->flags & ~priv->ctrlmode_supported)
694 return -EOPNOTSUPP;
695 priv->ctrlmode &= ~cm->mask;
696 priv->ctrlmode |= cm->flags;
697 }
698
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000699 if (data[IFLA_CAN_RESTART_MS]) {
700 /* Do not allow changing restart delay while running */
701 if (dev->flags & IFF_UP)
702 return -EBUSY;
703 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
704 }
705
706 if (data[IFLA_CAN_RESTART]) {
707 /* Do not allow a restart while not running */
708 if (!(dev->flags & IFF_UP))
709 return -EINVAL;
710 err = can_restart_now(dev);
711 if (err)
712 return err;
713 }
714
715 return 0;
716}
717
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +0000718static size_t can_get_size(const struct net_device *dev)
719{
720 struct can_priv *priv = netdev_priv(dev);
Marc Kleine-Buddec13c64d2013-10-05 20:34:47 +0200721 size_t size = 0;
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +0000722
Oliver Hartkoppb30749f2014-02-28 16:36:20 +0100723 if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
724 size += nla_total_size(sizeof(struct can_bittiming));
Marc Kleine-Buddec13c64d2013-10-05 20:34:47 +0200725 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
Marc Kleine-Buddefe119a02013-10-05 21:25:17 +0200726 size += nla_total_size(sizeof(struct can_bittiming_const));
Marc Kleine-Buddec13c64d2013-10-05 20:34:47 +0200727 size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
728 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
729 size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
730 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
731 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
732 size += nla_total_size(sizeof(struct can_berr_counter));
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +0000733
734 return size;
735}
736
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000737static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
738{
739 struct can_priv *priv = netdev_priv(dev);
740 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
Wolfgang Grandegger52c793f2010-02-22 22:21:17 +0000741 struct can_berr_counter bec;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000742 enum can_state state = priv->state;
743
744 if (priv->do_get_state)
745 priv->do_get_state(dev, &state);
Oliver Hartkoppb30749f2014-02-28 16:36:20 +0100746 if ((priv->bittiming.bitrate &&
747 nla_put(skb, IFLA_CAN_BITTIMING,
748 sizeof(priv->bittiming), &priv->bittiming)) ||
David S. Miller31e0e322012-04-01 20:21:11 -0400749 (priv->bittiming_const &&
750 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
Marc Kleine-Budde57a59b92013-10-04 13:35:57 +0200751 sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
752 nla_put(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock) ||
753 nla_put_u32(skb, IFLA_CAN_STATE, state) ||
754 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
755 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
756 (priv->do_get_berr_counter &&
757 !priv->do_get_berr_counter(dev, &bec) &&
758 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)))
759 return -EMSGSIZE;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000760 return 0;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000761}
762
Wolfgang Grandegger55369c02009-11-12 05:34:05 +0000763static size_t can_get_xstats_size(const struct net_device *dev)
764{
765 return sizeof(struct can_device_stats);
766}
767
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000768static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
769{
770 struct can_priv *priv = netdev_priv(dev);
771
David S. Miller31e0e322012-04-01 20:21:11 -0400772 if (nla_put(skb, IFLA_INFO_XSTATS,
773 sizeof(priv->can_stats), &priv->can_stats))
774 goto nla_put_failure;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000775 return 0;
776
777nla_put_failure:
778 return -EMSGSIZE;
779}
780
Eric W. Biederman81adee42009-11-08 00:53:51 -0800781static int can_newlink(struct net *src_net, struct net_device *dev,
Oliver Hartkopp993e6f22009-08-11 02:41:24 +0000782 struct nlattr *tb[], struct nlattr *data[])
783{
784 return -EOPNOTSUPP;
785}
786
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000787static struct rtnl_link_ops can_link_ops __read_mostly = {
788 .kind = "can",
789 .maxtype = IFLA_CAN_MAX,
790 .policy = can_policy,
791 .setup = can_setup,
Oliver Hartkopp993e6f22009-08-11 02:41:24 +0000792 .newlink = can_newlink,
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000793 .changelink = can_changelink,
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +0000794 .get_size = can_get_size,
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000795 .fill_info = can_fill_info,
Wolfgang Grandegger55369c02009-11-12 05:34:05 +0000796 .get_xstats_size = can_get_xstats_size,
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000797 .fill_xstats = can_fill_xstats,
798};
799
800/*
801 * Register the CAN network device
802 */
803int register_candev(struct net_device *dev)
804{
805 dev->rtnl_link_ops = &can_link_ops;
806 return register_netdev(dev);
807}
808EXPORT_SYMBOL_GPL(register_candev);
809
810/*
811 * Unregister the CAN network device
812 */
813void unregister_candev(struct net_device *dev)
814{
815 unregister_netdev(dev);
816}
817EXPORT_SYMBOL_GPL(unregister_candev);
818
Kurt Van Dijckbf03a532012-12-18 18:50:56 +0100819/*
820 * Test if a network device is a candev based device
821 * and return the can_priv* if so.
822 */
823struct can_priv *safe_candev_priv(struct net_device *dev)
824{
825 if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
826 return NULL;
827
828 return netdev_priv(dev);
829}
830EXPORT_SYMBOL_GPL(safe_candev_priv);
831
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000832static __init int can_dev_init(void)
833{
834 int err;
835
Kurt Van Dijcka1ef7bd2012-12-18 18:50:57 +0100836 can_led_notifier_init();
837
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000838 err = rtnl_link_register(&can_link_ops);
839 if (!err)
840 printk(KERN_INFO MOD_DESC "\n");
841
842 return err;
843}
844module_init(can_dev_init);
845
846static __exit void can_dev_exit(void)
847{
848 rtnl_link_unregister(&can_link_ops);
Kurt Van Dijcka1ef7bd2012-12-18 18:50:57 +0100849
850 can_led_notifier_exit();
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000851}
852module_exit(can_dev_exit);
853
854MODULE_ALIAS_RTNL_LINK("can");