]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/can/dev.c
netvsc: fix race during initialization
[mirror_ubuntu-artful-kernel.git] / drivers / net / can / dev.c
CommitLineData
39549eef
WG
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>
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
05780d98 16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
39549eef
WG
17 */
18
19#include <linux/module.h>
20#include <linux/kernel.h>
5a0e3ad6 21#include <linux/slab.h>
39549eef
WG
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
9abefcb1 24#include <linux/workqueue.h>
39549eef
WG
25#include <linux/can.h>
26#include <linux/can/dev.h>
156c2bb9 27#include <linux/can/skb.h>
39549eef 28#include <linux/can/netlink.h>
a1ef7bd9 29#include <linux/can/led.h>
39549eef
WG
30#include <net/rtnetlink.h>
31
32#define MOD_DESC "CAN device driver interface"
33
34MODULE_DESCRIPTION(MOD_DESC);
35MODULE_LICENSE("GPL v2");
36MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
37
1e0625fa
OH
38/* CAN DLC to real data length conversion helpers */
39
40static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
41 8, 12, 16, 20, 24, 32, 48, 64};
42
43/* get data length from can_dlc with sanitized can_dlc */
44u8 can_dlc2len(u8 can_dlc)
45{
46 return dlc2len[can_dlc & 0x0F];
47}
48EXPORT_SYMBOL_GPL(can_dlc2len);
49
50static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
51 9, 9, 9, 9, /* 9 - 12 */
52 10, 10, 10, 10, /* 13 - 16 */
53 11, 11, 11, 11, /* 17 - 20 */
54 12, 12, 12, 12, /* 21 - 24 */
55 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
56 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
57 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
58 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
59 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
60
61/* map the sanitized data length to an appropriate data length code */
62u8 can_len2dlc(u8 len)
63{
64 if (unlikely(len > 64))
65 return 0xF;
66
67 return len2dlc[len];
68}
69EXPORT_SYMBOL_GPL(can_len2dlc);
70
39549eef
WG
71#ifdef CONFIG_CAN_CALC_BITTIMING
72#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
7da29f97 73#define CAN_CALC_SYNC_SEG 1
39549eef
WG
74
75/*
76 * Bit-timing calculation derived from:
77 *
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
82 *
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.
87 */
7da29f97
MKB
88static int can_update_sample_point(const struct can_bittiming_const *btc,
89 unsigned int sample_point_nominal, unsigned int tseg,
90 unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
91 unsigned int *sample_point_error_ptr)
39549eef 92{
7da29f97
MKB
93 unsigned int sample_point_error, best_sample_point_error = UINT_MAX;
94 unsigned int sample_point, best_sample_point = 0;
95 unsigned int tseg1, tseg2;
96 int i;
97
98 for (i = 0; i <= 1; i++) {
99 tseg2 = tseg + CAN_CALC_SYNC_SEG - (sample_point_nominal * (tseg + CAN_CALC_SYNC_SEG)) / 1000 - i;
100 tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
101 tseg1 = tseg - tseg2;
102 if (tseg1 > btc->tseg1_max) {
103 tseg1 = btc->tseg1_max;
104 tseg2 = tseg - tseg1;
105 }
106
107 sample_point = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) / (tseg + CAN_CALC_SYNC_SEG);
108 sample_point_error = abs(sample_point_nominal - sample_point);
109
110 if ((sample_point <= sample_point_nominal) && (sample_point_error < best_sample_point_error)) {
111 best_sample_point = sample_point;
112 best_sample_point_error = sample_point_error;
113 *tseg1_ptr = tseg1;
114 *tseg2_ptr = tseg2;
115 }
39549eef 116 }
7da29f97
MKB
117
118 if (sample_point_error_ptr)
119 *sample_point_error_ptr = best_sample_point_error;
120
121 return best_sample_point;
39549eef
WG
122}
123
08da7da4
OH
124static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
125 const struct can_bittiming_const *btc)
39549eef
WG
126{
127 struct can_priv *priv = netdev_priv(dev);
7da29f97
MKB
128 unsigned int bitrate; /* current bitrate */
129 unsigned int bitrate_error; /* difference between current and nominal value */
130 unsigned int best_bitrate_error = UINT_MAX;
131 unsigned int sample_point_error; /* difference between current and nominal value */
132 unsigned int best_sample_point_error = UINT_MAX;
133 unsigned int sample_point_nominal; /* nominal sample point */
134 unsigned int best_tseg = 0; /* current best value for tseg */
135 unsigned int best_brp = 0; /* current best value for brp */
136 unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0;
39549eef
WG
137 u64 v64;
138
67b5909e 139 /* Use CiA recommended sample points */
39549eef 140 if (bt->sample_point) {
7da29f97 141 sample_point_nominal = bt->sample_point;
39549eef
WG
142 } else {
143 if (bt->bitrate > 800000)
7da29f97 144 sample_point_nominal = 750;
39549eef 145 else if (bt->bitrate > 500000)
7da29f97 146 sample_point_nominal = 800;
39549eef 147 else
7da29f97 148 sample_point_nominal = 875;
39549eef
WG
149 }
150
151 /* tseg even = round down, odd = round up */
152 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
153 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
7da29f97
MKB
154 tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
155
39549eef
WG
156 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
157 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
7da29f97
MKB
158
159 /* choose brp step which is possible in system */
39549eef
WG
160 brp = (brp / btc->brp_inc) * btc->brp_inc;
161 if ((brp < btc->brp_min) || (brp > btc->brp_max))
162 continue;
7da29f97
MKB
163
164 bitrate = priv->clock.freq / (brp * tsegall);
165 bitrate_error = abs(bt->bitrate - bitrate);
166
39549eef 167 /* tseg brp biterror */
7da29f97 168 if (bitrate_error > best_bitrate_error)
39549eef 169 continue;
7da29f97
MKB
170
171 /* reset sample point error if we have a better bitrate */
172 if (bitrate_error < best_bitrate_error)
173 best_sample_point_error = UINT_MAX;
174
175 can_update_sample_point(btc, sample_point_nominal, tseg / 2, &tseg1, &tseg2, &sample_point_error);
176 if (sample_point_error > best_sample_point_error)
177 continue;
178
179 best_sample_point_error = sample_point_error;
180 best_bitrate_error = bitrate_error;
39549eef
WG
181 best_tseg = tseg / 2;
182 best_brp = brp;
7da29f97
MKB
183
184 if (bitrate_error == 0 && sample_point_error == 0)
39549eef
WG
185 break;
186 }
187
7da29f97 188 if (best_bitrate_error) {
39549eef 189 /* Error in one-tenth of a percent */
7da29f97
MKB
190 v64 = (u64)best_bitrate_error * 1000;
191 do_div(v64, bt->bitrate);
192 bitrate_error = (u32)v64;
193 if (bitrate_error > CAN_CALC_MAX_ERROR) {
aabdfd6a 194 netdev_err(dev,
7da29f97
MKB
195 "bitrate error %d.%d%% too high\n",
196 bitrate_error / 10, bitrate_error % 10);
39549eef 197 return -EDOM;
39549eef 198 }
7da29f97
MKB
199 netdev_warn(dev, "bitrate error %d.%d%%\n",
200 bitrate_error / 10, bitrate_error % 10);
39549eef
WG
201 }
202
203 /* real sample point */
7da29f97
MKB
204 bt->sample_point = can_update_sample_point(btc, sample_point_nominal, best_tseg,
205 &tseg1, &tseg2, NULL);
39549eef 206
7da29f97 207 v64 = (u64)best_brp * 1000 * 1000 * 1000;
39549eef
WG
208 do_div(v64, priv->clock.freq);
209 bt->tq = (u32)v64;
210 bt->prop_seg = tseg1 / 2;
211 bt->phase_seg1 = tseg1 - bt->prop_seg;
212 bt->phase_seg2 = tseg2;
2e114374
OH
213
214 /* check for sjw user settings */
7da29f97 215 if (!bt->sjw || !btc->sjw_max) {
2e114374 216 bt->sjw = 1;
7da29f97 217 } else {
2e114374
OH
218 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
219 if (bt->sjw > btc->sjw_max)
220 bt->sjw = btc->sjw_max;
221 /* bt->sjw must not be higher than tseg2 */
222 if (tseg2 < bt->sjw)
223 bt->sjw = tseg2;
224 }
225
39549eef 226 bt->brp = best_brp;
7da29f97
MKB
227
228 /* real bitrate */
229 bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2));
39549eef
WG
230
231 return 0;
232}
233#else /* !CONFIG_CAN_CALC_BITTIMING */
08da7da4
OH
234static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
235 const struct can_bittiming_const *btc)
39549eef 236{
aabdfd6a 237 netdev_err(dev, "bit-timing calculation not available\n");
39549eef
WG
238 return -EINVAL;
239}
240#endif /* CONFIG_CAN_CALC_BITTIMING */
241
242/*
243 * Checks the validity of the specified bit-timing parameters prop_seg,
244 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
245 * prescaler value brp. You can find more information in the header
246 * file linux/can/netlink.h.
247 */
08da7da4
OH
248static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt,
249 const struct can_bittiming_const *btc)
39549eef
WG
250{
251 struct can_priv *priv = netdev_priv(dev);
39549eef
WG
252 int tseg1, alltseg;
253 u64 brp64;
254
39549eef
WG
255 tseg1 = bt->prop_seg + bt->phase_seg1;
256 if (!bt->sjw)
257 bt->sjw = 1;
258 if (bt->sjw > btc->sjw_max ||
259 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
260 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
261 return -ERANGE;
262
263 brp64 = (u64)priv->clock.freq * (u64)bt->tq;
264 if (btc->brp_inc > 1)
265 do_div(brp64, btc->brp_inc);
266 brp64 += 500000000UL - 1;
267 do_div(brp64, 1000000000UL); /* the practicable BRP */
268 if (btc->brp_inc > 1)
269 brp64 *= btc->brp_inc;
270 bt->brp = (u32)brp64;
271
272 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
273 return -EINVAL;
274
275 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
276 bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
277 bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
278
279 return 0;
280}
281
431af779
MKB
282/* Checks the validity of predefined bitrate settings */
283static int can_validate_bitrate(struct net_device *dev, struct can_bittiming *bt,
284 const u32 *bitrate_const,
285 const unsigned int bitrate_const_cnt)
286{
287 struct can_priv *priv = netdev_priv(dev);
288 unsigned int i;
289
290 for (i = 0; i < bitrate_const_cnt; i++) {
291 if (bt->bitrate == bitrate_const[i])
292 break;
293 }
294
295 if (i >= priv->bitrate_const_cnt)
296 return -EINVAL;
297
298 return 0;
299}
300
08da7da4 301static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
431af779
MKB
302 const struct can_bittiming_const *btc,
303 const u32 *bitrate_const,
304 const unsigned int bitrate_const_cnt)
39549eef 305{
39549eef
WG
306 int err;
307
d5298dff
OH
308 /*
309 * Depending on the given can_bittiming parameter structure the CAN
310 * timing parameters are calculated based on the provided bitrate OR
311 * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
312 * provided directly which are then checked and fixed up.
313 */
431af779 314 if (!bt->tq && bt->bitrate && btc)
08da7da4 315 err = can_calc_bittiming(dev, bt, btc);
431af779 316 else if (bt->tq && !bt->bitrate && btc)
08da7da4 317 err = can_fixup_bittiming(dev, bt, btc);
431af779
MKB
318 else if (!bt->tq && bt->bitrate && bitrate_const)
319 err = can_validate_bitrate(dev, bt, bitrate_const,
320 bitrate_const_cnt);
d5298dff
OH
321 else
322 err = -EINVAL;
39549eef 323
d5298dff 324 return err;
39549eef
WG
325}
326
bac78aab
AY
327static void can_update_state_error_stats(struct net_device *dev,
328 enum can_state new_state)
329{
330 struct can_priv *priv = netdev_priv(dev);
331
332 if (new_state <= priv->state)
333 return;
334
335 switch (new_state) {
336 case CAN_STATE_ERROR_WARNING:
337 priv->can_stats.error_warning++;
338 break;
339 case CAN_STATE_ERROR_PASSIVE:
340 priv->can_stats.error_passive++;
341 break;
342 case CAN_STATE_BUS_OFF:
be38a6f9
AY
343 priv->can_stats.bus_off++;
344 break;
bac78aab
AY
345 default:
346 break;
5b5ba2af 347 }
bac78aab
AY
348}
349
350static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
351{
352 switch (state) {
353 case CAN_STATE_ERROR_ACTIVE:
354 return CAN_ERR_CRTL_ACTIVE;
355 case CAN_STATE_ERROR_WARNING:
356 return CAN_ERR_CRTL_TX_WARNING;
357 case CAN_STATE_ERROR_PASSIVE:
358 return CAN_ERR_CRTL_TX_PASSIVE;
359 default:
360 return 0;
361 }
362}
363
364static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
365{
366 switch (state) {
367 case CAN_STATE_ERROR_ACTIVE:
368 return CAN_ERR_CRTL_ACTIVE;
369 case CAN_STATE_ERROR_WARNING:
370 return CAN_ERR_CRTL_RX_WARNING;
371 case CAN_STATE_ERROR_PASSIVE:
372 return CAN_ERR_CRTL_RX_PASSIVE;
373 default:
374 return 0;
375 }
376}
377
378void can_change_state(struct net_device *dev, struct can_frame *cf,
379 enum can_state tx_state, enum can_state rx_state)
380{
381 struct can_priv *priv = netdev_priv(dev);
382 enum can_state new_state = max(tx_state, rx_state);
383
384 if (unlikely(new_state == priv->state)) {
385 netdev_warn(dev, "%s: oops, state did not change", __func__);
386 return;
387 }
388
389 netdev_dbg(dev, "New error state: %d\n", new_state);
390
391 can_update_state_error_stats(dev, new_state);
392 priv->state = new_state;
393
394 if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
395 cf->can_id |= CAN_ERR_BUSOFF;
396 return;
397 }
398
399 cf->can_id |= CAN_ERR_CRTL;
400 cf->data[1] |= tx_state >= rx_state ?
401 can_tx_state_to_frame(dev, tx_state) : 0;
402 cf->data[1] |= tx_state <= rx_state ?
403 can_rx_state_to_frame(dev, rx_state) : 0;
404}
405EXPORT_SYMBOL_GPL(can_change_state);
406
39549eef
WG
407/*
408 * Local echo of CAN messages
409 *
410 * CAN network devices *should* support a local echo functionality
411 * (see Documentation/networking/can.txt). To test the handling of CAN
412 * interfaces that do not support the local echo both driver types are
413 * implemented. In the case that the driver does not support the echo
414 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
415 * to perform the echo as a fallback solution.
416 */
417static void can_flush_echo_skb(struct net_device *dev)
418{
419 struct can_priv *priv = netdev_priv(dev);
420 struct net_device_stats *stats = &dev->stats;
421 int i;
422
a6e4bc53 423 for (i = 0; i < priv->echo_skb_max; i++) {
39549eef
WG
424 if (priv->echo_skb[i]) {
425 kfree_skb(priv->echo_skb[i]);
426 priv->echo_skb[i] = NULL;
427 stats->tx_dropped++;
428 stats->tx_aborted_errors++;
429 }
430 }
431}
432
433/*
434 * Put the skb on the stack to be looped backed locally lateron
435 *
436 * The function is typically called in the start_xmit function
437 * of the device driver. The driver must protect access to
438 * priv->echo_skb, if necessary.
439 */
a6e4bc53
WG
440void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
441 unsigned int idx)
39549eef
WG
442{
443 struct can_priv *priv = netdev_priv(dev);
444
a6e4bc53
WG
445 BUG_ON(idx >= priv->echo_skb_max);
446
39549eef 447 /* check flag whether this packet has to be looped back */
a94bc9c4
OH
448 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
449 (skb->protocol != htons(ETH_P_CAN) &&
450 skb->protocol != htons(ETH_P_CANFD))) {
39549eef
WG
451 kfree_skb(skb);
452 return;
453 }
454
455 if (!priv->echo_skb[idx]) {
39549eef 456
0ae89beb
OH
457 skb = can_create_echo_skb(skb);
458 if (!skb)
459 return;
39549eef
WG
460
461 /* make settings for echo to reduce code in irq context */
39549eef
WG
462 skb->pkt_type = PACKET_BROADCAST;
463 skb->ip_summed = CHECKSUM_UNNECESSARY;
464 skb->dev = dev;
465
466 /* save this skb for tx interrupt echo handling */
467 priv->echo_skb[idx] = skb;
468 } else {
469 /* locking problem with netif_stop_queue() ?? */
aabdfd6a 470 netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
39549eef
WG
471 kfree_skb(skb);
472 }
473}
474EXPORT_SYMBOL_GPL(can_put_echo_skb);
475
476/*
477 * Get the skb from the stack and loop it back locally
478 *
479 * The function is typically called when the TX done interrupt
480 * is handled in the device driver. The driver must protect
481 * access to priv->echo_skb, if necessary.
482 */
cf5046b3 483unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
39549eef
WG
484{
485 struct can_priv *priv = netdev_priv(dev);
486
a6e4bc53
WG
487 BUG_ON(idx >= priv->echo_skb_max);
488
39e3ab6f 489 if (priv->echo_skb[idx]) {
cf5046b3
MKB
490 struct sk_buff *skb = priv->echo_skb[idx];
491 struct can_frame *cf = (struct can_frame *)skb->data;
492 u8 dlc = cf->can_dlc;
493
39549eef
WG
494 netif_rx(priv->echo_skb[idx]);
495 priv->echo_skb[idx] = NULL;
cf5046b3
MKB
496
497 return dlc;
39549eef 498 }
cf5046b3
MKB
499
500 return 0;
39549eef
WG
501}
502EXPORT_SYMBOL_GPL(can_get_echo_skb);
503
39e3ab6f
WG
504/*
505 * Remove the skb from the stack and free it.
506 *
507 * The function is typically called when TX failed.
508 */
a6e4bc53 509void can_free_echo_skb(struct net_device *dev, unsigned int idx)
39e3ab6f
WG
510{
511 struct can_priv *priv = netdev_priv(dev);
512
a6e4bc53
WG
513 BUG_ON(idx >= priv->echo_skb_max);
514
39e3ab6f 515 if (priv->echo_skb[idx]) {
5247a589 516 dev_kfree_skb_any(priv->echo_skb[idx]);
39e3ab6f
WG
517 priv->echo_skb[idx] = NULL;
518 }
519}
520EXPORT_SYMBOL_GPL(can_free_echo_skb);
521
39549eef
WG
522/*
523 * CAN device restart for bus-off recovery
524 */
9abefcb1 525static void can_restart(struct net_device *dev)
39549eef 526{
39549eef
WG
527 struct can_priv *priv = netdev_priv(dev);
528 struct net_device_stats *stats = &dev->stats;
529 struct sk_buff *skb;
530 struct can_frame *cf;
531 int err;
532
533 BUG_ON(netif_carrier_ok(dev));
534
535 /*
536 * No synchronization needed because the device is bus-off and
537 * no messages can come in or go out.
538 */
539 can_flush_echo_skb(dev);
540
541 /* send restart message upstream */
7b6856a0 542 skb = alloc_can_err_skb(dev, &cf);
39549eef
WG
543 if (skb == NULL) {
544 err = -ENOMEM;
b3d0df7c 545 goto restart;
39549eef 546 }
7b6856a0 547 cf->can_id |= CAN_ERR_RESTARTED;
39549eef
WG
548
549 netif_rx(skb);
550
39549eef
WG
551 stats->rx_packets++;
552 stats->rx_bytes += cf->can_dlc;
553
b3d0df7c 554restart:
aabdfd6a 555 netdev_dbg(dev, "restarted\n");
39549eef
WG
556 priv->can_stats.restarts++;
557
558 /* Now restart the device */
559 err = priv->do_set_mode(dev, CAN_MODE_START);
560
39549eef
WG
561 netif_carrier_on(dev);
562 if (err)
aabdfd6a 563 netdev_err(dev, "Error %d during restart", err);
39549eef
WG
564}
565
9abefcb1
SM
566static void can_restart_work(struct work_struct *work)
567{
568 struct delayed_work *dwork = to_delayed_work(work);
569 struct can_priv *priv = container_of(dwork, struct can_priv, restart_work);
570
571 can_restart(priv->dev);
572}
573
39549eef
WG
574int can_restart_now(struct net_device *dev)
575{
576 struct can_priv *priv = netdev_priv(dev);
577
578 /*
579 * A manual restart is only permitted if automatic restart is
580 * disabled and the device is in the bus-off state
581 */
582 if (priv->restart_ms)
583 return -EINVAL;
584 if (priv->state != CAN_STATE_BUS_OFF)
585 return -EBUSY;
586
9abefcb1
SM
587 cancel_delayed_work_sync(&priv->restart_work);
588 can_restart(dev);
39549eef
WG
589
590 return 0;
591}
592
593/*
594 * CAN bus-off
595 *
596 * This functions should be called when the device goes bus-off to
597 * tell the netif layer that no more packets can be sent or received.
598 * If enabled, a timer is started to trigger bus-off recovery.
599 */
600void can_bus_off(struct net_device *dev)
601{
602 struct can_priv *priv = netdev_priv(dev);
603
aabdfd6a 604 netdev_dbg(dev, "bus-off\n");
39549eef
WG
605
606 netif_carrier_off(dev);
39549eef
WG
607
608 if (priv->restart_ms)
9abefcb1
SM
609 schedule_delayed_work(&priv->restart_work,
610 msecs_to_jiffies(priv->restart_ms));
39549eef
WG
611}
612EXPORT_SYMBOL_GPL(can_bus_off);
613
614static void can_setup(struct net_device *dev)
615{
616 dev->type = ARPHRD_CAN;
1e0625fa 617 dev->mtu = CAN_MTU;
39549eef
WG
618 dev->hard_header_len = 0;
619 dev->addr_len = 0;
620 dev->tx_queue_len = 10;
621
622 /* New-style flags. */
623 dev->flags = IFF_NOARP;
34324dc2 624 dev->features = NETIF_F_HW_CSUM;
39549eef
WG
625}
626
7b6856a0
WG
627struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
628{
629 struct sk_buff *skb;
630
156c2bb9
OH
631 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
632 sizeof(struct can_frame));
7b6856a0
WG
633 if (unlikely(!skb))
634 return NULL;
635
636 skb->protocol = htons(ETH_P_CAN);
637 skb->pkt_type = PACKET_BROADCAST;
638 skb->ip_summed = CHECKSUM_UNNECESSARY;
156c2bb9 639
96943901
OH
640 skb_reset_mac_header(skb);
641 skb_reset_network_header(skb);
642 skb_reset_transport_header(skb);
643
2bf3440d
OH
644 can_skb_reserve(skb);
645 can_skb_prv(skb)->ifindex = dev->ifindex;
d3b58c47 646 can_skb_prv(skb)->skbcnt = 0;
156c2bb9 647
7b6856a0
WG
648 *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
649 memset(*cf, 0, sizeof(struct can_frame));
650
651 return skb;
652}
653EXPORT_SYMBOL_GPL(alloc_can_skb);
654
cb2518ca
SG
655struct sk_buff *alloc_canfd_skb(struct net_device *dev,
656 struct canfd_frame **cfd)
657{
658 struct sk_buff *skb;
659
660 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
661 sizeof(struct canfd_frame));
662 if (unlikely(!skb))
663 return NULL;
664
665 skb->protocol = htons(ETH_P_CANFD);
666 skb->pkt_type = PACKET_BROADCAST;
667 skb->ip_summed = CHECKSUM_UNNECESSARY;
668
96943901
OH
669 skb_reset_mac_header(skb);
670 skb_reset_network_header(skb);
671 skb_reset_transport_header(skb);
672
cb2518ca
SG
673 can_skb_reserve(skb);
674 can_skb_prv(skb)->ifindex = dev->ifindex;
d3b58c47 675 can_skb_prv(skb)->skbcnt = 0;
cb2518ca
SG
676
677 *cfd = (struct canfd_frame *)skb_put(skb, sizeof(struct canfd_frame));
678 memset(*cfd, 0, sizeof(struct canfd_frame));
679
680 return skb;
681}
682EXPORT_SYMBOL_GPL(alloc_canfd_skb);
683
7b6856a0
WG
684struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
685{
686 struct sk_buff *skb;
687
688 skb = alloc_can_skb(dev, cf);
689 if (unlikely(!skb))
690 return NULL;
691
692 (*cf)->can_id = CAN_ERR_FLAG;
693 (*cf)->can_dlc = CAN_ERR_DLC;
694
695 return skb;
696}
697EXPORT_SYMBOL_GPL(alloc_can_err_skb);
698
39549eef
WG
699/*
700 * Allocate and setup space for the CAN network device
701 */
a6e4bc53 702struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
39549eef
WG
703{
704 struct net_device *dev;
705 struct can_priv *priv;
a6e4bc53 706 int size;
39549eef 707
a6e4bc53
WG
708 if (echo_skb_max)
709 size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
710 echo_skb_max * sizeof(struct sk_buff *);
711 else
712 size = sizeof_priv;
713
c835a677 714 dev = alloc_netdev(size, "can%d", NET_NAME_UNKNOWN, can_setup);
39549eef
WG
715 if (!dev)
716 return NULL;
717
718 priv = netdev_priv(dev);
9abefcb1 719 priv->dev = dev;
39549eef 720
a6e4bc53
WG
721 if (echo_skb_max) {
722 priv->echo_skb_max = echo_skb_max;
723 priv->echo_skb = (void *)priv +
724 ALIGN(sizeof_priv, sizeof(struct sk_buff *));
725 }
726
39549eef
WG
727 priv->state = CAN_STATE_STOPPED;
728
9abefcb1 729 INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
39549eef
WG
730
731 return dev;
732}
733EXPORT_SYMBOL_GPL(alloc_candev);
734
735/*
736 * Free space of the CAN network device
737 */
738void free_candev(struct net_device *dev)
739{
740 free_netdev(dev);
741}
742EXPORT_SYMBOL_GPL(free_candev);
743
bc05a894
OH
744/*
745 * changing MTU and control mode for CAN/CANFD devices
746 */
747int can_change_mtu(struct net_device *dev, int new_mtu)
748{
749 struct can_priv *priv = netdev_priv(dev);
750
751 /* Do not allow changing the MTU while running */
752 if (dev->flags & IFF_UP)
753 return -EBUSY;
754
755 /* allow change of MTU according to the CANFD ability of the device */
756 switch (new_mtu) {
757 case CAN_MTU:
bb208f14
OH
758 /* 'CANFD-only' controllers can not switch to CAN_MTU */
759 if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
760 return -EINVAL;
761
bc05a894
OH
762 priv->ctrlmode &= ~CAN_CTRLMODE_FD;
763 break;
764
765 case CANFD_MTU:
bb208f14
OH
766 /* check for potential CANFD ability */
767 if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
768 !(priv->ctrlmode_static & CAN_CTRLMODE_FD))
bc05a894
OH
769 return -EINVAL;
770
771 priv->ctrlmode |= CAN_CTRLMODE_FD;
772 break;
773
774 default:
775 return -EINVAL;
776 }
777
778 dev->mtu = new_mtu;
779 return 0;
780}
781EXPORT_SYMBOL_GPL(can_change_mtu);
782
39549eef
WG
783/*
784 * Common open function when the device gets opened.
785 *
786 * This function should be called in the open function of the device
787 * driver.
788 */
789int open_candev(struct net_device *dev)
790{
791 struct can_priv *priv = netdev_priv(dev);
792
b30749fd 793 if (!priv->bittiming.bitrate) {
aabdfd6a 794 netdev_err(dev, "bit-timing not yet defined\n");
39549eef
WG
795 return -EINVAL;
796 }
797
dd22586d
OH
798 /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
799 if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
800 (!priv->data_bittiming.bitrate ||
801 (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) {
802 netdev_err(dev, "incorrect/missing data bit-timing\n");
803 return -EINVAL;
804 }
805
1b0d9224
WG
806 /* Switch carrier on if device was stopped while in bus-off state */
807 if (!netif_carrier_ok(dev))
808 netif_carrier_on(dev);
809
39549eef
WG
810 return 0;
811}
128ced8f 812EXPORT_SYMBOL_GPL(open_candev);
39549eef
WG
813
814/*
815 * Common close function for cleanup before the device gets closed.
816 *
817 * This function should be called in the close function of the device
818 * driver.
819 */
820void close_candev(struct net_device *dev)
821{
822 struct can_priv *priv = netdev_priv(dev);
823
9abefcb1 824 cancel_delayed_work_sync(&priv->restart_work);
39549eef
WG
825 can_flush_echo_skb(dev);
826}
827EXPORT_SYMBOL_GPL(close_candev);
828
829/*
830 * CAN netlink interface
831 */
832static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
833 [IFLA_CAN_STATE] = { .type = NLA_U32 },
834 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
835 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
836 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
837 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
838 [IFLA_CAN_BITTIMING_CONST]
839 = { .len = sizeof(struct can_bittiming_const) },
840 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
52c793f2 841 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
9859ccd2
OH
842 [IFLA_CAN_DATA_BITTIMING]
843 = { .len = sizeof(struct can_bittiming) },
844 [IFLA_CAN_DATA_BITTIMING_CONST]
845 = { .len = sizeof(struct can_bittiming_const) },
39549eef
WG
846};
847
bb208f14
OH
848static int can_validate(struct nlattr *tb[], struct nlattr *data[])
849{
850 bool is_can_fd = false;
851
852 /* Make sure that valid CAN FD configurations always consist of
853 * - nominal/arbitration bittiming
854 * - data bittiming
855 * - control mode with CAN_CTRLMODE_FD set
856 */
857
bce271f2
OH
858 if (!data)
859 return 0;
860
bb208f14
OH
861 if (data[IFLA_CAN_CTRLMODE]) {
862 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
863
864 is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
865 }
866
867 if (is_can_fd) {
868 if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
869 return -EOPNOTSUPP;
870 }
871
872 if (data[IFLA_CAN_DATA_BITTIMING]) {
873 if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
874 return -EOPNOTSUPP;
875 }
876
877 return 0;
878}
879
39549eef
WG
880static int can_changelink(struct net_device *dev,
881 struct nlattr *tb[], struct nlattr *data[])
882{
883 struct can_priv *priv = netdev_priv(dev);
884 int err;
885
886 /* We need synchronization with dev->stop() */
887 ASSERT_RTNL();
888
39549eef
WG
889 if (data[IFLA_CAN_BITTIMING]) {
890 struct can_bittiming bt;
891
892 /* Do not allow changing bittiming while running */
893 if (dev->flags & IFF_UP)
894 return -EBUSY;
c3606d43
MKB
895
896 /* Calculate bittiming parameters based on
897 * bittiming_const if set, otherwise pass bitrate
898 * directly via do_set_bitrate(). Bail out if neither
899 * is given.
900 */
901 if (!priv->bittiming_const && !priv->do_set_bittiming)
902 return -EOPNOTSUPP;
903
39549eef 904 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
431af779
MKB
905 err = can_get_bittiming(dev, &bt,
906 priv->bittiming_const,
907 priv->bitrate_const,
908 priv->bitrate_const_cnt);
909 if (err)
910 return err;
39549eef
WG
911 memcpy(&priv->bittiming, &bt, sizeof(bt));
912
913 if (priv->do_set_bittiming) {
914 /* Finally, set the bit-timing registers */
915 err = priv->do_set_bittiming(dev);
916 if (err)
917 return err;
918 }
919 }
920
49cb5c0e
MKB
921 if (data[IFLA_CAN_CTRLMODE]) {
922 struct can_ctrlmode *cm;
bb208f14
OH
923 u32 ctrlstatic;
924 u32 maskedflags;
49cb5c0e
MKB
925
926 /* Do not allow changing controller mode while running */
927 if (dev->flags & IFF_UP)
928 return -EBUSY;
929 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
bb208f14
OH
930 ctrlstatic = priv->ctrlmode_static;
931 maskedflags = cm->flags & cm->mask;
932
933 /* check whether provided bits are allowed to be passed */
934 if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic))
935 return -EOPNOTSUPP;
936
937 /* do not check for static fd-non-iso if 'fd' is disabled */
938 if (!(maskedflags & CAN_CTRLMODE_FD))
939 ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
9b1087aa 940
bb208f14
OH
941 /* make sure static options are provided by configuration */
942 if ((maskedflags & ctrlstatic) != ctrlstatic)
49cb5c0e 943 return -EOPNOTSUPP;
9b1087aa
OH
944
945 /* clear bits to be modified and copy the flag values */
49cb5c0e 946 priv->ctrlmode &= ~cm->mask;
bb208f14 947 priv->ctrlmode |= maskedflags;
bc05a894
OH
948
949 /* CAN_CTRLMODE_FD can only be set when driver supports FD */
950 if (priv->ctrlmode & CAN_CTRLMODE_FD)
951 dev->mtu = CANFD_MTU;
952 else
953 dev->mtu = CAN_MTU;
49cb5c0e
MKB
954 }
955
39549eef
WG
956 if (data[IFLA_CAN_RESTART_MS]) {
957 /* Do not allow changing restart delay while running */
958 if (dev->flags & IFF_UP)
959 return -EBUSY;
960 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
961 }
962
963 if (data[IFLA_CAN_RESTART]) {
964 /* Do not allow a restart while not running */
965 if (!(dev->flags & IFF_UP))
966 return -EINVAL;
967 err = can_restart_now(dev);
968 if (err)
969 return err;
970 }
971
9859ccd2
OH
972 if (data[IFLA_CAN_DATA_BITTIMING]) {
973 struct can_bittiming dbt;
974
975 /* Do not allow changing bittiming while running */
976 if (dev->flags & IFF_UP)
977 return -EBUSY;
c3606d43
MKB
978
979 /* Calculate bittiming parameters based on
980 * data_bittiming_const if set, otherwise pass bitrate
981 * directly via do_set_bitrate(). Bail out if neither
982 * is given.
983 */
984 if (!priv->data_bittiming_const && !priv->do_set_data_bittiming)
985 return -EOPNOTSUPP;
986
9859ccd2
OH
987 memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
988 sizeof(dbt));
431af779
MKB
989 err = can_get_bittiming(dev, &dbt,
990 priv->data_bittiming_const,
991 priv->data_bitrate_const,
992 priv->data_bitrate_const_cnt);
993 if (err)
994 return err;
9859ccd2
OH
995 memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
996
997 if (priv->do_set_data_bittiming) {
998 /* Finally, set the bit-timing registers */
999 err = priv->do_set_data_bittiming(dev);
1000 if (err)
1001 return err;
1002 }
1003 }
1004
12a6075c
OH
1005 if (data[IFLA_CAN_TERMINATION]) {
1006 const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
1007 const unsigned int num_term = priv->termination_const_cnt;
1008 unsigned int i;
1009
1010 if (!priv->do_set_termination)
1011 return -EOPNOTSUPP;
1012
1013 /* check whether given value is supported by the interface */
1014 for (i = 0; i < num_term; i++) {
1015 if (termval == priv->termination_const[i])
1016 break;
1017 }
1018 if (i >= num_term)
1019 return -EINVAL;
1020
1021 /* Finally, set the termination value */
1022 err = priv->do_set_termination(dev, termval);
1023 if (err)
1024 return err;
1025
1026 priv->termination = termval;
1027 }
1028
39549eef
WG
1029 return 0;
1030}
1031
53a0ef86
WG
1032static size_t can_get_size(const struct net_device *dev)
1033{
1034 struct can_priv *priv = netdev_priv(dev);
c13c64d8
MKB
1035 size_t size = 0;
1036
b30749fd
OH
1037 if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
1038 size += nla_total_size(sizeof(struct can_bittiming));
c13c64d8 1039 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
fe119a05 1040 size += nla_total_size(sizeof(struct can_bittiming_const));
c13c64d8
MKB
1041 size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
1042 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
1043 size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
1044 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
1045 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
1046 size += nla_total_size(sizeof(struct can_berr_counter));
9859ccd2
OH
1047 if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
1048 size += nla_total_size(sizeof(struct can_bittiming));
1049 if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
1050 size += nla_total_size(sizeof(struct can_bittiming_const));
12a6075c
OH
1051 if (priv->termination_const) {
1052 size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */
1053 size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */
1054 priv->termination_const_cnt);
1055 }
431af779
MKB
1056 if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */
1057 size += nla_total_size(sizeof(*priv->bitrate_const) *
1058 priv->bitrate_const_cnt);
1059 if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */
1060 size += nla_total_size(sizeof(*priv->data_bitrate_const) *
1061 priv->data_bitrate_const_cnt);
53a0ef86
WG
1062
1063 return size;
1064}
1065
39549eef
WG
1066static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
1067{
1068 struct can_priv *priv = netdev_priv(dev);
1069 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
52c793f2 1070 struct can_berr_counter bec;
39549eef
WG
1071 enum can_state state = priv->state;
1072
1073 if (priv->do_get_state)
1074 priv->do_get_state(dev, &state);
9859ccd2 1075
b30749fd
OH
1076 if ((priv->bittiming.bitrate &&
1077 nla_put(skb, IFLA_CAN_BITTIMING,
1078 sizeof(priv->bittiming), &priv->bittiming)) ||
9859ccd2 1079
57a59b9e
MKB
1080 (priv->bittiming_const &&
1081 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
1082 sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
9859ccd2 1083
562b103a 1084 nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
57a59b9e
MKB
1085 nla_put_u32(skb, IFLA_CAN_STATE, state) ||
1086 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
1087 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
9859ccd2 1088
31e0e328
DM
1089 (priv->do_get_berr_counter &&
1090 !priv->do_get_berr_counter(dev, &bec) &&
9859ccd2
OH
1091 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
1092
1093 (priv->data_bittiming.bitrate &&
1094 nla_put(skb, IFLA_CAN_DATA_BITTIMING,
1095 sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
1096
1097 (priv->data_bittiming_const &&
1098 nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
1099 sizeof(*priv->data_bittiming_const),
12a6075c
OH
1100 priv->data_bittiming_const)) ||
1101
1102 (priv->termination_const &&
1103 (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
1104 nla_put(skb, IFLA_CAN_TERMINATION_CONST,
1105 sizeof(*priv->termination_const) *
1106 priv->termination_const_cnt,
431af779
MKB
1107 priv->termination_const))) ||
1108
1109 (priv->bitrate_const &&
1110 nla_put(skb, IFLA_CAN_BITRATE_CONST,
1111 sizeof(*priv->bitrate_const) *
1112 priv->bitrate_const_cnt,
1113 priv->bitrate_const)) ||
1114
1115 (priv->data_bitrate_const &&
1116 nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
1117 sizeof(*priv->data_bitrate_const) *
1118 priv->data_bitrate_const_cnt,
1119 priv->data_bitrate_const))
1120 )
12a6075c 1121
57a59b9e 1122 return -EMSGSIZE;
9859ccd2 1123
39549eef 1124 return 0;
39549eef
WG
1125}
1126
55369c0a
WG
1127static size_t can_get_xstats_size(const struct net_device *dev)
1128{
1129 return sizeof(struct can_device_stats);
1130}
1131
39549eef
WG
1132static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
1133{
1134 struct can_priv *priv = netdev_priv(dev);
1135
31e0e328
DM
1136 if (nla_put(skb, IFLA_INFO_XSTATS,
1137 sizeof(priv->can_stats), &priv->can_stats))
1138 goto nla_put_failure;
39549eef
WG
1139 return 0;
1140
1141nla_put_failure:
1142 return -EMSGSIZE;
1143}
1144
81adee47 1145static int can_newlink(struct net *src_net, struct net_device *dev,
993e6f2f
OH
1146 struct nlattr *tb[], struct nlattr *data[])
1147{
1148 return -EOPNOTSUPP;
1149}
1150
25e1ed6e
OH
1151static void can_dellink(struct net_device *dev, struct list_head *head)
1152{
1153 return;
1154}
1155
39549eef
WG
1156static struct rtnl_link_ops can_link_ops __read_mostly = {
1157 .kind = "can",
1158 .maxtype = IFLA_CAN_MAX,
1159 .policy = can_policy,
1160 .setup = can_setup,
bb208f14 1161 .validate = can_validate,
993e6f2f 1162 .newlink = can_newlink,
39549eef 1163 .changelink = can_changelink,
25e1ed6e 1164 .dellink = can_dellink,
53a0ef86 1165 .get_size = can_get_size,
39549eef 1166 .fill_info = can_fill_info,
55369c0a 1167 .get_xstats_size = can_get_xstats_size,
39549eef
WG
1168 .fill_xstats = can_fill_xstats,
1169};
1170
1171/*
1172 * Register the CAN network device
1173 */
1174int register_candev(struct net_device *dev)
1175{
12a6075c
OH
1176 struct can_priv *priv = netdev_priv(dev);
1177
1178 /* Ensure termination_const, termination_const_cnt and
1179 * do_set_termination consistency. All must be either set or
1180 * unset.
1181 */
1182 if ((!priv->termination_const != !priv->termination_const_cnt) ||
1183 (!priv->termination_const != !priv->do_set_termination))
1184 return -EINVAL;
1185
431af779
MKB
1186 if (!priv->bitrate_const != !priv->bitrate_const_cnt)
1187 return -EINVAL;
1188
1189 if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
1190 return -EINVAL;
1191
39549eef
WG
1192 dev->rtnl_link_ops = &can_link_ops;
1193 return register_netdev(dev);
1194}
1195EXPORT_SYMBOL_GPL(register_candev);
1196
1197/*
1198 * Unregister the CAN network device
1199 */
1200void unregister_candev(struct net_device *dev)
1201{
1202 unregister_netdev(dev);
1203}
1204EXPORT_SYMBOL_GPL(unregister_candev);
1205
bf03a537
KVD
1206/*
1207 * Test if a network device is a candev based device
1208 * and return the can_priv* if so.
1209 */
1210struct can_priv *safe_candev_priv(struct net_device *dev)
1211{
1212 if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
1213 return NULL;
1214
1215 return netdev_priv(dev);
1216}
1217EXPORT_SYMBOL_GPL(safe_candev_priv);
1218
39549eef
WG
1219static __init int can_dev_init(void)
1220{
1221 int err;
1222
a1ef7bd9
KVD
1223 can_led_notifier_init();
1224
39549eef
WG
1225 err = rtnl_link_register(&can_link_ops);
1226 if (!err)
1227 printk(KERN_INFO MOD_DESC "\n");
1228
1229 return err;
1230}
1231module_init(can_dev_init);
1232
1233static __exit void can_dev_exit(void)
1234{
1235 rtnl_link_unregister(&can_link_ops);
a1ef7bd9
KVD
1236
1237 can_led_notifier_exit();
39549eef
WG
1238}
1239module_exit(can_dev_exit);
1240
1241MODULE_ALIAS_RTNL_LINK("can");