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