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/can.h>
25 #include <linux/can/dev.h>
26 #include <linux/can/skb.h>
27 #include <linux/can/netlink.h>
28 #include <linux/can/led.h>
29 #include <net/rtnetlink.h>
31 #define MOD_DESC "CAN device driver interface"
33 MODULE_DESCRIPTION(MOD_DESC
);
34 MODULE_LICENSE("GPL v2");
35 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
37 /* CAN DLC to real data length conversion helpers */
39 static const u8 dlc2len
[] = {0, 1, 2, 3, 4, 5, 6, 7,
40 8, 12, 16, 20, 24, 32, 48, 64};
42 /* get data length from can_dlc with sanitized can_dlc */
43 u8
can_dlc2len(u8 can_dlc
)
45 return dlc2len
[can_dlc
& 0x0F];
47 EXPORT_SYMBOL_GPL(can_dlc2len
);
49 static 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 */
60 /* map the sanitized data length to an appropriate data length code */
61 u8
can_len2dlc(u8 len
)
63 if (unlikely(len
> 64))
68 EXPORT_SYMBOL_GPL(can_len2dlc
);
70 #ifdef CONFIG_CAN_CALC_BITTIMING
71 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
72 #define CAN_CALC_SYNC_SEG 1
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_sample_point(const struct can_bittiming_const
*btc
,
88 unsigned int sample_point_nominal
, unsigned int tseg
,
89 unsigned int *tseg1_ptr
, unsigned int *tseg2_ptr
,
90 unsigned int *sample_point_error_ptr
)
92 unsigned int sample_point_error
, best_sample_point_error
= UINT_MAX
;
93 unsigned int sample_point
, best_sample_point
= 0;
94 unsigned int tseg1
, tseg2
;
97 for (i
= 0; i
<= 1; i
++) {
98 tseg2
= tseg
+ CAN_CALC_SYNC_SEG
- (sample_point_nominal
* (tseg
+ CAN_CALC_SYNC_SEG
)) / 1000 - i
;
99 tseg2
= clamp(tseg2
, btc
->tseg2_min
, btc
->tseg2_max
);
100 tseg1
= tseg
- tseg2
;
101 if (tseg1
> btc
->tseg1_max
) {
102 tseg1
= btc
->tseg1_max
;
103 tseg2
= tseg
- tseg1
;
106 sample_point
= 1000 * (tseg
+ CAN_CALC_SYNC_SEG
- tseg2
) / (tseg
+ CAN_CALC_SYNC_SEG
);
107 sample_point_error
= abs(sample_point_nominal
- sample_point
);
109 if ((sample_point
<= sample_point_nominal
) && (sample_point_error
< best_sample_point_error
)) {
110 best_sample_point
= sample_point
;
111 best_sample_point_error
= sample_point_error
;
117 if (sample_point_error_ptr
)
118 *sample_point_error_ptr
= best_sample_point_error
;
120 return best_sample_point
;
123 static int can_calc_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
,
124 const struct can_bittiming_const
*btc
)
126 struct can_priv
*priv
= netdev_priv(dev
);
127 unsigned int bitrate
; /* current bitrate */
128 unsigned int bitrate_error
; /* difference between current and nominal value */
129 unsigned int best_bitrate_error
= UINT_MAX
;
130 unsigned int sample_point_error
; /* difference between current and nominal value */
131 unsigned int best_sample_point_error
= UINT_MAX
;
132 unsigned int sample_point_nominal
; /* nominal sample point */
133 unsigned int best_tseg
= 0; /* current best value for tseg */
134 unsigned int best_brp
= 0; /* current best value for brp */
135 unsigned int brp
, tsegall
, tseg
, tseg1
= 0, tseg2
= 0;
138 /* Use CiA recommended sample points */
139 if (bt
->sample_point
) {
140 sample_point_nominal
= bt
->sample_point
;
142 if (bt
->bitrate
> 800000)
143 sample_point_nominal
= 750;
144 else if (bt
->bitrate
> 500000)
145 sample_point_nominal
= 800;
147 sample_point_nominal
= 875;
150 /* tseg even = round down, odd = round up */
151 for (tseg
= (btc
->tseg1_max
+ btc
->tseg2_max
) * 2 + 1;
152 tseg
>= (btc
->tseg1_min
+ btc
->tseg2_min
) * 2; tseg
--) {
153 tsegall
= CAN_CALC_SYNC_SEG
+ tseg
/ 2;
155 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
156 brp
= priv
->clock
.freq
/ (tsegall
* bt
->bitrate
) + tseg
% 2;
158 /* choose brp step which is possible in system */
159 brp
= (brp
/ btc
->brp_inc
) * btc
->brp_inc
;
160 if ((brp
< btc
->brp_min
) || (brp
> btc
->brp_max
))
163 bitrate
= priv
->clock
.freq
/ (brp
* tsegall
);
164 bitrate_error
= abs(bt
->bitrate
- bitrate
);
166 /* tseg brp biterror */
167 if (bitrate_error
> best_bitrate_error
)
170 /* reset sample point error if we have a better bitrate */
171 if (bitrate_error
< best_bitrate_error
)
172 best_sample_point_error
= UINT_MAX
;
174 can_update_sample_point(btc
, sample_point_nominal
, tseg
/ 2, &tseg1
, &tseg2
, &sample_point_error
);
175 if (sample_point_error
> best_sample_point_error
)
178 best_sample_point_error
= sample_point_error
;
179 best_bitrate_error
= bitrate_error
;
180 best_tseg
= tseg
/ 2;
183 if (bitrate_error
== 0 && sample_point_error
== 0)
187 if (best_bitrate_error
) {
188 /* Error in one-tenth of a percent */
189 v64
= (u64
)best_bitrate_error
* 1000;
190 do_div(v64
, bt
->bitrate
);
191 bitrate_error
= (u32
)v64
;
192 if (bitrate_error
> CAN_CALC_MAX_ERROR
) {
194 "bitrate error %d.%d%% too high\n",
195 bitrate_error
/ 10, bitrate_error
% 10);
198 netdev_warn(dev
, "bitrate error %d.%d%%\n",
199 bitrate_error
/ 10, bitrate_error
% 10);
202 /* real sample point */
203 bt
->sample_point
= can_update_sample_point(btc
, sample_point_nominal
, best_tseg
,
204 &tseg1
, &tseg2
, NULL
);
206 v64
= (u64
)best_brp
* 1000 * 1000 * 1000;
207 do_div(v64
, priv
->clock
.freq
);
209 bt
->prop_seg
= tseg1
/ 2;
210 bt
->phase_seg1
= tseg1
- bt
->prop_seg
;
211 bt
->phase_seg2
= tseg2
;
213 /* check for sjw user settings */
214 if (!bt
->sjw
|| !btc
->sjw_max
) {
217 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
218 if (bt
->sjw
> btc
->sjw_max
)
219 bt
->sjw
= btc
->sjw_max
;
220 /* bt->sjw must not be higher than tseg2 */
228 bt
->bitrate
= priv
->clock
.freq
/ (bt
->brp
* (CAN_CALC_SYNC_SEG
+ tseg1
+ tseg2
));
232 #else /* !CONFIG_CAN_CALC_BITTIMING */
233 static int can_calc_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
,
234 const struct can_bittiming_const
*btc
)
236 netdev_err(dev
, "bit-timing calculation not available\n");
239 #endif /* CONFIG_CAN_CALC_BITTIMING */
242 * Checks the validity of the specified bit-timing parameters prop_seg,
243 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
244 * prescaler value brp. You can find more information in the header
245 * file linux/can/netlink.h.
247 static int can_fixup_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
,
248 const struct can_bittiming_const
*btc
)
250 struct can_priv
*priv
= netdev_priv(dev
);
254 tseg1
= bt
->prop_seg
+ bt
->phase_seg1
;
257 if (bt
->sjw
> btc
->sjw_max
||
258 tseg1
< btc
->tseg1_min
|| tseg1
> btc
->tseg1_max
||
259 bt
->phase_seg2
< btc
->tseg2_min
|| bt
->phase_seg2
> btc
->tseg2_max
)
262 brp64
= (u64
)priv
->clock
.freq
* (u64
)bt
->tq
;
263 if (btc
->brp_inc
> 1)
264 do_div(brp64
, btc
->brp_inc
);
265 brp64
+= 500000000UL - 1;
266 do_div(brp64
, 1000000000UL); /* the practicable BRP */
267 if (btc
->brp_inc
> 1)
268 brp64
*= btc
->brp_inc
;
269 bt
->brp
= (u32
)brp64
;
271 if (bt
->brp
< btc
->brp_min
|| bt
->brp
> btc
->brp_max
)
274 alltseg
= bt
->prop_seg
+ bt
->phase_seg1
+ bt
->phase_seg2
+ 1;
275 bt
->bitrate
= priv
->clock
.freq
/ (bt
->brp
* alltseg
);
276 bt
->sample_point
= ((tseg1
+ 1) * 1000) / alltseg
;
281 static int can_get_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
,
282 const struct can_bittiming_const
*btc
)
286 /* Check if the CAN device has bit-timing parameters */
291 * Depending on the given can_bittiming parameter structure the CAN
292 * timing parameters are calculated based on the provided bitrate OR
293 * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
294 * provided directly which are then checked and fixed up.
296 if (!bt
->tq
&& bt
->bitrate
)
297 err
= can_calc_bittiming(dev
, bt
, btc
);
298 else if (bt
->tq
&& !bt
->bitrate
)
299 err
= can_fixup_bittiming(dev
, bt
, btc
);
306 static void can_update_state_error_stats(struct net_device
*dev
,
307 enum can_state new_state
)
309 struct can_priv
*priv
= netdev_priv(dev
);
311 if (new_state
<= priv
->state
)
315 case CAN_STATE_ERROR_WARNING
:
316 priv
->can_stats
.error_warning
++;
318 case CAN_STATE_ERROR_PASSIVE
:
319 priv
->can_stats
.error_passive
++;
321 case CAN_STATE_BUS_OFF
:
322 priv
->can_stats
.bus_off
++;
329 static int can_tx_state_to_frame(struct net_device
*dev
, enum can_state state
)
332 case CAN_STATE_ERROR_ACTIVE
:
333 return CAN_ERR_CRTL_ACTIVE
;
334 case CAN_STATE_ERROR_WARNING
:
335 return CAN_ERR_CRTL_TX_WARNING
;
336 case CAN_STATE_ERROR_PASSIVE
:
337 return CAN_ERR_CRTL_TX_PASSIVE
;
343 static int can_rx_state_to_frame(struct net_device
*dev
, enum can_state state
)
346 case CAN_STATE_ERROR_ACTIVE
:
347 return CAN_ERR_CRTL_ACTIVE
;
348 case CAN_STATE_ERROR_WARNING
:
349 return CAN_ERR_CRTL_RX_WARNING
;
350 case CAN_STATE_ERROR_PASSIVE
:
351 return CAN_ERR_CRTL_RX_PASSIVE
;
357 void can_change_state(struct net_device
*dev
, struct can_frame
*cf
,
358 enum can_state tx_state
, enum can_state rx_state
)
360 struct can_priv
*priv
= netdev_priv(dev
);
361 enum can_state new_state
= max(tx_state
, rx_state
);
363 if (unlikely(new_state
== priv
->state
)) {
364 netdev_warn(dev
, "%s: oops, state did not change", __func__
);
368 netdev_dbg(dev
, "New error state: %d\n", new_state
);
370 can_update_state_error_stats(dev
, new_state
);
371 priv
->state
= new_state
;
373 if (unlikely(new_state
== CAN_STATE_BUS_OFF
)) {
374 cf
->can_id
|= CAN_ERR_BUSOFF
;
378 cf
->can_id
|= CAN_ERR_CRTL
;
379 cf
->data
[1] |= tx_state
>= rx_state
?
380 can_tx_state_to_frame(dev
, tx_state
) : 0;
381 cf
->data
[1] |= tx_state
<= rx_state
?
382 can_rx_state_to_frame(dev
, rx_state
) : 0;
384 EXPORT_SYMBOL_GPL(can_change_state
);
387 * Local echo of CAN messages
389 * CAN network devices *should* support a local echo functionality
390 * (see Documentation/networking/can.txt). To test the handling of CAN
391 * interfaces that do not support the local echo both driver types are
392 * implemented. In the case that the driver does not support the echo
393 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
394 * to perform the echo as a fallback solution.
396 static void can_flush_echo_skb(struct net_device
*dev
)
398 struct can_priv
*priv
= netdev_priv(dev
);
399 struct net_device_stats
*stats
= &dev
->stats
;
402 for (i
= 0; i
< priv
->echo_skb_max
; i
++) {
403 if (priv
->echo_skb
[i
]) {
404 kfree_skb(priv
->echo_skb
[i
]);
405 priv
->echo_skb
[i
] = NULL
;
407 stats
->tx_aborted_errors
++;
413 * Put the skb on the stack to be looped backed locally lateron
415 * The function is typically called in the start_xmit function
416 * of the device driver. The driver must protect access to
417 * priv->echo_skb, if necessary.
419 void can_put_echo_skb(struct sk_buff
*skb
, struct net_device
*dev
,
422 struct can_priv
*priv
= netdev_priv(dev
);
424 BUG_ON(idx
>= priv
->echo_skb_max
);
426 /* check flag whether this packet has to be looped back */
427 if (!(dev
->flags
& IFF_ECHO
) || skb
->pkt_type
!= PACKET_LOOPBACK
||
428 (skb
->protocol
!= htons(ETH_P_CAN
) &&
429 skb
->protocol
!= htons(ETH_P_CANFD
))) {
434 if (!priv
->echo_skb
[idx
]) {
436 skb
= can_create_echo_skb(skb
);
440 /* make settings for echo to reduce code in irq context */
441 skb
->pkt_type
= PACKET_BROADCAST
;
442 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
445 /* save this skb for tx interrupt echo handling */
446 priv
->echo_skb
[idx
] = skb
;
448 /* locking problem with netif_stop_queue() ?? */
449 netdev_err(dev
, "%s: BUG! echo_skb is occupied!\n", __func__
);
453 EXPORT_SYMBOL_GPL(can_put_echo_skb
);
456 * Get the skb from the stack and loop it back locally
458 * The function is typically called when the TX done interrupt
459 * is handled in the device driver. The driver must protect
460 * access to priv->echo_skb, if necessary.
462 unsigned int can_get_echo_skb(struct net_device
*dev
, unsigned int idx
)
464 struct can_priv
*priv
= netdev_priv(dev
);
466 BUG_ON(idx
>= priv
->echo_skb_max
);
468 if (priv
->echo_skb
[idx
]) {
469 struct sk_buff
*skb
= priv
->echo_skb
[idx
];
470 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
471 u8 dlc
= cf
->can_dlc
;
473 netif_rx(priv
->echo_skb
[idx
]);
474 priv
->echo_skb
[idx
] = NULL
;
481 EXPORT_SYMBOL_GPL(can_get_echo_skb
);
484 * Remove the skb from the stack and free it.
486 * The function is typically called when TX failed.
488 void can_free_echo_skb(struct net_device
*dev
, unsigned int idx
)
490 struct can_priv
*priv
= netdev_priv(dev
);
492 BUG_ON(idx
>= priv
->echo_skb_max
);
494 if (priv
->echo_skb
[idx
]) {
495 dev_kfree_skb_any(priv
->echo_skb
[idx
]);
496 priv
->echo_skb
[idx
] = NULL
;
499 EXPORT_SYMBOL_GPL(can_free_echo_skb
);
502 * CAN device restart for bus-off recovery
504 static void can_restart(unsigned long data
)
506 struct net_device
*dev
= (struct net_device
*)data
;
507 struct can_priv
*priv
= netdev_priv(dev
);
508 struct net_device_stats
*stats
= &dev
->stats
;
510 struct can_frame
*cf
;
513 BUG_ON(netif_carrier_ok(dev
));
516 * No synchronization needed because the device is bus-off and
517 * no messages can come in or go out.
519 can_flush_echo_skb(dev
);
521 /* send restart message upstream */
522 skb
= alloc_can_err_skb(dev
, &cf
);
527 cf
->can_id
|= CAN_ERR_RESTARTED
;
532 stats
->rx_bytes
+= cf
->can_dlc
;
535 netdev_dbg(dev
, "restarted\n");
536 priv
->can_stats
.restarts
++;
538 /* Now restart the device */
539 err
= priv
->do_set_mode(dev
, CAN_MODE_START
);
541 netif_carrier_on(dev
);
543 netdev_err(dev
, "Error %d during restart", err
);
546 int can_restart_now(struct net_device
*dev
)
548 struct can_priv
*priv
= netdev_priv(dev
);
551 * A manual restart is only permitted if automatic restart is
552 * disabled and the device is in the bus-off state
554 if (priv
->restart_ms
)
556 if (priv
->state
!= CAN_STATE_BUS_OFF
)
559 /* Runs as soon as possible in the timer context */
560 mod_timer(&priv
->restart_timer
, jiffies
);
568 * This functions should be called when the device goes bus-off to
569 * tell the netif layer that no more packets can be sent or received.
570 * If enabled, a timer is started to trigger bus-off recovery.
572 void can_bus_off(struct net_device
*dev
)
574 struct can_priv
*priv
= netdev_priv(dev
);
576 netdev_dbg(dev
, "bus-off\n");
578 netif_carrier_off(dev
);
580 if (priv
->restart_ms
)
581 mod_timer(&priv
->restart_timer
,
582 jiffies
+ (priv
->restart_ms
* HZ
) / 1000);
584 EXPORT_SYMBOL_GPL(can_bus_off
);
586 static void can_setup(struct net_device
*dev
)
588 dev
->type
= ARPHRD_CAN
;
590 dev
->hard_header_len
= 0;
592 dev
->tx_queue_len
= 10;
594 /* New-style flags. */
595 dev
->flags
= IFF_NOARP
;
596 dev
->features
= NETIF_F_HW_CSUM
;
599 struct sk_buff
*alloc_can_skb(struct net_device
*dev
, struct can_frame
**cf
)
603 skb
= netdev_alloc_skb(dev
, sizeof(struct can_skb_priv
) +
604 sizeof(struct can_frame
));
608 skb
->protocol
= htons(ETH_P_CAN
);
609 skb
->pkt_type
= PACKET_BROADCAST
;
610 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
612 skb_reset_mac_header(skb
);
613 skb_reset_network_header(skb
);
614 skb_reset_transport_header(skb
);
616 can_skb_reserve(skb
);
617 can_skb_prv(skb
)->ifindex
= dev
->ifindex
;
618 can_skb_prv(skb
)->skbcnt
= 0;
620 *cf
= (struct can_frame
*)skb_put(skb
, sizeof(struct can_frame
));
621 memset(*cf
, 0, sizeof(struct can_frame
));
625 EXPORT_SYMBOL_GPL(alloc_can_skb
);
627 struct sk_buff
*alloc_canfd_skb(struct net_device
*dev
,
628 struct canfd_frame
**cfd
)
632 skb
= netdev_alloc_skb(dev
, sizeof(struct can_skb_priv
) +
633 sizeof(struct canfd_frame
));
637 skb
->protocol
= htons(ETH_P_CANFD
);
638 skb
->pkt_type
= PACKET_BROADCAST
;
639 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
641 skb_reset_mac_header(skb
);
642 skb_reset_network_header(skb
);
643 skb_reset_transport_header(skb
);
645 can_skb_reserve(skb
);
646 can_skb_prv(skb
)->ifindex
= dev
->ifindex
;
647 can_skb_prv(skb
)->skbcnt
= 0;
649 *cfd
= (struct canfd_frame
*)skb_put(skb
, sizeof(struct canfd_frame
));
650 memset(*cfd
, 0, sizeof(struct canfd_frame
));
654 EXPORT_SYMBOL_GPL(alloc_canfd_skb
);
656 struct sk_buff
*alloc_can_err_skb(struct net_device
*dev
, struct can_frame
**cf
)
660 skb
= alloc_can_skb(dev
, cf
);
664 (*cf
)->can_id
= CAN_ERR_FLAG
;
665 (*cf
)->can_dlc
= CAN_ERR_DLC
;
669 EXPORT_SYMBOL_GPL(alloc_can_err_skb
);
672 * Allocate and setup space for the CAN network device
674 struct net_device
*alloc_candev(int sizeof_priv
, unsigned int echo_skb_max
)
676 struct net_device
*dev
;
677 struct can_priv
*priv
;
681 size
= ALIGN(sizeof_priv
, sizeof(struct sk_buff
*)) +
682 echo_skb_max
* sizeof(struct sk_buff
*);
686 dev
= alloc_netdev(size
, "can%d", NET_NAME_UNKNOWN
, can_setup
);
690 priv
= netdev_priv(dev
);
693 priv
->echo_skb_max
= echo_skb_max
;
694 priv
->echo_skb
= (void *)priv
+
695 ALIGN(sizeof_priv
, sizeof(struct sk_buff
*));
698 priv
->state
= CAN_STATE_STOPPED
;
700 init_timer(&priv
->restart_timer
);
704 EXPORT_SYMBOL_GPL(alloc_candev
);
707 * Free space of the CAN network device
709 void free_candev(struct net_device
*dev
)
713 EXPORT_SYMBOL_GPL(free_candev
);
716 * changing MTU and control mode for CAN/CANFD devices
718 int can_change_mtu(struct net_device
*dev
, int new_mtu
)
720 struct can_priv
*priv
= netdev_priv(dev
);
722 /* Do not allow changing the MTU while running */
723 if (dev
->flags
& IFF_UP
)
726 /* allow change of MTU according to the CANFD ability of the device */
729 /* 'CANFD-only' controllers can not switch to CAN_MTU */
730 if (priv
->ctrlmode_static
& CAN_CTRLMODE_FD
)
733 priv
->ctrlmode
&= ~CAN_CTRLMODE_FD
;
737 /* check for potential CANFD ability */
738 if (!(priv
->ctrlmode_supported
& CAN_CTRLMODE_FD
) &&
739 !(priv
->ctrlmode_static
& CAN_CTRLMODE_FD
))
742 priv
->ctrlmode
|= CAN_CTRLMODE_FD
;
752 EXPORT_SYMBOL_GPL(can_change_mtu
);
755 * Common open function when the device gets opened.
757 * This function should be called in the open function of the device
760 int open_candev(struct net_device
*dev
)
762 struct can_priv
*priv
= netdev_priv(dev
);
764 if (!priv
->bittiming
.bitrate
) {
765 netdev_err(dev
, "bit-timing not yet defined\n");
769 /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
770 if ((priv
->ctrlmode
& CAN_CTRLMODE_FD
) &&
771 (!priv
->data_bittiming
.bitrate
||
772 (priv
->data_bittiming
.bitrate
< priv
->bittiming
.bitrate
))) {
773 netdev_err(dev
, "incorrect/missing data bit-timing\n");
777 /* Switch carrier on if device was stopped while in bus-off state */
778 if (!netif_carrier_ok(dev
))
779 netif_carrier_on(dev
);
781 setup_timer(&priv
->restart_timer
, can_restart
, (unsigned long)dev
);
785 EXPORT_SYMBOL_GPL(open_candev
);
788 * Common close function for cleanup before the device gets closed.
790 * This function should be called in the close function of the device
793 void close_candev(struct net_device
*dev
)
795 struct can_priv
*priv
= netdev_priv(dev
);
797 del_timer_sync(&priv
->restart_timer
);
798 can_flush_echo_skb(dev
);
800 EXPORT_SYMBOL_GPL(close_candev
);
803 * CAN netlink interface
805 static const struct nla_policy can_policy
[IFLA_CAN_MAX
+ 1] = {
806 [IFLA_CAN_STATE
] = { .type
= NLA_U32
},
807 [IFLA_CAN_CTRLMODE
] = { .len
= sizeof(struct can_ctrlmode
) },
808 [IFLA_CAN_RESTART_MS
] = { .type
= NLA_U32
},
809 [IFLA_CAN_RESTART
] = { .type
= NLA_U32
},
810 [IFLA_CAN_BITTIMING
] = { .len
= sizeof(struct can_bittiming
) },
811 [IFLA_CAN_BITTIMING_CONST
]
812 = { .len
= sizeof(struct can_bittiming_const
) },
813 [IFLA_CAN_CLOCK
] = { .len
= sizeof(struct can_clock
) },
814 [IFLA_CAN_BERR_COUNTER
] = { .len
= sizeof(struct can_berr_counter
) },
815 [IFLA_CAN_DATA_BITTIMING
]
816 = { .len
= sizeof(struct can_bittiming
) },
817 [IFLA_CAN_DATA_BITTIMING_CONST
]
818 = { .len
= sizeof(struct can_bittiming_const
) },
821 static int can_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
823 bool is_can_fd
= false;
825 /* Make sure that valid CAN FD configurations always consist of
826 * - nominal/arbitration bittiming
828 * - control mode with CAN_CTRLMODE_FD set
834 if (data
[IFLA_CAN_CTRLMODE
]) {
835 struct can_ctrlmode
*cm
= nla_data(data
[IFLA_CAN_CTRLMODE
]);
837 is_can_fd
= cm
->flags
& cm
->mask
& CAN_CTRLMODE_FD
;
841 if (!data
[IFLA_CAN_BITTIMING
] || !data
[IFLA_CAN_DATA_BITTIMING
])
845 if (data
[IFLA_CAN_DATA_BITTIMING
]) {
846 if (!is_can_fd
|| !data
[IFLA_CAN_BITTIMING
])
853 static int can_changelink(struct net_device
*dev
,
854 struct nlattr
*tb
[], struct nlattr
*data
[])
856 struct can_priv
*priv
= netdev_priv(dev
);
859 /* We need synchronization with dev->stop() */
862 if (data
[IFLA_CAN_BITTIMING
]) {
863 struct can_bittiming bt
;
865 /* Do not allow changing bittiming while running */
866 if (dev
->flags
& IFF_UP
)
868 memcpy(&bt
, nla_data(data
[IFLA_CAN_BITTIMING
]), sizeof(bt
));
869 err
= can_get_bittiming(dev
, &bt
, priv
->bittiming_const
);
872 memcpy(&priv
->bittiming
, &bt
, sizeof(bt
));
874 if (priv
->do_set_bittiming
) {
875 /* Finally, set the bit-timing registers */
876 err
= priv
->do_set_bittiming(dev
);
882 if (data
[IFLA_CAN_CTRLMODE
]) {
883 struct can_ctrlmode
*cm
;
887 /* Do not allow changing controller mode while running */
888 if (dev
->flags
& IFF_UP
)
890 cm
= nla_data(data
[IFLA_CAN_CTRLMODE
]);
891 ctrlstatic
= priv
->ctrlmode_static
;
892 maskedflags
= cm
->flags
& cm
->mask
;
894 /* check whether provided bits are allowed to be passed */
895 if (cm
->mask
& ~(priv
->ctrlmode_supported
| ctrlstatic
))
898 /* do not check for static fd-non-iso if 'fd' is disabled */
899 if (!(maskedflags
& CAN_CTRLMODE_FD
))
900 ctrlstatic
&= ~CAN_CTRLMODE_FD_NON_ISO
;
902 /* make sure static options are provided by configuration */
903 if ((maskedflags
& ctrlstatic
) != ctrlstatic
)
906 /* clear bits to be modified and copy the flag values */
907 priv
->ctrlmode
&= ~cm
->mask
;
908 priv
->ctrlmode
|= maskedflags
;
910 /* CAN_CTRLMODE_FD can only be set when driver supports FD */
911 if (priv
->ctrlmode
& CAN_CTRLMODE_FD
)
912 dev
->mtu
= CANFD_MTU
;
917 if (data
[IFLA_CAN_RESTART_MS
]) {
918 /* Do not allow changing restart delay while running */
919 if (dev
->flags
& IFF_UP
)
921 priv
->restart_ms
= nla_get_u32(data
[IFLA_CAN_RESTART_MS
]);
924 if (data
[IFLA_CAN_RESTART
]) {
925 /* Do not allow a restart while not running */
926 if (!(dev
->flags
& IFF_UP
))
928 err
= can_restart_now(dev
);
933 if (data
[IFLA_CAN_DATA_BITTIMING
]) {
934 struct can_bittiming dbt
;
936 /* Do not allow changing bittiming while running */
937 if (dev
->flags
& IFF_UP
)
939 memcpy(&dbt
, nla_data(data
[IFLA_CAN_DATA_BITTIMING
]),
941 err
= can_get_bittiming(dev
, &dbt
, priv
->data_bittiming_const
);
944 memcpy(&priv
->data_bittiming
, &dbt
, sizeof(dbt
));
946 if (priv
->do_set_data_bittiming
) {
947 /* Finally, set the bit-timing registers */
948 err
= priv
->do_set_data_bittiming(dev
);
957 static size_t can_get_size(const struct net_device
*dev
)
959 struct can_priv
*priv
= netdev_priv(dev
);
962 if (priv
->bittiming
.bitrate
) /* IFLA_CAN_BITTIMING */
963 size
+= nla_total_size(sizeof(struct can_bittiming
));
964 if (priv
->bittiming_const
) /* IFLA_CAN_BITTIMING_CONST */
965 size
+= nla_total_size(sizeof(struct can_bittiming_const
));
966 size
+= nla_total_size(sizeof(struct can_clock
)); /* IFLA_CAN_CLOCK */
967 size
+= nla_total_size(sizeof(u32
)); /* IFLA_CAN_STATE */
968 size
+= nla_total_size(sizeof(struct can_ctrlmode
)); /* IFLA_CAN_CTRLMODE */
969 size
+= nla_total_size(sizeof(u32
)); /* IFLA_CAN_RESTART_MS */
970 if (priv
->do_get_berr_counter
) /* IFLA_CAN_BERR_COUNTER */
971 size
+= nla_total_size(sizeof(struct can_berr_counter
));
972 if (priv
->data_bittiming
.bitrate
) /* IFLA_CAN_DATA_BITTIMING */
973 size
+= nla_total_size(sizeof(struct can_bittiming
));
974 if (priv
->data_bittiming_const
) /* IFLA_CAN_DATA_BITTIMING_CONST */
975 size
+= nla_total_size(sizeof(struct can_bittiming_const
));
980 static int can_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
982 struct can_priv
*priv
= netdev_priv(dev
);
983 struct can_ctrlmode cm
= {.flags
= priv
->ctrlmode
};
984 struct can_berr_counter bec
;
985 enum can_state state
= priv
->state
;
987 if (priv
->do_get_state
)
988 priv
->do_get_state(dev
, &state
);
990 if ((priv
->bittiming
.bitrate
&&
991 nla_put(skb
, IFLA_CAN_BITTIMING
,
992 sizeof(priv
->bittiming
), &priv
->bittiming
)) ||
994 (priv
->bittiming_const
&&
995 nla_put(skb
, IFLA_CAN_BITTIMING_CONST
,
996 sizeof(*priv
->bittiming_const
), priv
->bittiming_const
)) ||
998 nla_put(skb
, IFLA_CAN_CLOCK
, sizeof(priv
->clock
), &priv
->clock
) ||
999 nla_put_u32(skb
, IFLA_CAN_STATE
, state
) ||
1000 nla_put(skb
, IFLA_CAN_CTRLMODE
, sizeof(cm
), &cm
) ||
1001 nla_put_u32(skb
, IFLA_CAN_RESTART_MS
, priv
->restart_ms
) ||
1003 (priv
->do_get_berr_counter
&&
1004 !priv
->do_get_berr_counter(dev
, &bec
) &&
1005 nla_put(skb
, IFLA_CAN_BERR_COUNTER
, sizeof(bec
), &bec
)) ||
1007 (priv
->data_bittiming
.bitrate
&&
1008 nla_put(skb
, IFLA_CAN_DATA_BITTIMING
,
1009 sizeof(priv
->data_bittiming
), &priv
->data_bittiming
)) ||
1011 (priv
->data_bittiming_const
&&
1012 nla_put(skb
, IFLA_CAN_DATA_BITTIMING_CONST
,
1013 sizeof(*priv
->data_bittiming_const
),
1014 priv
->data_bittiming_const
)))
1020 static size_t can_get_xstats_size(const struct net_device
*dev
)
1022 return sizeof(struct can_device_stats
);
1025 static int can_fill_xstats(struct sk_buff
*skb
, const struct net_device
*dev
)
1027 struct can_priv
*priv
= netdev_priv(dev
);
1029 if (nla_put(skb
, IFLA_INFO_XSTATS
,
1030 sizeof(priv
->can_stats
), &priv
->can_stats
))
1031 goto nla_put_failure
;
1038 static int can_newlink(struct net
*src_net
, struct net_device
*dev
,
1039 struct nlattr
*tb
[], struct nlattr
*data
[])
1044 static void can_dellink(struct net_device
*dev
, struct list_head
*head
)
1049 static struct rtnl_link_ops can_link_ops __read_mostly
= {
1051 .maxtype
= IFLA_CAN_MAX
,
1052 .policy
= can_policy
,
1054 .validate
= can_validate
,
1055 .newlink
= can_newlink
,
1056 .changelink
= can_changelink
,
1057 .dellink
= can_dellink
,
1058 .get_size
= can_get_size
,
1059 .fill_info
= can_fill_info
,
1060 .get_xstats_size
= can_get_xstats_size
,
1061 .fill_xstats
= can_fill_xstats
,
1065 * Register the CAN network device
1067 int register_candev(struct net_device
*dev
)
1069 dev
->rtnl_link_ops
= &can_link_ops
;
1070 return register_netdev(dev
);
1072 EXPORT_SYMBOL_GPL(register_candev
);
1075 * Unregister the CAN network device
1077 void unregister_candev(struct net_device
*dev
)
1079 unregister_netdev(dev
);
1081 EXPORT_SYMBOL_GPL(unregister_candev
);
1084 * Test if a network device is a candev based device
1085 * and return the can_priv* if so.
1087 struct can_priv
*safe_candev_priv(struct net_device
*dev
)
1089 if ((dev
->type
!= ARPHRD_CAN
) || (dev
->rtnl_link_ops
!= &can_link_ops
))
1092 return netdev_priv(dev
);
1094 EXPORT_SYMBOL_GPL(safe_candev_priv
);
1096 static __init
int can_dev_init(void)
1100 can_led_notifier_init();
1102 err
= rtnl_link_register(&can_link_ops
);
1104 printk(KERN_INFO MOD_DESC
"\n");
1108 module_init(can_dev_init
);
1110 static __exit
void can_dev_exit(void)
1112 rtnl_link_unregister(&can_link_ops
);
1114 can_led_notifier_exit();
1116 module_exit(can_dev_exit
);
1118 MODULE_ALIAS_RTNL_LINK("can");