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