blob: f03d7a481a809902ea03ada0bb4ff6c9f2ca5196 [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) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100133 netdev_err(dev,
134 "bitrate error %ld.%ld%% too high\n",
135 error / 10, error % 10);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000136 return -EDOM;
137 } else {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100138 netdev_warn(dev, "bitrate error %ld.%ld%%\n",
139 error / 10, error % 10);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000140 }
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;
Oliver Hartkopp2e114372011-09-28 02:50:11 +0000153
154 /* check for sjw user settings */
155 if (!bt->sjw || !btc->sjw_max)
156 bt->sjw = 1;
157 else {
158 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
159 if (bt->sjw > btc->sjw_max)
160 bt->sjw = btc->sjw_max;
161 /* bt->sjw must not be higher than tseg2 */
162 if (tseg2 < bt->sjw)
163 bt->sjw = tseg2;
164 }
165
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000166 bt->brp = best_brp;
167 /* real bit-rate */
168 bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
169
170 return 0;
171}
172#else /* !CONFIG_CAN_CALC_BITTIMING */
173static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
174{
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100175 netdev_err(dev, "bit-timing calculation not available\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000176 return -EINVAL;
177}
178#endif /* CONFIG_CAN_CALC_BITTIMING */
179
180/*
181 * Checks the validity of the specified bit-timing parameters prop_seg,
182 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
183 * prescaler value brp. You can find more information in the header
184 * file linux/can/netlink.h.
185 */
186static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt)
187{
188 struct can_priv *priv = netdev_priv(dev);
189 const struct can_bittiming_const *btc = priv->bittiming_const;
190 int tseg1, alltseg;
191 u64 brp64;
192
193 if (!priv->bittiming_const)
194 return -ENOTSUPP;
195
196 tseg1 = bt->prop_seg + bt->phase_seg1;
197 if (!bt->sjw)
198 bt->sjw = 1;
199 if (bt->sjw > btc->sjw_max ||
200 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
201 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
202 return -ERANGE;
203
204 brp64 = (u64)priv->clock.freq * (u64)bt->tq;
205 if (btc->brp_inc > 1)
206 do_div(brp64, btc->brp_inc);
207 brp64 += 500000000UL - 1;
208 do_div(brp64, 1000000000UL); /* the practicable BRP */
209 if (btc->brp_inc > 1)
210 brp64 *= btc->brp_inc;
211 bt->brp = (u32)brp64;
212
213 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
214 return -EINVAL;
215
216 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
217 bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
218 bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
219
220 return 0;
221}
222
Thadeu Lima de Souza Cascardo61463a32011-07-21 16:22:31 +0000223static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000224{
225 struct can_priv *priv = netdev_priv(dev);
226 int err;
227
228 /* Check if the CAN device has bit-timing parameters */
229 if (priv->bittiming_const) {
230
231 /* Non-expert mode? Check if the bitrate has been pre-defined */
232 if (!bt->tq)
233 /* Determine bit-timing parameters */
234 err = can_calc_bittiming(dev, bt);
235 else
236 /* Check bit-timing params and calculate proper brp */
237 err = can_fixup_bittiming(dev, bt);
238 if (err)
239 return err;
240 }
241
242 return 0;
243}
244
245/*
246 * Local echo of CAN messages
247 *
248 * CAN network devices *should* support a local echo functionality
249 * (see Documentation/networking/can.txt). To test the handling of CAN
250 * interfaces that do not support the local echo both driver types are
251 * implemented. In the case that the driver does not support the echo
252 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
253 * to perform the echo as a fallback solution.
254 */
255static void can_flush_echo_skb(struct net_device *dev)
256{
257 struct can_priv *priv = netdev_priv(dev);
258 struct net_device_stats *stats = &dev->stats;
259 int i;
260
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000261 for (i = 0; i < priv->echo_skb_max; i++) {
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000262 if (priv->echo_skb[i]) {
263 kfree_skb(priv->echo_skb[i]);
264 priv->echo_skb[i] = NULL;
265 stats->tx_dropped++;
266 stats->tx_aborted_errors++;
267 }
268 }
269}
270
271/*
272 * Put the skb on the stack to be looped backed locally lateron
273 *
274 * The function is typically called in the start_xmit function
275 * of the device driver. The driver must protect access to
276 * priv->echo_skb, if necessary.
277 */
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000278void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
279 unsigned int idx)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000280{
281 struct can_priv *priv = netdev_priv(dev);
282
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000283 BUG_ON(idx >= priv->echo_skb_max);
284
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000285 /* check flag whether this packet has to be looped back */
286 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) {
287 kfree_skb(skb);
288 return;
289 }
290
291 if (!priv->echo_skb[idx]) {
292 struct sock *srcsk = skb->sk;
293
294 if (atomic_read(&skb->users) != 1) {
295 struct sk_buff *old_skb = skb;
296
297 skb = skb_clone(old_skb, GFP_ATOMIC);
298 kfree_skb(old_skb);
299 if (!skb)
300 return;
301 } else
302 skb_orphan(skb);
303
304 skb->sk = srcsk;
305
306 /* make settings for echo to reduce code in irq context */
307 skb->protocol = htons(ETH_P_CAN);
308 skb->pkt_type = PACKET_BROADCAST;
309 skb->ip_summed = CHECKSUM_UNNECESSARY;
310 skb->dev = dev;
311
312 /* save this skb for tx interrupt echo handling */
313 priv->echo_skb[idx] = skb;
314 } else {
315 /* locking problem with netif_stop_queue() ?? */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100316 netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000317 kfree_skb(skb);
318 }
319}
320EXPORT_SYMBOL_GPL(can_put_echo_skb);
321
322/*
323 * Get the skb from the stack and loop it back locally
324 *
325 * The function is typically called when the TX done interrupt
326 * is handled in the device driver. The driver must protect
327 * access to priv->echo_skb, if necessary.
328 */
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200329unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000330{
331 struct can_priv *priv = netdev_priv(dev);
332
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000333 BUG_ON(idx >= priv->echo_skb_max);
334
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000335 if (priv->echo_skb[idx]) {
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200336 struct sk_buff *skb = priv->echo_skb[idx];
337 struct can_frame *cf = (struct can_frame *)skb->data;
338 u8 dlc = cf->can_dlc;
339
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000340 netif_rx(priv->echo_skb[idx]);
341 priv->echo_skb[idx] = NULL;
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200342
343 return dlc;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000344 }
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200345
346 return 0;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000347}
348EXPORT_SYMBOL_GPL(can_get_echo_skb);
349
350/*
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000351 * Remove the skb from the stack and free it.
352 *
353 * The function is typically called when TX failed.
354 */
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000355void can_free_echo_skb(struct net_device *dev, unsigned int idx)
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000356{
357 struct can_priv *priv = netdev_priv(dev);
358
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000359 BUG_ON(idx >= priv->echo_skb_max);
360
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000361 if (priv->echo_skb[idx]) {
362 kfree_skb(priv->echo_skb[idx]);
363 priv->echo_skb[idx] = NULL;
364 }
365}
366EXPORT_SYMBOL_GPL(can_free_echo_skb);
367
368/*
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000369 * CAN device restart for bus-off recovery
370 */
371void can_restart(unsigned long data)
372{
373 struct net_device *dev = (struct net_device *)data;
374 struct can_priv *priv = netdev_priv(dev);
375 struct net_device_stats *stats = &dev->stats;
376 struct sk_buff *skb;
377 struct can_frame *cf;
378 int err;
379
380 BUG_ON(netif_carrier_ok(dev));
381
382 /*
383 * No synchronization needed because the device is bus-off and
384 * no messages can come in or go out.
385 */
386 can_flush_echo_skb(dev);
387
388 /* send restart message upstream */
Wolfgang Grandegger7b6856a2009-10-20 00:08:01 -0700389 skb = alloc_can_err_skb(dev, &cf);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000390 if (skb == NULL) {
391 err = -ENOMEM;
Wolfgang Grandeggerb3d0df72009-07-20 04:06:40 +0000392 goto restart;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000393 }
Wolfgang Grandegger7b6856a2009-10-20 00:08:01 -0700394 cf->can_id |= CAN_ERR_RESTARTED;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000395
396 netif_rx(skb);
397
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000398 stats->rx_packets++;
399 stats->rx_bytes += cf->can_dlc;
400
Wolfgang Grandeggerb3d0df72009-07-20 04:06:40 +0000401restart:
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100402 netdev_dbg(dev, "restarted\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000403 priv->can_stats.restarts++;
404
405 /* Now restart the device */
406 err = priv->do_set_mode(dev, CAN_MODE_START);
407
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000408 netif_carrier_on(dev);
409 if (err)
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100410 netdev_err(dev, "Error %d during restart", err);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000411}
412
413int can_restart_now(struct net_device *dev)
414{
415 struct can_priv *priv = netdev_priv(dev);
416
417 /*
418 * A manual restart is only permitted if automatic restart is
419 * disabled and the device is in the bus-off state
420 */
421 if (priv->restart_ms)
422 return -EINVAL;
423 if (priv->state != CAN_STATE_BUS_OFF)
424 return -EBUSY;
425
426 /* Runs as soon as possible in the timer context */
427 mod_timer(&priv->restart_timer, jiffies);
428
429 return 0;
430}
431
432/*
433 * CAN bus-off
434 *
435 * This functions should be called when the device goes bus-off to
436 * tell the netif layer that no more packets can be sent or received.
437 * If enabled, a timer is started to trigger bus-off recovery.
438 */
439void can_bus_off(struct net_device *dev)
440{
441 struct can_priv *priv = netdev_priv(dev);
442
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100443 netdev_dbg(dev, "bus-off\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000444
445 netif_carrier_off(dev);
446 priv->can_stats.bus_off++;
447
448 if (priv->restart_ms)
449 mod_timer(&priv->restart_timer,
450 jiffies + (priv->restart_ms * HZ) / 1000);
451}
452EXPORT_SYMBOL_GPL(can_bus_off);
453
454static void can_setup(struct net_device *dev)
455{
456 dev->type = ARPHRD_CAN;
457 dev->mtu = sizeof(struct can_frame);
458 dev->hard_header_len = 0;
459 dev->addr_len = 0;
460 dev->tx_queue_len = 10;
461
462 /* New-style flags. */
463 dev->flags = IFF_NOARP;
Michał Mirosław34324dc2011-11-15 15:29:55 +0000464 dev->features = NETIF_F_HW_CSUM;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000465}
466
Wolfgang Grandegger7b6856a2009-10-20 00:08:01 -0700467struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
468{
469 struct sk_buff *skb;
470
471 skb = netdev_alloc_skb(dev, sizeof(struct can_frame));
472 if (unlikely(!skb))
473 return NULL;
474
475 skb->protocol = htons(ETH_P_CAN);
476 skb->pkt_type = PACKET_BROADCAST;
477 skb->ip_summed = CHECKSUM_UNNECESSARY;
478 *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
479 memset(*cf, 0, sizeof(struct can_frame));
480
481 return skb;
482}
483EXPORT_SYMBOL_GPL(alloc_can_skb);
484
485struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
486{
487 struct sk_buff *skb;
488
489 skb = alloc_can_skb(dev, cf);
490 if (unlikely(!skb))
491 return NULL;
492
493 (*cf)->can_id = CAN_ERR_FLAG;
494 (*cf)->can_dlc = CAN_ERR_DLC;
495
496 return skb;
497}
498EXPORT_SYMBOL_GPL(alloc_can_err_skb);
499
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000500/*
501 * Allocate and setup space for the CAN network device
502 */
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000503struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000504{
505 struct net_device *dev;
506 struct can_priv *priv;
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000507 int size;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000508
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000509 if (echo_skb_max)
510 size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
511 echo_skb_max * sizeof(struct sk_buff *);
512 else
513 size = sizeof_priv;
514
515 dev = alloc_netdev(size, "can%d", can_setup);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000516 if (!dev)
517 return NULL;
518
519 priv = netdev_priv(dev);
520
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000521 if (echo_skb_max) {
522 priv->echo_skb_max = echo_skb_max;
523 priv->echo_skb = (void *)priv +
524 ALIGN(sizeof_priv, sizeof(struct sk_buff *));
525 }
526
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000527 priv->state = CAN_STATE_STOPPED;
528
529 init_timer(&priv->restart_timer);
530
531 return dev;
532}
533EXPORT_SYMBOL_GPL(alloc_candev);
534
535/*
536 * Free space of the CAN network device
537 */
538void free_candev(struct net_device *dev)
539{
540 free_netdev(dev);
541}
542EXPORT_SYMBOL_GPL(free_candev);
543
544/*
545 * Common open function when the device gets opened.
546 *
547 * This function should be called in the open function of the device
548 * driver.
549 */
550int open_candev(struct net_device *dev)
551{
552 struct can_priv *priv = netdev_priv(dev);
553
554 if (!priv->bittiming.tq && !priv->bittiming.bitrate) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100555 netdev_err(dev, "bit-timing not yet defined\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000556 return -EINVAL;
557 }
558
Wolfgang Grandegger1b0d9222009-07-20 04:06:41 +0000559 /* Switch carrier on if device was stopped while in bus-off state */
560 if (!netif_carrier_ok(dev))
561 netif_carrier_on(dev);
562
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000563 setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev);
564
565 return 0;
566}
Wolfgang Grandegger128ced82009-05-30 07:55:48 +0000567EXPORT_SYMBOL_GPL(open_candev);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000568
569/*
570 * Common close function for cleanup before the device gets closed.
571 *
572 * This function should be called in the close function of the device
573 * driver.
574 */
575void close_candev(struct net_device *dev)
576{
577 struct can_priv *priv = netdev_priv(dev);
578
579 if (del_timer_sync(&priv->restart_timer))
580 dev_put(dev);
581 can_flush_echo_skb(dev);
582}
583EXPORT_SYMBOL_GPL(close_candev);
584
585/*
586 * CAN netlink interface
587 */
588static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
589 [IFLA_CAN_STATE] = { .type = NLA_U32 },
590 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
591 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
592 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
593 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
594 [IFLA_CAN_BITTIMING_CONST]
595 = { .len = sizeof(struct can_bittiming_const) },
596 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
Wolfgang Grandegger52c793f2010-02-22 22:21:17 +0000597 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000598};
599
600static int can_changelink(struct net_device *dev,
601 struct nlattr *tb[], struct nlattr *data[])
602{
603 struct can_priv *priv = netdev_priv(dev);
604 int err;
605
606 /* We need synchronization with dev->stop() */
607 ASSERT_RTNL();
608
609 if (data[IFLA_CAN_CTRLMODE]) {
610 struct can_ctrlmode *cm;
611
612 /* Do not allow changing controller mode while running */
613 if (dev->flags & IFF_UP)
614 return -EBUSY;
615 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
Christian Pellegrinad72c342010-01-14 07:08:34 +0000616 if (cm->flags & ~priv->ctrlmode_supported)
617 return -EOPNOTSUPP;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000618 priv->ctrlmode &= ~cm->mask;
619 priv->ctrlmode |= cm->flags;
620 }
621
622 if (data[IFLA_CAN_BITTIMING]) {
623 struct can_bittiming bt;
624
625 /* Do not allow changing bittiming while running */
626 if (dev->flags & IFF_UP)
627 return -EBUSY;
628 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
629 if ((!bt.bitrate && !bt.tq) || (bt.bitrate && bt.tq))
630 return -EINVAL;
631 err = can_get_bittiming(dev, &bt);
632 if (err)
633 return err;
634 memcpy(&priv->bittiming, &bt, sizeof(bt));
635
636 if (priv->do_set_bittiming) {
637 /* Finally, set the bit-timing registers */
638 err = priv->do_set_bittiming(dev);
639 if (err)
640 return err;
641 }
642 }
643
644 if (data[IFLA_CAN_RESTART_MS]) {
645 /* Do not allow changing restart delay while running */
646 if (dev->flags & IFF_UP)
647 return -EBUSY;
648 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
649 }
650
651 if (data[IFLA_CAN_RESTART]) {
652 /* Do not allow a restart while not running */
653 if (!(dev->flags & IFF_UP))
654 return -EINVAL;
655 err = can_restart_now(dev);
656 if (err)
657 return err;
658 }
659
660 return 0;
661}
662
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +0000663static size_t can_get_size(const struct net_device *dev)
664{
665 struct can_priv *priv = netdev_priv(dev);
666 size_t size;
667
668 size = nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
669 size += sizeof(struct can_ctrlmode); /* IFLA_CAN_CTRLMODE */
670 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
671 size += sizeof(struct can_bittiming); /* IFLA_CAN_BITTIMING */
672 size += sizeof(struct can_clock); /* IFLA_CAN_CLOCK */
Wolfgang Grandegger52c793f2010-02-22 22:21:17 +0000673 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
674 size += sizeof(struct can_berr_counter);
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +0000675 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
676 size += sizeof(struct can_bittiming_const);
677
678 return size;
679}
680
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000681static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
682{
683 struct can_priv *priv = netdev_priv(dev);
684 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
Wolfgang Grandegger52c793f2010-02-22 22:21:17 +0000685 struct can_berr_counter bec;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000686 enum can_state state = priv->state;
687
688 if (priv->do_get_state)
689 priv->do_get_state(dev, &state);
David S. Miller31e0e322012-04-01 20:21:11 -0400690 if (nla_put_u32(skb, IFLA_CAN_STATE, state) ||
691 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
692 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
693 nla_put(skb, IFLA_CAN_BITTIMING,
694 sizeof(priv->bittiming), &priv->bittiming) ||
695 nla_put(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock) ||
696 (priv->do_get_berr_counter &&
697 !priv->do_get_berr_counter(dev, &bec) &&
698 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
699 (priv->bittiming_const &&
700 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
701 sizeof(*priv->bittiming_const), priv->bittiming_const)))
702 goto nla_put_failure;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000703 return 0;
704
705nla_put_failure:
706 return -EMSGSIZE;
707}
708
Wolfgang Grandegger55369c02009-11-12 05:34:05 +0000709static size_t can_get_xstats_size(const struct net_device *dev)
710{
711 return sizeof(struct can_device_stats);
712}
713
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000714static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
715{
716 struct can_priv *priv = netdev_priv(dev);
717
David S. Miller31e0e322012-04-01 20:21:11 -0400718 if (nla_put(skb, IFLA_INFO_XSTATS,
719 sizeof(priv->can_stats), &priv->can_stats))
720 goto nla_put_failure;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000721 return 0;
722
723nla_put_failure:
724 return -EMSGSIZE;
725}
726
Eric W. Biederman81adee42009-11-08 00:53:51 -0800727static int can_newlink(struct net *src_net, struct net_device *dev,
Oliver Hartkopp993e6f22009-08-11 02:41:24 +0000728 struct nlattr *tb[], struct nlattr *data[])
729{
730 return -EOPNOTSUPP;
731}
732
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000733static struct rtnl_link_ops can_link_ops __read_mostly = {
734 .kind = "can",
735 .maxtype = IFLA_CAN_MAX,
736 .policy = can_policy,
737 .setup = can_setup,
Oliver Hartkopp993e6f22009-08-11 02:41:24 +0000738 .newlink = can_newlink,
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000739 .changelink = can_changelink,
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +0000740 .get_size = can_get_size,
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000741 .fill_info = can_fill_info,
Wolfgang Grandegger55369c02009-11-12 05:34:05 +0000742 .get_xstats_size = can_get_xstats_size,
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000743 .fill_xstats = can_fill_xstats,
744};
745
746/*
747 * Register the CAN network device
748 */
749int register_candev(struct net_device *dev)
750{
751 dev->rtnl_link_ops = &can_link_ops;
752 return register_netdev(dev);
753}
754EXPORT_SYMBOL_GPL(register_candev);
755
756/*
757 * Unregister the CAN network device
758 */
759void unregister_candev(struct net_device *dev)
760{
761 unregister_netdev(dev);
762}
763EXPORT_SYMBOL_GPL(unregister_candev);
764
765static __init int can_dev_init(void)
766{
767 int err;
768
769 err = rtnl_link_register(&can_link_ops);
770 if (!err)
771 printk(KERN_INFO MOD_DESC "\n");
772
773 return err;
774}
775module_init(can_dev_init);
776
777static __exit void can_dev_exit(void)
778{
779 rtnl_link_unregister(&can_link_ops);
780}
781module_exit(can_dev_exit);
782
783MODULE_ALIAS_RTNL_LINK("can");