]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/dev.c
candev: allow SJW user setting for bittiming calculation
[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
164         /* check for sjw user settings */
165         if (!bt->sjw || !btc->sjw_max)
166                 bt->sjw = 1;
167         else {
168                 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
169                 if (bt->sjw > btc->sjw_max)
170                         bt->sjw = btc->sjw_max;
171                 /* bt->sjw must not be higher than tseg2 */
172                 if (tseg2 < bt->sjw)
173                         bt->sjw = tseg2;
174         }
175
176         bt->brp = best_brp;
177 #ifndef CONFIG_CAN_DEV_SYSFS
178         /* real bit-rate */
179         bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
180         /* real sample point */
181         bt->sample_point = spt;
182 #endif
183         return 0;
184 }
185 #else /* !CONFIG_CAN_CALC_BITTIMING */
186 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
187 {
188         dev_err(ND2D(dev), "bit-timing calculation not available\n");
189         return -EINVAL;
190 }
191 #endif /* CONFIG_CAN_CALC_BITTIMING */
192
193
194 #ifdef CONFIG_CAN_DEV_SYSFS
195 int can_sample_point(struct can_bittiming *bt)
196 {
197         return ((bt->prop_seg + bt->phase_seg1 + 1) * 1000) /
198                 (bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1);
199 }
200 #endif
201
202 /*
203  * Checks the validity of the specified bit-timing parameters prop_seg,
204  * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
205  * prescaler value brp. You can find more information in the header
206  * file socketcan/can/netlink.h.
207  */
208 static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt)
209 {
210         struct can_priv *priv = netdev_priv(dev);
211         const struct can_bittiming_const *btc = priv->bittiming_const;
212         int tseg1, alltseg;
213         u64 brp64;
214
215         if (!priv->bittiming_const)
216                 return -ENOTSUPP;
217
218         tseg1 = bt->prop_seg + bt->phase_seg1;
219         if (!bt->sjw)
220                 bt->sjw = 1;
221         if (bt->sjw > btc->sjw_max ||
222             tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
223             bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
224                 return -ERANGE;
225
226         brp64 = (u64)priv->clock.freq * (u64)bt->tq;
227         if (btc->brp_inc > 1)
228                 do_div(brp64, btc->brp_inc);
229         brp64 += 500000000UL - 1;
230         do_div(brp64, 1000000000UL); /* the practicable BRP */
231         if (btc->brp_inc > 1)
232                 brp64 *= btc->brp_inc;
233         bt->brp = (u32)brp64;
234
235         if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
236                 return -EINVAL;
237
238         alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
239         bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
240         bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
241
242         return 0;
243 }
244
245 int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt)
246 {
247         struct can_priv *priv = netdev_priv(dev);
248         int err;
249
250         /* Check if the CAN device has bit-timing parameters */
251         if (priv->bittiming_const) {
252
253                 /* Non-expert mode? Check if the bitrate has been pre-defined */
254                 if (!bt->tq)
255                         /* Determine bit-timing parameters */
256                         err = can_calc_bittiming(dev, bt);
257                 else
258                         /* Check bit-timing params and calculate proper brp */
259                         err = can_fixup_bittiming(dev, bt);
260                 if (err)
261                         return err;
262         }
263
264         return 0;
265 }
266
267 /*
268  * Local echo of CAN messages
269  *
270  * CAN network devices *should* support a local echo functionality
271  * (see Documentation/networking/can.txt). To test the handling of CAN
272  * interfaces that do not support the local echo both driver types are
273  * implemented. In the case that the driver does not support the echo
274  * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
275  * to perform the echo as a fallback solution.
276  */
277 static void can_flush_echo_skb(struct net_device *dev)
278 {
279         struct can_priv *priv = netdev_priv(dev);
280 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
281         struct net_device_stats *stats = can_get_stats(dev);
282 #else
283         struct net_device_stats *stats = &dev->stats;
284 #endif
285         int i;
286
287         for (i = 0; i < priv->echo_skb_max; i++) {
288                 if (priv->echo_skb[i]) {
289                         kfree_skb(priv->echo_skb[i]);
290                         priv->echo_skb[i] = NULL;
291                         stats->tx_dropped++;
292                         stats->tx_aborted_errors++;
293                 }
294         }
295 }
296
297 /*
298  * Put the skb on the stack to be looped backed locally lateron
299  *
300  * The function is typically called in the start_xmit function
301  * of the device driver. The driver must protect access to
302  * priv->echo_skb, if necessary.
303  */
304 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
305                       unsigned int idx)
306 {
307         struct can_priv *priv = netdev_priv(dev);
308
309         BUG_ON(idx >= priv->echo_skb_max);
310
311         /* check flag whether this packet has to be looped back */
312         if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
313                 kfree_skb(skb);
314                 return;
315         }
316
317         if (!priv->echo_skb[idx]) {
318                 struct sock *srcsk = skb->sk;
319
320                 if (atomic_read(&skb->users) != 1) {
321                         struct sk_buff *old_skb = skb;
322
323                         skb = skb_clone(old_skb, GFP_ATOMIC);
324                         kfree_skb(old_skb);
325                         if (!skb)
326                                 return;
327                 } else
328                         skb_orphan(skb);
329
330                 skb->sk = srcsk;
331
332                 /* make settings for echo to reduce code in irq context */
333                 skb->protocol = __constant_htons(ETH_P_CAN);
334                 skb->pkt_type = PACKET_BROADCAST;
335                 skb->ip_summed = CHECKSUM_UNNECESSARY;
336                 skb->dev = dev;
337
338                 /* save this skb for tx interrupt echo handling */
339                 priv->echo_skb[idx] = skb;
340         } else {
341                 /* locking problem with netif_stop_queue() ?? */
342                 dev_err(ND2D(dev), "%s: BUG! echo_skb is occupied!\n",
343                         __func__);
344                 kfree_skb(skb);
345         }
346 }
347 EXPORT_SYMBOL_GPL(can_put_echo_skb);
348
349 /*
350  * Get the skb from the stack and loop it back locally
351  *
352  * The function is typically called when the TX done interrupt
353  * is handled in the device driver. The driver must protect
354  * access to priv->echo_skb, if necessary.
355  */
356 void can_get_echo_skb(struct net_device *dev, unsigned int idx)
357 {
358         struct can_priv *priv = netdev_priv(dev);
359
360         BUG_ON(idx >= priv->echo_skb_max);
361
362         if (priv->echo_skb[idx]) {
363                 netif_rx(priv->echo_skb[idx]);
364                 priv->echo_skb[idx] = NULL;
365         }
366 }
367 EXPORT_SYMBOL_GPL(can_get_echo_skb);
368
369 /*
370   * Remove the skb from the stack and free it.
371   *
372   * The function is typically called when TX failed.
373   */
374 void can_free_echo_skb(struct net_device *dev, unsigned int idx)
375 {
376         struct can_priv *priv = netdev_priv(dev);
377
378         BUG_ON(idx >= priv->echo_skb_max);
379
380         if (priv->echo_skb[idx]) {
381                 kfree_skb(priv->echo_skb[idx]);
382                 priv->echo_skb[idx] = NULL;
383         }
384 }
385 EXPORT_SYMBOL_GPL(can_free_echo_skb);
386
387 /*
388  * CAN device restart for bus-off recovery
389  */
390 void can_restart(unsigned long data)
391 {
392         struct net_device *dev = (struct net_device *)data;
393         struct can_priv *priv = netdev_priv(dev);
394 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
395         struct net_device_stats *stats = can_get_stats(dev);
396 #else
397         struct net_device_stats *stats = &dev->stats;
398 #endif
399         struct sk_buff *skb;
400         struct can_frame *cf;
401         int err;
402
403         BUG_ON(netif_carrier_ok(dev));
404
405         /*
406          * No synchronization needed because the device is bus-off and
407          * no messages can come in or go out.
408          */
409         can_flush_echo_skb(dev);
410
411         /* send restart message upstream */
412         skb = alloc_can_err_skb(dev, &cf);
413         if (skb == NULL) {
414                 err = -ENOMEM;
415                 goto restart;
416         }
417         cf->can_id |= CAN_ERR_RESTARTED;
418
419         netif_rx(skb);
420
421 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
422         dev->last_rx = jiffies;
423 #endif
424         stats->rx_packets++;
425         stats->rx_bytes += cf->can_dlc;
426
427 restart:
428         dev_dbg(ND2D(dev), "restarted\n");
429         priv->can_stats.restarts++;
430
431         /* Now restart the device */
432         err = priv->do_set_mode(dev, CAN_MODE_START);
433
434         netif_carrier_on(dev);
435         if (err)
436                 dev_err(ND2D(dev), "Error %d during restart", err);
437 }
438
439 int can_restart_now(struct net_device *dev)
440 {
441         struct can_priv *priv = netdev_priv(dev);
442
443         /*
444          * A manual restart is only permitted if automatic restart is
445          * disabled and the device is in the bus-off state
446          */
447         if (priv->restart_ms)
448                 return -EINVAL;
449         if (priv->state != CAN_STATE_BUS_OFF)
450                 return -EBUSY;
451
452         /* Runs as soon as possible in the timer context */
453         mod_timer(&priv->restart_timer, jiffies);
454
455         return 0;
456 }
457
458 /*
459  * CAN bus-off
460  *
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.
464  */
465 void can_bus_off(struct net_device *dev)
466 {
467         struct can_priv *priv = netdev_priv(dev);
468
469         dev_dbg(ND2D(dev), "bus-off\n");
470
471         netif_carrier_off(dev);
472         priv->can_stats.bus_off++;
473
474         if (priv->restart_ms)
475                 mod_timer(&priv->restart_timer,
476                           jiffies + (priv->restart_ms * HZ) / 1000);
477 }
478 EXPORT_SYMBOL_GPL(can_bus_off);
479
480 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
481 struct net_device_stats *can_get_stats(struct net_device *dev)
482 {
483         struct can_priv *priv = netdev_priv(dev);
484
485         return &priv->net_stats;
486 }
487 EXPORT_SYMBOL_GPL(can_get_stats);
488 #endif
489
490 static void can_setup(struct net_device *dev)
491 {
492         dev->type = ARPHRD_CAN;
493         dev->mtu = sizeof(struct can_frame);
494         dev->hard_header_len = 0;
495         dev->addr_len = 0;
496         dev->tx_queue_len = 10;
497
498         /* New-style flags. */
499         dev->flags = IFF_NOARP;
500         dev->features = NETIF_F_NO_CSUM;
501 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
502         dev->get_stats = can_get_stats;
503 #endif
504 }
505
506 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
507 {
508         struct sk_buff *skb;
509
510         skb = netdev_alloc_skb(dev, sizeof(struct can_frame));
511         if (unlikely(!skb))
512                 return NULL;
513
514         skb->protocol = __constant_htons(ETH_P_CAN);
515         skb->pkt_type = PACKET_BROADCAST;
516         skb->ip_summed = CHECKSUM_UNNECESSARY;
517         *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
518         memset(*cf, 0, sizeof(struct can_frame));
519
520         return skb;
521 }
522 EXPORT_SYMBOL_GPL(alloc_can_skb);
523
524 struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
525 {
526         struct sk_buff *skb;
527
528         skb = alloc_can_skb(dev, cf);
529         if (unlikely(!skb))
530                 return NULL;
531
532         (*cf)->can_id = CAN_ERR_FLAG;
533         (*cf)->can_dlc = CAN_ERR_DLC;
534
535         return skb;
536 }
537 EXPORT_SYMBOL_GPL(alloc_can_err_skb);
538
539 /*
540  * Allocate and setup space for the CAN network device
541  */
542 struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
543 {
544         struct net_device *dev;
545         struct can_priv *priv;
546         int size;
547
548         if (echo_skb_max)
549                 size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
550                         echo_skb_max * sizeof(struct sk_buff *);
551         else
552                 size = sizeof_priv;
553
554         dev = alloc_netdev(size, "can%d", can_setup);
555         if (!dev)
556                 return NULL;
557
558         priv = netdev_priv(dev);
559
560         if (echo_skb_max) {
561                 priv->echo_skb_max = echo_skb_max;
562                 priv->echo_skb = (void *)priv +
563                         ALIGN(sizeof_priv, sizeof(struct sk_buff *));
564         }
565
566         priv->state = CAN_STATE_STOPPED;
567
568         init_timer(&priv->restart_timer);
569
570         return dev;
571 }
572 EXPORT_SYMBOL_GPL(alloc_candev);
573
574 /*
575  * Free space of the CAN network device
576  */
577 void free_candev(struct net_device *dev)
578 {
579         free_netdev(dev);
580 }
581 EXPORT_SYMBOL_GPL(free_candev);
582
583 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
584 static inline void setup_timer(struct timer_list * timer,
585                                 void (*function)(unsigned long),
586                                 unsigned long data)
587 {
588         timer->function = function;
589         timer->data = data;
590         init_timer(timer);
591 }
592 #endif
593
594 /*
595  * Common open function when the device gets opened.
596  *
597  * This function should be called in the open function of the device
598  * driver.
599  */
600 int open_candev(struct net_device *dev)
601 {
602         struct can_priv *priv = netdev_priv(dev);
603 #ifdef CONFIG_CAN_DEV_SYSFS
604         int err;
605 #endif
606
607         if (!priv->bittiming.tq && !priv->bittiming.bitrate) {
608                 dev_err(ND2D(dev), "bit-timing not yet defined\n");
609                 return -EINVAL;
610         }
611
612 #ifdef CONFIG_CAN_DEV_SYSFS
613         err = can_get_bittiming(dev, &priv->bittiming);
614         if (err)
615                 return err;
616
617         if (priv->do_set_bittiming) {
618                 /* Finally, set the bit-timing registers */
619                 err = priv->do_set_bittiming(dev);
620                 if (err)
621                         return err;
622         }
623 #endif
624
625         /* Switch carrier on if device was stopped while in bus-off state */
626         if (!netif_carrier_ok(dev))
627                 netif_carrier_on(dev);
628
629         setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev);
630
631         return 0;
632 }
633 EXPORT_SYMBOL_GPL(open_candev);
634
635 /*
636  * Common close function for cleanup before the device gets closed.
637  *
638  * This function should be called in the close function of the device
639  * driver.
640  */
641 void close_candev(struct net_device *dev)
642 {
643         struct can_priv *priv = netdev_priv(dev);
644
645         if (del_timer_sync(&priv->restart_timer))
646                 dev_put(dev);
647         can_flush_echo_skb(dev);
648 }
649 EXPORT_SYMBOL_GPL(close_candev);
650
651 #ifndef CONFIG_CAN_DEV_SYSFS
652 /*
653  * CAN netlink interface
654  */
655 static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
656         [IFLA_CAN_STATE]        = { .type = NLA_U32 },
657         [IFLA_CAN_CTRLMODE]     = { .len = sizeof(struct can_ctrlmode) },
658         [IFLA_CAN_RESTART_MS]   = { .type = NLA_U32 },
659         [IFLA_CAN_RESTART]      = { .type = NLA_U32 },
660         [IFLA_CAN_BITTIMING]    = { .len = sizeof(struct can_bittiming) },
661         [IFLA_CAN_BITTIMING_CONST]
662                                 = { .len = sizeof(struct can_bittiming_const) },
663         [IFLA_CAN_CLOCK]        = { .len = sizeof(struct can_clock) },
664         [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
665 };
666
667 static int can_changelink(struct net_device *dev,
668                           struct nlattr *tb[], struct nlattr *data[])
669 {
670         struct can_priv *priv = netdev_priv(dev);
671         int err;
672
673         /* We need synchronization with dev->stop() */
674         ASSERT_RTNL();
675
676         if (data[IFLA_CAN_CTRLMODE]) {
677                 struct can_ctrlmode *cm;
678
679                 /* Do not allow changing controller mode while running */
680                 if (dev->flags & IFF_UP)
681                         return -EBUSY;
682                 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
683                 if (cm->flags & ~priv->ctrlmode_supported)
684                         return -EOPNOTSUPP;
685                 priv->ctrlmode &= ~cm->mask;
686                 priv->ctrlmode |= cm->flags;
687         }
688
689         if (data[IFLA_CAN_BITTIMING]) {
690                 struct can_bittiming bt;
691
692                 /* Do not allow changing bittiming while running */
693                 if (dev->flags & IFF_UP)
694                         return -EBUSY;
695                 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
696                 if ((!bt.bitrate && !bt.tq) || (bt.bitrate && bt.tq))
697                         return -EINVAL;
698                 err = can_get_bittiming(dev, &bt);
699                 if (err)
700                         return err;
701                 memcpy(&priv->bittiming, &bt, sizeof(bt));
702
703                 if (priv->do_set_bittiming) {
704                         /* Finally, set the bit-timing registers */
705                         err = priv->do_set_bittiming(dev);
706                         if (err)
707                                 return err;
708                 }
709         }
710
711         if (data[IFLA_CAN_RESTART_MS]) {
712                 /* Do not allow changing restart delay while running */
713                 if (dev->flags & IFF_UP)
714                         return -EBUSY;
715                 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
716         }
717
718         if (data[IFLA_CAN_RESTART]) {
719                 /* Do not allow a restart while not running */
720                 if (!(dev->flags & IFF_UP))
721                         return -EINVAL;
722                 err = can_restart_now(dev);
723                 if (err)
724                         return err;
725         }
726
727         return 0;
728 }
729
730 static size_t can_get_size(const struct net_device *dev)
731 {
732         struct can_priv *priv = netdev_priv(dev);
733         size_t size;
734
735         size = nla_total_size(sizeof(u32));   /* IFLA_CAN_STATE */
736         size += sizeof(struct can_ctrlmode);  /* IFLA_CAN_CTRLMODE */
737         size += nla_total_size(sizeof(u32));  /* IFLA_CAN_RESTART_MS */
738         size += sizeof(struct can_bittiming); /* IFLA_CAN_BITTIMING */
739         size += sizeof(struct can_clock);     /* IFLA_CAN_CLOCK */
740         if (priv->do_get_berr_counter)        /* IFLA_CAN_BERR_COUNTER */
741                 size += sizeof(struct can_berr_counter);
742         if (priv->bittiming_const)            /* IFLA_CAN_BITTIMING_CONST */
743                 size += sizeof(struct can_bittiming_const);
744
745         return size;
746 }
747
748 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
749 {
750         struct can_priv *priv = netdev_priv(dev);
751         struct can_ctrlmode cm = {.flags = priv->ctrlmode};
752         struct can_berr_counter bec;
753         enum can_state state = priv->state;
754
755         if (priv->do_get_state)
756                 priv->do_get_state(dev, &state);
757         NLA_PUT_U32(skb, IFLA_CAN_STATE, state);
758         NLA_PUT(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm);
759         NLA_PUT_U32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms);
760         NLA_PUT(skb, IFLA_CAN_BITTIMING,
761                 sizeof(priv->bittiming), &priv->bittiming);
762         NLA_PUT(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock);
763         if (priv->do_get_berr_counter && !priv->do_get_berr_counter(dev, &bec))
764                 NLA_PUT(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec);
765         if (priv->bittiming_const)
766                 NLA_PUT(skb, IFLA_CAN_BITTIMING_CONST,
767                         sizeof(*priv->bittiming_const), priv->bittiming_const);
768
769         return 0;
770
771 nla_put_failure:
772         return -EMSGSIZE;
773 }
774
775 static size_t can_get_xstats_size(const struct net_device *dev)
776 {
777         return sizeof(struct can_device_stats);
778 }
779
780 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
781 {
782         struct can_priv *priv = netdev_priv(dev);
783
784         NLA_PUT(skb, IFLA_INFO_XSTATS,
785                 sizeof(priv->can_stats), &priv->can_stats);
786
787         return 0;
788
789 nla_put_failure:
790         return -EMSGSIZE;
791 }
792
793 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
794 static int can_newlink(struct net_device *dev,
795                        struct nlattr *tb[], struct nlattr *data[])
796 #else
797 static int can_newlink(struct net *src_net, struct net_device *dev,
798                        struct nlattr *tb[], struct nlattr *data[])
799 #endif
800 {
801         return -EOPNOTSUPP;
802 }
803
804 static struct rtnl_link_ops can_link_ops __read_mostly = {
805         .kind           = "can",
806         .maxtype        = IFLA_CAN_MAX,
807         .policy         = can_policy,
808         .setup          = can_setup,
809         .newlink        = can_newlink,
810         .changelink     = can_changelink,
811         .get_size       = can_get_size,
812         .fill_info      = can_fill_info,
813         .get_xstats_size = can_get_xstats_size,
814         .fill_xstats    = can_fill_xstats,
815 };
816
817 #endif /* !CONFIG_CAN_DEV_SYSFS */
818
819 /*
820  * Register the CAN network device
821  */
822 int register_candev(struct net_device *dev)
823 {
824 #ifdef CONFIG_CAN_DEV_SYSFS
825         int err;
826
827         err = register_netdev(dev);
828         if (!err)
829                 can_create_sysfs(dev);
830
831         return err;
832 #else
833         dev->rtnl_link_ops = &can_link_ops;
834         return register_netdev(dev);
835 #endif
836 }
837 EXPORT_SYMBOL_GPL(register_candev);
838
839 /*
840  * Unregister the CAN network device
841  */
842 void unregister_candev(struct net_device *dev)
843 {
844 #ifdef CONFIG_CAN_DEV_SYSFS
845         can_remove_sysfs(dev);
846 #endif
847         unregister_netdev(dev);
848 }
849 EXPORT_SYMBOL_GPL(unregister_candev);
850
851 static __init int can_dev_init(void)
852 {
853 #ifndef CONFIG_CAN_DEV_SYSFS
854         int err;
855
856         err = rtnl_link_register(&can_link_ops);
857         if (!err)
858                 printk(KERN_INFO MOD_DESC "\n");
859
860         return err;
861 #else
862         printk(KERN_INFO MOD_DESC " using the deprecated SYSFS interface\n");
863
864         return 0;
865 #endif
866 }
867 module_init(can_dev_init);
868
869 static __exit void can_dev_exit(void)
870 {
871 #ifndef CONFIG_CAN_DEV_SYSFS
872         rtnl_link_unregister(&can_link_ops);
873 #endif
874 }
875 module_exit(can_dev_exit);
876
877 #ifndef CONFIG_CAN_DEV_SYSFS
878 MODULE_ALIAS_RTNL_LINK("can");
879 #endif