1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
4 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
5 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/netdevice.h>
12 #include <linux/if_arp.h>
13 #include <linux/workqueue.h>
14 #include <linux/can.h>
15 #include <linux/can/dev.h>
16 #include <linux/can/skb.h>
17 #include <linux/can/netlink.h>
18 #include <linux/can/led.h>
20 #include <net/rtnetlink.h>
22 #define MOD_DESC "CAN device driver interface"
24 MODULE_DESCRIPTION(MOD_DESC);
25 MODULE_LICENSE("GPL v2");
26 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
28 /* CAN DLC to real data length conversion helpers */
30 static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
31 8, 12, 16, 20, 24, 32, 48, 64};
33 /* get data length from can_dlc with sanitized can_dlc */
34 u8 can_dlc2len(u8 can_dlc)
36 return dlc2len[can_dlc & 0x0F];
38 EXPORT_SYMBOL_GPL(can_dlc2len);
40 static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
41 9, 9, 9, 9, /* 9 - 12 */
42 10, 10, 10, 10, /* 13 - 16 */
43 11, 11, 11, 11, /* 17 - 20 */
44 12, 12, 12, 12, /* 21 - 24 */
45 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
46 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
47 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
48 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
49 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
51 /* map the sanitized data length to an appropriate data length code */
52 u8 can_len2dlc(u8 len)
54 if (unlikely(len > 64))
59 EXPORT_SYMBOL_GPL(can_len2dlc);
61 #ifdef CONFIG_CAN_CALC_BITTIMING
62 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
63 #define CAN_CALC_SYNC_SEG 1
66 * Bit-timing calculation derived from:
68 * Code based on LinCAN sources and H8S2638 project
69 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
70 * Copyright 2005 Stanislav Marek
71 * email: pisa@cmp.felk.cvut.cz
73 * Calculates proper bit-timing parameters for a specified bit-rate
74 * and sample-point, which can then be used to set the bit-timing
75 * registers of the CAN controller. You can find more information
76 * in the header file linux/can/netlink.h.
78 static int can_update_sample_point(const struct can_bittiming_const *btc,
79 unsigned int sample_point_nominal, unsigned int tseg,
80 unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
81 unsigned int *sample_point_error_ptr)
83 unsigned int sample_point_error, best_sample_point_error = UINT_MAX;
84 unsigned int sample_point, best_sample_point = 0;
85 unsigned int tseg1, tseg2;
88 for (i = 0; i <= 1; i++) {
89 tseg2 = tseg + CAN_CALC_SYNC_SEG - (sample_point_nominal * (tseg + CAN_CALC_SYNC_SEG)) / 1000 - i;
90 tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
92 if (tseg1 > btc->tseg1_max) {
93 tseg1 = btc->tseg1_max;
97 sample_point = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) / (tseg + CAN_CALC_SYNC_SEG);
98 sample_point_error = abs(sample_point_nominal - sample_point);
100 if ((sample_point <= sample_point_nominal) && (sample_point_error < best_sample_point_error)) {
101 best_sample_point = sample_point;
102 best_sample_point_error = sample_point_error;
108 if (sample_point_error_ptr)
109 *sample_point_error_ptr = best_sample_point_error;
111 return best_sample_point;
114 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
115 const struct can_bittiming_const *btc)
117 struct can_priv *priv = netdev_priv(dev);
118 unsigned int bitrate; /* current bitrate */
119 unsigned int bitrate_error; /* difference between current and nominal value */
120 unsigned int best_bitrate_error = UINT_MAX;
121 unsigned int sample_point_error; /* difference between current and nominal value */
122 unsigned int best_sample_point_error = UINT_MAX;
123 unsigned int sample_point_nominal; /* nominal sample point */
124 unsigned int best_tseg = 0; /* current best value for tseg */
125 unsigned int best_brp = 0; /* current best value for brp */
126 unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0;
129 /* Use CiA recommended sample points */
130 if (bt->sample_point) {
131 sample_point_nominal = bt->sample_point;
133 if (bt->bitrate > 800000)
134 sample_point_nominal = 750;
135 else if (bt->bitrate > 500000)
136 sample_point_nominal = 800;
138 sample_point_nominal = 875;
141 /* tseg even = round down, odd = round up */
142 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
143 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
144 tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
146 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
147 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
149 /* choose brp step which is possible in system */
150 brp = (brp / btc->brp_inc) * btc->brp_inc;
151 if ((brp < btc->brp_min) || (brp > btc->brp_max))
154 bitrate = priv->clock.freq / (brp * tsegall);
155 bitrate_error = abs(bt->bitrate - bitrate);
157 /* tseg brp biterror */
158 if (bitrate_error > best_bitrate_error)
161 /* reset sample point error if we have a better bitrate */
162 if (bitrate_error < best_bitrate_error)
163 best_sample_point_error = UINT_MAX;
165 can_update_sample_point(btc, sample_point_nominal, tseg / 2, &tseg1, &tseg2, &sample_point_error);
166 if (sample_point_error > best_sample_point_error)
169 best_sample_point_error = sample_point_error;
170 best_bitrate_error = bitrate_error;
171 best_tseg = tseg / 2;
174 if (bitrate_error == 0 && sample_point_error == 0)
178 if (best_bitrate_error) {
179 /* Error in one-tenth of a percent */
180 v64 = (u64)best_bitrate_error * 1000;
181 do_div(v64, bt->bitrate);
182 bitrate_error = (u32)v64;
183 if (bitrate_error > CAN_CALC_MAX_ERROR) {
185 "bitrate error %d.%d%% too high\n",
186 bitrate_error / 10, bitrate_error % 10);
189 netdev_warn(dev, "bitrate error %d.%d%%\n",
190 bitrate_error / 10, bitrate_error % 10);
193 /* real sample point */
194 bt->sample_point = can_update_sample_point(btc, sample_point_nominal, best_tseg,
195 &tseg1, &tseg2, NULL);
197 v64 = (u64)best_brp * 1000 * 1000 * 1000;
198 do_div(v64, priv->clock.freq);
200 bt->prop_seg = tseg1 / 2;
201 bt->phase_seg1 = tseg1 - bt->prop_seg;
202 bt->phase_seg2 = tseg2;
204 /* check for sjw user settings */
205 if (!bt->sjw || !btc->sjw_max) {
208 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
209 if (bt->sjw > btc->sjw_max)
210 bt->sjw = btc->sjw_max;
211 /* bt->sjw must not be higher than tseg2 */
219 bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2));
223 #else /* !CONFIG_CAN_CALC_BITTIMING */
224 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
225 const struct can_bittiming_const *btc)
227 netdev_err(dev, "bit-timing calculation not available\n");
230 #endif /* CONFIG_CAN_CALC_BITTIMING */
233 * Checks the validity of the specified bit-timing parameters prop_seg,
234 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
235 * prescaler value brp. You can find more information in the header
236 * file linux/can/netlink.h.
238 static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt,
239 const struct can_bittiming_const *btc)
241 struct can_priv *priv = netdev_priv(dev);
245 tseg1 = bt->prop_seg + bt->phase_seg1;
248 if (bt->sjw > btc->sjw_max ||
249 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
250 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
253 brp64 = (u64)priv->clock.freq * (u64)bt->tq;
254 if (btc->brp_inc > 1)
255 do_div(brp64, btc->brp_inc);
256 brp64 += 500000000UL - 1;
257 do_div(brp64, 1000000000UL); /* the practicable BRP */
258 if (btc->brp_inc > 1)
259 brp64 *= btc->brp_inc;
260 bt->brp = (u32)brp64;
262 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
265 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
266 bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
267 bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
272 /* Checks the validity of predefined bitrate settings */
273 static int can_validate_bitrate(struct net_device *dev, struct can_bittiming *bt,
274 const u32 *bitrate_const,
275 const unsigned int bitrate_const_cnt)
277 struct can_priv *priv = netdev_priv(dev);
280 for (i = 0; i < bitrate_const_cnt; i++) {
281 if (bt->bitrate == bitrate_const[i])
285 if (i >= priv->bitrate_const_cnt)
291 static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
292 const struct can_bittiming_const *btc,
293 const u32 *bitrate_const,
294 const unsigned int bitrate_const_cnt)
299 * Depending on the given can_bittiming parameter structure the CAN
300 * timing parameters are calculated based on the provided bitrate OR
301 * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
302 * provided directly which are then checked and fixed up.
304 if (!bt->tq && bt->bitrate && btc)
305 err = can_calc_bittiming(dev, bt, btc);
306 else if (bt->tq && !bt->bitrate && btc)
307 err = can_fixup_bittiming(dev, bt, btc);
308 else if (!bt->tq && bt->bitrate && bitrate_const)
309 err = can_validate_bitrate(dev, bt, bitrate_const,
317 static void can_update_state_error_stats(struct net_device *dev,
318 enum can_state new_state)
320 struct can_priv *priv = netdev_priv(dev);
322 if (new_state <= priv->state)
326 case CAN_STATE_ERROR_WARNING:
327 priv->can_stats.error_warning++;
329 case CAN_STATE_ERROR_PASSIVE:
330 priv->can_stats.error_passive++;
332 case CAN_STATE_BUS_OFF:
333 priv->can_stats.bus_off++;
340 static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
343 case CAN_STATE_ERROR_ACTIVE:
344 return CAN_ERR_CRTL_ACTIVE;
345 case CAN_STATE_ERROR_WARNING:
346 return CAN_ERR_CRTL_TX_WARNING;
347 case CAN_STATE_ERROR_PASSIVE:
348 return CAN_ERR_CRTL_TX_PASSIVE;
354 static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
357 case CAN_STATE_ERROR_ACTIVE:
358 return CAN_ERR_CRTL_ACTIVE;
359 case CAN_STATE_ERROR_WARNING:
360 return CAN_ERR_CRTL_RX_WARNING;
361 case CAN_STATE_ERROR_PASSIVE:
362 return CAN_ERR_CRTL_RX_PASSIVE;
368 void can_change_state(struct net_device *dev, struct can_frame *cf,
369 enum can_state tx_state, enum can_state rx_state)
371 struct can_priv *priv = netdev_priv(dev);
372 enum can_state new_state = max(tx_state, rx_state);
374 if (unlikely(new_state == priv->state)) {
375 netdev_warn(dev, "%s: oops, state did not change", __func__);
379 netdev_dbg(dev, "New error state: %d\n", new_state);
381 can_update_state_error_stats(dev, new_state);
382 priv->state = new_state;
387 if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
388 cf->can_id |= CAN_ERR_BUSOFF;
392 cf->can_id |= CAN_ERR_CRTL;
393 cf->data[1] |= tx_state >= rx_state ?
394 can_tx_state_to_frame(dev, tx_state) : 0;
395 cf->data[1] |= tx_state <= rx_state ?
396 can_rx_state_to_frame(dev, rx_state) : 0;
398 EXPORT_SYMBOL_GPL(can_change_state);
401 * Local echo of CAN messages
403 * CAN network devices *should* support a local echo functionality
404 * (see Documentation/networking/can.rst). To test the handling of CAN
405 * interfaces that do not support the local echo both driver types are
406 * implemented. In the case that the driver does not support the echo
407 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
408 * to perform the echo as a fallback solution.
410 static void can_flush_echo_skb(struct net_device *dev)
412 struct can_priv *priv = netdev_priv(dev);
413 struct net_device_stats *stats = &dev->stats;
416 for (i = 0; i < priv->echo_skb_max; i++) {
417 if (priv->echo_skb[i]) {
418 kfree_skb(priv->echo_skb[i]);
419 priv->echo_skb[i] = NULL;
421 stats->tx_aborted_errors++;
427 * Put the skb on the stack to be looped backed locally lateron
429 * The function is typically called in the start_xmit function
430 * of the device driver. The driver must protect access to
431 * priv->echo_skb, if necessary.
433 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
436 struct can_priv *priv = netdev_priv(dev);
438 BUG_ON(idx >= priv->echo_skb_max);
440 /* check flag whether this packet has to be looped back */
441 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
442 (skb->protocol != htons(ETH_P_CAN) &&
443 skb->protocol != htons(ETH_P_CANFD))) {
448 if (!priv->echo_skb[idx]) {
450 skb = can_create_echo_skb(skb);
454 /* make settings for echo to reduce code in irq context */
455 skb->pkt_type = PACKET_BROADCAST;
456 skb->ip_summed = CHECKSUM_UNNECESSARY;
459 /* save this skb for tx interrupt echo handling */
460 priv->echo_skb[idx] = skb;
462 /* locking problem with netif_stop_queue() ?? */
463 netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
467 EXPORT_SYMBOL_GPL(can_put_echo_skb);
469 struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr)
471 struct can_priv *priv = netdev_priv(dev);
473 if (idx >= priv->echo_skb_max) {
474 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
475 __func__, idx, priv->echo_skb_max);
479 if (priv->echo_skb[idx]) {
480 /* Using "struct canfd_frame::len" for the frame
481 * length is supported on both CAN and CANFD frames.
483 struct sk_buff *skb = priv->echo_skb[idx];
484 struct canfd_frame *cf = (struct canfd_frame *)skb->data;
488 priv->echo_skb[idx] = NULL;
497 * Get the skb from the stack and loop it back locally
499 * The function is typically called when the TX done interrupt
500 * is handled in the device driver. The driver must protect
501 * access to priv->echo_skb, if necessary.
503 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
508 skb = __can_get_echo_skb(dev, idx, &len);
516 EXPORT_SYMBOL_GPL(can_get_echo_skb);
519 * Remove the skb from the stack and free it.
521 * The function is typically called when TX failed.
523 void can_free_echo_skb(struct net_device *dev, unsigned int idx)
525 struct can_priv *priv = netdev_priv(dev);
527 BUG_ON(idx >= priv->echo_skb_max);
529 if (priv->echo_skb[idx]) {
530 dev_kfree_skb_any(priv->echo_skb[idx]);
531 priv->echo_skb[idx] = NULL;
534 EXPORT_SYMBOL_GPL(can_free_echo_skb);
537 * CAN device restart for bus-off recovery
539 static void can_restart(struct net_device *dev)
541 struct can_priv *priv = netdev_priv(dev);
542 struct net_device_stats *stats = &dev->stats;
544 struct can_frame *cf;
547 BUG_ON(netif_carrier_ok(dev));
550 * No synchronization needed because the device is bus-off and
551 * no messages can come in or go out.
553 can_flush_echo_skb(dev);
555 /* send restart message upstream */
556 skb = alloc_can_err_skb(dev, &cf);
561 cf->can_id |= CAN_ERR_RESTARTED;
566 stats->rx_bytes += cf->can_dlc;
569 netdev_dbg(dev, "restarted\n");
570 priv->can_stats.restarts++;
572 /* Now restart the device */
573 err = priv->do_set_mode(dev, CAN_MODE_START);
575 netif_carrier_on(dev);
577 netdev_err(dev, "Error %d during restart", err);
580 static void can_restart_work(struct work_struct *work)
582 struct delayed_work *dwork = to_delayed_work(work);
583 struct can_priv *priv = container_of(dwork, struct can_priv, restart_work);
585 can_restart(priv->dev);
588 int can_restart_now(struct net_device *dev)
590 struct can_priv *priv = netdev_priv(dev);
593 * A manual restart is only permitted if automatic restart is
594 * disabled and the device is in the bus-off state
596 if (priv->restart_ms)
598 if (priv->state != CAN_STATE_BUS_OFF)
601 cancel_delayed_work_sync(&priv->restart_work);
610 * This functions should be called when the device goes bus-off to
611 * tell the netif layer that no more packets can be sent or received.
612 * If enabled, a timer is started to trigger bus-off recovery.
614 void can_bus_off(struct net_device *dev)
616 struct can_priv *priv = netdev_priv(dev);
618 netdev_info(dev, "bus-off\n");
620 netif_carrier_off(dev);
622 if (priv->restart_ms)
623 schedule_delayed_work(&priv->restart_work,
624 msecs_to_jiffies(priv->restart_ms));
626 EXPORT_SYMBOL_GPL(can_bus_off);
628 static void can_setup(struct net_device *dev)
630 dev->type = ARPHRD_CAN;
632 dev->hard_header_len = 0;
634 dev->tx_queue_len = 10;
636 /* New-style flags. */
637 dev->flags = IFF_NOARP;
638 dev->features = NETIF_F_HW_CSUM;
641 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
645 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
646 sizeof(struct can_frame));
650 skb->protocol = htons(ETH_P_CAN);
651 skb->pkt_type = PACKET_BROADCAST;
652 skb->ip_summed = CHECKSUM_UNNECESSARY;
654 skb_reset_mac_header(skb);
655 skb_reset_network_header(skb);
656 skb_reset_transport_header(skb);
658 can_skb_reserve(skb);
659 can_skb_prv(skb)->ifindex = dev->ifindex;
660 can_skb_prv(skb)->skbcnt = 0;
662 *cf = skb_put_zero(skb, sizeof(struct can_frame));
666 EXPORT_SYMBOL_GPL(alloc_can_skb);
668 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
669 struct canfd_frame **cfd)
673 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
674 sizeof(struct canfd_frame));
678 skb->protocol = htons(ETH_P_CANFD);
679 skb->pkt_type = PACKET_BROADCAST;
680 skb->ip_summed = CHECKSUM_UNNECESSARY;
682 skb_reset_mac_header(skb);
683 skb_reset_network_header(skb);
684 skb_reset_transport_header(skb);
686 can_skb_reserve(skb);
687 can_skb_prv(skb)->ifindex = dev->ifindex;
688 can_skb_prv(skb)->skbcnt = 0;
690 *cfd = skb_put_zero(skb, sizeof(struct canfd_frame));
694 EXPORT_SYMBOL_GPL(alloc_canfd_skb);
696 struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
700 skb = alloc_can_skb(dev, cf);
704 (*cf)->can_id = CAN_ERR_FLAG;
705 (*cf)->can_dlc = CAN_ERR_DLC;
709 EXPORT_SYMBOL_GPL(alloc_can_err_skb);
712 * Allocate and setup space for the CAN network device
714 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
715 unsigned int txqs, unsigned int rxqs)
717 struct net_device *dev;
718 struct can_priv *priv;
722 size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
723 echo_skb_max * sizeof(struct sk_buff *);
727 dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
732 priv = netdev_priv(dev);
736 priv->echo_skb_max = echo_skb_max;
737 priv->echo_skb = (void *)priv +
738 ALIGN(sizeof_priv, sizeof(struct sk_buff *));
741 priv->state = CAN_STATE_STOPPED;
743 INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
747 EXPORT_SYMBOL_GPL(alloc_candev_mqs);
750 * Free space of the CAN network device
752 void free_candev(struct net_device *dev)
756 EXPORT_SYMBOL_GPL(free_candev);
759 * changing MTU and control mode for CAN/CANFD devices
761 int can_change_mtu(struct net_device *dev, int new_mtu)
763 struct can_priv *priv = netdev_priv(dev);
765 /* Do not allow changing the MTU while running */
766 if (dev->flags & IFF_UP)
769 /* allow change of MTU according to the CANFD ability of the device */
772 /* 'CANFD-only' controllers can not switch to CAN_MTU */
773 if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
776 priv->ctrlmode &= ~CAN_CTRLMODE_FD;
780 /* check for potential CANFD ability */
781 if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
782 !(priv->ctrlmode_static & CAN_CTRLMODE_FD))
785 priv->ctrlmode |= CAN_CTRLMODE_FD;
795 EXPORT_SYMBOL_GPL(can_change_mtu);
798 * Common open function when the device gets opened.
800 * This function should be called in the open function of the device
803 int open_candev(struct net_device *dev)
805 struct can_priv *priv = netdev_priv(dev);
807 if (!priv->bittiming.bitrate) {
808 netdev_err(dev, "bit-timing not yet defined\n");
812 /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
813 if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
814 (!priv->data_bittiming.bitrate ||
815 (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) {
816 netdev_err(dev, "incorrect/missing data bit-timing\n");
820 /* Switch carrier on if device was stopped while in bus-off state */
821 if (!netif_carrier_ok(dev))
822 netif_carrier_on(dev);
826 EXPORT_SYMBOL_GPL(open_candev);
829 /* Common function that can be used to understand the limitation of
830 * a transceiver when it provides no means to determine these limitations
833 void of_can_transceiver(struct net_device *dev)
835 struct device_node *dn;
836 struct can_priv *priv = netdev_priv(dev);
837 struct device_node *np = dev->dev.parent->of_node;
840 dn = of_get_child_by_name(np, "can-transceiver");
844 ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max);
845 if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max))
846 netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
848 EXPORT_SYMBOL_GPL(of_can_transceiver);
852 * Common close function for cleanup before the device gets closed.
854 * This function should be called in the close function of the device
857 void close_candev(struct net_device *dev)
859 struct can_priv *priv = netdev_priv(dev);
861 cancel_delayed_work_sync(&priv->restart_work);
862 can_flush_echo_skb(dev);
864 EXPORT_SYMBOL_GPL(close_candev);
867 * CAN netlink interface
869 static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
870 [IFLA_CAN_STATE] = { .type = NLA_U32 },
871 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
872 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
873 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
874 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
875 [IFLA_CAN_BITTIMING_CONST]
876 = { .len = sizeof(struct can_bittiming_const) },
877 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
878 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
879 [IFLA_CAN_DATA_BITTIMING]
880 = { .len = sizeof(struct can_bittiming) },
881 [IFLA_CAN_DATA_BITTIMING_CONST]
882 = { .len = sizeof(struct can_bittiming_const) },
885 static int can_validate(struct nlattr *tb[], struct nlattr *data[],
886 struct netlink_ext_ack *extack)
888 bool is_can_fd = false;
890 /* Make sure that valid CAN FD configurations always consist of
891 * - nominal/arbitration bittiming
893 * - control mode with CAN_CTRLMODE_FD set
899 if (data[IFLA_CAN_CTRLMODE]) {
900 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
902 is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
906 if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
910 if (data[IFLA_CAN_DATA_BITTIMING]) {
911 if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
918 static int can_changelink(struct net_device *dev, struct nlattr *tb[],
919 struct nlattr *data[],
920 struct netlink_ext_ack *extack)
922 struct can_priv *priv = netdev_priv(dev);
925 /* We need synchronization with dev->stop() */
928 if (data[IFLA_CAN_BITTIMING]) {
929 struct can_bittiming bt;
931 /* Do not allow changing bittiming while running */
932 if (dev->flags & IFF_UP)
935 /* Calculate bittiming parameters based on
936 * bittiming_const if set, otherwise pass bitrate
937 * directly via do_set_bitrate(). Bail out if neither
940 if (!priv->bittiming_const && !priv->do_set_bittiming)
943 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
944 err = can_get_bittiming(dev, &bt,
945 priv->bittiming_const,
947 priv->bitrate_const_cnt);
951 if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
952 netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n",
957 memcpy(&priv->bittiming, &bt, sizeof(bt));
959 if (priv->do_set_bittiming) {
960 /* Finally, set the bit-timing registers */
961 err = priv->do_set_bittiming(dev);
967 if (data[IFLA_CAN_CTRLMODE]) {
968 struct can_ctrlmode *cm;
972 /* Do not allow changing controller mode while running */
973 if (dev->flags & IFF_UP)
975 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
976 ctrlstatic = priv->ctrlmode_static;
977 maskedflags = cm->flags & cm->mask;
979 /* check whether provided bits are allowed to be passed */
980 if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic))
983 /* do not check for static fd-non-iso if 'fd' is disabled */
984 if (!(maskedflags & CAN_CTRLMODE_FD))
985 ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
987 /* make sure static options are provided by configuration */
988 if ((maskedflags & ctrlstatic) != ctrlstatic)
991 /* clear bits to be modified and copy the flag values */
992 priv->ctrlmode &= ~cm->mask;
993 priv->ctrlmode |= maskedflags;
995 /* CAN_CTRLMODE_FD can only be set when driver supports FD */
996 if (priv->ctrlmode & CAN_CTRLMODE_FD)
997 dev->mtu = CANFD_MTU;
1002 if (data[IFLA_CAN_RESTART_MS]) {
1003 /* Do not allow changing restart delay while running */
1004 if (dev->flags & IFF_UP)
1006 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
1009 if (data[IFLA_CAN_RESTART]) {
1010 /* Do not allow a restart while not running */
1011 if (!(dev->flags & IFF_UP))
1013 err = can_restart_now(dev);
1018 if (data[IFLA_CAN_DATA_BITTIMING]) {
1019 struct can_bittiming dbt;
1021 /* Do not allow changing bittiming while running */
1022 if (dev->flags & IFF_UP)
1025 /* Calculate bittiming parameters based on
1026 * data_bittiming_const if set, otherwise pass bitrate
1027 * directly via do_set_bitrate(). Bail out if neither
1030 if (!priv->data_bittiming_const && !priv->do_set_data_bittiming)
1033 memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
1035 err = can_get_bittiming(dev, &dbt,
1036 priv->data_bittiming_const,
1037 priv->data_bitrate_const,
1038 priv->data_bitrate_const_cnt);
1042 if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) {
1043 netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n",
1048 memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
1050 if (priv->do_set_data_bittiming) {
1051 /* Finally, set the bit-timing registers */
1052 err = priv->do_set_data_bittiming(dev);
1058 if (data[IFLA_CAN_TERMINATION]) {
1059 const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
1060 const unsigned int num_term = priv->termination_const_cnt;
1063 if (!priv->do_set_termination)
1066 /* check whether given value is supported by the interface */
1067 for (i = 0; i < num_term; i++) {
1068 if (termval == priv->termination_const[i])
1074 /* Finally, set the termination value */
1075 err = priv->do_set_termination(dev, termval);
1079 priv->termination = termval;
1085 static size_t can_get_size(const struct net_device *dev)
1087 struct can_priv *priv = netdev_priv(dev);
1090 if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
1091 size += nla_total_size(sizeof(struct can_bittiming));
1092 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
1093 size += nla_total_size(sizeof(struct can_bittiming_const));
1094 size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
1095 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
1096 size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
1097 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
1098 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
1099 size += nla_total_size(sizeof(struct can_berr_counter));
1100 if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
1101 size += nla_total_size(sizeof(struct can_bittiming));
1102 if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
1103 size += nla_total_size(sizeof(struct can_bittiming_const));
1104 if (priv->termination_const) {
1105 size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */
1106 size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */
1107 priv->termination_const_cnt);
1109 if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */
1110 size += nla_total_size(sizeof(*priv->bitrate_const) *
1111 priv->bitrate_const_cnt);
1112 if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */
1113 size += nla_total_size(sizeof(*priv->data_bitrate_const) *
1114 priv->data_bitrate_const_cnt);
1115 size += sizeof(priv->bitrate_max); /* IFLA_CAN_BITRATE_MAX */
1120 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
1122 struct can_priv *priv = netdev_priv(dev);
1123 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
1124 struct can_berr_counter bec;
1125 enum can_state state = priv->state;
1127 if (priv->do_get_state)
1128 priv->do_get_state(dev, &state);
1130 if ((priv->bittiming.bitrate &&
1131 nla_put(skb, IFLA_CAN_BITTIMING,
1132 sizeof(priv->bittiming), &priv->bittiming)) ||
1134 (priv->bittiming_const &&
1135 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
1136 sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
1138 nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
1139 nla_put_u32(skb, IFLA_CAN_STATE, state) ||
1140 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
1141 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
1143 (priv->do_get_berr_counter &&
1144 !priv->do_get_berr_counter(dev, &bec) &&
1145 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
1147 (priv->data_bittiming.bitrate &&
1148 nla_put(skb, IFLA_CAN_DATA_BITTIMING,
1149 sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
1151 (priv->data_bittiming_const &&
1152 nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
1153 sizeof(*priv->data_bittiming_const),
1154 priv->data_bittiming_const)) ||
1156 (priv->termination_const &&
1157 (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
1158 nla_put(skb, IFLA_CAN_TERMINATION_CONST,
1159 sizeof(*priv->termination_const) *
1160 priv->termination_const_cnt,
1161 priv->termination_const))) ||
1163 (priv->bitrate_const &&
1164 nla_put(skb, IFLA_CAN_BITRATE_CONST,
1165 sizeof(*priv->bitrate_const) *
1166 priv->bitrate_const_cnt,
1167 priv->bitrate_const)) ||
1169 (priv->data_bitrate_const &&
1170 nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
1171 sizeof(*priv->data_bitrate_const) *
1172 priv->data_bitrate_const_cnt,
1173 priv->data_bitrate_const)) ||
1175 (nla_put(skb, IFLA_CAN_BITRATE_MAX,
1176 sizeof(priv->bitrate_max),
1177 &priv->bitrate_max))
1185 static size_t can_get_xstats_size(const struct net_device *dev)
1187 return sizeof(struct can_device_stats);
1190 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
1192 struct can_priv *priv = netdev_priv(dev);
1194 if (nla_put(skb, IFLA_INFO_XSTATS,
1195 sizeof(priv->can_stats), &priv->can_stats))
1196 goto nla_put_failure;
1203 static int can_newlink(struct net *src_net, struct net_device *dev,
1204 struct nlattr *tb[], struct nlattr *data[],
1205 struct netlink_ext_ack *extack)
1210 static void can_dellink(struct net_device *dev, struct list_head *head)
1215 static struct rtnl_link_ops can_link_ops __read_mostly = {
1217 .maxtype = IFLA_CAN_MAX,
1218 .policy = can_policy,
1220 .validate = can_validate,
1221 .newlink = can_newlink,
1222 .changelink = can_changelink,
1223 .dellink = can_dellink,
1224 .get_size = can_get_size,
1225 .fill_info = can_fill_info,
1226 .get_xstats_size = can_get_xstats_size,
1227 .fill_xstats = can_fill_xstats,
1231 * Register the CAN network device
1233 int register_candev(struct net_device *dev)
1235 struct can_priv *priv = netdev_priv(dev);
1237 /* Ensure termination_const, termination_const_cnt and
1238 * do_set_termination consistency. All must be either set or
1241 if ((!priv->termination_const != !priv->termination_const_cnt) ||
1242 (!priv->termination_const != !priv->do_set_termination))
1245 if (!priv->bitrate_const != !priv->bitrate_const_cnt)
1248 if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
1251 dev->rtnl_link_ops = &can_link_ops;
1252 netif_carrier_off(dev);
1254 return register_netdev(dev);
1256 EXPORT_SYMBOL_GPL(register_candev);
1259 * Unregister the CAN network device
1261 void unregister_candev(struct net_device *dev)
1263 unregister_netdev(dev);
1265 EXPORT_SYMBOL_GPL(unregister_candev);
1268 * Test if a network device is a candev based device
1269 * and return the can_priv* if so.
1271 struct can_priv *safe_candev_priv(struct net_device *dev)
1273 if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
1276 return netdev_priv(dev);
1278 EXPORT_SYMBOL_GPL(safe_candev_priv);
1280 static __init int can_dev_init(void)
1284 can_led_notifier_init();
1286 err = rtnl_link_register(&can_link_ops);
1288 printk(KERN_INFO MOD_DESC "\n");
1292 module_init(can_dev_init);
1294 static __exit void can_dev_exit(void)
1296 rtnl_link_unregister(&can_link_ops);
1298 can_led_notifier_exit();
1300 module_exit(can_dev_exit);
1302 MODULE_ALIAS_RTNL_LINK("can");