]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/can/dev.c
candev: add/update helpers for CAN FD
[mirror_ubuntu-bionic-kernel.git] / drivers / net / can / dev.c
CommitLineData
39549eef
WG
1/*
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>
5a0e3ad6 22#include <linux/slab.h>
39549eef
WG
23#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
1e0625fa
OH
36/* 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
39549eef
WG
69#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) {
aabdfd6a
WG
166 netdev_err(dev,
167 "bitrate error %ld.%ld%% too high\n",
168 error / 10, error % 10);
39549eef
WG
169 return -EDOM;
170 } else {
aabdfd6a
WG
171 netdev_warn(dev, "bitrate error %ld.%ld%%\n",
172 error / 10, error % 10);
39549eef
WG
173 }
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;
2e114374
OH
186
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
39549eef
WG
199 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{
aabdfd6a 208 netdev_err(dev, "bit-timing calculation not available\n");
39549eef
WG
209 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
61463a30 256static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt)
39549eef
WG
257{
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
a6e4bc53 294 for (i = 0; i < priv->echo_skb_max; i++) {
39549eef
WG
295 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 */
a6e4bc53
WG
311void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
312 unsigned int idx)
39549eef
WG
313{
314 struct can_priv *priv = netdev_priv(dev);
315
a6e4bc53
WG
316 BUG_ON(idx >= priv->echo_skb_max);
317
39549eef
WG
318 /* 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() ?? */
aabdfd6a 349 netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
39549eef
WG
350 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 */
cf5046b3 362unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
39549eef
WG
363{
364 struct can_priv *priv = netdev_priv(dev);
365
a6e4bc53
WG
366 BUG_ON(idx >= priv->echo_skb_max);
367
39e3ab6f 368 if (priv->echo_skb[idx]) {
cf5046b3
MKB
369 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
39549eef
WG
373 netif_rx(priv->echo_skb[idx]);
374 priv->echo_skb[idx] = NULL;
cf5046b3
MKB
375
376 return dlc;
39549eef 377 }
cf5046b3
MKB
378
379 return 0;
39549eef
WG
380}
381EXPORT_SYMBOL_GPL(can_get_echo_skb);
382
39e3ab6f
WG
383/*
384 * Remove the skb from the stack and free it.
385 *
386 * The function is typically called when TX failed.
387 */
a6e4bc53 388void can_free_echo_skb(struct net_device *dev, unsigned int idx)
39e3ab6f
WG
389{
390 struct can_priv *priv = netdev_priv(dev);
391
a6e4bc53
WG
392 BUG_ON(idx >= priv->echo_skb_max);
393
39e3ab6f
WG
394 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
39549eef
WG
401/*
402 * CAN device restart for bus-off recovery
403 */
404void can_restart(unsigned long data)
405{
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 */
7b6856a0 422 skb = alloc_can_err_skb(dev, &cf);
39549eef
WG
423 if (skb == NULL) {
424 err = -ENOMEM;
b3d0df7c 425 goto restart;
39549eef 426 }
7b6856a0 427 cf->can_id |= CAN_ERR_RESTARTED;
39549eef
WG
428
429 netif_rx(skb);
430
39549eef
WG
431 stats->rx_packets++;
432 stats->rx_bytes += cf->can_dlc;
433
b3d0df7c 434restart:
aabdfd6a 435 netdev_dbg(dev, "restarted\n");
39549eef
WG
436 priv->can_stats.restarts++;
437
438 /* Now restart the device */
439 err = priv->do_set_mode(dev, CAN_MODE_START);
440
39549eef
WG
441 netif_carrier_on(dev);
442 if (err)
aabdfd6a 443 netdev_err(dev, "Error %d during restart", err);
39549eef
WG
444}
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
aabdfd6a 476 netdev_dbg(dev, "bus-off\n");
39549eef
WG
477
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;
1e0625fa 490 dev->mtu = CAN_MTU;
39549eef
WG
491 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;
34324dc2 497 dev->features = NETIF_F_HW_CSUM;
39549eef
WG
498}
499
7b6856a0
WG
500struct 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
39549eef
WG
533/*
534 * Allocate and setup space for the CAN network device
535 */
a6e4bc53 536struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
39549eef
WG
537{
538 struct net_device *dev;
539 struct can_priv *priv;
a6e4bc53 540 int size;
39549eef 541
a6e4bc53
WG
542 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);
39549eef
WG
549 if (!dev)
550 return NULL;
551
552 priv = netdev_priv(dev);
553
a6e4bc53
WG
554 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
39549eef
WG
560 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) {
aabdfd6a 588 netdev_err(dev, "bit-timing not yet defined\n");
39549eef
WG
589 return -EINVAL;
590 }
591
1b0d9224
WG
592 /* Switch carrier on if device was stopped while in bus-off state */
593 if (!netif_carrier_ok(dev))
594 netif_carrier_on(dev);
595
39549eef
WG
596 setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev);
597
598 return 0;
599}
128ced8f 600EXPORT_SYMBOL_GPL(open_candev);
39549eef
WG
601
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
612 if (del_timer_sync(&priv->restart_timer))
613 dev_put(dev);
614 can_flush_echo_skb(dev);
615}
616EXPORT_SYMBOL_GPL(close_candev);
617
618/*
619 * CAN netlink interface
620 */
621static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
622 [IFLA_CAN_STATE] = { .type = NLA_U32 },
623 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
624 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
625 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
626 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
627 [IFLA_CAN_BITTIMING_CONST]
628 = { .len = sizeof(struct can_bittiming_const) },
629 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
52c793f2 630 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
39549eef
WG
631};
632
633static int can_changelink(struct net_device *dev,
634 struct nlattr *tb[], struct nlattr *data[])
635{
636 struct can_priv *priv = netdev_priv(dev);
637 int err;
638
639 /* We need synchronization with dev->stop() */
640 ASSERT_RTNL();
641
642 if (data[IFLA_CAN_CTRLMODE]) {
643 struct can_ctrlmode *cm;
644
645 /* Do not allow changing controller mode while running */
646 if (dev->flags & IFF_UP)
647 return -EBUSY;
648 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
ad72c347
CP
649 if (cm->flags & ~priv->ctrlmode_supported)
650 return -EOPNOTSUPP;
39549eef
WG
651 priv->ctrlmode &= ~cm->mask;
652 priv->ctrlmode |= cm->flags;
653 }
654
655 if (data[IFLA_CAN_BITTIMING]) {
656 struct can_bittiming bt;
657
658 /* Do not allow changing bittiming while running */
659 if (dev->flags & IFF_UP)
660 return -EBUSY;
661 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
662 if ((!bt.bitrate && !bt.tq) || (bt.bitrate && bt.tq))
663 return -EINVAL;
664 err = can_get_bittiming(dev, &bt);
665 if (err)
666 return err;
667 memcpy(&priv->bittiming, &bt, sizeof(bt));
668
669 if (priv->do_set_bittiming) {
670 /* Finally, set the bit-timing registers */
671 err = priv->do_set_bittiming(dev);
672 if (err)
673 return err;
674 }
675 }
676
677 if (data[IFLA_CAN_RESTART_MS]) {
678 /* Do not allow changing restart delay while running */
679 if (dev->flags & IFF_UP)
680 return -EBUSY;
681 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
682 }
683
684 if (data[IFLA_CAN_RESTART]) {
685 /* Do not allow a restart while not running */
686 if (!(dev->flags & IFF_UP))
687 return -EINVAL;
688 err = can_restart_now(dev);
689 if (err)
690 return err;
691 }
692
693 return 0;
694}
695
53a0ef86
WG
696static size_t can_get_size(const struct net_device *dev)
697{
698 struct can_priv *priv = netdev_priv(dev);
699 size_t size;
700
701 size = nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
702 size += sizeof(struct can_ctrlmode); /* IFLA_CAN_CTRLMODE */
703 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
704 size += sizeof(struct can_bittiming); /* IFLA_CAN_BITTIMING */
705 size += sizeof(struct can_clock); /* IFLA_CAN_CLOCK */
52c793f2
WG
706 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
707 size += sizeof(struct can_berr_counter);
53a0ef86
WG
708 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
709 size += sizeof(struct can_bittiming_const);
710
711 return size;
712}
713
39549eef
WG
714static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
715{
716 struct can_priv *priv = netdev_priv(dev);
717 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
52c793f2 718 struct can_berr_counter bec;
39549eef
WG
719 enum can_state state = priv->state;
720
721 if (priv->do_get_state)
722 priv->do_get_state(dev, &state);
31e0e328
DM
723 if (nla_put_u32(skb, IFLA_CAN_STATE, state) ||
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,
727 sizeof(priv->bittiming), &priv->bittiming) ||
728 nla_put(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock) ||
729 (priv->do_get_berr_counter &&
730 !priv->do_get_berr_counter(dev, &bec) &&
731 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
732 (priv->bittiming_const &&
733 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
734 sizeof(*priv->bittiming_const), priv->bittiming_const)))
735 goto nla_put_failure;
39549eef
WG
736 return 0;
737
738nla_put_failure:
739 return -EMSGSIZE;
740}
741
55369c0a
WG
742static size_t can_get_xstats_size(const struct net_device *dev)
743{
744 return sizeof(struct can_device_stats);
745}
746
39549eef
WG
747static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
748{
749 struct can_priv *priv = netdev_priv(dev);
750
31e0e328
DM
751 if (nla_put(skb, IFLA_INFO_XSTATS,
752 sizeof(priv->can_stats), &priv->can_stats))
753 goto nla_put_failure;
39549eef
WG
754 return 0;
755
756nla_put_failure:
757 return -EMSGSIZE;
758}
759
81adee47 760static int can_newlink(struct net *src_net, struct net_device *dev,
993e6f2f
OH
761 struct nlattr *tb[], struct nlattr *data[])
762{
763 return -EOPNOTSUPP;
764}
765
39549eef
WG
766static struct rtnl_link_ops can_link_ops __read_mostly = {
767 .kind = "can",
768 .maxtype = IFLA_CAN_MAX,
769 .policy = can_policy,
770 .setup = can_setup,
993e6f2f 771 .newlink = can_newlink,
39549eef 772 .changelink = can_changelink,
53a0ef86 773 .get_size = can_get_size,
39549eef 774 .fill_info = can_fill_info,
55369c0a 775 .get_xstats_size = can_get_xstats_size,
39549eef
WG
776 .fill_xstats = can_fill_xstats,
777};
778
779/*
780 * Register the CAN network device
781 */
782int register_candev(struct net_device *dev)
783{
784 dev->rtnl_link_ops = &can_link_ops;
785 return register_netdev(dev);
786}
787EXPORT_SYMBOL_GPL(register_candev);
788
789/*
790 * Unregister the CAN network device
791 */
792void unregister_candev(struct net_device *dev)
793{
794 unregister_netdev(dev);
795}
796EXPORT_SYMBOL_GPL(unregister_candev);
797
798static __init int can_dev_init(void)
799{
800 int err;
801
802 err = rtnl_link_register(&can_link_ops);
803 if (!err)
804 printk(KERN_INFO MOD_DESC "\n");
805
806 return err;
807}
808module_init(can_dev_init);
809
810static __exit void can_dev_exit(void)
811{
812 rtnl_link_unregister(&can_link_ops);
813}
814module_exit(can_dev_exit);
815
816MODULE_ALIAS_RTNL_LINK("can");