]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/dev.c
can: Handle netdev->last_rx and netdev_tx_t properly for 2.6.31
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / dev.c
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
5  * Copyright (C) 2006 Andrey Volkov, Varma Electronics
6  * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the version 2 of the GNU General Public License
10  * as published by the Free Software Foundation
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <linux/module.h>
23 #include <linux/version.h>
24 #include <linux/kernel.h>
25 #include <linux/netdevice.h>
26 #include <linux/if_arp.h>
27 #include <socketcan/can.h>
28 #include <socketcan/can/dev.h>
29 #ifndef CONFIG_CAN_DEV_SYSFS
30 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
31 #error "CAN netlink interface not support by this kernel version"
32 #endif
33 #include <socketcan/can/netlink.h>
34 #include <net/rtnetlink.h>
35 #else
36 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)
37 #error "CAN sysfs interface not support by this kernel version"
38 #endif
39 #include "sysfs.h"
40 #endif
41
42 #define MOD_DESC "CAN device driver interface"
43
44 MODULE_DESCRIPTION(MOD_DESC);
45 MODULE_LICENSE("GPL v2");
46 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
47
48 #ifdef CONFIG_CAN_CALC_BITTIMING
49 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
50
51 /*
52  * Bit-timing calculation derived from:
53  *
54  * Code based on LinCAN sources and H8S2638 project
55  * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
56  * Copyright 2005      Stanislav Marek
57  * email: pisa@cmp.felk.cvut.cz
58  *
59  * Calculates proper bit-timing parameters for a specified bit-rate
60  * and sample-point, which can then be used to set the bit-timing
61  * registers of the CAN controller. You can find more information
62  * in the header file socketcan/can/netlink.h.
63  */
64 static int can_update_spt(const struct can_bittiming_const *btc,
65                           int sampl_pt, int tseg, int *tseg1, int *tseg2)
66 {
67         *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
68         if (*tseg2 < btc->tseg2_min)
69                 *tseg2 = btc->tseg2_min;
70         if (*tseg2 > btc->tseg2_max)
71                 *tseg2 = btc->tseg2_max;
72         *tseg1 = tseg - *tseg2;
73         if (*tseg1 > btc->tseg1_max) {
74                 *tseg1 = btc->tseg1_max;
75                 *tseg2 = tseg - *tseg1;
76         }
77         return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
78 }
79
80 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
81 {
82         struct can_priv *priv = netdev_priv(dev);
83         const struct can_bittiming_const *btc = priv->bittiming_const;
84         long rate, best_rate = 0;
85         long best_error = 1000000000, error = 0;
86         int best_tseg = 0, best_brp = 0, brp = 0;
87         int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
88         int spt_error = 1000, spt = 0, sampl_pt;
89         u64 v64;
90
91         if (!priv->bittiming_const)
92                 return -ENOTSUPP;
93
94         /* Use CIA recommended sample points */
95         if (bt->sample_point) {
96                 sampl_pt = bt->sample_point;
97         } else {
98                 if (bt->bitrate > 800000)
99                         sampl_pt = 750;
100                 else if (bt->bitrate > 500000)
101                         sampl_pt = 800;
102                 else
103                         sampl_pt = 875;
104         }
105
106         /* tseg even = round down, odd = round up */
107         for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
108              tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
109                 tsegall = 1 + tseg / 2;
110                 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
111                 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
112                 /* chose brp step which is possible in system */
113                 brp = (brp / btc->brp_inc) * btc->brp_inc;
114                 if ((brp < btc->brp_min) || (brp > btc->brp_max))
115                         continue;
116                 rate = priv->clock.freq / (brp * tsegall);
117                 error = bt->bitrate - rate;
118                 /* tseg brp biterror */
119                 if (error < 0)
120                         error = -error;
121                 if (error > best_error)
122                         continue;
123                 best_error = error;
124                 if (error == 0) {
125                         spt = can_update_spt(btc, sampl_pt, tseg / 2,
126                                              &tseg1, &tseg2);
127                         error = sampl_pt - spt;
128                         if (error < 0)
129                                 error = -error;
130                         if (error > spt_error)
131                                 continue;
132                         spt_error = error;
133                 }
134                 best_tseg = tseg / 2;
135                 best_brp = brp;
136                 best_rate = rate;
137                 if (error == 0)
138                         break;
139         }
140
141         if (best_error) {
142                 /* Error in one-tenth of a percent */
143                 error = (best_error * 1000) / bt->bitrate;
144                 if (error > CAN_CALC_MAX_ERROR) {
145                         dev_err(ND2D(dev),
146                                 "bitrate error %ld.%ld%% too high\n",
147                                 error / 10, error % 10);
148                         return -EDOM;
149                 } else {
150                         dev_warn(ND2D(dev), "bitrate error %ld.%ld%%\n",
151                                  error / 10, error % 10);
152                 }
153         }
154
155         spt = can_update_spt(btc, sampl_pt, best_tseg, &tseg1, &tseg2);
156
157         v64 = (u64)best_brp * 1000000000UL;
158         do_div(v64, priv->clock.freq);
159         bt->tq = (u32)v64;
160         bt->prop_seg = tseg1 / 2;
161         bt->phase_seg1 = tseg1 - bt->prop_seg;
162         bt->phase_seg2 = tseg2;
163         bt->sjw = 1;
164         bt->brp = best_brp;
165 #ifndef CONFIG_CAN_DEV_SYSFS
166         /* real bit-rate */
167         bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
168         /* real sample point */
169         bt->sample_point = spt;
170 #endif
171         return 0;
172 }
173 #else /* !CONFIG_CAN_CALC_BITTIMING */
174 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
175 {
176         dev_err(ND2D(dev), "bit-timing calculation not available\n");
177         return -EINVAL;
178 }
179 #endif /* CONFIG_CAN_CALC_BITTIMING */
180
181
182 #ifdef CONFIG_CAN_DEV_SYSFS
183 int can_sample_point(struct can_bittiming *bt)
184 {
185         return ((bt->prop_seg + bt->phase_seg1 + 1) * 1000) /
186                 (bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1);
187 }
188 #endif
189
190 /*
191  * Checks the validity of the specified bit-timing parameters prop_seg,
192  * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
193  * prescaler value brp. You can find more information in the header
194  * file socketcan/can/netlink.h.
195  */
196 static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt)
197 {
198         struct can_priv *priv = netdev_priv(dev);
199         const struct can_bittiming_const *btc = priv->bittiming_const;
200         int tseg1, alltseg;
201         u64 brp64;
202
203         if (!priv->bittiming_const)
204                 return -ENOTSUPP;
205
206         tseg1 = bt->prop_seg + bt->phase_seg1;
207         if (!bt->sjw)
208                 bt->sjw = 1;
209         if (bt->sjw > btc->sjw_max ||
210             tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
211             bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
212                 return -ERANGE;
213
214         brp64 = (u64)priv->clock.freq * (u64)bt->tq;
215         if (btc->brp_inc > 1)
216                 do_div(brp64, btc->brp_inc);
217         brp64 += 500000000UL - 1;
218         do_div(brp64, 1000000000UL); /* the practicable BRP */
219         if (btc->brp_inc > 1)
220                 brp64 *= btc->brp_inc;
221         bt->brp = (u32)brp64;
222
223         if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
224                 return -EINVAL;
225
226         alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
227         bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
228         bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
229
230         return 0;
231 }
232
233 int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt)
234 {
235         struct can_priv *priv = netdev_priv(dev);
236         int err;
237
238         /* Check if the CAN device has bit-timing parameters */
239         if (priv->bittiming_const) {
240
241                 /* Non-expert mode? Check if the bitrate has been pre-defined */
242                 if (!bt->tq)
243                         /* Determine bit-timing parameters */
244                         err = can_calc_bittiming(dev, bt);
245                 else
246                         /* Check bit-timing params and calculate proper brp */
247                         err = can_fixup_bittiming(dev, bt);
248                 if (err)
249                         return err;
250         }
251
252         return 0;
253 }
254
255 /*
256  * Local echo of CAN messages
257  *
258  * CAN network devices *should* support a local echo functionality
259  * (see Documentation/networking/can.txt). To test the handling of CAN
260  * interfaces that do not support the local echo both driver types are
261  * implemented. In the case that the driver does not support the echo
262  * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
263  * to perform the echo as a fallback solution.
264  */
265 static void can_flush_echo_skb(struct net_device *dev)
266 {
267         struct can_priv *priv = netdev_priv(dev);
268 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
269         struct net_device_stats *stats = can_get_stats(dev);
270 #else
271         struct net_device_stats *stats = &dev->stats;
272 #endif
273         int i;
274
275         for (i = 0; i < CAN_ECHO_SKB_MAX; i++) {
276                 if (priv->echo_skb[i]) {
277                         kfree_skb(priv->echo_skb[i]);
278                         priv->echo_skb[i] = NULL;
279                         stats->tx_dropped++;
280                         stats->tx_aborted_errors++;
281                 }
282         }
283 }
284
285 /*
286  * Put the skb on the stack to be looped backed locally lateron
287  *
288  * The function is typically called in the start_xmit function
289  * of the device driver. The driver must protect access to
290  * priv->echo_skb, if necessary.
291  */
292 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, int idx)
293 {
294         struct can_priv *priv = netdev_priv(dev);
295
296         /* check flag whether this packet has to be looped back */
297         if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
298                 kfree_skb(skb);
299                 return;
300         }
301
302         if (!priv->echo_skb[idx]) {
303                 struct sock *srcsk = skb->sk;
304
305                 if (atomic_read(&skb->users) != 1) {
306                         struct sk_buff *old_skb = skb;
307
308                         skb = skb_clone(old_skb, GFP_ATOMIC);
309                         kfree_skb(old_skb);
310                         if (!skb)
311                                 return;
312                 } else
313                         skb_orphan(skb);
314
315                 skb->sk = srcsk;
316
317                 /* make settings for echo to reduce code in irq context */
318                 skb->protocol = htons(ETH_P_CAN);
319                 skb->pkt_type = PACKET_BROADCAST;
320                 skb->ip_summed = CHECKSUM_UNNECESSARY;
321                 skb->dev = dev;
322
323                 /* save this skb for tx interrupt echo handling */
324                 priv->echo_skb[idx] = skb;
325         } else {
326                 /* locking problem with netif_stop_queue() ?? */
327                 dev_err(ND2D(dev), "%s: BUG! echo_skb is occupied!\n",
328                         __func__);
329                 kfree_skb(skb);
330         }
331 }
332 EXPORT_SYMBOL_GPL(can_put_echo_skb);
333
334 /*
335  * Get the skb from the stack and loop it back locally
336  *
337  * The function is typically called when the TX done interrupt
338  * is handled in the device driver. The driver must protect
339  * access to priv->echo_skb, if necessary.
340  */
341 void can_get_echo_skb(struct net_device *dev, int idx)
342 {
343         struct can_priv *priv = netdev_priv(dev);
344
345         if (priv->echo_skb[idx]) {
346                 netif_rx(priv->echo_skb[idx]);
347                 priv->echo_skb[idx] = NULL;
348         }
349 }
350 EXPORT_SYMBOL_GPL(can_get_echo_skb);
351
352 /*
353   * Remove the skb from the stack and free it.
354   *
355   * The function is typically called when TX failed.
356   */
357 void can_free_echo_skb(struct net_device *dev, int idx)
358 {
359         struct can_priv *priv = netdev_priv(dev);
360
361         if (priv->echo_skb[idx]) {
362                 kfree_skb(priv->echo_skb[idx]);
363                 priv->echo_skb[idx] = NULL;
364         }
365 }
366 EXPORT_SYMBOL_GPL(can_free_echo_skb);
367
368 /*
369  * CAN device restart for bus-off recovery
370  */
371 void can_restart(unsigned long data)
372 {
373         struct net_device *dev = (struct net_device *)data;
374         struct can_priv *priv = netdev_priv(dev);
375 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
376         struct net_device_stats *stats = can_get_stats(dev);
377 #else
378         struct net_device_stats *stats = &dev->stats;
379 #endif
380         struct sk_buff *skb;
381         struct can_frame *cf;
382         int err;
383
384         BUG_ON(netif_carrier_ok(dev));
385
386         /*
387          * No synchronization needed because the device is bus-off and
388          * no messages can come in or go out.
389          */
390         can_flush_echo_skb(dev);
391
392         /* send restart message upstream */
393         skb = dev_alloc_skb(sizeof(struct can_frame));
394         if (skb == NULL) {
395                 err = -ENOMEM;
396                 goto restart;
397         }
398         skb->dev = dev;
399         skb->protocol = htons(ETH_P_CAN);
400         cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
401         memset(cf, 0, sizeof(struct can_frame));
402         cf->can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED;
403         cf->can_dlc = CAN_ERR_DLC;
404
405         netif_rx(skb);
406
407 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31)
408         dev->last_rx = jiffies;
409 #endif
410         stats->rx_packets++;
411         stats->rx_bytes += cf->can_dlc;
412
413 restart:
414         dev_dbg(ND2D(dev), "restarted\n");
415         priv->can_stats.restarts++;
416
417         /* Now restart the device */
418         err = priv->do_set_mode(dev, CAN_MODE_START);
419
420         netif_carrier_on(dev);
421         if (err)
422                 dev_err(ND2D(dev), "Error %d during restart", err);
423 }
424
425 int can_restart_now(struct net_device *dev)
426 {
427         struct can_priv *priv = netdev_priv(dev);
428
429         /*
430          * A manual restart is only permitted if automatic restart is
431          * disabled and the device is in the bus-off state
432          */
433         if (priv->restart_ms)
434                 return -EINVAL;
435         if (priv->state != CAN_STATE_BUS_OFF)
436                 return -EBUSY;
437
438         /* Runs as soon as possible in the timer context */
439         mod_timer(&priv->restart_timer, jiffies);
440
441         return 0;
442 }
443
444 /*
445  * CAN bus-off
446  *
447  * This functions should be called when the device goes bus-off to
448  * tell the netif layer that no more packets can be sent or received.
449  * If enabled, a timer is started to trigger bus-off recovery.
450  */
451 void can_bus_off(struct net_device *dev)
452 {
453         struct can_priv *priv = netdev_priv(dev);
454
455         dev_dbg(ND2D(dev), "bus-off\n");
456
457         netif_carrier_off(dev);
458         priv->can_stats.bus_off++;
459
460         if (priv->restart_ms)
461                 mod_timer(&priv->restart_timer,
462                           jiffies + (priv->restart_ms * HZ) / 1000);
463 }
464 EXPORT_SYMBOL_GPL(can_bus_off);
465
466 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
467 struct net_device_stats *can_get_stats(struct net_device *dev)
468 {
469         struct can_priv *priv = netdev_priv(dev);
470
471         return &priv->net_stats;
472 }
473 EXPORT_SYMBOL_GPL(can_get_stats);
474 #endif
475
476 static void can_setup(struct net_device *dev)
477 {
478         dev->type = ARPHRD_CAN;
479         dev->mtu = sizeof(struct can_frame);
480         dev->hard_header_len = 0;
481         dev->addr_len = 0;
482         dev->tx_queue_len = 10;
483
484         /* New-style flags. */
485         dev->flags = IFF_NOARP;
486         dev->features = NETIF_F_NO_CSUM;
487 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
488         dev->get_stats = can_get_stats;
489 #endif
490 }
491
492 /*
493  * Allocate and setup space for the CAN network device
494  */
495 struct net_device *alloc_candev(int sizeof_priv)
496 {
497         struct net_device *dev;
498         struct can_priv *priv;
499
500         dev = alloc_netdev(sizeof_priv, "can%d", can_setup);
501         if (!dev)
502                 return NULL;
503
504         priv = netdev_priv(dev);
505
506         priv->state = CAN_STATE_STOPPED;
507
508         init_timer(&priv->restart_timer);
509
510         return dev;
511 }
512 EXPORT_SYMBOL_GPL(alloc_candev);
513
514 /*
515  * Free space of the CAN network device
516  */
517 void free_candev(struct net_device *dev)
518 {
519         free_netdev(dev);
520 }
521 EXPORT_SYMBOL_GPL(free_candev);
522
523 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
524 static inline void setup_timer(struct timer_list * timer,
525                                 void (*function)(unsigned long),
526                                 unsigned long data)
527 {
528         timer->function = function;
529         timer->data = data;
530         init_timer(timer);
531 }
532 #endif
533
534 /*
535  * Common open function when the device gets opened.
536  *
537  * This function should be called in the open function of the device
538  * driver.
539  */
540 int open_candev(struct net_device *dev)
541 {
542         struct can_priv *priv = netdev_priv(dev);
543 #ifdef CONFIG_CAN_DEV_SYSFS
544         int err;
545 #endif
546
547         if (!priv->bittiming.tq && !priv->bittiming.bitrate) {
548                 dev_err(ND2D(dev), "bit-timing not yet defined\n");
549                 return -EINVAL;
550         }
551
552 #ifdef CONFIG_CAN_DEV_SYSFS
553         err = can_get_bittiming(dev, &priv->bittiming);
554         if (err)
555                 return err;
556
557         if (priv->do_set_bittiming) {
558                 /* Finally, set the bit-timing registers */
559                 err = priv->do_set_bittiming(dev);
560                 if (err)
561                         return err;
562         }
563 #endif
564
565         /* Switch carrier on if device was stopped while in bus-off state */
566         if (!netif_carrier_ok(dev))
567                 netif_carrier_on(dev);
568
569         setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev);
570
571         return 0;
572 }
573 EXPORT_SYMBOL_GPL(open_candev);
574
575 /*
576  * Common close function for cleanup before the device gets closed.
577  *
578  * This function should be called in the close function of the device
579  * driver.
580  */
581 void close_candev(struct net_device *dev)
582 {
583         struct can_priv *priv = netdev_priv(dev);
584
585         if (del_timer_sync(&priv->restart_timer))
586                 dev_put(dev);
587         can_flush_echo_skb(dev);
588 }
589 EXPORT_SYMBOL_GPL(close_candev);
590
591 #ifndef CONFIG_CAN_DEV_SYSFS
592 /*
593  * CAN netlink interface
594  */
595 static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
596         [IFLA_CAN_STATE]        = { .type = NLA_U32 },
597         [IFLA_CAN_CTRLMODE]     = { .len = sizeof(struct can_ctrlmode) },
598         [IFLA_CAN_RESTART_MS]   = { .type = NLA_U32 },
599         [IFLA_CAN_RESTART]      = { .type = NLA_U32 },
600         [IFLA_CAN_BITTIMING]    = { .len = sizeof(struct can_bittiming) },
601         [IFLA_CAN_BITTIMING_CONST]
602                                 = { .len = sizeof(struct can_bittiming_const) },
603         [IFLA_CAN_CLOCK]        = { .len = sizeof(struct can_clock) },
604 };
605
606 static int can_changelink(struct net_device *dev,
607                           struct nlattr *tb[], struct nlattr *data[])
608 {
609         struct can_priv *priv = netdev_priv(dev);
610         int err;
611
612         /* We need synchronization with dev->stop() */
613         ASSERT_RTNL();
614
615         if (data[IFLA_CAN_CTRLMODE]) {
616                 struct can_ctrlmode *cm;
617
618                 /* Do not allow changing controller mode while running */
619                 if (dev->flags & IFF_UP)
620                         return -EBUSY;
621                 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
622                 priv->ctrlmode &= ~cm->mask;
623                 priv->ctrlmode |= cm->flags;
624         }
625
626         if (data[IFLA_CAN_BITTIMING]) {
627                 struct can_bittiming bt;
628
629                 /* Do not allow changing bittiming while running */
630                 if (dev->flags & IFF_UP)
631                         return -EBUSY;
632                 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
633                 if ((!bt.bitrate && !bt.tq) || (bt.bitrate && bt.tq))
634                         return -EINVAL;
635                 err = can_get_bittiming(dev, &bt);
636                 if (err)
637                         return err;
638                 memcpy(&priv->bittiming, &bt, sizeof(bt));
639
640                 if (priv->do_set_bittiming) {
641                         /* Finally, set the bit-timing registers */
642                         err = priv->do_set_bittiming(dev);
643                         if (err)
644                                 return err;
645                 }
646         }
647
648         if (data[IFLA_CAN_RESTART_MS]) {
649                 /* Do not allow changing restart delay while running */
650                 if (dev->flags & IFF_UP)
651                         return -EBUSY;
652                 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
653         }
654
655         if (data[IFLA_CAN_RESTART]) {
656                 /* Do not allow a restart while not running */
657                 if (!(dev->flags & IFF_UP))
658                         return -EINVAL;
659                 err = can_restart_now(dev);
660                 if (err)
661                         return err;
662         }
663
664         return 0;
665 }
666
667 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
668 {
669         struct can_priv *priv = netdev_priv(dev);
670         struct can_ctrlmode cm = {.flags = priv->ctrlmode};
671         enum can_state state = priv->state;
672
673         if (priv->do_get_state)
674                 priv->do_get_state(dev, &state);
675         NLA_PUT_U32(skb, IFLA_CAN_STATE, state);
676         NLA_PUT(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm);
677         NLA_PUT_U32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms);
678         NLA_PUT(skb, IFLA_CAN_BITTIMING,
679                 sizeof(priv->bittiming), &priv->bittiming);
680         NLA_PUT(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock);
681         if (priv->bittiming_const)
682                 NLA_PUT(skb, IFLA_CAN_BITTIMING_CONST,
683                         sizeof(*priv->bittiming_const), priv->bittiming_const);
684
685         return 0;
686
687 nla_put_failure:
688         return -EMSGSIZE;
689 }
690
691 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
692 {
693         struct can_priv *priv = netdev_priv(dev);
694
695         NLA_PUT(skb, IFLA_INFO_XSTATS,
696                 sizeof(priv->can_stats), &priv->can_stats);
697
698         return 0;
699
700 nla_put_failure:
701         return -EMSGSIZE;
702 }
703
704 static int can_newlink(struct net_device *dev,
705                        struct nlattr *tb[], struct nlattr *data[])
706 {
707         return -EOPNOTSUPP;
708 }
709
710 static struct rtnl_link_ops can_link_ops __read_mostly = {
711         .kind           = "can",
712         .maxtype        = IFLA_CAN_MAX,
713         .policy         = can_policy,
714         .setup          = can_setup,
715         .newlink        = can_newlink,
716         .changelink     = can_changelink,
717         .fill_info      = can_fill_info,
718         .fill_xstats    = can_fill_xstats,
719 };
720
721 #endif /* !CONFIG_CAN_DEV_SYSFS */
722
723 /*
724  * Register the CAN network device
725  */
726 int register_candev(struct net_device *dev)
727 {
728 #ifdef CONFIG_CAN_DEV_SYSFS
729         int err;
730
731         err = register_netdev(dev);
732         if (!err)
733                 can_create_sysfs(dev);
734
735         return err;
736 #else
737         dev->rtnl_link_ops = &can_link_ops;
738         return register_netdev(dev);
739 #endif
740 }
741 EXPORT_SYMBOL_GPL(register_candev);
742
743 /*
744  * Unregister the CAN network device
745  */
746 void unregister_candev(struct net_device *dev)
747 {
748 #ifdef CONFIG_CAN_DEV_SYSFS
749         can_remove_sysfs(dev);
750 #endif
751         unregister_netdev(dev);
752 }
753 EXPORT_SYMBOL_GPL(unregister_candev);
754
755 static __init int can_dev_init(void)
756 {
757 #ifndef CONFIG_CAN_DEV_SYSFS
758         int err;
759
760         err = rtnl_link_register(&can_link_ops);
761         if (!err)
762                 printk(KERN_INFO MOD_DESC "\n");
763
764         return err;
765 #else
766         printk(KERN_INFO MOD_DESC " using the deprecated SYSFS interface\n");
767
768         return 0;
769 #endif
770 }
771 module_init(can_dev_init);
772
773 static __exit void can_dev_exit(void)
774 {
775 #ifndef CONFIG_CAN_DEV_SYSFS
776         rtnl_link_unregister(&can_link_ops);
777 #endif
778 }
779 module_exit(can_dev_exit);
780
781 #ifndef CONFIG_CAN_DEV_SYSFS
782 MODULE_ALIAS_RTNL_LINK("can");
783 #endif