]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/net/can/dev.c
can: only send bitrate data via netlink when available
[mirror_ubuntu-zesty-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
05780d98 16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
39549eef
WG
17 */
18
19#include <linux/module.h>
20#include <linux/kernel.h>
5a0e3ad6 21#include <linux/slab.h>
39549eef
WG
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/can.h>
25#include <linux/can/dev.h>
156c2bb9 26#include <linux/can/skb.h>
39549eef 27#include <linux/can/netlink.h>
a1ef7bd9 28#include <linux/can/led.h>
39549eef
WG
29#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
1e0625fa
OH
37/* 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
39549eef
WG
70#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) {
aabdfd6a
WG
167 netdev_err(dev,
168 "bitrate error %ld.%ld%% too high\n",
169 error / 10, error % 10);
39549eef
WG
170 return -EDOM;
171 } else {
aabdfd6a
WG
172 netdev_warn(dev, "bitrate error %ld.%ld%%\n",
173 error / 10, error % 10);
39549eef
WG
174 }
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;
2e114374
OH
187
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
39549eef
WG
200 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{
aabdfd6a 209 netdev_err(dev, "bit-timing calculation not available\n");
39549eef
WG
210 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
61463a30 257static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt)
39549eef
WG
258{
259 struct can_priv *priv = netdev_priv(dev);
260 int err;
261
262 /* Check if the CAN device has bit-timing parameters */
263 if (priv->bittiming_const) {
264
265 /* Non-expert mode? Check if the bitrate has been pre-defined */
266 if (!bt->tq)
267 /* Determine bit-timing parameters */
268 err = can_calc_bittiming(dev, bt);
269 else
270 /* Check bit-timing params and calculate proper brp */
271 err = can_fixup_bittiming(dev, bt);
272 if (err)
273 return err;
274 }
275
276 return 0;
277}
278
279/*
280 * Local echo of CAN messages
281 *
282 * CAN network devices *should* support a local echo functionality
283 * (see Documentation/networking/can.txt). To test the handling of CAN
284 * interfaces that do not support the local echo both driver types are
285 * implemented. In the case that the driver does not support the echo
286 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
287 * to perform the echo as a fallback solution.
288 */
289static void can_flush_echo_skb(struct net_device *dev)
290{
291 struct can_priv *priv = netdev_priv(dev);
292 struct net_device_stats *stats = &dev->stats;
293 int i;
294
a6e4bc53 295 for (i = 0; i < priv->echo_skb_max; i++) {
39549eef
WG
296 if (priv->echo_skb[i]) {
297 kfree_skb(priv->echo_skb[i]);
298 priv->echo_skb[i] = NULL;
299 stats->tx_dropped++;
300 stats->tx_aborted_errors++;
301 }
302 }
303}
304
305/*
306 * Put the skb on the stack to be looped backed locally lateron
307 *
308 * The function is typically called in the start_xmit function
309 * of the device driver. The driver must protect access to
310 * priv->echo_skb, if necessary.
311 */
a6e4bc53
WG
312void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
313 unsigned int idx)
39549eef
WG
314{
315 struct can_priv *priv = netdev_priv(dev);
316
a6e4bc53
WG
317 BUG_ON(idx >= priv->echo_skb_max);
318
39549eef 319 /* check flag whether this packet has to be looped back */
a94bc9c4
OH
320 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
321 (skb->protocol != htons(ETH_P_CAN) &&
322 skb->protocol != htons(ETH_P_CANFD))) {
39549eef
WG
323 kfree_skb(skb);
324 return;
325 }
326
327 if (!priv->echo_skb[idx]) {
39549eef 328
0ae89beb
OH
329 skb = can_create_echo_skb(skb);
330 if (!skb)
331 return;
39549eef
WG
332
333 /* make settings for echo to reduce code in irq context */
39549eef
WG
334 skb->pkt_type = PACKET_BROADCAST;
335 skb->ip_summed = CHECKSUM_UNNECESSARY;
336 skb->dev = dev;
337
338 /* save this skb for tx interrupt echo handling */
339 priv->echo_skb[idx] = skb;
340 } else {
341 /* locking problem with netif_stop_queue() ?? */
aabdfd6a 342 netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
39549eef
WG
343 kfree_skb(skb);
344 }
345}
346EXPORT_SYMBOL_GPL(can_put_echo_skb);
347
348/*
349 * Get the skb from the stack and loop it back locally
350 *
351 * The function is typically called when the TX done interrupt
352 * is handled in the device driver. The driver must protect
353 * access to priv->echo_skb, if necessary.
354 */
cf5046b3 355unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
39549eef
WG
356{
357 struct can_priv *priv = netdev_priv(dev);
358
a6e4bc53
WG
359 BUG_ON(idx >= priv->echo_skb_max);
360
39e3ab6f 361 if (priv->echo_skb[idx]) {
cf5046b3
MKB
362 struct sk_buff *skb = priv->echo_skb[idx];
363 struct can_frame *cf = (struct can_frame *)skb->data;
364 u8 dlc = cf->can_dlc;
365
39549eef
WG
366 netif_rx(priv->echo_skb[idx]);
367 priv->echo_skb[idx] = NULL;
cf5046b3
MKB
368
369 return dlc;
39549eef 370 }
cf5046b3
MKB
371
372 return 0;
39549eef
WG
373}
374EXPORT_SYMBOL_GPL(can_get_echo_skb);
375
39e3ab6f
WG
376/*
377 * Remove the skb from the stack and free it.
378 *
379 * The function is typically called when TX failed.
380 */
a6e4bc53 381void can_free_echo_skb(struct net_device *dev, unsigned int idx)
39e3ab6f
WG
382{
383 struct can_priv *priv = netdev_priv(dev);
384
a6e4bc53
WG
385 BUG_ON(idx >= priv->echo_skb_max);
386
39e3ab6f
WG
387 if (priv->echo_skb[idx]) {
388 kfree_skb(priv->echo_skb[idx]);
389 priv->echo_skb[idx] = NULL;
390 }
391}
392EXPORT_SYMBOL_GPL(can_free_echo_skb);
393
39549eef
WG
394/*
395 * CAN device restart for bus-off recovery
396 */
77fc95a3 397static void can_restart(unsigned long data)
39549eef
WG
398{
399 struct net_device *dev = (struct net_device *)data;
400 struct can_priv *priv = netdev_priv(dev);
401 struct net_device_stats *stats = &dev->stats;
402 struct sk_buff *skb;
403 struct can_frame *cf;
404 int err;
405
406 BUG_ON(netif_carrier_ok(dev));
407
408 /*
409 * No synchronization needed because the device is bus-off and
410 * no messages can come in or go out.
411 */
412 can_flush_echo_skb(dev);
413
414 /* send restart message upstream */
7b6856a0 415 skb = alloc_can_err_skb(dev, &cf);
39549eef
WG
416 if (skb == NULL) {
417 err = -ENOMEM;
b3d0df7c 418 goto restart;
39549eef 419 }
7b6856a0 420 cf->can_id |= CAN_ERR_RESTARTED;
39549eef
WG
421
422 netif_rx(skb);
423
39549eef
WG
424 stats->rx_packets++;
425 stats->rx_bytes += cf->can_dlc;
426
b3d0df7c 427restart:
aabdfd6a 428 netdev_dbg(dev, "restarted\n");
39549eef
WG
429 priv->can_stats.restarts++;
430
431 /* Now restart the device */
432 err = priv->do_set_mode(dev, CAN_MODE_START);
433
39549eef
WG
434 netif_carrier_on(dev);
435 if (err)
aabdfd6a 436 netdev_err(dev, "Error %d during restart", err);
39549eef
WG
437}
438
439int can_restart_now(struct net_device *dev)
440{
441 struct can_priv *priv = netdev_priv(dev);
442
443 /*
444 * A manual restart is only permitted if automatic restart is
445 * disabled and the device is in the bus-off state
446 */
447 if (priv->restart_ms)
448 return -EINVAL;
449 if (priv->state != CAN_STATE_BUS_OFF)
450 return -EBUSY;
451
452 /* Runs as soon as possible in the timer context */
453 mod_timer(&priv->restart_timer, jiffies);
454
455 return 0;
456}
457
458/*
459 * CAN bus-off
460 *
461 * This functions should be called when the device goes bus-off to
462 * tell the netif layer that no more packets can be sent or received.
463 * If enabled, a timer is started to trigger bus-off recovery.
464 */
465void can_bus_off(struct net_device *dev)
466{
467 struct can_priv *priv = netdev_priv(dev);
468
aabdfd6a 469 netdev_dbg(dev, "bus-off\n");
39549eef
WG
470
471 netif_carrier_off(dev);
472 priv->can_stats.bus_off++;
473
474 if (priv->restart_ms)
475 mod_timer(&priv->restart_timer,
476 jiffies + (priv->restart_ms * HZ) / 1000);
477}
478EXPORT_SYMBOL_GPL(can_bus_off);
479
480static void can_setup(struct net_device *dev)
481{
482 dev->type = ARPHRD_CAN;
1e0625fa 483 dev->mtu = CAN_MTU;
39549eef
WG
484 dev->hard_header_len = 0;
485 dev->addr_len = 0;
486 dev->tx_queue_len = 10;
487
488 /* New-style flags. */
489 dev->flags = IFF_NOARP;
34324dc2 490 dev->features = NETIF_F_HW_CSUM;
39549eef
WG
491}
492
7b6856a0
WG
493struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
494{
495 struct sk_buff *skb;
496
156c2bb9
OH
497 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
498 sizeof(struct can_frame));
7b6856a0
WG
499 if (unlikely(!skb))
500 return NULL;
501
502 skb->protocol = htons(ETH_P_CAN);
503 skb->pkt_type = PACKET_BROADCAST;
504 skb->ip_summed = CHECKSUM_UNNECESSARY;
156c2bb9 505
2bf3440d
OH
506 can_skb_reserve(skb);
507 can_skb_prv(skb)->ifindex = dev->ifindex;
156c2bb9 508
7b6856a0
WG
509 *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
510 memset(*cf, 0, sizeof(struct can_frame));
511
512 return skb;
513}
514EXPORT_SYMBOL_GPL(alloc_can_skb);
515
cb2518ca
SG
516struct sk_buff *alloc_canfd_skb(struct net_device *dev,
517 struct canfd_frame **cfd)
518{
519 struct sk_buff *skb;
520
521 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
522 sizeof(struct canfd_frame));
523 if (unlikely(!skb))
524 return NULL;
525
526 skb->protocol = htons(ETH_P_CANFD);
527 skb->pkt_type = PACKET_BROADCAST;
528 skb->ip_summed = CHECKSUM_UNNECESSARY;
529
530 can_skb_reserve(skb);
531 can_skb_prv(skb)->ifindex = dev->ifindex;
532
533 *cfd = (struct canfd_frame *)skb_put(skb, sizeof(struct canfd_frame));
534 memset(*cfd, 0, sizeof(struct canfd_frame));
535
536 return skb;
537}
538EXPORT_SYMBOL_GPL(alloc_canfd_skb);
539
7b6856a0
WG
540struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
541{
542 struct sk_buff *skb;
543
544 skb = alloc_can_skb(dev, cf);
545 if (unlikely(!skb))
546 return NULL;
547
548 (*cf)->can_id = CAN_ERR_FLAG;
549 (*cf)->can_dlc = CAN_ERR_DLC;
550
551 return skb;
552}
553EXPORT_SYMBOL_GPL(alloc_can_err_skb);
554
39549eef
WG
555/*
556 * Allocate and setup space for the CAN network device
557 */
a6e4bc53 558struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
39549eef
WG
559{
560 struct net_device *dev;
561 struct can_priv *priv;
a6e4bc53 562 int size;
39549eef 563
a6e4bc53
WG
564 if (echo_skb_max)
565 size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
566 echo_skb_max * sizeof(struct sk_buff *);
567 else
568 size = sizeof_priv;
569
570 dev = alloc_netdev(size, "can%d", can_setup);
39549eef
WG
571 if (!dev)
572 return NULL;
573
574 priv = netdev_priv(dev);
575
a6e4bc53
WG
576 if (echo_skb_max) {
577 priv->echo_skb_max = echo_skb_max;
578 priv->echo_skb = (void *)priv +
579 ALIGN(sizeof_priv, sizeof(struct sk_buff *));
580 }
581
39549eef
WG
582 priv->state = CAN_STATE_STOPPED;
583
584 init_timer(&priv->restart_timer);
585
586 return dev;
587}
588EXPORT_SYMBOL_GPL(alloc_candev);
589
590/*
591 * Free space of the CAN network device
592 */
593void free_candev(struct net_device *dev)
594{
595 free_netdev(dev);
596}
597EXPORT_SYMBOL_GPL(free_candev);
598
599/*
600 * Common open function when the device gets opened.
601 *
602 * This function should be called in the open function of the device
603 * driver.
604 */
605int open_candev(struct net_device *dev)
606{
607 struct can_priv *priv = netdev_priv(dev);
608
b30749fd 609 if (!priv->bittiming.bitrate) {
aabdfd6a 610 netdev_err(dev, "bit-timing not yet defined\n");
39549eef
WG
611 return -EINVAL;
612 }
613
1b0d9224
WG
614 /* Switch carrier on if device was stopped while in bus-off state */
615 if (!netif_carrier_ok(dev))
616 netif_carrier_on(dev);
617
39549eef
WG
618 setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev);
619
620 return 0;
621}
128ced8f 622EXPORT_SYMBOL_GPL(open_candev);
39549eef
WG
623
624/*
625 * Common close function for cleanup before the device gets closed.
626 *
627 * This function should be called in the close function of the device
628 * driver.
629 */
630void close_candev(struct net_device *dev)
631{
632 struct can_priv *priv = netdev_priv(dev);
633
ab48b03e 634 del_timer_sync(&priv->restart_timer);
39549eef
WG
635 can_flush_echo_skb(dev);
636}
637EXPORT_SYMBOL_GPL(close_candev);
638
639/*
640 * CAN netlink interface
641 */
642static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
643 [IFLA_CAN_STATE] = { .type = NLA_U32 },
644 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
645 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
646 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
647 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
648 [IFLA_CAN_BITTIMING_CONST]
649 = { .len = sizeof(struct can_bittiming_const) },
650 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
52c793f2 651 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
39549eef
WG
652};
653
654static int can_changelink(struct net_device *dev,
655 struct nlattr *tb[], struct nlattr *data[])
656{
657 struct can_priv *priv = netdev_priv(dev);
658 int err;
659
660 /* We need synchronization with dev->stop() */
661 ASSERT_RTNL();
662
39549eef
WG
663 if (data[IFLA_CAN_BITTIMING]) {
664 struct can_bittiming bt;
665
666 /* Do not allow changing bittiming while running */
667 if (dev->flags & IFF_UP)
668 return -EBUSY;
669 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
670 if ((!bt.bitrate && !bt.tq) || (bt.bitrate && bt.tq))
671 return -EINVAL;
672 err = can_get_bittiming(dev, &bt);
673 if (err)
674 return err;
675 memcpy(&priv->bittiming, &bt, sizeof(bt));
676
677 if (priv->do_set_bittiming) {
678 /* Finally, set the bit-timing registers */
679 err = priv->do_set_bittiming(dev);
680 if (err)
681 return err;
682 }
683 }
684
49cb5c0e
MKB
685 if (data[IFLA_CAN_CTRLMODE]) {
686 struct can_ctrlmode *cm;
687
688 /* Do not allow changing controller mode while running */
689 if (dev->flags & IFF_UP)
690 return -EBUSY;
691 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
692 if (cm->flags & ~priv->ctrlmode_supported)
693 return -EOPNOTSUPP;
694 priv->ctrlmode &= ~cm->mask;
695 priv->ctrlmode |= cm->flags;
696 }
697
39549eef
WG
698 if (data[IFLA_CAN_RESTART_MS]) {
699 /* Do not allow changing restart delay while running */
700 if (dev->flags & IFF_UP)
701 return -EBUSY;
702 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
703 }
704
705 if (data[IFLA_CAN_RESTART]) {
706 /* Do not allow a restart while not running */
707 if (!(dev->flags & IFF_UP))
708 return -EINVAL;
709 err = can_restart_now(dev);
710 if (err)
711 return err;
712 }
713
714 return 0;
715}
716
53a0ef86
WG
717static size_t can_get_size(const struct net_device *dev)
718{
719 struct can_priv *priv = netdev_priv(dev);
c13c64d8
MKB
720 size_t size = 0;
721
b30749fd
OH
722 if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
723 size += nla_total_size(sizeof(struct can_bittiming));
c13c64d8 724 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
fe119a05 725 size += nla_total_size(sizeof(struct can_bittiming_const));
c13c64d8
MKB
726 size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
727 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
728 size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
729 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
730 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
731 size += nla_total_size(sizeof(struct can_berr_counter));
53a0ef86
WG
732
733 return size;
734}
735
39549eef
WG
736static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
737{
738 struct can_priv *priv = netdev_priv(dev);
739 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
52c793f2 740 struct can_berr_counter bec;
39549eef
WG
741 enum can_state state = priv->state;
742
743 if (priv->do_get_state)
744 priv->do_get_state(dev, &state);
b30749fd
OH
745 if ((priv->bittiming.bitrate &&
746 nla_put(skb, IFLA_CAN_BITTIMING,
747 sizeof(priv->bittiming), &priv->bittiming)) ||
57a59b9e
MKB
748 (priv->bittiming_const &&
749 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
750 sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
31e0e328 751 nla_put(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock) ||
57a59b9e
MKB
752 nla_put_u32(skb, IFLA_CAN_STATE, state) ||
753 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
754 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
31e0e328
DM
755 (priv->do_get_berr_counter &&
756 !priv->do_get_berr_counter(dev, &bec) &&
57a59b9e
MKB
757 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)))
758 return -EMSGSIZE;
39549eef 759 return 0;
39549eef
WG
760}
761
55369c0a
WG
762static size_t can_get_xstats_size(const struct net_device *dev)
763{
764 return sizeof(struct can_device_stats);
765}
766
39549eef
WG
767static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
768{
769 struct can_priv *priv = netdev_priv(dev);
770
31e0e328
DM
771 if (nla_put(skb, IFLA_INFO_XSTATS,
772 sizeof(priv->can_stats), &priv->can_stats))
773 goto nla_put_failure;
39549eef
WG
774 return 0;
775
776nla_put_failure:
777 return -EMSGSIZE;
778}
779
81adee47 780static int can_newlink(struct net *src_net, struct net_device *dev,
993e6f2f
OH
781 struct nlattr *tb[], struct nlattr *data[])
782{
783 return -EOPNOTSUPP;
784}
785
39549eef
WG
786static struct rtnl_link_ops can_link_ops __read_mostly = {
787 .kind = "can",
788 .maxtype = IFLA_CAN_MAX,
789 .policy = can_policy,
790 .setup = can_setup,
993e6f2f 791 .newlink = can_newlink,
39549eef 792 .changelink = can_changelink,
53a0ef86 793 .get_size = can_get_size,
39549eef 794 .fill_info = can_fill_info,
55369c0a 795 .get_xstats_size = can_get_xstats_size,
39549eef
WG
796 .fill_xstats = can_fill_xstats,
797};
798
799/*
800 * Register the CAN network device
801 */
802int register_candev(struct net_device *dev)
803{
804 dev->rtnl_link_ops = &can_link_ops;
805 return register_netdev(dev);
806}
807EXPORT_SYMBOL_GPL(register_candev);
808
809/*
810 * Unregister the CAN network device
811 */
812void unregister_candev(struct net_device *dev)
813{
814 unregister_netdev(dev);
815}
816EXPORT_SYMBOL_GPL(unregister_candev);
817
bf03a537
KVD
818/*
819 * Test if a network device is a candev based device
820 * and return the can_priv* if so.
821 */
822struct can_priv *safe_candev_priv(struct net_device *dev)
823{
824 if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
825 return NULL;
826
827 return netdev_priv(dev);
828}
829EXPORT_SYMBOL_GPL(safe_candev_priv);
830
39549eef
WG
831static __init int can_dev_init(void)
832{
833 int err;
834
a1ef7bd9
KVD
835 can_led_notifier_init();
836
39549eef
WG
837 err = rtnl_link_register(&can_link_ops);
838 if (!err)
839 printk(KERN_INFO MOD_DESC "\n");
840
841 return err;
842}
843module_init(can_dev_init);
844
845static __exit void can_dev_exit(void)
846{
847 rtnl_link_unregister(&can_link_ops);
a1ef7bd9
KVD
848
849 can_led_notifier_exit();
39549eef
WG
850}
851module_exit(can_dev_exit);
852
853MODULE_ALIAS_RTNL_LINK("can");