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