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