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