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 */
73 #define CAN_CALC_SYNC_SEG 1
76 * Bit-timing calculation derived from:
78 * Code based on LinCAN sources and H8S2638 project
79 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
80 * Copyright 2005 Stanislav Marek
81 * email: pisa@cmp.felk.cvut.cz
83 * Calculates proper bit-timing parameters for a specified bit-rate
84 * and sample-point, which can then be used to set the bit-timing
85 * registers of the CAN controller. You can find more information
86 * in the header file linux/can/netlink.h.
88 static int can_update_sample_point(const struct can_bittiming_const
*btc
,
89 unsigned int sample_point_nominal
, unsigned int tseg
,
90 unsigned int *tseg1_ptr
, unsigned int *tseg2_ptr
,
91 unsigned int *sample_point_error_ptr
)
93 unsigned int sample_point_error
, best_sample_point_error
= UINT_MAX
;
94 unsigned int sample_point
, best_sample_point
= 0;
95 unsigned int tseg1
, tseg2
;
98 for (i
= 0; i
<= 1; i
++) {
99 tseg2
= tseg
+ CAN_CALC_SYNC_SEG
- (sample_point_nominal
* (tseg
+ CAN_CALC_SYNC_SEG
)) / 1000 - i
;
100 tseg2
= clamp(tseg2
, btc
->tseg2_min
, btc
->tseg2_max
);
101 tseg1
= tseg
- tseg2
;
102 if (tseg1
> btc
->tseg1_max
) {
103 tseg1
= btc
->tseg1_max
;
104 tseg2
= tseg
- tseg1
;
107 sample_point
= 1000 * (tseg
+ CAN_CALC_SYNC_SEG
- tseg2
) / (tseg
+ CAN_CALC_SYNC_SEG
);
108 sample_point_error
= abs(sample_point_nominal
- sample_point
);
110 if ((sample_point
<= sample_point_nominal
) && (sample_point_error
< best_sample_point_error
)) {
111 best_sample_point
= sample_point
;
112 best_sample_point_error
= sample_point_error
;
118 if (sample_point_error_ptr
)
119 *sample_point_error_ptr
= best_sample_point_error
;
121 return best_sample_point
;
124 static int can_calc_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
,
125 const struct can_bittiming_const
*btc
)
127 struct can_priv
*priv
= netdev_priv(dev
);
128 unsigned int bitrate
; /* current bitrate */
129 unsigned int bitrate_error
; /* difference between current and nominal value */
130 unsigned int best_bitrate_error
= UINT_MAX
;
131 unsigned int sample_point_error
; /* difference between current and nominal value */
132 unsigned int best_sample_point_error
= UINT_MAX
;
133 unsigned int sample_point_nominal
; /* nominal sample point */
134 unsigned int best_tseg
= 0; /* current best value for tseg */
135 unsigned int best_brp
= 0; /* current best value for brp */
136 unsigned int brp
, tsegall
, tseg
, tseg1
= 0, tseg2
= 0;
139 /* Use CiA recommended sample points */
140 if (bt
->sample_point
) {
141 sample_point_nominal
= bt
->sample_point
;
143 if (bt
->bitrate
> 800000)
144 sample_point_nominal
= 750;
145 else if (bt
->bitrate
> 500000)
146 sample_point_nominal
= 800;
148 sample_point_nominal
= 875;
151 /* tseg even = round down, odd = round up */
152 for (tseg
= (btc
->tseg1_max
+ btc
->tseg2_max
) * 2 + 1;
153 tseg
>= (btc
->tseg1_min
+ btc
->tseg2_min
) * 2; tseg
--) {
154 tsegall
= CAN_CALC_SYNC_SEG
+ tseg
/ 2;
156 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
157 brp
= priv
->clock
.freq
/ (tsegall
* bt
->bitrate
) + tseg
% 2;
159 /* choose brp step which is possible in system */
160 brp
= (brp
/ btc
->brp_inc
) * btc
->brp_inc
;
161 if ((brp
< btc
->brp_min
) || (brp
> btc
->brp_max
))
164 bitrate
= priv
->clock
.freq
/ (brp
* tsegall
);
165 bitrate_error
= abs(bt
->bitrate
- bitrate
);
167 /* tseg brp biterror */
168 if (bitrate_error
> best_bitrate_error
)
171 /* reset sample point error if we have a better bitrate */
172 if (bitrate_error
< best_bitrate_error
)
173 best_sample_point_error
= UINT_MAX
;
175 can_update_sample_point(btc
, sample_point_nominal
, tseg
/ 2, &tseg1
, &tseg2
, &sample_point_error
);
176 if (sample_point_error
> best_sample_point_error
)
179 best_sample_point_error
= sample_point_error
;
180 best_bitrate_error
= bitrate_error
;
181 best_tseg
= tseg
/ 2;
184 if (bitrate_error
== 0 && sample_point_error
== 0)
188 if (best_bitrate_error
) {
189 /* Error in one-tenth of a percent */
190 v64
= (u64
)best_bitrate_error
* 1000;
191 do_div(v64
, bt
->bitrate
);
192 bitrate_error
= (u32
)v64
;
193 if (bitrate_error
> CAN_CALC_MAX_ERROR
) {
195 "bitrate error %d.%d%% too high\n",
196 bitrate_error
/ 10, bitrate_error
% 10);
199 netdev_warn(dev
, "bitrate error %d.%d%%\n",
200 bitrate_error
/ 10, bitrate_error
% 10);
203 /* real sample point */
204 bt
->sample_point
= can_update_sample_point(btc
, sample_point_nominal
, best_tseg
,
205 &tseg1
, &tseg2
, NULL
);
207 v64
= (u64
)best_brp
* 1000 * 1000 * 1000;
208 do_div(v64
, priv
->clock
.freq
);
210 bt
->prop_seg
= tseg1
/ 2;
211 bt
->phase_seg1
= tseg1
- bt
->prop_seg
;
212 bt
->phase_seg2
= tseg2
;
214 /* check for sjw user settings */
215 if (!bt
->sjw
|| !btc
->sjw_max
) {
218 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
219 if (bt
->sjw
> btc
->sjw_max
)
220 bt
->sjw
= btc
->sjw_max
;
221 /* bt->sjw must not be higher than tseg2 */
229 bt
->bitrate
= priv
->clock
.freq
/ (bt
->brp
* (CAN_CALC_SYNC_SEG
+ tseg1
+ tseg2
));
233 #else /* !CONFIG_CAN_CALC_BITTIMING */
234 static int can_calc_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
,
235 const struct can_bittiming_const
*btc
)
237 netdev_err(dev
, "bit-timing calculation not available\n");
240 #endif /* CONFIG_CAN_CALC_BITTIMING */
243 * Checks the validity of the specified bit-timing parameters prop_seg,
244 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
245 * prescaler value brp. You can find more information in the header
246 * file linux/can/netlink.h.
248 static int can_fixup_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
,
249 const struct can_bittiming_const
*btc
)
251 struct can_priv
*priv
= netdev_priv(dev
);
255 tseg1
= bt
->prop_seg
+ bt
->phase_seg1
;
258 if (bt
->sjw
> btc
->sjw_max
||
259 tseg1
< btc
->tseg1_min
|| tseg1
> btc
->tseg1_max
||
260 bt
->phase_seg2
< btc
->tseg2_min
|| bt
->phase_seg2
> btc
->tseg2_max
)
263 brp64
= (u64
)priv
->clock
.freq
* (u64
)bt
->tq
;
264 if (btc
->brp_inc
> 1)
265 do_div(brp64
, btc
->brp_inc
);
266 brp64
+= 500000000UL - 1;
267 do_div(brp64
, 1000000000UL); /* the practicable BRP */
268 if (btc
->brp_inc
> 1)
269 brp64
*= btc
->brp_inc
;
270 bt
->brp
= (u32
)brp64
;
272 if (bt
->brp
< btc
->brp_min
|| bt
->brp
> btc
->brp_max
)
275 alltseg
= bt
->prop_seg
+ bt
->phase_seg1
+ bt
->phase_seg2
+ 1;
276 bt
->bitrate
= priv
->clock
.freq
/ (bt
->brp
* alltseg
);
277 bt
->sample_point
= ((tseg1
+ 1) * 1000) / alltseg
;
282 /* Checks the validity of predefined bitrate settings */
283 static int can_validate_bitrate(struct net_device
*dev
, struct can_bittiming
*bt
,
284 const u32
*bitrate_const
,
285 const unsigned int bitrate_const_cnt
)
287 struct can_priv
*priv
= netdev_priv(dev
);
290 for (i
= 0; i
< bitrate_const_cnt
; i
++) {
291 if (bt
->bitrate
== bitrate_const
[i
])
295 if (i
>= priv
->bitrate_const_cnt
)
301 static int can_get_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
,
302 const struct can_bittiming_const
*btc
,
303 const u32
*bitrate_const
,
304 const unsigned int bitrate_const_cnt
)
309 * Depending on the given can_bittiming parameter structure the CAN
310 * timing parameters are calculated based on the provided bitrate OR
311 * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
312 * provided directly which are then checked and fixed up.
314 if (!bt
->tq
&& bt
->bitrate
&& btc
)
315 err
= can_calc_bittiming(dev
, bt
, btc
);
316 else if (bt
->tq
&& !bt
->bitrate
&& btc
)
317 err
= can_fixup_bittiming(dev
, bt
, btc
);
318 else if (!bt
->tq
&& bt
->bitrate
&& bitrate_const
)
319 err
= can_validate_bitrate(dev
, bt
, bitrate_const
,
327 static void can_update_state_error_stats(struct net_device
*dev
,
328 enum can_state new_state
)
330 struct can_priv
*priv
= netdev_priv(dev
);
332 if (new_state
<= priv
->state
)
336 case CAN_STATE_ERROR_WARNING
:
337 priv
->can_stats
.error_warning
++;
339 case CAN_STATE_ERROR_PASSIVE
:
340 priv
->can_stats
.error_passive
++;
342 case CAN_STATE_BUS_OFF
:
343 priv
->can_stats
.bus_off
++;
350 static int can_tx_state_to_frame(struct net_device
*dev
, enum can_state state
)
353 case CAN_STATE_ERROR_ACTIVE
:
354 return CAN_ERR_CRTL_ACTIVE
;
355 case CAN_STATE_ERROR_WARNING
:
356 return CAN_ERR_CRTL_TX_WARNING
;
357 case CAN_STATE_ERROR_PASSIVE
:
358 return CAN_ERR_CRTL_TX_PASSIVE
;
364 static int can_rx_state_to_frame(struct net_device
*dev
, enum can_state state
)
367 case CAN_STATE_ERROR_ACTIVE
:
368 return CAN_ERR_CRTL_ACTIVE
;
369 case CAN_STATE_ERROR_WARNING
:
370 return CAN_ERR_CRTL_RX_WARNING
;
371 case CAN_STATE_ERROR_PASSIVE
:
372 return CAN_ERR_CRTL_RX_PASSIVE
;
378 void can_change_state(struct net_device
*dev
, struct can_frame
*cf
,
379 enum can_state tx_state
, enum can_state rx_state
)
381 struct can_priv
*priv
= netdev_priv(dev
);
382 enum can_state new_state
= max(tx_state
, rx_state
);
384 if (unlikely(new_state
== priv
->state
)) {
385 netdev_warn(dev
, "%s: oops, state did not change", __func__
);
389 netdev_dbg(dev
, "New error state: %d\n", new_state
);
391 can_update_state_error_stats(dev
, new_state
);
392 priv
->state
= new_state
;
397 if (unlikely(new_state
== CAN_STATE_BUS_OFF
)) {
398 cf
->can_id
|= CAN_ERR_BUSOFF
;
402 cf
->can_id
|= CAN_ERR_CRTL
;
403 cf
->data
[1] |= tx_state
>= rx_state
?
404 can_tx_state_to_frame(dev
, tx_state
) : 0;
405 cf
->data
[1] |= tx_state
<= rx_state
?
406 can_rx_state_to_frame(dev
, rx_state
) : 0;
408 EXPORT_SYMBOL_GPL(can_change_state
);
411 * Local echo of CAN messages
413 * CAN network devices *should* support a local echo functionality
414 * (see Documentation/networking/can.txt). To test the handling of CAN
415 * interfaces that do not support the local echo both driver types are
416 * implemented. In the case that the driver does not support the echo
417 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
418 * to perform the echo as a fallback solution.
420 static void can_flush_echo_skb(struct net_device
*dev
)
422 struct can_priv
*priv
= netdev_priv(dev
);
423 struct net_device_stats
*stats
= &dev
->stats
;
426 for (i
= 0; i
< priv
->echo_skb_max
; i
++) {
427 if (priv
->echo_skb
[i
]) {
428 kfree_skb(priv
->echo_skb
[i
]);
429 priv
->echo_skb
[i
] = NULL
;
431 stats
->tx_aborted_errors
++;
437 * Put the skb on the stack to be looped backed locally lateron
439 * The function is typically called in the start_xmit function
440 * of the device driver. The driver must protect access to
441 * priv->echo_skb, if necessary.
443 void can_put_echo_skb(struct sk_buff
*skb
, struct net_device
*dev
,
446 struct can_priv
*priv
= netdev_priv(dev
);
448 BUG_ON(idx
>= priv
->echo_skb_max
);
450 /* check flag whether this packet has to be looped back */
451 if (!(dev
->flags
& IFF_ECHO
) || skb
->pkt_type
!= PACKET_LOOPBACK
||
452 (skb
->protocol
!= htons(ETH_P_CAN
) &&
453 skb
->protocol
!= htons(ETH_P_CANFD
))) {
458 if (!priv
->echo_skb
[idx
]) {
460 skb
= can_create_echo_skb(skb
);
464 /* make settings for echo to reduce code in irq context */
465 skb
->pkt_type
= PACKET_BROADCAST
;
466 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
469 /* save this skb for tx interrupt echo handling */
470 priv
->echo_skb
[idx
] = skb
;
472 /* locking problem with netif_stop_queue() ?? */
473 netdev_err(dev
, "%s: BUG! echo_skb is occupied!\n", __func__
);
477 EXPORT_SYMBOL_GPL(can_put_echo_skb
);
480 * Get the skb from the stack and loop it back locally
482 * The function is typically called when the TX done interrupt
483 * is handled in the device driver. The driver must protect
484 * access to priv->echo_skb, if necessary.
486 unsigned int can_get_echo_skb(struct net_device
*dev
, unsigned int idx
)
488 struct can_priv
*priv
= netdev_priv(dev
);
490 BUG_ON(idx
>= priv
->echo_skb_max
);
492 if (priv
->echo_skb
[idx
]) {
493 struct sk_buff
*skb
= priv
->echo_skb
[idx
];
494 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
495 u8 dlc
= cf
->can_dlc
;
497 netif_rx(priv
->echo_skb
[idx
]);
498 priv
->echo_skb
[idx
] = NULL
;
505 EXPORT_SYMBOL_GPL(can_get_echo_skb
);
508 * Remove the skb from the stack and free it.
510 * The function is typically called when TX failed.
512 void can_free_echo_skb(struct net_device
*dev
, unsigned int idx
)
514 struct can_priv
*priv
= netdev_priv(dev
);
516 BUG_ON(idx
>= priv
->echo_skb_max
);
518 if (priv
->echo_skb
[idx
]) {
519 dev_kfree_skb_any(priv
->echo_skb
[idx
]);
520 priv
->echo_skb
[idx
] = NULL
;
523 EXPORT_SYMBOL_GPL(can_free_echo_skb
);
526 * CAN device restart for bus-off recovery
528 static void can_restart(struct net_device
*dev
)
530 struct can_priv
*priv
= netdev_priv(dev
);
531 struct net_device_stats
*stats
= &dev
->stats
;
533 struct can_frame
*cf
;
536 BUG_ON(netif_carrier_ok(dev
));
539 * No synchronization needed because the device is bus-off and
540 * no messages can come in or go out.
542 can_flush_echo_skb(dev
);
544 /* send restart message upstream */
545 skb
= alloc_can_err_skb(dev
, &cf
);
550 cf
->can_id
|= CAN_ERR_RESTARTED
;
555 stats
->rx_bytes
+= cf
->can_dlc
;
558 netdev_dbg(dev
, "restarted\n");
559 priv
->can_stats
.restarts
++;
561 /* Now restart the device */
562 err
= priv
->do_set_mode(dev
, CAN_MODE_START
);
564 netif_carrier_on(dev
);
566 netdev_err(dev
, "Error %d during restart", err
);
569 static void can_restart_work(struct work_struct
*work
)
571 struct delayed_work
*dwork
= to_delayed_work(work
);
572 struct can_priv
*priv
= container_of(dwork
, struct can_priv
, restart_work
);
574 can_restart(priv
->dev
);
577 int can_restart_now(struct net_device
*dev
)
579 struct can_priv
*priv
= netdev_priv(dev
);
582 * A manual restart is only permitted if automatic restart is
583 * disabled and the device is in the bus-off state
585 if (priv
->restart_ms
)
587 if (priv
->state
!= CAN_STATE_BUS_OFF
)
590 cancel_delayed_work_sync(&priv
->restart_work
);
599 * This functions should be called when the device goes bus-off to
600 * tell the netif layer that no more packets can be sent or received.
601 * If enabled, a timer is started to trigger bus-off recovery.
603 void can_bus_off(struct net_device
*dev
)
605 struct can_priv
*priv
= netdev_priv(dev
);
607 netdev_dbg(dev
, "bus-off\n");
609 netif_carrier_off(dev
);
611 if (priv
->restart_ms
)
612 schedule_delayed_work(&priv
->restart_work
,
613 msecs_to_jiffies(priv
->restart_ms
));
615 EXPORT_SYMBOL_GPL(can_bus_off
);
617 static void can_setup(struct net_device
*dev
)
619 dev
->type
= ARPHRD_CAN
;
621 dev
->hard_header_len
= 0;
623 dev
->tx_queue_len
= 10;
625 /* New-style flags. */
626 dev
->flags
= IFF_NOARP
;
627 dev
->features
= NETIF_F_HW_CSUM
;
630 struct sk_buff
*alloc_can_skb(struct net_device
*dev
, struct can_frame
**cf
)
634 skb
= netdev_alloc_skb(dev
, sizeof(struct can_skb_priv
) +
635 sizeof(struct can_frame
));
639 skb
->protocol
= htons(ETH_P_CAN
);
640 skb
->pkt_type
= PACKET_BROADCAST
;
641 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
643 skb_reset_mac_header(skb
);
644 skb_reset_network_header(skb
);
645 skb_reset_transport_header(skb
);
647 can_skb_reserve(skb
);
648 can_skb_prv(skb
)->ifindex
= dev
->ifindex
;
649 can_skb_prv(skb
)->skbcnt
= 0;
651 *cf
= skb_put(skb
, sizeof(struct can_frame
));
652 memset(*cf
, 0, sizeof(struct can_frame
));
656 EXPORT_SYMBOL_GPL(alloc_can_skb
);
658 struct sk_buff
*alloc_canfd_skb(struct net_device
*dev
,
659 struct canfd_frame
**cfd
)
663 skb
= netdev_alloc_skb(dev
, sizeof(struct can_skb_priv
) +
664 sizeof(struct canfd_frame
));
668 skb
->protocol
= htons(ETH_P_CANFD
);
669 skb
->pkt_type
= PACKET_BROADCAST
;
670 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
672 skb_reset_mac_header(skb
);
673 skb_reset_network_header(skb
);
674 skb_reset_transport_header(skb
);
676 can_skb_reserve(skb
);
677 can_skb_prv(skb
)->ifindex
= dev
->ifindex
;
678 can_skb_prv(skb
)->skbcnt
= 0;
680 *cfd
= skb_put(skb
, sizeof(struct canfd_frame
));
681 memset(*cfd
, 0, sizeof(struct canfd_frame
));
685 EXPORT_SYMBOL_GPL(alloc_canfd_skb
);
687 struct sk_buff
*alloc_can_err_skb(struct net_device
*dev
, struct can_frame
**cf
)
691 skb
= alloc_can_skb(dev
, cf
);
695 (*cf
)->can_id
= CAN_ERR_FLAG
;
696 (*cf
)->can_dlc
= CAN_ERR_DLC
;
700 EXPORT_SYMBOL_GPL(alloc_can_err_skb
);
703 * Allocate and setup space for the CAN network device
705 struct net_device
*alloc_candev(int sizeof_priv
, unsigned int echo_skb_max
)
707 struct net_device
*dev
;
708 struct can_priv
*priv
;
712 size
= ALIGN(sizeof_priv
, sizeof(struct sk_buff
*)) +
713 echo_skb_max
* sizeof(struct sk_buff
*);
717 dev
= alloc_netdev(size
, "can%d", NET_NAME_UNKNOWN
, can_setup
);
721 priv
= netdev_priv(dev
);
725 priv
->echo_skb_max
= echo_skb_max
;
726 priv
->echo_skb
= (void *)priv
+
727 ALIGN(sizeof_priv
, sizeof(struct sk_buff
*));
730 priv
->state
= CAN_STATE_STOPPED
;
732 INIT_DELAYED_WORK(&priv
->restart_work
, can_restart_work
);
736 EXPORT_SYMBOL_GPL(alloc_candev
);
739 * Free space of the CAN network device
741 void free_candev(struct net_device
*dev
)
745 EXPORT_SYMBOL_GPL(free_candev
);
748 * changing MTU and control mode for CAN/CANFD devices
750 int can_change_mtu(struct net_device
*dev
, int new_mtu
)
752 struct can_priv
*priv
= netdev_priv(dev
);
754 /* Do not allow changing the MTU while running */
755 if (dev
->flags
& IFF_UP
)
758 /* allow change of MTU according to the CANFD ability of the device */
761 /* 'CANFD-only' controllers can not switch to CAN_MTU */
762 if (priv
->ctrlmode_static
& CAN_CTRLMODE_FD
)
765 priv
->ctrlmode
&= ~CAN_CTRLMODE_FD
;
769 /* check for potential CANFD ability */
770 if (!(priv
->ctrlmode_supported
& CAN_CTRLMODE_FD
) &&
771 !(priv
->ctrlmode_static
& CAN_CTRLMODE_FD
))
774 priv
->ctrlmode
|= CAN_CTRLMODE_FD
;
784 EXPORT_SYMBOL_GPL(can_change_mtu
);
787 * Common open function when the device gets opened.
789 * This function should be called in the open function of the device
792 int open_candev(struct net_device
*dev
)
794 struct can_priv
*priv
= netdev_priv(dev
);
796 if (!priv
->bittiming
.bitrate
) {
797 netdev_err(dev
, "bit-timing not yet defined\n");
801 /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
802 if ((priv
->ctrlmode
& CAN_CTRLMODE_FD
) &&
803 (!priv
->data_bittiming
.bitrate
||
804 (priv
->data_bittiming
.bitrate
< priv
->bittiming
.bitrate
))) {
805 netdev_err(dev
, "incorrect/missing data bit-timing\n");
809 /* Switch carrier on if device was stopped while in bus-off state */
810 if (!netif_carrier_ok(dev
))
811 netif_carrier_on(dev
);
815 EXPORT_SYMBOL_GPL(open_candev
);
818 * Common close function for cleanup before the device gets closed.
820 * This function should be called in the close function of the device
823 void close_candev(struct net_device
*dev
)
825 struct can_priv
*priv
= netdev_priv(dev
);
827 cancel_delayed_work_sync(&priv
->restart_work
);
828 can_flush_echo_skb(dev
);
830 EXPORT_SYMBOL_GPL(close_candev
);
833 * CAN netlink interface
835 static const struct nla_policy can_policy
[IFLA_CAN_MAX
+ 1] = {
836 [IFLA_CAN_STATE
] = { .type
= NLA_U32
},
837 [IFLA_CAN_CTRLMODE
] = { .len
= sizeof(struct can_ctrlmode
) },
838 [IFLA_CAN_RESTART_MS
] = { .type
= NLA_U32
},
839 [IFLA_CAN_RESTART
] = { .type
= NLA_U32
},
840 [IFLA_CAN_BITTIMING
] = { .len
= sizeof(struct can_bittiming
) },
841 [IFLA_CAN_BITTIMING_CONST
]
842 = { .len
= sizeof(struct can_bittiming_const
) },
843 [IFLA_CAN_CLOCK
] = { .len
= sizeof(struct can_clock
) },
844 [IFLA_CAN_BERR_COUNTER
] = { .len
= sizeof(struct can_berr_counter
) },
845 [IFLA_CAN_DATA_BITTIMING
]
846 = { .len
= sizeof(struct can_bittiming
) },
847 [IFLA_CAN_DATA_BITTIMING_CONST
]
848 = { .len
= sizeof(struct can_bittiming_const
) },
851 static int can_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
853 bool is_can_fd
= false;
855 /* Make sure that valid CAN FD configurations always consist of
856 * - nominal/arbitration bittiming
858 * - control mode with CAN_CTRLMODE_FD set
864 if (data
[IFLA_CAN_CTRLMODE
]) {
865 struct can_ctrlmode
*cm
= nla_data(data
[IFLA_CAN_CTRLMODE
]);
867 is_can_fd
= cm
->flags
& cm
->mask
& CAN_CTRLMODE_FD
;
871 if (!data
[IFLA_CAN_BITTIMING
] || !data
[IFLA_CAN_DATA_BITTIMING
])
875 if (data
[IFLA_CAN_DATA_BITTIMING
]) {
876 if (!is_can_fd
|| !data
[IFLA_CAN_BITTIMING
])
883 static int can_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
884 struct nlattr
*data
[],
885 struct netlink_ext_ack
*extack
)
887 struct can_priv
*priv
= netdev_priv(dev
);
890 /* We need synchronization with dev->stop() */
893 if (data
[IFLA_CAN_BITTIMING
]) {
894 struct can_bittiming bt
;
896 /* Do not allow changing bittiming while running */
897 if (dev
->flags
& IFF_UP
)
900 /* Calculate bittiming parameters based on
901 * bittiming_const if set, otherwise pass bitrate
902 * directly via do_set_bitrate(). Bail out if neither
905 if (!priv
->bittiming_const
&& !priv
->do_set_bittiming
)
908 memcpy(&bt
, nla_data(data
[IFLA_CAN_BITTIMING
]), sizeof(bt
));
909 err
= can_get_bittiming(dev
, &bt
,
910 priv
->bittiming_const
,
912 priv
->bitrate_const_cnt
);
915 memcpy(&priv
->bittiming
, &bt
, sizeof(bt
));
917 if (priv
->do_set_bittiming
) {
918 /* Finally, set the bit-timing registers */
919 err
= priv
->do_set_bittiming(dev
);
925 if (data
[IFLA_CAN_CTRLMODE
]) {
926 struct can_ctrlmode
*cm
;
930 /* Do not allow changing controller mode while running */
931 if (dev
->flags
& IFF_UP
)
933 cm
= nla_data(data
[IFLA_CAN_CTRLMODE
]);
934 ctrlstatic
= priv
->ctrlmode_static
;
935 maskedflags
= cm
->flags
& cm
->mask
;
937 /* check whether provided bits are allowed to be passed */
938 if (cm
->mask
& ~(priv
->ctrlmode_supported
| ctrlstatic
))
941 /* do not check for static fd-non-iso if 'fd' is disabled */
942 if (!(maskedflags
& CAN_CTRLMODE_FD
))
943 ctrlstatic
&= ~CAN_CTRLMODE_FD_NON_ISO
;
945 /* make sure static options are provided by configuration */
946 if ((maskedflags
& ctrlstatic
) != ctrlstatic
)
949 /* clear bits to be modified and copy the flag values */
950 priv
->ctrlmode
&= ~cm
->mask
;
951 priv
->ctrlmode
|= maskedflags
;
953 /* CAN_CTRLMODE_FD can only be set when driver supports FD */
954 if (priv
->ctrlmode
& CAN_CTRLMODE_FD
)
955 dev
->mtu
= CANFD_MTU
;
960 if (data
[IFLA_CAN_RESTART_MS
]) {
961 /* Do not allow changing restart delay while running */
962 if (dev
->flags
& IFF_UP
)
964 priv
->restart_ms
= nla_get_u32(data
[IFLA_CAN_RESTART_MS
]);
967 if (data
[IFLA_CAN_RESTART
]) {
968 /* Do not allow a restart while not running */
969 if (!(dev
->flags
& IFF_UP
))
971 err
= can_restart_now(dev
);
976 if (data
[IFLA_CAN_DATA_BITTIMING
]) {
977 struct can_bittiming dbt
;
979 /* Do not allow changing bittiming while running */
980 if (dev
->flags
& IFF_UP
)
983 /* Calculate bittiming parameters based on
984 * data_bittiming_const if set, otherwise pass bitrate
985 * directly via do_set_bitrate(). Bail out if neither
988 if (!priv
->data_bittiming_const
&& !priv
->do_set_data_bittiming
)
991 memcpy(&dbt
, nla_data(data
[IFLA_CAN_DATA_BITTIMING
]),
993 err
= can_get_bittiming(dev
, &dbt
,
994 priv
->data_bittiming_const
,
995 priv
->data_bitrate_const
,
996 priv
->data_bitrate_const_cnt
);
999 memcpy(&priv
->data_bittiming
, &dbt
, sizeof(dbt
));
1001 if (priv
->do_set_data_bittiming
) {
1002 /* Finally, set the bit-timing registers */
1003 err
= priv
->do_set_data_bittiming(dev
);
1009 if (data
[IFLA_CAN_TERMINATION
]) {
1010 const u16 termval
= nla_get_u16(data
[IFLA_CAN_TERMINATION
]);
1011 const unsigned int num_term
= priv
->termination_const_cnt
;
1014 if (!priv
->do_set_termination
)
1017 /* check whether given value is supported by the interface */
1018 for (i
= 0; i
< num_term
; i
++) {
1019 if (termval
== priv
->termination_const
[i
])
1025 /* Finally, set the termination value */
1026 err
= priv
->do_set_termination(dev
, termval
);
1030 priv
->termination
= termval
;
1036 static size_t can_get_size(const struct net_device
*dev
)
1038 struct can_priv
*priv
= netdev_priv(dev
);
1041 if (priv
->bittiming
.bitrate
) /* IFLA_CAN_BITTIMING */
1042 size
+= nla_total_size(sizeof(struct can_bittiming
));
1043 if (priv
->bittiming_const
) /* IFLA_CAN_BITTIMING_CONST */
1044 size
+= nla_total_size(sizeof(struct can_bittiming_const
));
1045 size
+= nla_total_size(sizeof(struct can_clock
)); /* IFLA_CAN_CLOCK */
1046 size
+= nla_total_size(sizeof(u32
)); /* IFLA_CAN_STATE */
1047 size
+= nla_total_size(sizeof(struct can_ctrlmode
)); /* IFLA_CAN_CTRLMODE */
1048 size
+= nla_total_size(sizeof(u32
)); /* IFLA_CAN_RESTART_MS */
1049 if (priv
->do_get_berr_counter
) /* IFLA_CAN_BERR_COUNTER */
1050 size
+= nla_total_size(sizeof(struct can_berr_counter
));
1051 if (priv
->data_bittiming
.bitrate
) /* IFLA_CAN_DATA_BITTIMING */
1052 size
+= nla_total_size(sizeof(struct can_bittiming
));
1053 if (priv
->data_bittiming_const
) /* IFLA_CAN_DATA_BITTIMING_CONST */
1054 size
+= nla_total_size(sizeof(struct can_bittiming_const
));
1055 if (priv
->termination_const
) {
1056 size
+= nla_total_size(sizeof(priv
->termination
)); /* IFLA_CAN_TERMINATION */
1057 size
+= nla_total_size(sizeof(*priv
->termination_const
) * /* IFLA_CAN_TERMINATION_CONST */
1058 priv
->termination_const_cnt
);
1060 if (priv
->bitrate_const
) /* IFLA_CAN_BITRATE_CONST */
1061 size
+= nla_total_size(sizeof(*priv
->bitrate_const
) *
1062 priv
->bitrate_const_cnt
);
1063 if (priv
->data_bitrate_const
) /* IFLA_CAN_DATA_BITRATE_CONST */
1064 size
+= nla_total_size(sizeof(*priv
->data_bitrate_const
) *
1065 priv
->data_bitrate_const_cnt
);
1070 static int can_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
1072 struct can_priv
*priv
= netdev_priv(dev
);
1073 struct can_ctrlmode cm
= {.flags
= priv
->ctrlmode
};
1074 struct can_berr_counter bec
;
1075 enum can_state state
= priv
->state
;
1077 if (priv
->do_get_state
)
1078 priv
->do_get_state(dev
, &state
);
1080 if ((priv
->bittiming
.bitrate
&&
1081 nla_put(skb
, IFLA_CAN_BITTIMING
,
1082 sizeof(priv
->bittiming
), &priv
->bittiming
)) ||
1084 (priv
->bittiming_const
&&
1085 nla_put(skb
, IFLA_CAN_BITTIMING_CONST
,
1086 sizeof(*priv
->bittiming_const
), priv
->bittiming_const
)) ||
1088 nla_put(skb
, IFLA_CAN_CLOCK
, sizeof(priv
->clock
), &priv
->clock
) ||
1089 nla_put_u32(skb
, IFLA_CAN_STATE
, state
) ||
1090 nla_put(skb
, IFLA_CAN_CTRLMODE
, sizeof(cm
), &cm
) ||
1091 nla_put_u32(skb
, IFLA_CAN_RESTART_MS
, priv
->restart_ms
) ||
1093 (priv
->do_get_berr_counter
&&
1094 !priv
->do_get_berr_counter(dev
, &bec
) &&
1095 nla_put(skb
, IFLA_CAN_BERR_COUNTER
, sizeof(bec
), &bec
)) ||
1097 (priv
->data_bittiming
.bitrate
&&
1098 nla_put(skb
, IFLA_CAN_DATA_BITTIMING
,
1099 sizeof(priv
->data_bittiming
), &priv
->data_bittiming
)) ||
1101 (priv
->data_bittiming_const
&&
1102 nla_put(skb
, IFLA_CAN_DATA_BITTIMING_CONST
,
1103 sizeof(*priv
->data_bittiming_const
),
1104 priv
->data_bittiming_const
)) ||
1106 (priv
->termination_const
&&
1107 (nla_put_u16(skb
, IFLA_CAN_TERMINATION
, priv
->termination
) ||
1108 nla_put(skb
, IFLA_CAN_TERMINATION_CONST
,
1109 sizeof(*priv
->termination_const
) *
1110 priv
->termination_const_cnt
,
1111 priv
->termination_const
))) ||
1113 (priv
->bitrate_const
&&
1114 nla_put(skb
, IFLA_CAN_BITRATE_CONST
,
1115 sizeof(*priv
->bitrate_const
) *
1116 priv
->bitrate_const_cnt
,
1117 priv
->bitrate_const
)) ||
1119 (priv
->data_bitrate_const
&&
1120 nla_put(skb
, IFLA_CAN_DATA_BITRATE_CONST
,
1121 sizeof(*priv
->data_bitrate_const
) *
1122 priv
->data_bitrate_const_cnt
,
1123 priv
->data_bitrate_const
))
1131 static size_t can_get_xstats_size(const struct net_device
*dev
)
1133 return sizeof(struct can_device_stats
);
1136 static int can_fill_xstats(struct sk_buff
*skb
, const struct net_device
*dev
)
1138 struct can_priv
*priv
= netdev_priv(dev
);
1140 if (nla_put(skb
, IFLA_INFO_XSTATS
,
1141 sizeof(priv
->can_stats
), &priv
->can_stats
))
1142 goto nla_put_failure
;
1149 static int can_newlink(struct net
*src_net
, struct net_device
*dev
,
1150 struct nlattr
*tb
[], struct nlattr
*data
[],
1151 struct netlink_ext_ack
*extack
)
1156 static void can_dellink(struct net_device
*dev
, struct list_head
*head
)
1161 static struct rtnl_link_ops can_link_ops __read_mostly
= {
1163 .maxtype
= IFLA_CAN_MAX
,
1164 .policy
= can_policy
,
1166 .validate
= can_validate
,
1167 .newlink
= can_newlink
,
1168 .changelink
= can_changelink
,
1169 .dellink
= can_dellink
,
1170 .get_size
= can_get_size
,
1171 .fill_info
= can_fill_info
,
1172 .get_xstats_size
= can_get_xstats_size
,
1173 .fill_xstats
= can_fill_xstats
,
1177 * Register the CAN network device
1179 int register_candev(struct net_device
*dev
)
1181 struct can_priv
*priv
= netdev_priv(dev
);
1183 /* Ensure termination_const, termination_const_cnt and
1184 * do_set_termination consistency. All must be either set or
1187 if ((!priv
->termination_const
!= !priv
->termination_const_cnt
) ||
1188 (!priv
->termination_const
!= !priv
->do_set_termination
))
1191 if (!priv
->bitrate_const
!= !priv
->bitrate_const_cnt
)
1194 if (!priv
->data_bitrate_const
!= !priv
->data_bitrate_const_cnt
)
1197 dev
->rtnl_link_ops
= &can_link_ops
;
1198 return register_netdev(dev
);
1200 EXPORT_SYMBOL_GPL(register_candev
);
1203 * Unregister the CAN network device
1205 void unregister_candev(struct net_device
*dev
)
1207 unregister_netdev(dev
);
1209 EXPORT_SYMBOL_GPL(unregister_candev
);
1212 * Test if a network device is a candev based device
1213 * and return the can_priv* if so.
1215 struct can_priv
*safe_candev_priv(struct net_device
*dev
)
1217 if ((dev
->type
!= ARPHRD_CAN
) || (dev
->rtnl_link_ops
!= &can_link_ops
))
1220 return netdev_priv(dev
);
1222 EXPORT_SYMBOL_GPL(safe_candev_priv
);
1224 static __init
int can_dev_init(void)
1228 can_led_notifier_init();
1230 err
= rtnl_link_register(&can_link_ops
);
1232 printk(KERN_INFO MOD_DESC
"\n");
1236 module_init(can_dev_init
);
1238 static __exit
void can_dev_exit(void)
1240 rtnl_link_unregister(&can_link_ops
);
1242 can_led_notifier_exit();
1244 module_exit(can_dev_exit
);
1246 MODULE_ALIAS_RTNL_LINK("can");