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>
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
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/netdevice.h>
23 #include <linux/if_arp.h>
24 #include <linux/workqueue.h>
25 #include <linux/can.h>
26 #include <linux/can/dev.h>
27 #include <linux/can/skb.h>
28 #include <linux/can/netlink.h>
29 #include <linux/can/led.h>
30 #include <net/rtnetlink.h>
32 #define MOD_DESC "CAN device driver interface"
34 MODULE_DESCRIPTION(MOD_DESC);
35 MODULE_LICENSE("GPL v2");
36 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
38 /* CAN DLC to real data length conversion helpers */
40 static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
41 8, 12, 16, 20, 24, 32, 48, 64};
43 /* get data length from can_dlc with sanitized can_dlc */
44 u8 can_dlc2len(u8 can_dlc)
46 return dlc2len[can_dlc & 0x0F];
48 EXPORT_SYMBOL_GPL(can_dlc2len);
50 static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
51 9, 9, 9, 9, /* 9 - 12 */
52 10, 10, 10, 10, /* 13 - 16 */
53 11, 11, 11, 11, /* 17 - 20 */
54 12, 12, 12, 12, /* 21 - 24 */
55 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
56 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
57 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
58 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
59 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
61 /* map the sanitized data length to an appropriate data length code */
62 u8 can_len2dlc(u8 len)
64 if (unlikely(len > 64))
69 EXPORT_SYMBOL_GPL(can_len2dlc);
71 #ifdef CONFIG_CAN_CALC_BITTIMING
72 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
75 * Bit-timing calculation derived from:
77 * Code based on LinCAN sources and H8S2638 project
78 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
79 * Copyright 2005 Stanislav Marek
80 * email: pisa@cmp.felk.cvut.cz
82 * Calculates proper bit-timing parameters for a specified bit-rate
83 * and sample-point, which can then be used to set the bit-timing
84 * registers of the CAN controller. You can find more information
85 * in the header file linux/can/netlink.h.
87 static int can_update_spt(const struct can_bittiming_const *btc,
88 int sampl_pt, int tseg, int *tseg1, int *tseg2)
90 *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
91 if (*tseg2 < btc->tseg2_min)
92 *tseg2 = btc->tseg2_min;
93 if (*tseg2 > btc->tseg2_max)
94 *tseg2 = btc->tseg2_max;
95 *tseg1 = tseg - *tseg2;
96 if (*tseg1 > btc->tseg1_max) {
97 *tseg1 = btc->tseg1_max;
98 *tseg2 = tseg - *tseg1;
100 return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
103 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
104 const struct can_bittiming_const *btc)
106 struct can_priv *priv = netdev_priv(dev);
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;
114 /* Use CiA recommended sample points */
115 if (bt->sample_point) {
116 sampl_pt = bt->sample_point;
118 if (bt->bitrate > 800000)
120 else if (bt->bitrate > 500000)
126 /* tseg even = round down, odd = round up */
127 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
128 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
129 tsegall = 1 + tseg / 2;
130 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
131 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
132 /* chose brp step which is possible in system */
133 brp = (brp / btc->brp_inc) * btc->brp_inc;
134 if ((brp < btc->brp_min) || (brp > btc->brp_max))
136 rate = priv->clock.freq / (brp * tsegall);
137 error = bt->bitrate - rate;
138 /* tseg brp biterror */
141 if (error > best_error)
145 spt = can_update_spt(btc, sampl_pt, tseg / 2,
147 error = sampl_pt - spt;
150 if (error > spt_error)
154 best_tseg = tseg / 2;
161 /* Error in one-tenth of a percent */
162 error = (best_error * 1000) / bt->bitrate;
163 if (error > CAN_CALC_MAX_ERROR) {
165 "bitrate error %ld.%ld%% too high\n",
166 error / 10, error % 10);
169 netdev_warn(dev, "bitrate error %ld.%ld%%\n",
170 error / 10, error % 10);
174 /* real sample point */
175 bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
178 v64 = (u64)best_brp * 1000000000UL;
179 do_div(v64, priv->clock.freq);
181 bt->prop_seg = tseg1 / 2;
182 bt->phase_seg1 = tseg1 - bt->prop_seg;
183 bt->phase_seg2 = tseg2;
185 /* check for sjw user settings */
186 if (!bt->sjw || !btc->sjw_max)
189 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
190 if (bt->sjw > btc->sjw_max)
191 bt->sjw = btc->sjw_max;
192 /* bt->sjw must not be higher than tseg2 */
199 bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
203 #else /* !CONFIG_CAN_CALC_BITTIMING */
204 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
205 const struct can_bittiming_const *btc)
207 netdev_err(dev, "bit-timing calculation not available\n");
210 #endif /* CONFIG_CAN_CALC_BITTIMING */
213 * Checks the validity of the specified bit-timing parameters prop_seg,
214 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
215 * prescaler value brp. You can find more information in the header
216 * file linux/can/netlink.h.
218 static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt,
219 const struct can_bittiming_const *btc)
221 struct can_priv *priv = netdev_priv(dev);
225 tseg1 = bt->prop_seg + bt->phase_seg1;
228 if (bt->sjw > btc->sjw_max ||
229 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
230 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
233 brp64 = (u64)priv->clock.freq * (u64)bt->tq;
234 if (btc->brp_inc > 1)
235 do_div(brp64, btc->brp_inc);
236 brp64 += 500000000UL - 1;
237 do_div(brp64, 1000000000UL); /* the practicable BRP */
238 if (btc->brp_inc > 1)
239 brp64 *= btc->brp_inc;
240 bt->brp = (u32)brp64;
242 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
245 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
246 bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
247 bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
252 static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
253 const struct can_bittiming_const *btc)
257 /* Check if the CAN device has bit-timing parameters */
262 * Depending on the given can_bittiming parameter structure the CAN
263 * timing parameters are calculated based on the provided bitrate OR
264 * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
265 * provided directly which are then checked and fixed up.
267 if (!bt->tq && bt->bitrate)
268 err = can_calc_bittiming(dev, bt, btc);
269 else if (bt->tq && !bt->bitrate)
270 err = can_fixup_bittiming(dev, bt, btc);
277 static void can_update_state_error_stats(struct net_device *dev,
278 enum can_state new_state)
280 struct can_priv *priv = netdev_priv(dev);
282 if (new_state <= priv->state)
286 case CAN_STATE_ERROR_WARNING:
287 priv->can_stats.error_warning++;
289 case CAN_STATE_ERROR_PASSIVE:
290 priv->can_stats.error_passive++;
292 case CAN_STATE_BUS_OFF:
293 priv->can_stats.bus_off++;
300 static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
303 case CAN_STATE_ERROR_ACTIVE:
304 return CAN_ERR_CRTL_ACTIVE;
305 case CAN_STATE_ERROR_WARNING:
306 return CAN_ERR_CRTL_TX_WARNING;
307 case CAN_STATE_ERROR_PASSIVE:
308 return CAN_ERR_CRTL_TX_PASSIVE;
314 static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
317 case CAN_STATE_ERROR_ACTIVE:
318 return CAN_ERR_CRTL_ACTIVE;
319 case CAN_STATE_ERROR_WARNING:
320 return CAN_ERR_CRTL_RX_WARNING;
321 case CAN_STATE_ERROR_PASSIVE:
322 return CAN_ERR_CRTL_RX_PASSIVE;
328 void can_change_state(struct net_device *dev, struct can_frame *cf,
329 enum can_state tx_state, enum can_state rx_state)
331 struct can_priv *priv = netdev_priv(dev);
332 enum can_state new_state = max(tx_state, rx_state);
334 if (unlikely(new_state == priv->state)) {
335 netdev_warn(dev, "%s: oops, state did not change", __func__);
339 netdev_dbg(dev, "New error state: %d\n", new_state);
341 can_update_state_error_stats(dev, new_state);
342 priv->state = new_state;
344 if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
345 cf->can_id |= CAN_ERR_BUSOFF;
349 cf->can_id |= CAN_ERR_CRTL;
350 cf->data[1] |= tx_state >= rx_state ?
351 can_tx_state_to_frame(dev, tx_state) : 0;
352 cf->data[1] |= tx_state <= rx_state ?
353 can_rx_state_to_frame(dev, rx_state) : 0;
355 EXPORT_SYMBOL_GPL(can_change_state);
358 * Local echo of CAN messages
360 * CAN network devices *should* support a local echo functionality
361 * (see Documentation/networking/can.txt). To test the handling of CAN
362 * interfaces that do not support the local echo both driver types are
363 * implemented. In the case that the driver does not support the echo
364 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
365 * to perform the echo as a fallback solution.
367 static void can_flush_echo_skb(struct net_device *dev)
369 struct can_priv *priv = netdev_priv(dev);
370 struct net_device_stats *stats = &dev->stats;
373 for (i = 0; i < priv->echo_skb_max; i++) {
374 if (priv->echo_skb[i]) {
375 kfree_skb(priv->echo_skb[i]);
376 priv->echo_skb[i] = NULL;
378 stats->tx_aborted_errors++;
384 * Put the skb on the stack to be looped backed locally lateron
386 * The function is typically called in the start_xmit function
387 * of the device driver. The driver must protect access to
388 * priv->echo_skb, if necessary.
390 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
393 struct can_priv *priv = netdev_priv(dev);
395 BUG_ON(idx >= priv->echo_skb_max);
397 /* check flag whether this packet has to be looped back */
398 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
399 (skb->protocol != htons(ETH_P_CAN) &&
400 skb->protocol != htons(ETH_P_CANFD))) {
405 if (!priv->echo_skb[idx]) {
407 skb = can_create_echo_skb(skb);
411 /* make settings for echo to reduce code in irq context */
412 skb->pkt_type = PACKET_BROADCAST;
413 skb->ip_summed = CHECKSUM_UNNECESSARY;
416 /* save this skb for tx interrupt echo handling */
417 priv->echo_skb[idx] = skb;
419 /* locking problem with netif_stop_queue() ?? */
420 netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
424 EXPORT_SYMBOL_GPL(can_put_echo_skb);
427 * Get the skb from the stack and loop it back locally
429 * The function is typically called when the TX done interrupt
430 * is handled in the device driver. The driver must protect
431 * access to priv->echo_skb, if necessary.
433 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
435 struct can_priv *priv = netdev_priv(dev);
437 BUG_ON(idx >= priv->echo_skb_max);
439 if (priv->echo_skb[idx]) {
440 struct sk_buff *skb = priv->echo_skb[idx];
441 struct can_frame *cf = (struct can_frame *)skb->data;
442 u8 dlc = cf->can_dlc;
444 netif_rx(priv->echo_skb[idx]);
445 priv->echo_skb[idx] = NULL;
452 EXPORT_SYMBOL_GPL(can_get_echo_skb);
455 * Remove the skb from the stack and free it.
457 * The function is typically called when TX failed.
459 void can_free_echo_skb(struct net_device *dev, unsigned int idx)
461 struct can_priv *priv = netdev_priv(dev);
463 BUG_ON(idx >= priv->echo_skb_max);
465 if (priv->echo_skb[idx]) {
466 dev_kfree_skb_any(priv->echo_skb[idx]);
467 priv->echo_skb[idx] = NULL;
470 EXPORT_SYMBOL_GPL(can_free_echo_skb);
473 * CAN device restart for bus-off recovery
475 static void can_restart(struct net_device *dev)
477 struct can_priv *priv = netdev_priv(dev);
478 struct net_device_stats *stats = &dev->stats;
480 struct can_frame *cf;
483 BUG_ON(netif_carrier_ok(dev));
486 * No synchronization needed because the device is bus-off and
487 * no messages can come in or go out.
489 can_flush_echo_skb(dev);
491 /* send restart message upstream */
492 skb = alloc_can_err_skb(dev, &cf);
497 cf->can_id |= CAN_ERR_RESTARTED;
502 stats->rx_bytes += cf->can_dlc;
505 netdev_dbg(dev, "restarted\n");
506 priv->can_stats.restarts++;
508 /* Now restart the device */
509 err = priv->do_set_mode(dev, CAN_MODE_START);
511 netif_carrier_on(dev);
513 netdev_err(dev, "Error %d during restart", err);
516 static void can_restart_work(struct work_struct *work)
518 struct delayed_work *dwork = to_delayed_work(work);
519 struct can_priv *priv = container_of(dwork, struct can_priv, restart_work);
521 can_restart(priv->dev);
524 int can_restart_now(struct net_device *dev)
526 struct can_priv *priv = netdev_priv(dev);
529 * A manual restart is only permitted if automatic restart is
530 * disabled and the device is in the bus-off state
532 if (priv->restart_ms)
534 if (priv->state != CAN_STATE_BUS_OFF)
537 cancel_delayed_work_sync(&priv->restart_work);
546 * This functions should be called when the device goes bus-off to
547 * tell the netif layer that no more packets can be sent or received.
548 * If enabled, a timer is started to trigger bus-off recovery.
550 void can_bus_off(struct net_device *dev)
552 struct can_priv *priv = netdev_priv(dev);
554 netdev_dbg(dev, "bus-off\n");
556 netif_carrier_off(dev);
558 if (priv->restart_ms)
559 schedule_delayed_work(&priv->restart_work,
560 msecs_to_jiffies(priv->restart_ms));
562 EXPORT_SYMBOL_GPL(can_bus_off);
564 static void can_setup(struct net_device *dev)
566 dev->type = ARPHRD_CAN;
568 dev->hard_header_len = 0;
570 dev->tx_queue_len = 10;
572 /* New-style flags. */
573 dev->flags = IFF_NOARP;
574 dev->features = NETIF_F_HW_CSUM;
577 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
581 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
582 sizeof(struct can_frame));
586 skb->protocol = htons(ETH_P_CAN);
587 skb->pkt_type = PACKET_BROADCAST;
588 skb->ip_summed = CHECKSUM_UNNECESSARY;
590 skb_reset_mac_header(skb);
591 skb_reset_network_header(skb);
592 skb_reset_transport_header(skb);
594 can_skb_reserve(skb);
595 can_skb_prv(skb)->ifindex = dev->ifindex;
596 can_skb_prv(skb)->skbcnt = 0;
598 *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
599 memset(*cf, 0, sizeof(struct can_frame));
603 EXPORT_SYMBOL_GPL(alloc_can_skb);
605 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
606 struct canfd_frame **cfd)
610 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
611 sizeof(struct canfd_frame));
615 skb->protocol = htons(ETH_P_CANFD);
616 skb->pkt_type = PACKET_BROADCAST;
617 skb->ip_summed = CHECKSUM_UNNECESSARY;
619 skb_reset_mac_header(skb);
620 skb_reset_network_header(skb);
621 skb_reset_transport_header(skb);
623 can_skb_reserve(skb);
624 can_skb_prv(skb)->ifindex = dev->ifindex;
625 can_skb_prv(skb)->skbcnt = 0;
627 *cfd = (struct canfd_frame *)skb_put(skb, sizeof(struct canfd_frame));
628 memset(*cfd, 0, sizeof(struct canfd_frame));
632 EXPORT_SYMBOL_GPL(alloc_canfd_skb);
634 struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
638 skb = alloc_can_skb(dev, cf);
642 (*cf)->can_id = CAN_ERR_FLAG;
643 (*cf)->can_dlc = CAN_ERR_DLC;
647 EXPORT_SYMBOL_GPL(alloc_can_err_skb);
650 * Allocate and setup space for the CAN network device
652 struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
654 struct net_device *dev;
655 struct can_priv *priv;
659 size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
660 echo_skb_max * sizeof(struct sk_buff *);
664 dev = alloc_netdev(size, "can%d", NET_NAME_UNKNOWN, can_setup);
668 priv = netdev_priv(dev);
672 priv->echo_skb_max = echo_skb_max;
673 priv->echo_skb = (void *)priv +
674 ALIGN(sizeof_priv, sizeof(struct sk_buff *));
677 priv->state = CAN_STATE_STOPPED;
679 INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
683 EXPORT_SYMBOL_GPL(alloc_candev);
686 * Free space of the CAN network device
688 void free_candev(struct net_device *dev)
692 EXPORT_SYMBOL_GPL(free_candev);
695 * changing MTU and control mode for CAN/CANFD devices
697 int can_change_mtu(struct net_device *dev, int new_mtu)
699 struct can_priv *priv = netdev_priv(dev);
701 /* Do not allow changing the MTU while running */
702 if (dev->flags & IFF_UP)
705 /* allow change of MTU according to the CANFD ability of the device */
708 /* 'CANFD-only' controllers can not switch to CAN_MTU */
709 if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
712 priv->ctrlmode &= ~CAN_CTRLMODE_FD;
716 /* check for potential CANFD ability */
717 if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
718 !(priv->ctrlmode_static & CAN_CTRLMODE_FD))
721 priv->ctrlmode |= CAN_CTRLMODE_FD;
731 EXPORT_SYMBOL_GPL(can_change_mtu);
734 * Common open function when the device gets opened.
736 * This function should be called in the open function of the device
739 int open_candev(struct net_device *dev)
741 struct can_priv *priv = netdev_priv(dev);
743 if (!priv->bittiming.bitrate) {
744 netdev_err(dev, "bit-timing not yet defined\n");
748 /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
749 if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
750 (!priv->data_bittiming.bitrate ||
751 (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) {
752 netdev_err(dev, "incorrect/missing data bit-timing\n");
756 /* Switch carrier on if device was stopped while in bus-off state */
757 if (!netif_carrier_ok(dev))
758 netif_carrier_on(dev);
762 EXPORT_SYMBOL_GPL(open_candev);
765 * Common close function for cleanup before the device gets closed.
767 * This function should be called in the close function of the device
770 void close_candev(struct net_device *dev)
772 struct can_priv *priv = netdev_priv(dev);
774 cancel_delayed_work_sync(&priv->restart_work);
775 can_flush_echo_skb(dev);
777 EXPORT_SYMBOL_GPL(close_candev);
780 * CAN netlink interface
782 static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
783 [IFLA_CAN_STATE] = { .type = NLA_U32 },
784 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
785 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
786 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
787 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
788 [IFLA_CAN_BITTIMING_CONST]
789 = { .len = sizeof(struct can_bittiming_const) },
790 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
791 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
792 [IFLA_CAN_DATA_BITTIMING]
793 = { .len = sizeof(struct can_bittiming) },
794 [IFLA_CAN_DATA_BITTIMING_CONST]
795 = { .len = sizeof(struct can_bittiming_const) },
798 static int can_validate(struct nlattr *tb[], struct nlattr *data[])
800 bool is_can_fd = false;
802 /* Make sure that valid CAN FD configurations always consist of
803 * - nominal/arbitration bittiming
805 * - control mode with CAN_CTRLMODE_FD set
811 if (data[IFLA_CAN_CTRLMODE]) {
812 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
814 is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
818 if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
822 if (data[IFLA_CAN_DATA_BITTIMING]) {
823 if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
830 static int can_changelink(struct net_device *dev,
831 struct nlattr *tb[], struct nlattr *data[])
833 struct can_priv *priv = netdev_priv(dev);
836 /* We need synchronization with dev->stop() */
839 if (data[IFLA_CAN_BITTIMING]) {
840 struct can_bittiming bt;
842 /* Do not allow changing bittiming while running */
843 if (dev->flags & IFF_UP)
845 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
846 err = can_get_bittiming(dev, &bt, priv->bittiming_const);
849 memcpy(&priv->bittiming, &bt, sizeof(bt));
851 if (priv->do_set_bittiming) {
852 /* Finally, set the bit-timing registers */
853 err = priv->do_set_bittiming(dev);
859 if (data[IFLA_CAN_CTRLMODE]) {
860 struct can_ctrlmode *cm;
864 /* Do not allow changing controller mode while running */
865 if (dev->flags & IFF_UP)
867 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
868 ctrlstatic = priv->ctrlmode_static;
869 maskedflags = cm->flags & cm->mask;
871 /* check whether provided bits are allowed to be passed */
872 if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic))
875 /* do not check for static fd-non-iso if 'fd' is disabled */
876 if (!(maskedflags & CAN_CTRLMODE_FD))
877 ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
879 /* make sure static options are provided by configuration */
880 if ((maskedflags & ctrlstatic) != ctrlstatic)
883 /* clear bits to be modified and copy the flag values */
884 priv->ctrlmode &= ~cm->mask;
885 priv->ctrlmode |= maskedflags;
887 /* CAN_CTRLMODE_FD can only be set when driver supports FD */
888 if (priv->ctrlmode & CAN_CTRLMODE_FD)
889 dev->mtu = CANFD_MTU;
894 if (data[IFLA_CAN_RESTART_MS]) {
895 /* Do not allow changing restart delay while running */
896 if (dev->flags & IFF_UP)
898 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
901 if (data[IFLA_CAN_RESTART]) {
902 /* Do not allow a restart while not running */
903 if (!(dev->flags & IFF_UP))
905 err = can_restart_now(dev);
910 if (data[IFLA_CAN_DATA_BITTIMING]) {
911 struct can_bittiming dbt;
913 /* Do not allow changing bittiming while running */
914 if (dev->flags & IFF_UP)
916 memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
918 err = can_get_bittiming(dev, &dbt, priv->data_bittiming_const);
921 memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
923 if (priv->do_set_data_bittiming) {
924 /* Finally, set the bit-timing registers */
925 err = priv->do_set_data_bittiming(dev);
934 static size_t can_get_size(const struct net_device *dev)
936 struct can_priv *priv = netdev_priv(dev);
939 if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
940 size += nla_total_size(sizeof(struct can_bittiming));
941 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
942 size += nla_total_size(sizeof(struct can_bittiming_const));
943 size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
944 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
945 size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
946 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
947 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
948 size += nla_total_size(sizeof(struct can_berr_counter));
949 if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
950 size += nla_total_size(sizeof(struct can_bittiming));
951 if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
952 size += nla_total_size(sizeof(struct can_bittiming_const));
957 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
959 struct can_priv *priv = netdev_priv(dev);
960 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
961 struct can_berr_counter bec;
962 enum can_state state = priv->state;
964 if (priv->do_get_state)
965 priv->do_get_state(dev, &state);
967 if ((priv->bittiming.bitrate &&
968 nla_put(skb, IFLA_CAN_BITTIMING,
969 sizeof(priv->bittiming), &priv->bittiming)) ||
971 (priv->bittiming_const &&
972 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
973 sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
975 nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
976 nla_put_u32(skb, IFLA_CAN_STATE, state) ||
977 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
978 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
980 (priv->do_get_berr_counter &&
981 !priv->do_get_berr_counter(dev, &bec) &&
982 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
984 (priv->data_bittiming.bitrate &&
985 nla_put(skb, IFLA_CAN_DATA_BITTIMING,
986 sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
988 (priv->data_bittiming_const &&
989 nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
990 sizeof(*priv->data_bittiming_const),
991 priv->data_bittiming_const)))
997 static size_t can_get_xstats_size(const struct net_device *dev)
999 return sizeof(struct can_device_stats);
1002 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
1004 struct can_priv *priv = netdev_priv(dev);
1006 if (nla_put(skb, IFLA_INFO_XSTATS,
1007 sizeof(priv->can_stats), &priv->can_stats))
1008 goto nla_put_failure;
1015 static int can_newlink(struct net *src_net, struct net_device *dev,
1016 struct nlattr *tb[], struct nlattr *data[])
1021 static void can_dellink(struct net_device *dev, struct list_head *head)
1026 static struct rtnl_link_ops can_link_ops __read_mostly = {
1028 .maxtype = IFLA_CAN_MAX,
1029 .policy = can_policy,
1031 .validate = can_validate,
1032 .newlink = can_newlink,
1033 .changelink = can_changelink,
1034 .dellink = can_dellink,
1035 .get_size = can_get_size,
1036 .fill_info = can_fill_info,
1037 .get_xstats_size = can_get_xstats_size,
1038 .fill_xstats = can_fill_xstats,
1042 * Register the CAN network device
1044 int register_candev(struct net_device *dev)
1046 dev->rtnl_link_ops = &can_link_ops;
1047 return register_netdev(dev);
1049 EXPORT_SYMBOL_GPL(register_candev);
1052 * Unregister the CAN network device
1054 void unregister_candev(struct net_device *dev)
1056 unregister_netdev(dev);
1058 EXPORT_SYMBOL_GPL(unregister_candev);
1061 * Test if a network device is a candev based device
1062 * and return the can_priv* if so.
1064 struct can_priv *safe_candev_priv(struct net_device *dev)
1066 if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
1069 return netdev_priv(dev);
1071 EXPORT_SYMBOL_GPL(safe_candev_priv);
1073 static __init int can_dev_init(void)
1077 can_led_notifier_init();
1079 err = rtnl_link_register(&can_link_ops);
1081 printk(KERN_INFO MOD_DESC "\n");
1085 module_init(can_dev_init);
1087 static __exit void can_dev_exit(void)
1089 rtnl_link_unregister(&can_link_ops);
1091 can_led_notifier_exit();
1093 module_exit(can_dev_exit);
1095 MODULE_ALIAS_RTNL_LINK("can");