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 */
74 * Bit-timing calculation derived from:
76 * Code based on LinCAN sources and H8S2638 project
77 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
78 * Copyright 2005 Stanislav Marek
79 * email: pisa@cmp.felk.cvut.cz
81 * Calculates proper bit-timing parameters for a specified bit-rate
82 * and sample-point, which can then be used to set the bit-timing
83 * registers of the CAN controller. You can find more information
84 * in the header file linux/can/netlink.h.
86 static int can_update_spt(const struct can_bittiming_const
*btc
,
87 int sampl_pt
, int tseg
, int *tseg1
, int *tseg2
)
89 *tseg2
= tseg
+ 1 - (sampl_pt
* (tseg
+ 1)) / 1000;
90 if (*tseg2
< btc
->tseg2_min
)
91 *tseg2
= btc
->tseg2_min
;
92 if (*tseg2
> btc
->tseg2_max
)
93 *tseg2
= btc
->tseg2_max
;
94 *tseg1
= tseg
- *tseg2
;
95 if (*tseg1
> btc
->tseg1_max
) {
96 *tseg1
= btc
->tseg1_max
;
97 *tseg2
= tseg
- *tseg1
;
99 return 1000 * (tseg
+ 1 - *tseg2
) / (tseg
+ 1);
102 static int can_calc_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
)
104 struct can_priv
*priv
= netdev_priv(dev
);
105 const struct can_bittiming_const
*btc
= priv
->bittiming_const
;
106 long rate
, best_rate
= 0;
107 long best_error
= 1000000000, error
= 0;
108 int best_tseg
= 0, best_brp
= 0, brp
= 0;
109 int tsegall
, tseg
= 0, tseg1
= 0, tseg2
= 0;
110 int spt_error
= 1000, spt
= 0, sampl_pt
;
113 if (!priv
->bittiming_const
)
116 /* Use CIA recommended sample points */
117 if (bt
->sample_point
) {
118 sampl_pt
= bt
->sample_point
;
120 if (bt
->bitrate
> 800000)
122 else if (bt
->bitrate
> 500000)
128 /* tseg even = round down, odd = round up */
129 for (tseg
= (btc
->tseg1_max
+ btc
->tseg2_max
) * 2 + 1;
130 tseg
>= (btc
->tseg1_min
+ btc
->tseg2_min
) * 2; tseg
--) {
131 tsegall
= 1 + tseg
/ 2;
132 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
133 brp
= priv
->clock
.freq
/ (tsegall
* bt
->bitrate
) + tseg
% 2;
134 /* chose brp step which is possible in system */
135 brp
= (brp
/ btc
->brp_inc
) * btc
->brp_inc
;
136 if ((brp
< btc
->brp_min
) || (brp
> btc
->brp_max
))
138 rate
= priv
->clock
.freq
/ (brp
* tsegall
);
139 error
= bt
->bitrate
- rate
;
140 /* tseg brp biterror */
143 if (error
> best_error
)
147 spt
= can_update_spt(btc
, sampl_pt
, tseg
/ 2,
149 error
= sampl_pt
- spt
;
152 if (error
> spt_error
)
156 best_tseg
= tseg
/ 2;
164 /* Error in one-tenth of a percent */
165 error
= (best_error
* 1000) / bt
->bitrate
;
166 if (error
> CAN_CALC_MAX_ERROR
) {
168 "bitrate error %ld.%ld%% too high\n",
169 error
/ 10, error
% 10);
172 netdev_warn(dev
, "bitrate error %ld.%ld%%\n",
173 error
/ 10, error
% 10);
177 /* real sample point */
178 bt
->sample_point
= can_update_spt(btc
, sampl_pt
, best_tseg
,
181 v64
= (u64
)best_brp
* 1000000000UL;
182 do_div(v64
, priv
->clock
.freq
);
184 bt
->prop_seg
= tseg1
/ 2;
185 bt
->phase_seg1
= tseg1
- bt
->prop_seg
;
186 bt
->phase_seg2
= tseg2
;
188 /* check for sjw user settings */
189 if (!bt
->sjw
|| !btc
->sjw_max
)
192 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
193 if (bt
->sjw
> btc
->sjw_max
)
194 bt
->sjw
= btc
->sjw_max
;
195 /* bt->sjw must not be higher than tseg2 */
202 bt
->bitrate
= priv
->clock
.freq
/ (bt
->brp
* (tseg1
+ tseg2
+ 1));
206 #else /* !CONFIG_CAN_CALC_BITTIMING */
207 static int can_calc_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
)
209 netdev_err(dev
, "bit-timing calculation not available\n");
212 #endif /* CONFIG_CAN_CALC_BITTIMING */
215 * Checks the validity of the specified bit-timing parameters prop_seg,
216 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
217 * prescaler value brp. You can find more information in the header
218 * file linux/can/netlink.h.
220 static int can_fixup_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
)
222 struct can_priv
*priv
= netdev_priv(dev
);
223 const struct can_bittiming_const
*btc
= priv
->bittiming_const
;
227 if (!priv
->bittiming_const
)
230 tseg1
= bt
->prop_seg
+ bt
->phase_seg1
;
233 if (bt
->sjw
> btc
->sjw_max
||
234 tseg1
< btc
->tseg1_min
|| tseg1
> btc
->tseg1_max
||
235 bt
->phase_seg2
< btc
->tseg2_min
|| bt
->phase_seg2
> btc
->tseg2_max
)
238 brp64
= (u64
)priv
->clock
.freq
* (u64
)bt
->tq
;
239 if (btc
->brp_inc
> 1)
240 do_div(brp64
, btc
->brp_inc
);
241 brp64
+= 500000000UL - 1;
242 do_div(brp64
, 1000000000UL); /* the practicable BRP */
243 if (btc
->brp_inc
> 1)
244 brp64
*= btc
->brp_inc
;
245 bt
->brp
= (u32
)brp64
;
247 if (bt
->brp
< btc
->brp_min
|| bt
->brp
> btc
->brp_max
)
250 alltseg
= bt
->prop_seg
+ bt
->phase_seg1
+ bt
->phase_seg2
+ 1;
251 bt
->bitrate
= priv
->clock
.freq
/ (bt
->brp
* alltseg
);
252 bt
->sample_point
= ((tseg1
+ 1) * 1000) / alltseg
;
257 static int can_get_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
)
259 struct can_priv
*priv
= netdev_priv(dev
);
262 /* Check if the CAN device has bit-timing parameters */
263 if (priv
->bittiming_const
) {
265 /* Non-expert mode? Check if the bitrate has been pre-defined */
267 /* Determine bit-timing parameters */
268 err
= can_calc_bittiming(dev
, bt
);
270 /* Check bit-timing params and calculate proper brp */
271 err
= can_fixup_bittiming(dev
, bt
);
280 * Local echo of CAN messages
282 * CAN network devices *should* support a local echo functionality
283 * (see Documentation/networking/can.txt). To test the handling of CAN
284 * interfaces that do not support the local echo both driver types are
285 * implemented. In the case that the driver does not support the echo
286 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
287 * to perform the echo as a fallback solution.
289 static void can_flush_echo_skb(struct net_device
*dev
)
291 struct can_priv
*priv
= netdev_priv(dev
);
292 struct net_device_stats
*stats
= &dev
->stats
;
295 for (i
= 0; i
< priv
->echo_skb_max
; i
++) {
296 if (priv
->echo_skb
[i
]) {
297 kfree_skb(priv
->echo_skb
[i
]);
298 priv
->echo_skb
[i
] = NULL
;
300 stats
->tx_aborted_errors
++;
306 * Put the skb on the stack to be looped backed locally lateron
308 * The function is typically called in the start_xmit function
309 * of the device driver. The driver must protect access to
310 * priv->echo_skb, if necessary.
312 void can_put_echo_skb(struct sk_buff
*skb
, struct net_device
*dev
,
315 struct can_priv
*priv
= netdev_priv(dev
);
317 BUG_ON(idx
>= priv
->echo_skb_max
);
319 /* check flag whether this packet has to be looped back */
320 if (!(dev
->flags
& IFF_ECHO
) || skb
->pkt_type
!= PACKET_LOOPBACK
) {
325 if (!priv
->echo_skb
[idx
]) {
327 skb
= can_create_echo_skb(skb
);
331 /* make settings for echo to reduce code in irq context */
332 skb
->protocol
= htons(ETH_P_CAN
);
333 skb
->pkt_type
= PACKET_BROADCAST
;
334 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
337 /* save this skb for tx interrupt echo handling */
338 priv
->echo_skb
[idx
] = skb
;
340 /* locking problem with netif_stop_queue() ?? */
341 netdev_err(dev
, "%s: BUG! echo_skb is occupied!\n", __func__
);
345 EXPORT_SYMBOL_GPL(can_put_echo_skb
);
348 * Get the skb from the stack and loop it back locally
350 * The function is typically called when the TX done interrupt
351 * is handled in the device driver. The driver must protect
352 * access to priv->echo_skb, if necessary.
354 unsigned int can_get_echo_skb(struct net_device
*dev
, unsigned int idx
)
356 struct can_priv
*priv
= netdev_priv(dev
);
358 BUG_ON(idx
>= priv
->echo_skb_max
);
360 if (priv
->echo_skb
[idx
]) {
361 struct sk_buff
*skb
= priv
->echo_skb
[idx
];
362 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
363 u8 dlc
= cf
->can_dlc
;
365 netif_rx(priv
->echo_skb
[idx
]);
366 priv
->echo_skb
[idx
] = NULL
;
373 EXPORT_SYMBOL_GPL(can_get_echo_skb
);
376 * Remove the skb from the stack and free it.
378 * The function is typically called when TX failed.
380 void can_free_echo_skb(struct net_device
*dev
, unsigned int idx
)
382 struct can_priv
*priv
= netdev_priv(dev
);
384 BUG_ON(idx
>= priv
->echo_skb_max
);
386 if (priv
->echo_skb
[idx
]) {
387 kfree_skb(priv
->echo_skb
[idx
]);
388 priv
->echo_skb
[idx
] = NULL
;
391 EXPORT_SYMBOL_GPL(can_free_echo_skb
);
394 * CAN device restart for bus-off recovery
396 static void can_restart(unsigned long data
)
398 struct net_device
*dev
= (struct net_device
*)data
;
399 struct can_priv
*priv
= netdev_priv(dev
);
400 struct net_device_stats
*stats
= &dev
->stats
;
402 struct can_frame
*cf
;
405 BUG_ON(netif_carrier_ok(dev
));
408 * No synchronization needed because the device is bus-off and
409 * no messages can come in or go out.
411 can_flush_echo_skb(dev
);
413 /* send restart message upstream */
414 skb
= alloc_can_err_skb(dev
, &cf
);
419 cf
->can_id
|= CAN_ERR_RESTARTED
;
424 stats
->rx_bytes
+= cf
->can_dlc
;
427 netdev_dbg(dev
, "restarted\n");
428 priv
->can_stats
.restarts
++;
430 /* Now restart the device */
431 err
= priv
->do_set_mode(dev
, CAN_MODE_START
);
433 netif_carrier_on(dev
);
435 netdev_err(dev
, "Error %d during restart", err
);
438 int can_restart_now(struct net_device
*dev
)
440 struct can_priv
*priv
= netdev_priv(dev
);
443 * A manual restart is only permitted if automatic restart is
444 * disabled and the device is in the bus-off state
446 if (priv
->restart_ms
)
448 if (priv
->state
!= CAN_STATE_BUS_OFF
)
451 /* Runs as soon as possible in the timer context */
452 mod_timer(&priv
->restart_timer
, jiffies
);
460 * This functions should be called when the device goes bus-off to
461 * tell the netif layer that no more packets can be sent or received.
462 * If enabled, a timer is started to trigger bus-off recovery.
464 void can_bus_off(struct net_device
*dev
)
466 struct can_priv
*priv
= netdev_priv(dev
);
468 netdev_dbg(dev
, "bus-off\n");
470 netif_carrier_off(dev
);
471 priv
->can_stats
.bus_off
++;
473 if (priv
->restart_ms
)
474 mod_timer(&priv
->restart_timer
,
475 jiffies
+ (priv
->restart_ms
* HZ
) / 1000);
477 EXPORT_SYMBOL_GPL(can_bus_off
);
479 static void can_setup(struct net_device
*dev
)
481 dev
->type
= ARPHRD_CAN
;
483 dev
->hard_header_len
= 0;
485 dev
->tx_queue_len
= 10;
487 /* New-style flags. */
488 dev
->flags
= IFF_NOARP
;
489 dev
->features
= NETIF_F_HW_CSUM
;
492 struct sk_buff
*alloc_can_skb(struct net_device
*dev
, struct can_frame
**cf
)
496 skb
= netdev_alloc_skb(dev
, sizeof(struct can_skb_priv
) +
497 sizeof(struct can_frame
));
501 skb
->protocol
= htons(ETH_P_CAN
);
502 skb
->pkt_type
= PACKET_BROADCAST
;
503 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
505 can_skb_reserve(skb
);
506 can_skb_prv(skb
)->ifindex
= dev
->ifindex
;
508 *cf
= (struct can_frame
*)skb_put(skb
, sizeof(struct can_frame
));
509 memset(*cf
, 0, sizeof(struct can_frame
));
513 EXPORT_SYMBOL_GPL(alloc_can_skb
);
515 struct sk_buff
*alloc_can_err_skb(struct net_device
*dev
, struct can_frame
**cf
)
519 skb
= alloc_can_skb(dev
, cf
);
523 (*cf
)->can_id
= CAN_ERR_FLAG
;
524 (*cf
)->can_dlc
= CAN_ERR_DLC
;
528 EXPORT_SYMBOL_GPL(alloc_can_err_skb
);
531 * Allocate and setup space for the CAN network device
533 struct net_device
*alloc_candev(int sizeof_priv
, unsigned int echo_skb_max
)
535 struct net_device
*dev
;
536 struct can_priv
*priv
;
540 size
= ALIGN(sizeof_priv
, sizeof(struct sk_buff
*)) +
541 echo_skb_max
* sizeof(struct sk_buff
*);
545 dev
= alloc_netdev(size
, "can%d", can_setup
);
549 priv
= netdev_priv(dev
);
552 priv
->echo_skb_max
= echo_skb_max
;
553 priv
->echo_skb
= (void *)priv
+
554 ALIGN(sizeof_priv
, sizeof(struct sk_buff
*));
557 priv
->state
= CAN_STATE_STOPPED
;
559 init_timer(&priv
->restart_timer
);
563 EXPORT_SYMBOL_GPL(alloc_candev
);
566 * Free space of the CAN network device
568 void free_candev(struct net_device
*dev
)
572 EXPORT_SYMBOL_GPL(free_candev
);
575 * Common open function when the device gets opened.
577 * This function should be called in the open function of the device
580 int open_candev(struct net_device
*dev
)
582 struct can_priv
*priv
= netdev_priv(dev
);
584 if (!priv
->bittiming
.tq
&& !priv
->bittiming
.bitrate
) {
585 netdev_err(dev
, "bit-timing not yet defined\n");
589 /* Switch carrier on if device was stopped while in bus-off state */
590 if (!netif_carrier_ok(dev
))
591 netif_carrier_on(dev
);
593 setup_timer(&priv
->restart_timer
, can_restart
, (unsigned long)dev
);
597 EXPORT_SYMBOL_GPL(open_candev
);
600 * Common close function for cleanup before the device gets closed.
602 * This function should be called in the close function of the device
605 void close_candev(struct net_device
*dev
)
607 struct can_priv
*priv
= netdev_priv(dev
);
609 del_timer_sync(&priv
->restart_timer
);
610 can_flush_echo_skb(dev
);
612 EXPORT_SYMBOL_GPL(close_candev
);
615 * CAN netlink interface
617 static const struct nla_policy can_policy
[IFLA_CAN_MAX
+ 1] = {
618 [IFLA_CAN_STATE
] = { .type
= NLA_U32
},
619 [IFLA_CAN_CTRLMODE
] = { .len
= sizeof(struct can_ctrlmode
) },
620 [IFLA_CAN_RESTART_MS
] = { .type
= NLA_U32
},
621 [IFLA_CAN_RESTART
] = { .type
= NLA_U32
},
622 [IFLA_CAN_BITTIMING
] = { .len
= sizeof(struct can_bittiming
) },
623 [IFLA_CAN_BITTIMING_CONST
]
624 = { .len
= sizeof(struct can_bittiming_const
) },
625 [IFLA_CAN_CLOCK
] = { .len
= sizeof(struct can_clock
) },
626 [IFLA_CAN_BERR_COUNTER
] = { .len
= sizeof(struct can_berr_counter
) },
629 static int can_changelink(struct net_device
*dev
,
630 struct nlattr
*tb
[], struct nlattr
*data
[])
632 struct can_priv
*priv
= netdev_priv(dev
);
635 /* We need synchronization with dev->stop() */
638 if (data
[IFLA_CAN_BITTIMING
]) {
639 struct can_bittiming bt
;
641 /* Do not allow changing bittiming while running */
642 if (dev
->flags
& IFF_UP
)
644 memcpy(&bt
, nla_data(data
[IFLA_CAN_BITTIMING
]), sizeof(bt
));
645 if ((!bt
.bitrate
&& !bt
.tq
) || (bt
.bitrate
&& bt
.tq
))
647 err
= can_get_bittiming(dev
, &bt
);
650 memcpy(&priv
->bittiming
, &bt
, sizeof(bt
));
652 if (priv
->do_set_bittiming
) {
653 /* Finally, set the bit-timing registers */
654 err
= priv
->do_set_bittiming(dev
);
660 if (data
[IFLA_CAN_CTRLMODE
]) {
661 struct can_ctrlmode
*cm
;
663 /* Do not allow changing controller mode while running */
664 if (dev
->flags
& IFF_UP
)
666 cm
= nla_data(data
[IFLA_CAN_CTRLMODE
]);
667 if (cm
->flags
& ~priv
->ctrlmode_supported
)
669 priv
->ctrlmode
&= ~cm
->mask
;
670 priv
->ctrlmode
|= cm
->flags
;
673 if (data
[IFLA_CAN_RESTART_MS
]) {
674 /* Do not allow changing restart delay while running */
675 if (dev
->flags
& IFF_UP
)
677 priv
->restart_ms
= nla_get_u32(data
[IFLA_CAN_RESTART_MS
]);
680 if (data
[IFLA_CAN_RESTART
]) {
681 /* Do not allow a restart while not running */
682 if (!(dev
->flags
& IFF_UP
))
684 err
= can_restart_now(dev
);
692 static size_t can_get_size(const struct net_device
*dev
)
694 struct can_priv
*priv
= netdev_priv(dev
);
697 size
+= nla_total_size(sizeof(struct can_bittiming
)); /* IFLA_CAN_BITTIMING */
698 if (priv
->bittiming_const
) /* IFLA_CAN_BITTIMING_CONST */
699 size
+= nla_total_size(sizeof(struct can_bittiming_const
));
700 size
+= nla_total_size(sizeof(struct can_clock
)); /* IFLA_CAN_CLOCK */
701 size
+= nla_total_size(sizeof(u32
)); /* IFLA_CAN_STATE */
702 size
+= nla_total_size(sizeof(struct can_ctrlmode
)); /* IFLA_CAN_CTRLMODE */
703 size
+= nla_total_size(sizeof(u32
)); /* IFLA_CAN_RESTART_MS */
704 if (priv
->do_get_berr_counter
) /* IFLA_CAN_BERR_COUNTER */
705 size
+= nla_total_size(sizeof(struct can_berr_counter
));
710 static int can_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
712 struct can_priv
*priv
= netdev_priv(dev
);
713 struct can_ctrlmode cm
= {.flags
= priv
->ctrlmode
};
714 struct can_berr_counter bec
;
715 enum can_state state
= priv
->state
;
717 if (priv
->do_get_state
)
718 priv
->do_get_state(dev
, &state
);
719 if (nla_put(skb
, IFLA_CAN_BITTIMING
,
720 sizeof(priv
->bittiming
), &priv
->bittiming
) ||
721 (priv
->bittiming_const
&&
722 nla_put(skb
, IFLA_CAN_BITTIMING_CONST
,
723 sizeof(*priv
->bittiming_const
), priv
->bittiming_const
)) ||
724 nla_put(skb
, IFLA_CAN_CLOCK
, sizeof(cm
), &priv
->clock
) ||
725 nla_put_u32(skb
, IFLA_CAN_STATE
, state
) ||
726 nla_put(skb
, IFLA_CAN_CTRLMODE
, sizeof(cm
), &cm
) ||
727 nla_put_u32(skb
, IFLA_CAN_RESTART_MS
, priv
->restart_ms
) ||
728 (priv
->do_get_berr_counter
&&
729 !priv
->do_get_berr_counter(dev
, &bec
) &&
730 nla_put(skb
, IFLA_CAN_BERR_COUNTER
, sizeof(bec
), &bec
)))
735 static size_t can_get_xstats_size(const struct net_device
*dev
)
737 return sizeof(struct can_device_stats
);
740 static int can_fill_xstats(struct sk_buff
*skb
, const struct net_device
*dev
)
742 struct can_priv
*priv
= netdev_priv(dev
);
744 if (nla_put(skb
, IFLA_INFO_XSTATS
,
745 sizeof(priv
->can_stats
), &priv
->can_stats
))
746 goto nla_put_failure
;
753 static int can_newlink(struct net
*src_net
, struct net_device
*dev
,
754 struct nlattr
*tb
[], struct nlattr
*data
[])
759 static struct rtnl_link_ops can_link_ops __read_mostly
= {
761 .maxtype
= IFLA_CAN_MAX
,
762 .policy
= can_policy
,
764 .newlink
= can_newlink
,
765 .changelink
= can_changelink
,
766 .get_size
= can_get_size
,
767 .fill_info
= can_fill_info
,
768 .get_xstats_size
= can_get_xstats_size
,
769 .fill_xstats
= can_fill_xstats
,
773 * Register the CAN network device
775 int register_candev(struct net_device
*dev
)
777 dev
->rtnl_link_ops
= &can_link_ops
;
778 return register_netdev(dev
);
780 EXPORT_SYMBOL_GPL(register_candev
);
783 * Unregister the CAN network device
785 void unregister_candev(struct net_device
*dev
)
787 unregister_netdev(dev
);
789 EXPORT_SYMBOL_GPL(unregister_candev
);
792 * Test if a network device is a candev based device
793 * and return the can_priv* if so.
795 struct can_priv
*safe_candev_priv(struct net_device
*dev
)
797 if ((dev
->type
!= ARPHRD_CAN
) || (dev
->rtnl_link_ops
!= &can_link_ops
))
800 return netdev_priv(dev
);
802 EXPORT_SYMBOL_GPL(safe_candev_priv
);
804 static __init
int can_dev_init(void)
808 can_led_notifier_init();
810 err
= rtnl_link_register(&can_link_ops
);
812 printk(KERN_INFO MOD_DESC
"\n");
816 module_init(can_dev_init
);
818 static __exit
void can_dev_exit(void)
820 rtnl_link_unregister(&can_link_ops
);
822 can_led_notifier_exit();
824 module_exit(can_dev_exit
);
826 MODULE_ALIAS_RTNL_LINK("can");