blob: 8233e5ed2939dcfe415ee119edf8c3aaf042c47d [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
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
18 */
19
20#include <linux/module.h>
21#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000023#include <linux/netdevice.h>
24#include <linux/if_arp.h>
25#include <linux/can.h>
26#include <linux/can/dev.h>
27#include <linux/can/netlink.h>
28#include <net/rtnetlink.h>
29
30#define MOD_DESC "CAN device driver interface"
31
32MODULE_DESCRIPTION(MOD_DESC);
33MODULE_LICENSE("GPL v2");
34MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
35
Oliver Hartkopp1e0625f2012-06-13 20:48:21 +020036/* CAN DLC to real data length conversion helpers */
37
38static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
39 8, 12, 16, 20, 24, 32, 48, 64};
40
41/* get data length from can_dlc with sanitized can_dlc */
42u8 can_dlc2len(u8 can_dlc)
43{
44 return dlc2len[can_dlc & 0x0F];
45}
46EXPORT_SYMBOL_GPL(can_dlc2len);
47
48static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
49 9, 9, 9, 9, /* 9 - 12 */
50 10, 10, 10, 10, /* 13 - 16 */
51 11, 11, 11, 11, /* 17 - 20 */
52 12, 12, 12, 12, /* 21 - 24 */
53 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
54 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
55 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
56 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
57 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
58
59/* map the sanitized data length to an appropriate data length code */
60u8 can_len2dlc(u8 len)
61{
62 if (unlikely(len > 64))
63 return 0xF;
64
65 return len2dlc[len];
66}
67EXPORT_SYMBOL_GPL(can_len2dlc);
68
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000069#ifdef CONFIG_CAN_CALC_BITTIMING
70#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
71
72/*
73 * Bit-timing calculation derived from:
74 *
75 * Code based on LinCAN sources and H8S2638 project
76 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
77 * Copyright 2005 Stanislav Marek
78 * email: pisa@cmp.felk.cvut.cz
79 *
80 * Calculates proper bit-timing parameters for a specified bit-rate
81 * and sample-point, which can then be used to set the bit-timing
82 * registers of the CAN controller. You can find more information
83 * in the header file linux/can/netlink.h.
84 */
85static int can_update_spt(const struct can_bittiming_const *btc,
86 int sampl_pt, int tseg, int *tseg1, int *tseg2)
87{
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;
97 }
98 return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
99}
100
101static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
102{
103 struct can_priv *priv = netdev_priv(dev);
104 const struct can_bittiming_const *btc = priv->bittiming_const;
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;
110 u64 v64;
111
112 if (!priv->bittiming_const)
113 return -ENOTSUPP;
114
115 /* Use CIA recommended sample points */
116 if (bt->sample_point) {
117 sampl_pt = bt->sample_point;
118 } else {
119 if (bt->bitrate > 800000)
120 sampl_pt = 750;
121 else if (bt->bitrate > 500000)
122 sampl_pt = 800;
123 else
124 sampl_pt = 875;
125 }
126
127 /* tseg even = round down, odd = round up */
128 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
129 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
130 tsegall = 1 + tseg / 2;
131 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
132 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
133 /* chose brp step which is possible in system */
134 brp = (brp / btc->brp_inc) * btc->brp_inc;
135 if ((brp < btc->brp_min) || (brp > btc->brp_max))
136 continue;
137 rate = priv->clock.freq / (brp * tsegall);
138 error = bt->bitrate - rate;
139 /* tseg brp biterror */
140 if (error < 0)
141 error = -error;
142 if (error > best_error)
143 continue;
144 best_error = error;
145 if (error == 0) {
146 spt = can_update_spt(btc, sampl_pt, tseg / 2,
147 &tseg1, &tseg2);
148 error = sampl_pt - spt;
149 if (error < 0)
150 error = -error;
151 if (error > spt_error)
152 continue;
153 spt_error = error;
154 }
155 best_tseg = tseg / 2;
156 best_brp = brp;
157 best_rate = rate;
158 if (error == 0)
159 break;
160 }
161
162 if (best_error) {
163 /* Error in one-tenth of a percent */
164 error = (best_error * 1000) / bt->bitrate;
165 if (error > CAN_CALC_MAX_ERROR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100166 netdev_err(dev,
167 "bitrate error %ld.%ld%% too high\n",
168 error / 10, error % 10);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000169 return -EDOM;
170 } else {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100171 netdev_warn(dev, "bitrate error %ld.%ld%%\n",
172 error / 10, error % 10);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000173 }
174 }
175
176 /* real sample point */
177 bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
178 &tseg1, &tseg2);
179
180 v64 = (u64)best_brp * 1000000000UL;
181 do_div(v64, priv->clock.freq);
182 bt->tq = (u32)v64;
183 bt->prop_seg = tseg1 / 2;
184 bt->phase_seg1 = tseg1 - bt->prop_seg;
185 bt->phase_seg2 = tseg2;
Oliver Hartkopp2e114372011-09-28 02:50:11 +0000186
187 /* check for sjw user settings */
188 if (!bt->sjw || !btc->sjw_max)
189 bt->sjw = 1;
190 else {
191 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
192 if (bt->sjw > btc->sjw_max)
193 bt->sjw = btc->sjw_max;
194 /* bt->sjw must not be higher than tseg2 */
195 if (tseg2 < bt->sjw)
196 bt->sjw = tseg2;
197 }
198
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000199 bt->brp = best_brp;
200 /* real bit-rate */
201 bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
202
203 return 0;
204}
205#else /* !CONFIG_CAN_CALC_BITTIMING */
206static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
207{
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100208 netdev_err(dev, "bit-timing calculation not available\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000209 return -EINVAL;
210}
211#endif /* CONFIG_CAN_CALC_BITTIMING */
212
213/*
214 * Checks the validity of the specified bit-timing parameters prop_seg,
215 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
216 * prescaler value brp. You can find more information in the header
217 * file linux/can/netlink.h.
218 */
219static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt)
220{
221 struct can_priv *priv = netdev_priv(dev);
222 const struct can_bittiming_const *btc = priv->bittiming_const;
223 int tseg1, alltseg;
224 u64 brp64;
225
226 if (!priv->bittiming_const)
227 return -ENOTSUPP;
228
229 tseg1 = bt->prop_seg + bt->phase_seg1;
230 if (!bt->sjw)
231 bt->sjw = 1;
232 if (bt->sjw > btc->sjw_max ||
233 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
234 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
235 return -ERANGE;
236
237 brp64 = (u64)priv->clock.freq * (u64)bt->tq;
238 if (btc->brp_inc > 1)
239 do_div(brp64, btc->brp_inc);
240 brp64 += 500000000UL - 1;
241 do_div(brp64, 1000000000UL); /* the practicable BRP */
242 if (btc->brp_inc > 1)
243 brp64 *= btc->brp_inc;
244 bt->brp = (u32)brp64;
245
246 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
247 return -EINVAL;
248
249 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
250 bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
251 bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
252
253 return 0;
254}
255
Thadeu Lima de Souza Cascardo61463a32011-07-21 16:22:31 +0000256static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000257{
258 struct can_priv *priv = netdev_priv(dev);
259 int err;
260
261 /* Check if the CAN device has bit-timing parameters */
262 if (priv->bittiming_const) {
263
264 /* Non-expert mode? Check if the bitrate has been pre-defined */
265 if (!bt->tq)
266 /* Determine bit-timing parameters */
267 err = can_calc_bittiming(dev, bt);
268 else
269 /* Check bit-timing params and calculate proper brp */
270 err = can_fixup_bittiming(dev, bt);
271 if (err)
272 return err;
273 }
274
275 return 0;
276}
277
278/*
279 * Local echo of CAN messages
280 *
281 * CAN network devices *should* support a local echo functionality
282 * (see Documentation/networking/can.txt). To test the handling of CAN
283 * interfaces that do not support the local echo both driver types are
284 * implemented. In the case that the driver does not support the echo
285 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
286 * to perform the echo as a fallback solution.
287 */
288static void can_flush_echo_skb(struct net_device *dev)
289{
290 struct can_priv *priv = netdev_priv(dev);
291 struct net_device_stats *stats = &dev->stats;
292 int i;
293
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000294 for (i = 0; i < priv->echo_skb_max; i++) {
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000295 if (priv->echo_skb[i]) {
296 kfree_skb(priv->echo_skb[i]);
297 priv->echo_skb[i] = NULL;
298 stats->tx_dropped++;
299 stats->tx_aborted_errors++;
300 }
301 }
302}
303
304/*
305 * Put the skb on the stack to be looped backed locally lateron
306 *
307 * The function is typically called in the start_xmit function
308 * of the device driver. The driver must protect access to
309 * priv->echo_skb, if necessary.
310 */
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000311void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
312 unsigned int idx)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000313{
314 struct can_priv *priv = netdev_priv(dev);
315
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000316 BUG_ON(idx >= priv->echo_skb_max);
317
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000318 /* check flag whether this packet has to be looped back */
319 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
320 kfree_skb(skb);
321 return;
322 }
323
324 if (!priv->echo_skb[idx]) {
325 struct sock *srcsk = skb->sk;
326
327 if (atomic_read(&skb->users) != 1) {
328 struct sk_buff *old_skb = skb;
329
330 skb = skb_clone(old_skb, GFP_ATOMIC);
331 kfree_skb(old_skb);
332 if (!skb)
333 return;
334 } else
335 skb_orphan(skb);
336
337 skb->sk = srcsk;
338
339 /* make settings for echo to reduce code in irq context */
340 skb->protocol = htons(ETH_P_CAN);
341 skb->pkt_type = PACKET_BROADCAST;
342 skb->ip_summed = CHECKSUM_UNNECESSARY;
343 skb->dev = dev;
344
345 /* save this skb for tx interrupt echo handling */
346 priv->echo_skb[idx] = skb;
347 } else {
348 /* locking problem with netif_stop_queue() ?? */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100349 netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000350 kfree_skb(skb);
351 }
352}
353EXPORT_SYMBOL_GPL(can_put_echo_skb);
354
355/*
356 * Get the skb from the stack and loop it back locally
357 *
358 * The function is typically called when the TX done interrupt
359 * is handled in the device driver. The driver must protect
360 * access to priv->echo_skb, if necessary.
361 */
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200362unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000363{
364 struct can_priv *priv = netdev_priv(dev);
365
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000366 BUG_ON(idx >= priv->echo_skb_max);
367
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000368 if (priv->echo_skb[idx]) {
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200369 struct sk_buff *skb = priv->echo_skb[idx];
370 struct can_frame *cf = (struct can_frame *)skb->data;
371 u8 dlc = cf->can_dlc;
372
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000373 netif_rx(priv->echo_skb[idx]);
374 priv->echo_skb[idx] = NULL;
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200375
376 return dlc;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000377 }
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200378
379 return 0;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000380}
381EXPORT_SYMBOL_GPL(can_get_echo_skb);
382
383/*
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000384 * Remove the skb from the stack and free it.
385 *
386 * The function is typically called when TX failed.
387 */
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000388void can_free_echo_skb(struct net_device *dev, unsigned int idx)
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000389{
390 struct can_priv *priv = netdev_priv(dev);
391
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000392 BUG_ON(idx >= priv->echo_skb_max);
393
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000394 if (priv->echo_skb[idx]) {
395 kfree_skb(priv->echo_skb[idx]);
396 priv->echo_skb[idx] = NULL;
397 }
398}
399EXPORT_SYMBOL_GPL(can_free_echo_skb);
400
401/*
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000402 * CAN device restart for bus-off recovery
403 */
Marc Kleine-Budde77fc95a2012-02-06 22:21:13 +0100404static void can_restart(unsigned long data)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000405{
406 struct net_device *dev = (struct net_device *)data;
407 struct can_priv *priv = netdev_priv(dev);
408 struct net_device_stats *stats = &dev->stats;
409 struct sk_buff *skb;
410 struct can_frame *cf;
411 int err;
412
413 BUG_ON(netif_carrier_ok(dev));
414
415 /*
416 * No synchronization needed because the device is bus-off and
417 * no messages can come in or go out.
418 */
419 can_flush_echo_skb(dev);
420
421 /* send restart message upstream */
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700422 skb = alloc_can_err_skb(dev, &cf);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000423 if (skb == NULL) {
424 err = -ENOMEM;
Wolfgang Grandeggerb3d0df72009-07-20 04:06:40 +0000425 goto restart;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000426 }
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700427 cf->can_id |= CAN_ERR_RESTARTED;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000428
429 netif_rx(skb);
430
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000431 stats->rx_packets++;
432 stats->rx_bytes += cf->can_dlc;
433
Wolfgang Grandeggerb3d0df72009-07-20 04:06:40 +0000434restart:
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100435 netdev_dbg(dev, "restarted\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000436 priv->can_stats.restarts++;
437
438 /* Now restart the device */
439 err = priv->do_set_mode(dev, CAN_MODE_START);
440
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000441 netif_carrier_on(dev);
442 if (err)
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100443 netdev_err(dev, "Error %d during restart", err);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000444}
445
446int can_restart_now(struct net_device *dev)
447{
448 struct can_priv *priv = netdev_priv(dev);
449
450 /*
451 * A manual restart is only permitted if automatic restart is
452 * disabled and the device is in the bus-off state
453 */
454 if (priv->restart_ms)
455 return -EINVAL;
456 if (priv->state != CAN_STATE_BUS_OFF)
457 return -EBUSY;
458
459 /* Runs as soon as possible in the timer context */
460 mod_timer(&priv->restart_timer, jiffies);
461
462 return 0;
463}
464
465/*
466 * CAN bus-off
467 *
468 * This functions should be called when the device goes bus-off to
469 * tell the netif layer that no more packets can be sent or received.
470 * If enabled, a timer is started to trigger bus-off recovery.
471 */
472void can_bus_off(struct net_device *dev)
473{
474 struct can_priv *priv = netdev_priv(dev);
475
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100476 netdev_dbg(dev, "bus-off\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000477
478 netif_carrier_off(dev);
479 priv->can_stats.bus_off++;
480
481 if (priv->restart_ms)
482 mod_timer(&priv->restart_timer,
483 jiffies + (priv->restart_ms * HZ) / 1000);
484}
485EXPORT_SYMBOL_GPL(can_bus_off);
486
487static void can_setup(struct net_device *dev)
488{
489 dev->type = ARPHRD_CAN;
Oliver Hartkopp1e0625f2012-06-13 20:48:21 +0200490 dev->mtu = CAN_MTU;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000491 dev->hard_header_len = 0;
492 dev->addr_len = 0;
493 dev->tx_queue_len = 10;
494
495 /* New-style flags. */
496 dev->flags = IFF_NOARP;
Michał Mirosław34324dc2011-11-15 15:29:55 +0000497 dev->features = NETIF_F_HW_CSUM;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000498}
499
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700500struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
501{
502 struct sk_buff *skb;
503
504 skb = netdev_alloc_skb(dev, sizeof(struct can_frame));
505 if (unlikely(!skb))
506 return NULL;
507
508 skb->protocol = htons(ETH_P_CAN);
509 skb->pkt_type = PACKET_BROADCAST;
510 skb->ip_summed = CHECKSUM_UNNECESSARY;
511 *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
512 memset(*cf, 0, sizeof(struct can_frame));
513
514 return skb;
515}
516EXPORT_SYMBOL_GPL(alloc_can_skb);
517
518struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
519{
520 struct sk_buff *skb;
521
522 skb = alloc_can_skb(dev, cf);
523 if (unlikely(!skb))
524 return NULL;
525
526 (*cf)->can_id = CAN_ERR_FLAG;
527 (*cf)->can_dlc = CAN_ERR_DLC;
528
529 return skb;
530}
531EXPORT_SYMBOL_GPL(alloc_can_err_skb);
532
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000533/*
534 * Allocate and setup space for the CAN network device
535 */
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000536struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000537{
538 struct net_device *dev;
539 struct can_priv *priv;
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000540 int size;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000541
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000542 if (echo_skb_max)
543 size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
544 echo_skb_max * sizeof(struct sk_buff *);
545 else
546 size = sizeof_priv;
547
548 dev = alloc_netdev(size, "can%d", can_setup);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000549 if (!dev)
550 return NULL;
551
552 priv = netdev_priv(dev);
553
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000554 if (echo_skb_max) {
555 priv->echo_skb_max = echo_skb_max;
556 priv->echo_skb = (void *)priv +
557 ALIGN(sizeof_priv, sizeof(struct sk_buff *));
558 }
559
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000560 priv->state = CAN_STATE_STOPPED;
561
562 init_timer(&priv->restart_timer);
563
564 return dev;
565}
566EXPORT_SYMBOL_GPL(alloc_candev);
567
568/*
569 * Free space of the CAN network device
570 */
571void free_candev(struct net_device *dev)
572{
573 free_netdev(dev);
574}
575EXPORT_SYMBOL_GPL(free_candev);
576
577/*
578 * Common open function when the device gets opened.
579 *
580 * This function should be called in the open function of the device
581 * driver.
582 */
583int open_candev(struct net_device *dev)
584{
585 struct can_priv *priv = netdev_priv(dev);
586
587 if (!priv->bittiming.tq && !priv->bittiming.bitrate) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100588 netdev_err(dev, "bit-timing not yet defined\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000589 return -EINVAL;
590 }
591
Wolfgang Grandegger1b0d9222009-07-20 04:06:41 +0000592 /* Switch carrier on if device was stopped while in bus-off state */
593 if (!netif_carrier_ok(dev))
594 netif_carrier_on(dev);
595
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000596 setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev);
597
598 return 0;
599}
Wolfgang Grandegger128ced82009-05-30 07:55:48 +0000600EXPORT_SYMBOL_GPL(open_candev);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000601
602/*
603 * Common close function for cleanup before the device gets closed.
604 *
605 * This function should be called in the close function of the device
606 * driver.
607 */
608void close_candev(struct net_device *dev)
609{
610 struct can_priv *priv = netdev_priv(dev);
611
Alexander Steinab48b032012-11-27 08:52:34 +0100612 del_timer_sync(&priv->restart_timer);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000613 can_flush_echo_skb(dev);
614}
615EXPORT_SYMBOL_GPL(close_candev);
616
617/*
618 * CAN netlink interface
619 */
620static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
621 [IFLA_CAN_STATE] = { .type = NLA_U32 },
622 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
623 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
624 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
625 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
626 [IFLA_CAN_BITTIMING_CONST]
627 = { .len = sizeof(struct can_bittiming_const) },
628 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
Wolfgang Grandegger52c793f2010-02-22 22:21:17 +0000629 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000630};
631
632static int can_changelink(struct net_device *dev,
633 struct nlattr *tb[], struct nlattr *data[])
634{
635 struct can_priv *priv = netdev_priv(dev);
636 int err;
637
638 /* We need synchronization with dev->stop() */
639 ASSERT_RTNL();
640
641 if (data[IFLA_CAN_CTRLMODE]) {
642 struct can_ctrlmode *cm;
643
644 /* Do not allow changing controller mode while running */
645 if (dev->flags & IFF_UP)
646 return -EBUSY;
647 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
Christian Pellegrinad72c342010-01-14 07:08:34 +0000648 if (cm->flags & ~priv->ctrlmode_supported)
649 return -EOPNOTSUPP;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000650 priv->ctrlmode &= ~cm->mask;
651 priv->ctrlmode |= cm->flags;
652 }
653
654 if (data[IFLA_CAN_BITTIMING]) {
655 struct can_bittiming bt;
656
657 /* Do not allow changing bittiming while running */
658 if (dev->flags & IFF_UP)
659 return -EBUSY;
660 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
661 if ((!bt.bitrate && !bt.tq) || (bt.bitrate && bt.tq))
662 return -EINVAL;
663 err = can_get_bittiming(dev, &bt);
664 if (err)
665 return err;
666 memcpy(&priv->bittiming, &bt, sizeof(bt));
667
668 if (priv->do_set_bittiming) {
669 /* Finally, set the bit-timing registers */
670 err = priv->do_set_bittiming(dev);
671 if (err)
672 return err;
673 }
674 }
675
676 if (data[IFLA_CAN_RESTART_MS]) {
677 /* Do not allow changing restart delay while running */
678 if (dev->flags & IFF_UP)
679 return -EBUSY;
680 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
681 }
682
683 if (data[IFLA_CAN_RESTART]) {
684 /* Do not allow a restart while not running */
685 if (!(dev->flags & IFF_UP))
686 return -EINVAL;
687 err = can_restart_now(dev);
688 if (err)
689 return err;
690 }
691
692 return 0;
693}
694
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +0000695static size_t can_get_size(const struct net_device *dev)
696{
697 struct can_priv *priv = netdev_priv(dev);
698 size_t size;
699
700 size = nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
701 size += sizeof(struct can_ctrlmode); /* IFLA_CAN_CTRLMODE */
702 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
703 size += sizeof(struct can_bittiming); /* IFLA_CAN_BITTIMING */
704 size += sizeof(struct can_clock); /* IFLA_CAN_CLOCK */
Wolfgang Grandegger52c793f2010-02-22 22:21:17 +0000705 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
706 size += sizeof(struct can_berr_counter);
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +0000707 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
708 size += sizeof(struct can_bittiming_const);
709
710 return size;
711}
712
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000713static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
714{
715 struct can_priv *priv = netdev_priv(dev);
716 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
Wolfgang Grandegger52c793f2010-02-22 22:21:17 +0000717 struct can_berr_counter bec;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000718 enum can_state state = priv->state;
719
720 if (priv->do_get_state)
721 priv->do_get_state(dev, &state);
David S. Miller31e0e322012-04-01 20:21:11 -0400722 if (nla_put_u32(skb, IFLA_CAN_STATE, state) ||
723 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
724 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
725 nla_put(skb, IFLA_CAN_BITTIMING,
726 sizeof(priv->bittiming), &priv->bittiming) ||
727 nla_put(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock) ||
728 (priv->do_get_berr_counter &&
729 !priv->do_get_berr_counter(dev, &bec) &&
730 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
731 (priv->bittiming_const &&
732 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
733 sizeof(*priv->bittiming_const), priv->bittiming_const)))
734 goto nla_put_failure;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000735 return 0;
736
737nla_put_failure:
738 return -EMSGSIZE;
739}
740
Wolfgang Grandegger55369c02009-11-12 05:34:05 +0000741static size_t can_get_xstats_size(const struct net_device *dev)
742{
743 return sizeof(struct can_device_stats);
744}
745
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000746static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
747{
748 struct can_priv *priv = netdev_priv(dev);
749
David S. Miller31e0e322012-04-01 20:21:11 -0400750 if (nla_put(skb, IFLA_INFO_XSTATS,
751 sizeof(priv->can_stats), &priv->can_stats))
752 goto nla_put_failure;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000753 return 0;
754
755nla_put_failure:
756 return -EMSGSIZE;
757}
758
Eric W. Biederman81adee42009-11-08 00:53:51 -0800759static int can_newlink(struct net *src_net, struct net_device *dev,
Oliver Hartkopp993e6f22009-08-11 02:41:24 +0000760 struct nlattr *tb[], struct nlattr *data[])
761{
762 return -EOPNOTSUPP;
763}
764
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000765static struct rtnl_link_ops can_link_ops __read_mostly = {
766 .kind = "can",
767 .maxtype = IFLA_CAN_MAX,
768 .policy = can_policy,
769 .setup = can_setup,
Oliver Hartkopp993e6f22009-08-11 02:41:24 +0000770 .newlink = can_newlink,
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000771 .changelink = can_changelink,
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +0000772 .get_size = can_get_size,
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000773 .fill_info = can_fill_info,
Wolfgang Grandegger55369c02009-11-12 05:34:05 +0000774 .get_xstats_size = can_get_xstats_size,
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000775 .fill_xstats = can_fill_xstats,
776};
777
778/*
779 * Register the CAN network device
780 */
781int register_candev(struct net_device *dev)
782{
783 dev->rtnl_link_ops = &can_link_ops;
784 return register_netdev(dev);
785}
786EXPORT_SYMBOL_GPL(register_candev);
787
788/*
789 * Unregister the CAN network device
790 */
791void unregister_candev(struct net_device *dev)
792{
793 unregister_netdev(dev);
794}
795EXPORT_SYMBOL_GPL(unregister_candev);
796
797static __init int can_dev_init(void)
798{
799 int err;
800
801 err = rtnl_link_register(&can_link_ops);
802 if (!err)
803 printk(KERN_INFO MOD_DESC "\n");
804
805 return err;
806}
807module_init(can_dev_init);
808
809static __exit void can_dev_exit(void)
810{
811 rtnl_link_unregister(&can_link_ops);
812}
813module_exit(can_dev_exit);
814
815MODULE_ALIAS_RTNL_LINK("can");