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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/netdevice.h>
24 #include <linux/if_arp.h>
25 #include <linux/can.h>
26 #include <linux/can/dev.h>
27 #include <linux/can/skb.h>
28 #include <linux/can/netlink.h>
29 #include <linux/can/led.h>
30 #include <net/rtnetlink.h>
32 #define MOD_DESC "CAN device driver interface"
34 MODULE_DESCRIPTION(MOD_DESC
);
35 MODULE_LICENSE("GPL v2");
36 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
38 /* CAN DLC to real data length conversion helpers */
40 static const u8 dlc2len
[] = {0, 1, 2, 3, 4, 5, 6, 7,
41 8, 12, 16, 20, 24, 32, 48, 64};
43 /* get data length from can_dlc with sanitized can_dlc */
44 u8
can_dlc2len(u8 can_dlc
)
46 return dlc2len
[can_dlc
& 0x0F];
48 EXPORT_SYMBOL_GPL(can_dlc2len
);
50 static const u8 len2dlc
[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
51 9, 9, 9, 9, /* 9 - 12 */
52 10, 10, 10, 10, /* 13 - 16 */
53 11, 11, 11, 11, /* 17 - 20 */
54 12, 12, 12, 12, /* 21 - 24 */
55 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
56 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
57 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
58 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
59 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
61 /* map the sanitized data length to an appropriate data length code */
62 u8
can_len2dlc(u8 len
)
64 if (unlikely(len
> 64))
69 EXPORT_SYMBOL_GPL(can_len2dlc
);
71 #ifdef CONFIG_CAN_CALC_BITTIMING
72 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
75 * Bit-timing calculation derived from:
77 * Code based on LinCAN sources and H8S2638 project
78 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
79 * Copyright 2005 Stanislav Marek
80 * email: pisa@cmp.felk.cvut.cz
82 * Calculates proper bit-timing parameters for a specified bit-rate
83 * and sample-point, which can then be used to set the bit-timing
84 * registers of the CAN controller. You can find more information
85 * in the header file linux/can/netlink.h.
87 static int can_update_spt(const struct can_bittiming_const
*btc
,
88 int sampl_pt
, int tseg
, int *tseg1
, int *tseg2
)
90 *tseg2
= tseg
+ 1 - (sampl_pt
* (tseg
+ 1)) / 1000;
91 if (*tseg2
< btc
->tseg2_min
)
92 *tseg2
= btc
->tseg2_min
;
93 if (*tseg2
> btc
->tseg2_max
)
94 *tseg2
= btc
->tseg2_max
;
95 *tseg1
= tseg
- *tseg2
;
96 if (*tseg1
> btc
->tseg1_max
) {
97 *tseg1
= btc
->tseg1_max
;
98 *tseg2
= tseg
- *tseg1
;
100 return 1000 * (tseg
+ 1 - *tseg2
) / (tseg
+ 1);
103 static int can_calc_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
)
105 struct can_priv
*priv
= netdev_priv(dev
);
106 const struct can_bittiming_const
*btc
= priv
->bittiming_const
;
107 long rate
, best_rate
= 0;
108 long best_error
= 1000000000, error
= 0;
109 int best_tseg
= 0, best_brp
= 0, brp
= 0;
110 int tsegall
, tseg
= 0, tseg1
= 0, tseg2
= 0;
111 int spt_error
= 1000, spt
= 0, sampl_pt
;
114 if (!priv
->bittiming_const
)
117 /* Use CIA recommended sample points */
118 if (bt
->sample_point
) {
119 sampl_pt
= bt
->sample_point
;
121 if (bt
->bitrate
> 800000)
123 else if (bt
->bitrate
> 500000)
129 /* tseg even = round down, odd = round up */
130 for (tseg
= (btc
->tseg1_max
+ btc
->tseg2_max
) * 2 + 1;
131 tseg
>= (btc
->tseg1_min
+ btc
->tseg2_min
) * 2; tseg
--) {
132 tsegall
= 1 + tseg
/ 2;
133 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
134 brp
= priv
->clock
.freq
/ (tsegall
* bt
->bitrate
) + tseg
% 2;
135 /* chose brp step which is possible in system */
136 brp
= (brp
/ btc
->brp_inc
) * btc
->brp_inc
;
137 if ((brp
< btc
->brp_min
) || (brp
> btc
->brp_max
))
139 rate
= priv
->clock
.freq
/ (brp
* tsegall
);
140 error
= bt
->bitrate
- rate
;
141 /* tseg brp biterror */
144 if (error
> best_error
)
148 spt
= can_update_spt(btc
, sampl_pt
, tseg
/ 2,
150 error
= sampl_pt
- spt
;
153 if (error
> spt_error
)
157 best_tseg
= tseg
/ 2;
165 /* Error in one-tenth of a percent */
166 error
= (best_error
* 1000) / bt
->bitrate
;
167 if (error
> CAN_CALC_MAX_ERROR
) {
169 "bitrate error %ld.%ld%% too high\n",
170 error
/ 10, error
% 10);
173 netdev_warn(dev
, "bitrate error %ld.%ld%%\n",
174 error
/ 10, error
% 10);
178 /* real sample point */
179 bt
->sample_point
= can_update_spt(btc
, sampl_pt
, best_tseg
,
182 v64
= (u64
)best_brp
* 1000000000UL;
183 do_div(v64
, priv
->clock
.freq
);
185 bt
->prop_seg
= tseg1
/ 2;
186 bt
->phase_seg1
= tseg1
- bt
->prop_seg
;
187 bt
->phase_seg2
= tseg2
;
189 /* check for sjw user settings */
190 if (!bt
->sjw
|| !btc
->sjw_max
)
193 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
194 if (bt
->sjw
> btc
->sjw_max
)
195 bt
->sjw
= btc
->sjw_max
;
196 /* bt->sjw must not be higher than tseg2 */
203 bt
->bitrate
= priv
->clock
.freq
/ (bt
->brp
* (tseg1
+ tseg2
+ 1));
207 #else /* !CONFIG_CAN_CALC_BITTIMING */
208 static int can_calc_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
)
210 netdev_err(dev
, "bit-timing calculation not available\n");
213 #endif /* CONFIG_CAN_CALC_BITTIMING */
216 * Checks the validity of the specified bit-timing parameters prop_seg,
217 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
218 * prescaler value brp. You can find more information in the header
219 * file linux/can/netlink.h.
221 static int can_fixup_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
)
223 struct can_priv
*priv
= netdev_priv(dev
);
224 const struct can_bittiming_const
*btc
= priv
->bittiming_const
;
228 if (!priv
->bittiming_const
)
231 tseg1
= bt
->prop_seg
+ bt
->phase_seg1
;
234 if (bt
->sjw
> btc
->sjw_max
||
235 tseg1
< btc
->tseg1_min
|| tseg1
> btc
->tseg1_max
||
236 bt
->phase_seg2
< btc
->tseg2_min
|| bt
->phase_seg2
> btc
->tseg2_max
)
239 brp64
= (u64
)priv
->clock
.freq
* (u64
)bt
->tq
;
240 if (btc
->brp_inc
> 1)
241 do_div(brp64
, btc
->brp_inc
);
242 brp64
+= 500000000UL - 1;
243 do_div(brp64
, 1000000000UL); /* the practicable BRP */
244 if (btc
->brp_inc
> 1)
245 brp64
*= btc
->brp_inc
;
246 bt
->brp
= (u32
)brp64
;
248 if (bt
->brp
< btc
->brp_min
|| bt
->brp
> btc
->brp_max
)
251 alltseg
= bt
->prop_seg
+ bt
->phase_seg1
+ bt
->phase_seg2
+ 1;
252 bt
->bitrate
= priv
->clock
.freq
/ (bt
->brp
* alltseg
);
253 bt
->sample_point
= ((tseg1
+ 1) * 1000) / alltseg
;
258 static int can_get_bittiming(struct net_device
*dev
, struct can_bittiming
*bt
)
260 struct can_priv
*priv
= netdev_priv(dev
);
263 /* Check if the CAN device has bit-timing parameters */
264 if (priv
->bittiming_const
) {
266 /* Non-expert mode? Check if the bitrate has been pre-defined */
268 /* Determine bit-timing parameters */
269 err
= can_calc_bittiming(dev
, bt
);
271 /* Check bit-timing params and calculate proper brp */
272 err
= can_fixup_bittiming(dev
, bt
);
281 * Local echo of CAN messages
283 * CAN network devices *should* support a local echo functionality
284 * (see Documentation/networking/can.txt). To test the handling of CAN
285 * interfaces that do not support the local echo both driver types are
286 * implemented. In the case that the driver does not support the echo
287 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
288 * to perform the echo as a fallback solution.
290 static void can_flush_echo_skb(struct net_device
*dev
)
292 struct can_priv
*priv
= netdev_priv(dev
);
293 struct net_device_stats
*stats
= &dev
->stats
;
296 for (i
= 0; i
< priv
->echo_skb_max
; i
++) {
297 if (priv
->echo_skb
[i
]) {
298 kfree_skb(priv
->echo_skb
[i
]);
299 priv
->echo_skb
[i
] = NULL
;
301 stats
->tx_aborted_errors
++;
307 * Put the skb on the stack to be looped backed locally lateron
309 * The function is typically called in the start_xmit function
310 * of the device driver. The driver must protect access to
311 * priv->echo_skb, if necessary.
313 void can_put_echo_skb(struct sk_buff
*skb
, struct net_device
*dev
,
316 struct can_priv
*priv
= netdev_priv(dev
);
318 BUG_ON(idx
>= priv
->echo_skb_max
);
320 /* check flag whether this packet has to be looped back */
321 if (!(dev
->flags
& IFF_ECHO
) || skb
->pkt_type
!= PACKET_LOOPBACK
) {
326 if (!priv
->echo_skb
[idx
]) {
328 skb
= can_create_echo_skb(skb
);
332 /* make settings for echo to reduce code in irq context */
333 skb
->protocol
= htons(ETH_P_CAN
);
334 skb
->pkt_type
= PACKET_BROADCAST
;
335 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
338 /* save this skb for tx interrupt echo handling */
339 priv
->echo_skb
[idx
] = skb
;
341 /* locking problem with netif_stop_queue() ?? */
342 netdev_err(dev
, "%s: BUG! echo_skb is occupied!\n", __func__
);
346 EXPORT_SYMBOL_GPL(can_put_echo_skb
);
349 * Get the skb from the stack and loop it back locally
351 * The function is typically called when the TX done interrupt
352 * is handled in the device driver. The driver must protect
353 * access to priv->echo_skb, if necessary.
355 unsigned int can_get_echo_skb(struct net_device
*dev
, unsigned int idx
)
357 struct can_priv
*priv
= netdev_priv(dev
);
359 BUG_ON(idx
>= priv
->echo_skb_max
);
361 if (priv
->echo_skb
[idx
]) {
362 struct sk_buff
*skb
= priv
->echo_skb
[idx
];
363 struct can_frame
*cf
= (struct can_frame
*)skb
->data
;
364 u8 dlc
= cf
->can_dlc
;
366 netif_rx(priv
->echo_skb
[idx
]);
367 priv
->echo_skb
[idx
] = NULL
;
374 EXPORT_SYMBOL_GPL(can_get_echo_skb
);
377 * Remove the skb from the stack and free it.
379 * The function is typically called when TX failed.
381 void can_free_echo_skb(struct net_device
*dev
, unsigned int idx
)
383 struct can_priv
*priv
= netdev_priv(dev
);
385 BUG_ON(idx
>= priv
->echo_skb_max
);
387 if (priv
->echo_skb
[idx
]) {
388 kfree_skb(priv
->echo_skb
[idx
]);
389 priv
->echo_skb
[idx
] = NULL
;
392 EXPORT_SYMBOL_GPL(can_free_echo_skb
);
395 * CAN device restart for bus-off recovery
397 static void can_restart(unsigned long data
)
399 struct net_device
*dev
= (struct net_device
*)data
;
400 struct can_priv
*priv
= netdev_priv(dev
);
401 struct net_device_stats
*stats
= &dev
->stats
;
403 struct can_frame
*cf
;
406 BUG_ON(netif_carrier_ok(dev
));
409 * No synchronization needed because the device is bus-off and
410 * no messages can come in or go out.
412 can_flush_echo_skb(dev
);
414 /* send restart message upstream */
415 skb
= alloc_can_err_skb(dev
, &cf
);
420 cf
->can_id
|= CAN_ERR_RESTARTED
;
425 stats
->rx_bytes
+= cf
->can_dlc
;
428 netdev_dbg(dev
, "restarted\n");
429 priv
->can_stats
.restarts
++;
431 /* Now restart the device */
432 err
= priv
->do_set_mode(dev
, CAN_MODE_START
);
434 netif_carrier_on(dev
);
436 netdev_err(dev
, "Error %d during restart", err
);
439 int can_restart_now(struct net_device
*dev
)
441 struct can_priv
*priv
= netdev_priv(dev
);
444 * A manual restart is only permitted if automatic restart is
445 * disabled and the device is in the bus-off state
447 if (priv
->restart_ms
)
449 if (priv
->state
!= CAN_STATE_BUS_OFF
)
452 /* Runs as soon as possible in the timer context */
453 mod_timer(&priv
->restart_timer
, jiffies
);
461 * This functions should be called when the device goes bus-off to
462 * tell the netif layer that no more packets can be sent or received.
463 * If enabled, a timer is started to trigger bus-off recovery.
465 void can_bus_off(struct net_device
*dev
)
467 struct can_priv
*priv
= netdev_priv(dev
);
469 netdev_dbg(dev
, "bus-off\n");
471 netif_carrier_off(dev
);
472 priv
->can_stats
.bus_off
++;
474 if (priv
->restart_ms
)
475 mod_timer(&priv
->restart_timer
,
476 jiffies
+ (priv
->restart_ms
* HZ
) / 1000);
478 EXPORT_SYMBOL_GPL(can_bus_off
);
480 static void can_setup(struct net_device
*dev
)
482 dev
->type
= ARPHRD_CAN
;
484 dev
->hard_header_len
= 0;
486 dev
->tx_queue_len
= 10;
488 /* New-style flags. */
489 dev
->flags
= IFF_NOARP
;
490 dev
->features
= NETIF_F_HW_CSUM
;
493 struct sk_buff
*alloc_can_skb(struct net_device
*dev
, struct can_frame
**cf
)
497 skb
= netdev_alloc_skb(dev
, sizeof(struct can_skb_priv
) +
498 sizeof(struct can_frame
));
502 skb
->protocol
= htons(ETH_P_CAN
);
503 skb
->pkt_type
= PACKET_BROADCAST
;
504 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
506 can_skb_reserve(skb
);
507 can_skb_prv(skb
)->ifindex
= dev
->ifindex
;
509 *cf
= (struct can_frame
*)skb_put(skb
, sizeof(struct can_frame
));
510 memset(*cf
, 0, sizeof(struct can_frame
));
514 EXPORT_SYMBOL_GPL(alloc_can_skb
);
516 struct sk_buff
*alloc_can_err_skb(struct net_device
*dev
, struct can_frame
**cf
)
520 skb
= alloc_can_skb(dev
, cf
);
524 (*cf
)->can_id
= CAN_ERR_FLAG
;
525 (*cf
)->can_dlc
= CAN_ERR_DLC
;
529 EXPORT_SYMBOL_GPL(alloc_can_err_skb
);
532 * Allocate and setup space for the CAN network device
534 struct net_device
*alloc_candev(int sizeof_priv
, unsigned int echo_skb_max
)
536 struct net_device
*dev
;
537 struct can_priv
*priv
;
541 size
= ALIGN(sizeof_priv
, sizeof(struct sk_buff
*)) +
542 echo_skb_max
* sizeof(struct sk_buff
*);
546 dev
= alloc_netdev(size
, "can%d", can_setup
);
550 priv
= netdev_priv(dev
);
553 priv
->echo_skb_max
= echo_skb_max
;
554 priv
->echo_skb
= (void *)priv
+
555 ALIGN(sizeof_priv
, sizeof(struct sk_buff
*));
558 priv
->state
= CAN_STATE_STOPPED
;
560 init_timer(&priv
->restart_timer
);
564 EXPORT_SYMBOL_GPL(alloc_candev
);
567 * Free space of the CAN network device
569 void free_candev(struct net_device
*dev
)
573 EXPORT_SYMBOL_GPL(free_candev
);
576 * Common open function when the device gets opened.
578 * This function should be called in the open function of the device
581 int open_candev(struct net_device
*dev
)
583 struct can_priv
*priv
= netdev_priv(dev
);
585 if (!priv
->bittiming
.tq
&& !priv
->bittiming
.bitrate
) {
586 netdev_err(dev
, "bit-timing not yet defined\n");
590 /* Switch carrier on if device was stopped while in bus-off state */
591 if (!netif_carrier_ok(dev
))
592 netif_carrier_on(dev
);
594 setup_timer(&priv
->restart_timer
, can_restart
, (unsigned long)dev
);
598 EXPORT_SYMBOL_GPL(open_candev
);
601 * Common close function for cleanup before the device gets closed.
603 * This function should be called in the close function of the device
606 void close_candev(struct net_device
*dev
)
608 struct can_priv
*priv
= netdev_priv(dev
);
610 del_timer_sync(&priv
->restart_timer
);
611 can_flush_echo_skb(dev
);
613 EXPORT_SYMBOL_GPL(close_candev
);
616 * CAN netlink interface
618 static const struct nla_policy can_policy
[IFLA_CAN_MAX
+ 1] = {
619 [IFLA_CAN_STATE
] = { .type
= NLA_U32
},
620 [IFLA_CAN_CTRLMODE
] = { .len
= sizeof(struct can_ctrlmode
) },
621 [IFLA_CAN_RESTART_MS
] = { .type
= NLA_U32
},
622 [IFLA_CAN_RESTART
] = { .type
= NLA_U32
},
623 [IFLA_CAN_BITTIMING
] = { .len
= sizeof(struct can_bittiming
) },
624 [IFLA_CAN_BITTIMING_CONST
]
625 = { .len
= sizeof(struct can_bittiming_const
) },
626 [IFLA_CAN_CLOCK
] = { .len
= sizeof(struct can_clock
) },
627 [IFLA_CAN_BERR_COUNTER
] = { .len
= sizeof(struct can_berr_counter
) },
630 static int can_changelink(struct net_device
*dev
,
631 struct nlattr
*tb
[], struct nlattr
*data
[])
633 struct can_priv
*priv
= netdev_priv(dev
);
636 /* We need synchronization with dev->stop() */
639 if (data
[IFLA_CAN_CTRLMODE
]) {
640 struct can_ctrlmode
*cm
;
642 /* Do not allow changing controller mode while running */
643 if (dev
->flags
& IFF_UP
)
645 cm
= nla_data(data
[IFLA_CAN_CTRLMODE
]);
646 if (cm
->flags
& ~priv
->ctrlmode_supported
)
648 priv
->ctrlmode
&= ~cm
->mask
;
649 priv
->ctrlmode
|= cm
->flags
;
652 if (data
[IFLA_CAN_BITTIMING
]) {
653 struct can_bittiming bt
;
655 /* Do not allow changing bittiming while running */
656 if (dev
->flags
& IFF_UP
)
658 memcpy(&bt
, nla_data(data
[IFLA_CAN_BITTIMING
]), sizeof(bt
));
659 if ((!bt
.bitrate
&& !bt
.tq
) || (bt
.bitrate
&& bt
.tq
))
661 err
= can_get_bittiming(dev
, &bt
);
664 memcpy(&priv
->bittiming
, &bt
, sizeof(bt
));
666 if (priv
->do_set_bittiming
) {
667 /* Finally, set the bit-timing registers */
668 err
= priv
->do_set_bittiming(dev
);
674 if (data
[IFLA_CAN_RESTART_MS
]) {
675 /* Do not allow changing restart delay while running */
676 if (dev
->flags
& IFF_UP
)
678 priv
->restart_ms
= nla_get_u32(data
[IFLA_CAN_RESTART_MS
]);
681 if (data
[IFLA_CAN_RESTART
]) {
682 /* Do not allow a restart while not running */
683 if (!(dev
->flags
& IFF_UP
))
685 err
= can_restart_now(dev
);
693 static size_t can_get_size(const struct net_device
*dev
)
695 struct can_priv
*priv
= netdev_priv(dev
);
698 size
= nla_total_size(sizeof(u32
)); /* IFLA_CAN_STATE */
699 size
+= nla_total_size(sizeof(struct can_ctrlmode
)); /* IFLA_CAN_CTRLMODE */
700 size
+= nla_total_size(sizeof(u32
)); /* IFLA_CAN_RESTART_MS */
701 size
+= nla_total_size(sizeof(struct can_bittiming
)); /* IFLA_CAN_BITTIMING */
702 size
+= nla_total_size(sizeof(struct can_clock
)); /* IFLA_CAN_CLOCK */
703 if (priv
->do_get_berr_counter
) /* IFLA_CAN_BERR_COUNTER */
704 size
+= nla_total_size(sizeof(struct can_berr_counter
));
705 if (priv
->bittiming_const
) /* IFLA_CAN_BITTIMING_CONST */
706 size
+= nla_total_size(sizeof(struct can_bittiming_const
));
711 static int can_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
713 struct can_priv
*priv
= netdev_priv(dev
);
714 struct can_ctrlmode cm
= {.flags
= priv
->ctrlmode
};
715 struct can_berr_counter bec
;
716 enum can_state state
= priv
->state
;
718 if (priv
->do_get_state
)
719 priv
->do_get_state(dev
, &state
);
720 if (nla_put_u32(skb
, IFLA_CAN_STATE
, state
) ||
721 nla_put(skb
, IFLA_CAN_CTRLMODE
, sizeof(cm
), &cm
) ||
722 nla_put_u32(skb
, IFLA_CAN_RESTART_MS
, priv
->restart_ms
) ||
723 nla_put(skb
, IFLA_CAN_BITTIMING
,
724 sizeof(priv
->bittiming
), &priv
->bittiming
) ||
725 nla_put(skb
, IFLA_CAN_CLOCK
, sizeof(cm
), &priv
->clock
) ||
726 (priv
->do_get_berr_counter
&&
727 !priv
->do_get_berr_counter(dev
, &bec
) &&
728 nla_put(skb
, IFLA_CAN_BERR_COUNTER
, sizeof(bec
), &bec
)) ||
729 (priv
->bittiming_const
&&
730 nla_put(skb
, IFLA_CAN_BITTIMING_CONST
,
731 sizeof(*priv
->bittiming_const
), priv
->bittiming_const
)))
732 goto nla_put_failure
;
739 static size_t can_get_xstats_size(const struct net_device
*dev
)
741 return sizeof(struct can_device_stats
);
744 static int can_fill_xstats(struct sk_buff
*skb
, const struct net_device
*dev
)
746 struct can_priv
*priv
= netdev_priv(dev
);
748 if (nla_put(skb
, IFLA_INFO_XSTATS
,
749 sizeof(priv
->can_stats
), &priv
->can_stats
))
750 goto nla_put_failure
;
757 static int can_newlink(struct net
*src_net
, struct net_device
*dev
,
758 struct nlattr
*tb
[], struct nlattr
*data
[])
763 static struct rtnl_link_ops can_link_ops __read_mostly
= {
765 .maxtype
= IFLA_CAN_MAX
,
766 .policy
= can_policy
,
768 .newlink
= can_newlink
,
769 .changelink
= can_changelink
,
770 .get_size
= can_get_size
,
771 .fill_info
= can_fill_info
,
772 .get_xstats_size
= can_get_xstats_size
,
773 .fill_xstats
= can_fill_xstats
,
777 * Register the CAN network device
779 int register_candev(struct net_device
*dev
)
781 dev
->rtnl_link_ops
= &can_link_ops
;
782 return register_netdev(dev
);
784 EXPORT_SYMBOL_GPL(register_candev
);
787 * Unregister the CAN network device
789 void unregister_candev(struct net_device
*dev
)
791 unregister_netdev(dev
);
793 EXPORT_SYMBOL_GPL(unregister_candev
);
796 * Test if a network device is a candev based device
797 * and return the can_priv* if so.
799 struct can_priv
*safe_candev_priv(struct net_device
*dev
)
801 if ((dev
->type
!= ARPHRD_CAN
) || (dev
->rtnl_link_ops
!= &can_link_ops
))
804 return netdev_priv(dev
);
806 EXPORT_SYMBOL_GPL(safe_candev_priv
);
808 static __init
int can_dev_init(void)
812 can_led_notifier_init();
814 err
= rtnl_link_register(&can_link_ops
);
816 printk(KERN_INFO MOD_DESC
"\n");
820 module_init(can_dev_init
);
822 static __exit
void can_dev_exit(void)
824 rtnl_link_unregister(&can_link_ops
);
826 can_led_notifier_exit();
828 module_exit(can_dev_exit
);
830 MODULE_ALIAS_RTNL_LINK("can");