mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / net / can / dev.c
blob284d751ea97fb37d5ea934593ff1473fdbdbe744
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>
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
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.
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
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/skb.h>
29 #include <linux/can/netlink.h>
30 #include <linux/can/led.h>
31 #include <net/rtnetlink.h>
33 #define MOD_DESC "CAN device driver interface"
35 MODULE_DESCRIPTION(MOD_DESC);
36 MODULE_LICENSE("GPL v2");
37 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
39 /* CAN DLC to real data length conversion helpers */
41 static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
42 8, 12, 16, 20, 24, 32, 48, 64};
44 /* get data length from can_dlc with sanitized can_dlc */
45 u8 can_dlc2len(u8 can_dlc)
47 return dlc2len[can_dlc & 0x0F];
49 EXPORT_SYMBOL_GPL(can_dlc2len);
51 static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
52 9, 9, 9, 9, /* 9 - 12 */
53 10, 10, 10, 10, /* 13 - 16 */
54 11, 11, 11, 11, /* 17 - 20 */
55 12, 12, 12, 12, /* 21 - 24 */
56 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
57 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
58 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
59 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
60 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
62 /* map the sanitized data length to an appropriate data length code */
63 u8 can_len2dlc(u8 len)
65 if (unlikely(len > 64))
66 return 0xF;
68 return len2dlc[len];
70 EXPORT_SYMBOL_GPL(can_len2dlc);
72 #ifdef CONFIG_CAN_CALC_BITTIMING
73 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
76 * Bit-timing calculation derived from:
78 * Code based on LinCAN sources and H8S2638 project
79 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
80 * Copyright 2005 Stanislav Marek
81 * email: pisa@cmp.felk.cvut.cz
83 * Calculates proper bit-timing parameters for a specified bit-rate
84 * and sample-point, which can then be used to set the bit-timing
85 * registers of the CAN controller. You can find more information
86 * in the header file linux/can/netlink.h.
88 static int can_update_spt(const struct can_bittiming_const *btc,
89 int sampl_pt, int tseg, int *tseg1, int *tseg2)
91 *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
92 if (*tseg2 < btc->tseg2_min)
93 *tseg2 = btc->tseg2_min;
94 if (*tseg2 > btc->tseg2_max)
95 *tseg2 = btc->tseg2_max;
96 *tseg1 = tseg - *tseg2;
97 if (*tseg1 > btc->tseg1_max) {
98 *tseg1 = btc->tseg1_max;
99 *tseg2 = tseg - *tseg1;
101 return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
104 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
106 struct can_priv *priv = netdev_priv(dev);
107 const struct can_bittiming_const *btc = priv->bittiming_const;
108 long rate, best_rate = 0;
109 long best_error = 1000000000, error = 0;
110 int best_tseg = 0, best_brp = 0, brp = 0;
111 int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
112 int spt_error = 1000, spt = 0, sampl_pt;
113 u64 v64;
115 if (!priv->bittiming_const)
116 return -ENOTSUPP;
118 /* Use CIA recommended sample points */
119 if (bt->sample_point) {
120 sampl_pt = bt->sample_point;
121 } else {
122 if (bt->bitrate > 800000)
123 sampl_pt = 750;
124 else if (bt->bitrate > 500000)
125 sampl_pt = 800;
126 else
127 sampl_pt = 875;
130 /* tseg even = round down, odd = round up */
131 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
132 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
133 tsegall = 1 + tseg / 2;
134 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
135 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
136 /* chose brp step which is possible in system */
137 brp = (brp / btc->brp_inc) * btc->brp_inc;
138 if ((brp < btc->brp_min) || (brp > btc->brp_max))
139 continue;
140 rate = priv->clock.freq / (brp * tsegall);
141 error = bt->bitrate - rate;
142 /* tseg brp biterror */
143 if (error < 0)
144 error = -error;
145 if (error > best_error)
146 continue;
147 best_error = error;
148 if (error == 0) {
149 spt = can_update_spt(btc, sampl_pt, tseg / 2,
150 &tseg1, &tseg2);
151 error = sampl_pt - spt;
152 if (error < 0)
153 error = -error;
154 if (error > spt_error)
155 continue;
156 spt_error = error;
158 best_tseg = tseg / 2;
159 best_brp = brp;
160 best_rate = rate;
161 if (error == 0)
162 break;
165 if (best_error) {
166 /* Error in one-tenth of a percent */
167 error = (best_error * 1000) / bt->bitrate;
168 if (error > CAN_CALC_MAX_ERROR) {
169 netdev_err(dev,
170 "bitrate error %ld.%ld%% too high\n",
171 error / 10, error % 10);
172 return -EDOM;
173 } else {
174 netdev_warn(dev, "bitrate error %ld.%ld%%\n",
175 error / 10, error % 10);
179 /* real sample point */
180 bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
181 &tseg1, &tseg2);
183 v64 = (u64)best_brp * 1000000000UL;
184 do_div(v64, priv->clock.freq);
185 bt->tq = (u32)v64;
186 bt->prop_seg = tseg1 / 2;
187 bt->phase_seg1 = tseg1 - bt->prop_seg;
188 bt->phase_seg2 = tseg2;
190 /* check for sjw user settings */
191 if (!bt->sjw || !btc->sjw_max)
192 bt->sjw = 1;
193 else {
194 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
195 if (bt->sjw > btc->sjw_max)
196 bt->sjw = btc->sjw_max;
197 /* bt->sjw must not be higher than tseg2 */
198 if (tseg2 < bt->sjw)
199 bt->sjw = tseg2;
202 bt->brp = best_brp;
203 /* real bit-rate */
204 bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
206 return 0;
208 #else /* !CONFIG_CAN_CALC_BITTIMING */
209 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
211 netdev_err(dev, "bit-timing calculation not available\n");
212 return -EINVAL;
214 #endif /* CONFIG_CAN_CALC_BITTIMING */
217 * Checks the validity of the specified bit-timing parameters prop_seg,
218 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
219 * prescaler value brp. You can find more information in the header
220 * file linux/can/netlink.h.
222 static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt)
224 struct can_priv *priv = netdev_priv(dev);
225 const struct can_bittiming_const *btc = priv->bittiming_const;
226 int tseg1, alltseg;
227 u64 brp64;
229 if (!priv->bittiming_const)
230 return -ENOTSUPP;
232 tseg1 = bt->prop_seg + bt->phase_seg1;
233 if (!bt->sjw)
234 bt->sjw = 1;
235 if (bt->sjw > btc->sjw_max ||
236 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
237 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
238 return -ERANGE;
240 brp64 = (u64)priv->clock.freq * (u64)bt->tq;
241 if (btc->brp_inc > 1)
242 do_div(brp64, btc->brp_inc);
243 brp64 += 500000000UL - 1;
244 do_div(brp64, 1000000000UL); /* the practicable BRP */
245 if (btc->brp_inc > 1)
246 brp64 *= btc->brp_inc;
247 bt->brp = (u32)brp64;
249 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
250 return -EINVAL;
252 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
253 bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
254 bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
256 return 0;
259 static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt)
261 struct can_priv *priv = netdev_priv(dev);
262 int err;
264 /* Check if the CAN device has bit-timing parameters */
265 if (priv->bittiming_const) {
267 /* Non-expert mode? Check if the bitrate has been pre-defined */
268 if (!bt->tq)
269 /* Determine bit-timing parameters */
270 err = can_calc_bittiming(dev, bt);
271 else
272 /* Check bit-timing params and calculate proper brp */
273 err = can_fixup_bittiming(dev, bt);
274 if (err)
275 return err;
278 return 0;
282 * Local echo of CAN messages
284 * CAN network devices *should* support a local echo functionality
285 * (see Documentation/networking/can.txt). To test the handling of CAN
286 * interfaces that do not support the local echo both driver types are
287 * implemented. In the case that the driver does not support the echo
288 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
289 * to perform the echo as a fallback solution.
291 static void can_flush_echo_skb(struct net_device *dev)
293 struct can_priv *priv = netdev_priv(dev);
294 struct net_device_stats *stats = &dev->stats;
295 int i;
297 for (i = 0; i < priv->echo_skb_max; i++) {
298 if (priv->echo_skb[i]) {
299 kfree_skb(priv->echo_skb[i]);
300 priv->echo_skb[i] = NULL;
301 stats->tx_dropped++;
302 stats->tx_aborted_errors++;
308 * Put the skb on the stack to be looped backed locally lateron
310 * The function is typically called in the start_xmit function
311 * of the device driver. The driver must protect access to
312 * priv->echo_skb, if necessary.
314 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
315 unsigned int idx)
317 struct can_priv *priv = netdev_priv(dev);
319 BUG_ON(idx >= priv->echo_skb_max);
321 /* check flag whether this packet has to be looped back */
322 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
323 kfree_skb(skb);
324 return;
327 if (!priv->echo_skb[idx]) {
329 skb = can_create_echo_skb(skb);
330 if (!skb)
331 return;
333 /* make settings for echo to reduce code in irq context */
334 skb->protocol = htons(ETH_P_CAN);
335 skb->pkt_type = PACKET_BROADCAST;
336 skb->ip_summed = CHECKSUM_UNNECESSARY;
337 skb->dev = dev;
339 /* save this skb for tx interrupt echo handling */
340 priv->echo_skb[idx] = skb;
341 } else {
342 /* locking problem with netif_stop_queue() ?? */
343 netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
344 kfree_skb(skb);
347 EXPORT_SYMBOL_GPL(can_put_echo_skb);
350 * Get the skb from the stack and loop it back locally
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.
356 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
358 struct can_priv *priv = netdev_priv(dev);
360 BUG_ON(idx >= priv->echo_skb_max);
362 if (priv->echo_skb[idx]) {
363 struct sk_buff *skb = priv->echo_skb[idx];
364 struct can_frame *cf = (struct can_frame *)skb->data;
365 u8 dlc = cf->can_dlc;
367 netif_rx(priv->echo_skb[idx]);
368 priv->echo_skb[idx] = NULL;
370 return dlc;
373 return 0;
375 EXPORT_SYMBOL_GPL(can_get_echo_skb);
378 * Remove the skb from the stack and free it.
380 * The function is typically called when TX failed.
382 void can_free_echo_skb(struct net_device *dev, unsigned int idx)
384 struct can_priv *priv = netdev_priv(dev);
386 BUG_ON(idx >= priv->echo_skb_max);
388 if (priv->echo_skb[idx]) {
389 dev_kfree_skb_any(priv->echo_skb[idx]);
390 priv->echo_skb[idx] = NULL;
393 EXPORT_SYMBOL_GPL(can_free_echo_skb);
396 * CAN device restart for bus-off recovery
398 static void can_restart(struct net_device *dev)
400 struct can_priv *priv = netdev_priv(dev);
401 struct net_device_stats *stats = &dev->stats;
402 struct sk_buff *skb;
403 struct can_frame *cf;
404 int err;
406 BUG_ON(netif_carrier_ok(dev));
409 * No synchronization needed because the device is bus-off and
410 * no messages can come in or go out.
412 can_flush_echo_skb(dev);
414 /* send restart message upstream */
415 skb = alloc_can_err_skb(dev, &cf);
416 if (skb == NULL) {
417 err = -ENOMEM;
418 goto restart;
420 cf->can_id |= CAN_ERR_RESTARTED;
422 netif_rx(skb);
424 stats->rx_packets++;
425 stats->rx_bytes += cf->can_dlc;
427 restart:
428 netdev_dbg(dev, "restarted\n");
429 priv->can_stats.restarts++;
431 /* Now restart the device */
432 err = priv->do_set_mode(dev, CAN_MODE_START);
434 netif_carrier_on(dev);
435 if (err)
436 netdev_err(dev, "Error %d during restart", err);
439 static void can_restart_work(struct work_struct *work)
441 struct delayed_work *dwork = to_delayed_work(work);
442 struct can_priv *priv = container_of(dwork, struct can_priv, restart_work);
444 can_restart(priv->dev);
447 int can_restart_now(struct net_device *dev)
449 struct can_priv *priv = netdev_priv(dev);
452 * A manual restart is only permitted if automatic restart is
453 * disabled and the device is in the bus-off state
455 if (priv->restart_ms)
456 return -EINVAL;
457 if (priv->state != CAN_STATE_BUS_OFF)
458 return -EBUSY;
460 cancel_delayed_work_sync(&priv->restart_work);
461 can_restart(dev);
463 return 0;
467 * CAN bus-off
469 * This functions should be called when the device goes bus-off to
470 * tell the netif layer that no more packets can be sent or received.
471 * If enabled, a timer is started to trigger bus-off recovery.
473 void can_bus_off(struct net_device *dev)
475 struct can_priv *priv = netdev_priv(dev);
477 netdev_dbg(dev, "bus-off\n");
479 netif_carrier_off(dev);
480 priv->can_stats.bus_off++;
482 if (priv->restart_ms)
483 schedule_delayed_work(&priv->restart_work,
484 msecs_to_jiffies(priv->restart_ms));
486 EXPORT_SYMBOL_GPL(can_bus_off);
488 static void can_setup(struct net_device *dev)
490 dev->type = ARPHRD_CAN;
491 dev->mtu = CAN_MTU;
492 dev->hard_header_len = 0;
493 dev->addr_len = 0;
494 dev->tx_queue_len = 10;
496 /* New-style flags. */
497 dev->flags = IFF_NOARP;
498 dev->features = NETIF_F_HW_CSUM;
501 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
503 struct sk_buff *skb;
505 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
506 sizeof(struct can_frame));
507 if (unlikely(!skb))
508 return NULL;
510 skb->protocol = htons(ETH_P_CAN);
511 skb->pkt_type = PACKET_BROADCAST;
512 skb->ip_summed = CHECKSUM_UNNECESSARY;
514 skb_reset_mac_header(skb);
515 skb_reset_network_header(skb);
516 skb_reset_transport_header(skb);
518 skb_reset_mac_header(skb);
519 skb_reset_network_header(skb);
520 skb_reset_transport_header(skb);
522 can_skb_reserve(skb);
523 can_skb_prv(skb)->ifindex = dev->ifindex;
525 *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
526 memset(*cf, 0, sizeof(struct can_frame));
528 return skb;
530 EXPORT_SYMBOL_GPL(alloc_can_skb);
532 struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
534 struct sk_buff *skb;
536 skb = alloc_can_skb(dev, cf);
537 if (unlikely(!skb))
538 return NULL;
540 (*cf)->can_id = CAN_ERR_FLAG;
541 (*cf)->can_dlc = CAN_ERR_DLC;
543 return skb;
545 EXPORT_SYMBOL_GPL(alloc_can_err_skb);
548 * Allocate and setup space for the CAN network device
550 struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
552 struct net_device *dev;
553 struct can_priv *priv;
554 int size;
556 if (echo_skb_max)
557 size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
558 echo_skb_max * sizeof(struct sk_buff *);
559 else
560 size = sizeof_priv;
562 dev = alloc_netdev(size, "can%d", can_setup);
563 if (!dev)
564 return NULL;
566 priv = netdev_priv(dev);
567 priv->dev = dev;
569 if (echo_skb_max) {
570 priv->echo_skb_max = echo_skb_max;
571 priv->echo_skb = (void *)priv +
572 ALIGN(sizeof_priv, sizeof(struct sk_buff *));
575 priv->state = CAN_STATE_STOPPED;
577 INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
579 return dev;
581 EXPORT_SYMBOL_GPL(alloc_candev);
584 * Free space of the CAN network device
586 void free_candev(struct net_device *dev)
588 free_netdev(dev);
590 EXPORT_SYMBOL_GPL(free_candev);
593 * Common open function when the device gets opened.
595 * This function should be called in the open function of the device
596 * driver.
598 int open_candev(struct net_device *dev)
600 struct can_priv *priv = netdev_priv(dev);
602 if (!priv->bittiming.tq && !priv->bittiming.bitrate) {
603 netdev_err(dev, "bit-timing not yet defined\n");
604 return -EINVAL;
607 /* Switch carrier on if device was stopped while in bus-off state */
608 if (!netif_carrier_ok(dev))
609 netif_carrier_on(dev);
611 return 0;
613 EXPORT_SYMBOL_GPL(open_candev);
616 * Common close function for cleanup before the device gets closed.
618 * This function should be called in the close function of the device
619 * driver.
621 void close_candev(struct net_device *dev)
623 struct can_priv *priv = netdev_priv(dev);
625 cancel_delayed_work_sync(&priv->restart_work);
626 can_flush_echo_skb(dev);
628 EXPORT_SYMBOL_GPL(close_candev);
631 * CAN netlink interface
633 static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
634 [IFLA_CAN_STATE] = { .type = NLA_U32 },
635 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
636 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
637 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
638 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
639 [IFLA_CAN_BITTIMING_CONST]
640 = { .len = sizeof(struct can_bittiming_const) },
641 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
642 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
645 static int can_changelink(struct net_device *dev,
646 struct nlattr *tb[], struct nlattr *data[])
648 struct can_priv *priv = netdev_priv(dev);
649 int err;
651 /* We need synchronization with dev->stop() */
652 ASSERT_RTNL();
654 if (data[IFLA_CAN_CTRLMODE]) {
655 struct can_ctrlmode *cm;
657 /* Do not allow changing controller mode while running */
658 if (dev->flags & IFF_UP)
659 return -EBUSY;
660 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
662 /* check whether changed bits are allowed to be modified */
663 if (cm->mask & ~priv->ctrlmode_supported)
664 return -EOPNOTSUPP;
666 /* clear bits to be modified and copy the flag values */
667 priv->ctrlmode &= ~cm->mask;
668 priv->ctrlmode |= (cm->flags & cm->mask);
671 if (data[IFLA_CAN_BITTIMING]) {
672 struct can_bittiming bt;
674 /* Do not allow changing bittiming while running */
675 if (dev->flags & IFF_UP)
676 return -EBUSY;
677 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
678 if ((!bt.bitrate && !bt.tq) || (bt.bitrate && bt.tq))
679 return -EINVAL;
680 err = can_get_bittiming(dev, &bt);
681 if (err)
682 return err;
683 memcpy(&priv->bittiming, &bt, sizeof(bt));
685 if (priv->do_set_bittiming) {
686 /* Finally, set the bit-timing registers */
687 err = priv->do_set_bittiming(dev);
688 if (err)
689 return err;
693 if (data[IFLA_CAN_RESTART_MS]) {
694 /* Do not allow changing restart delay while running */
695 if (dev->flags & IFF_UP)
696 return -EBUSY;
697 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
700 if (data[IFLA_CAN_RESTART]) {
701 /* Do not allow a restart while not running */
702 if (!(dev->flags & IFF_UP))
703 return -EINVAL;
704 err = can_restart_now(dev);
705 if (err)
706 return err;
709 return 0;
712 static size_t can_get_size(const struct net_device *dev)
714 struct can_priv *priv = netdev_priv(dev);
715 size_t size;
717 size = nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
718 size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
719 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
720 size += nla_total_size(sizeof(struct can_bittiming)); /* IFLA_CAN_BITTIMING */
721 size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
722 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
723 size += nla_total_size(sizeof(struct can_berr_counter));
724 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
725 size += nla_total_size(sizeof(struct can_bittiming_const));
727 return size;
730 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
732 struct can_priv *priv = netdev_priv(dev);
733 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
734 struct can_berr_counter bec;
735 enum can_state state = priv->state;
737 if (priv->do_get_state)
738 priv->do_get_state(dev, &state);
739 if (nla_put_u32(skb, IFLA_CAN_STATE, state) ||
740 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
741 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
742 nla_put(skb, IFLA_CAN_BITTIMING,
743 sizeof(priv->bittiming), &priv->bittiming) ||
744 nla_put(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock) ||
745 (priv->do_get_berr_counter &&
746 !priv->do_get_berr_counter(dev, &bec) &&
747 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
748 (priv->bittiming_const &&
749 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
750 sizeof(*priv->bittiming_const), priv->bittiming_const)))
751 goto nla_put_failure;
752 return 0;
754 nla_put_failure:
755 return -EMSGSIZE;
758 static size_t can_get_xstats_size(const struct net_device *dev)
760 return sizeof(struct can_device_stats);
763 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
765 struct can_priv *priv = netdev_priv(dev);
767 if (nla_put(skb, IFLA_INFO_XSTATS,
768 sizeof(priv->can_stats), &priv->can_stats))
769 goto nla_put_failure;
770 return 0;
772 nla_put_failure:
773 return -EMSGSIZE;
776 static int can_newlink(struct net *src_net, struct net_device *dev,
777 struct nlattr *tb[], struct nlattr *data[])
779 return -EOPNOTSUPP;
782 static void can_dellink(struct net_device *dev, struct list_head *head)
784 return;
787 static struct rtnl_link_ops can_link_ops __read_mostly = {
788 .kind = "can",
789 .maxtype = IFLA_CAN_MAX,
790 .policy = can_policy,
791 .setup = can_setup,
792 .newlink = can_newlink,
793 .changelink = can_changelink,
794 .dellink = can_dellink,
795 .get_size = can_get_size,
796 .fill_info = can_fill_info,
797 .get_xstats_size = can_get_xstats_size,
798 .fill_xstats = can_fill_xstats,
802 * Register the CAN network device
804 int register_candev(struct net_device *dev)
806 dev->rtnl_link_ops = &can_link_ops;
807 return register_netdev(dev);
809 EXPORT_SYMBOL_GPL(register_candev);
812 * Unregister the CAN network device
814 void unregister_candev(struct net_device *dev)
816 unregister_netdev(dev);
818 EXPORT_SYMBOL_GPL(unregister_candev);
821 * Test if a network device is a candev based device
822 * and return the can_priv* if so.
824 struct can_priv *safe_candev_priv(struct net_device *dev)
826 if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
827 return NULL;
829 return netdev_priv(dev);
831 EXPORT_SYMBOL_GPL(safe_candev_priv);
833 static __init int can_dev_init(void)
835 int err;
837 can_led_notifier_init();
839 err = rtnl_link_register(&can_link_ops);
840 if (!err)
841 printk(KERN_INFO MOD_DESC "\n");
843 return err;
845 module_init(can_dev_init);
847 static __exit void can_dev_exit(void)
849 rtnl_link_unregister(&can_link_ops);
851 can_led_notifier_exit();
853 module_exit(can_dev_exit);
855 MODULE_ALIAS_RTNL_LINK("can");