Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 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 |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/if_arp.h> |
| 25 | #include <linux/can.h> |
| 26 | #include <linux/can/dev.h> |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 27 | #include <linux/can/skb.h> |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 28 | #include <linux/can/netlink.h> |
Kurt Van Dijck | a1ef7bd | 2012-12-18 18:50:57 +0100 | [diff] [blame] | 29 | #include <linux/can/led.h> |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 30 | #include <net/rtnetlink.h> |
| 31 | |
| 32 | #define MOD_DESC "CAN device driver interface" |
| 33 | |
| 34 | MODULE_DESCRIPTION(MOD_DESC); |
| 35 | MODULE_LICENSE("GPL v2"); |
| 36 | MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>"); |
| 37 | |
Oliver Hartkopp | 1e0625f | 2012-06-13 20:48:21 +0200 | [diff] [blame] | 38 | /* CAN DLC to real data length conversion helpers */ |
| 39 | |
| 40 | static 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 */ |
| 44 | u8 can_dlc2len(u8 can_dlc) |
| 45 | { |
| 46 | return dlc2len[can_dlc & 0x0F]; |
| 47 | } |
| 48 | EXPORT_SYMBOL_GPL(can_dlc2len); |
| 49 | |
| 50 | static 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 */ |
| 62 | u8 can_len2dlc(u8 len) |
| 63 | { |
| 64 | if (unlikely(len > 64)) |
| 65 | return 0xF; |
| 66 | |
| 67 | return len2dlc[len]; |
| 68 | } |
| 69 | EXPORT_SYMBOL_GPL(can_len2dlc); |
| 70 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 71 | #ifdef CONFIG_CAN_CALC_BITTIMING |
| 72 | #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */ |
| 73 | |
| 74 | /* |
| 75 | * Bit-timing calculation derived from: |
| 76 | * |
| 77 | * Code based on LinCAN sources and H8S2638 project |
| 78 | * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz |
| 79 | * Copyright 2005 Stanislav Marek |
| 80 | * email: pisa@cmp.felk.cvut.cz |
| 81 | * |
| 82 | * Calculates proper bit-timing parameters for a specified bit-rate |
| 83 | * and sample-point, which can then be used to set the bit-timing |
| 84 | * registers of the CAN controller. You can find more information |
| 85 | * in the header file linux/can/netlink.h. |
| 86 | */ |
| 87 | static int can_update_spt(const struct can_bittiming_const *btc, |
| 88 | int sampl_pt, int tseg, int *tseg1, int *tseg2) |
| 89 | { |
| 90 | *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000; |
| 91 | if (*tseg2 < btc->tseg2_min) |
| 92 | *tseg2 = btc->tseg2_min; |
| 93 | if (*tseg2 > btc->tseg2_max) |
| 94 | *tseg2 = btc->tseg2_max; |
| 95 | *tseg1 = tseg - *tseg2; |
| 96 | if (*tseg1 > btc->tseg1_max) { |
| 97 | *tseg1 = btc->tseg1_max; |
| 98 | *tseg2 = tseg - *tseg1; |
| 99 | } |
| 100 | return 1000 * (tseg + 1 - *tseg2) / (tseg + 1); |
| 101 | } |
| 102 | |
| 103 | static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt) |
| 104 | { |
| 105 | struct can_priv *priv = netdev_priv(dev); |
| 106 | const struct can_bittiming_const *btc = priv->bittiming_const; |
| 107 | long rate, best_rate = 0; |
| 108 | long best_error = 1000000000, error = 0; |
| 109 | int best_tseg = 0, best_brp = 0, brp = 0; |
| 110 | int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0; |
| 111 | int spt_error = 1000, spt = 0, sampl_pt; |
| 112 | u64 v64; |
| 113 | |
| 114 | if (!priv->bittiming_const) |
| 115 | return -ENOTSUPP; |
| 116 | |
| 117 | /* Use CIA recommended sample points */ |
| 118 | if (bt->sample_point) { |
| 119 | sampl_pt = bt->sample_point; |
| 120 | } else { |
| 121 | if (bt->bitrate > 800000) |
| 122 | sampl_pt = 750; |
| 123 | else if (bt->bitrate > 500000) |
| 124 | sampl_pt = 800; |
| 125 | else |
| 126 | sampl_pt = 875; |
| 127 | } |
| 128 | |
| 129 | /* tseg even = round down, odd = round up */ |
| 130 | for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1; |
| 131 | tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) { |
| 132 | tsegall = 1 + tseg / 2; |
| 133 | /* Compute all possible tseg choices (tseg=tseg1+tseg2) */ |
| 134 | brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2; |
| 135 | /* chose brp step which is possible in system */ |
| 136 | brp = (brp / btc->brp_inc) * btc->brp_inc; |
| 137 | if ((brp < btc->brp_min) || (brp > btc->brp_max)) |
| 138 | continue; |
| 139 | rate = priv->clock.freq / (brp * tsegall); |
| 140 | error = bt->bitrate - rate; |
| 141 | /* tseg brp biterror */ |
| 142 | if (error < 0) |
| 143 | error = -error; |
| 144 | if (error > best_error) |
| 145 | continue; |
| 146 | best_error = error; |
| 147 | if (error == 0) { |
| 148 | spt = can_update_spt(btc, sampl_pt, tseg / 2, |
| 149 | &tseg1, &tseg2); |
| 150 | error = sampl_pt - spt; |
| 151 | if (error < 0) |
| 152 | error = -error; |
| 153 | if (error > spt_error) |
| 154 | continue; |
| 155 | spt_error = error; |
| 156 | } |
| 157 | best_tseg = tseg / 2; |
| 158 | best_brp = brp; |
| 159 | best_rate = rate; |
| 160 | if (error == 0) |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | if (best_error) { |
| 165 | /* Error in one-tenth of a percent */ |
| 166 | error = (best_error * 1000) / bt->bitrate; |
| 167 | if (error > CAN_CALC_MAX_ERROR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 168 | netdev_err(dev, |
| 169 | "bitrate error %ld.%ld%% too high\n", |
| 170 | error / 10, error % 10); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 171 | return -EDOM; |
| 172 | } else { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 173 | netdev_warn(dev, "bitrate error %ld.%ld%%\n", |
| 174 | error / 10, error % 10); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | /* real sample point */ |
| 179 | bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg, |
| 180 | &tseg1, &tseg2); |
| 181 | |
| 182 | v64 = (u64)best_brp * 1000000000UL; |
| 183 | do_div(v64, priv->clock.freq); |
| 184 | bt->tq = (u32)v64; |
| 185 | bt->prop_seg = tseg1 / 2; |
| 186 | bt->phase_seg1 = tseg1 - bt->prop_seg; |
| 187 | bt->phase_seg2 = tseg2; |
Oliver Hartkopp | 2e11437 | 2011-09-28 02:50:11 +0000 | [diff] [blame] | 188 | |
| 189 | /* check for sjw user settings */ |
| 190 | if (!bt->sjw || !btc->sjw_max) |
| 191 | bt->sjw = 1; |
| 192 | else { |
| 193 | /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */ |
| 194 | if (bt->sjw > btc->sjw_max) |
| 195 | bt->sjw = btc->sjw_max; |
| 196 | /* bt->sjw must not be higher than tseg2 */ |
| 197 | if (tseg2 < bt->sjw) |
| 198 | bt->sjw = tseg2; |
| 199 | } |
| 200 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 201 | bt->brp = best_brp; |
| 202 | /* real bit-rate */ |
| 203 | bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1)); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | #else /* !CONFIG_CAN_CALC_BITTIMING */ |
| 208 | static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt) |
| 209 | { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 210 | netdev_err(dev, "bit-timing calculation not available\n"); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 211 | return -EINVAL; |
| 212 | } |
| 213 | #endif /* CONFIG_CAN_CALC_BITTIMING */ |
| 214 | |
| 215 | /* |
| 216 | * Checks the validity of the specified bit-timing parameters prop_seg, |
| 217 | * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate |
| 218 | * prescaler value brp. You can find more information in the header |
| 219 | * file linux/can/netlink.h. |
| 220 | */ |
| 221 | static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt) |
| 222 | { |
| 223 | struct can_priv *priv = netdev_priv(dev); |
| 224 | const struct can_bittiming_const *btc = priv->bittiming_const; |
| 225 | int tseg1, alltseg; |
| 226 | u64 brp64; |
| 227 | |
| 228 | if (!priv->bittiming_const) |
| 229 | return -ENOTSUPP; |
| 230 | |
| 231 | tseg1 = bt->prop_seg + bt->phase_seg1; |
| 232 | if (!bt->sjw) |
| 233 | bt->sjw = 1; |
| 234 | if (bt->sjw > btc->sjw_max || |
| 235 | tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max || |
| 236 | bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max) |
| 237 | return -ERANGE; |
| 238 | |
| 239 | brp64 = (u64)priv->clock.freq * (u64)bt->tq; |
| 240 | if (btc->brp_inc > 1) |
| 241 | do_div(brp64, btc->brp_inc); |
| 242 | brp64 += 500000000UL - 1; |
| 243 | do_div(brp64, 1000000000UL); /* the practicable BRP */ |
| 244 | if (btc->brp_inc > 1) |
| 245 | brp64 *= btc->brp_inc; |
| 246 | bt->brp = (u32)brp64; |
| 247 | |
| 248 | if (bt->brp < btc->brp_min || bt->brp > btc->brp_max) |
| 249 | return -EINVAL; |
| 250 | |
| 251 | alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1; |
| 252 | bt->bitrate = priv->clock.freq / (bt->brp * alltseg); |
| 253 | bt->sample_point = ((tseg1 + 1) * 1000) / alltseg; |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
Thadeu Lima de Souza Cascardo | 61463a3 | 2011-07-21 16:22:31 +0000 | [diff] [blame] | 258 | static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 259 | { |
| 260 | struct can_priv *priv = netdev_priv(dev); |
| 261 | int err; |
| 262 | |
| 263 | /* Check if the CAN device has bit-timing parameters */ |
| 264 | if (priv->bittiming_const) { |
| 265 | |
| 266 | /* Non-expert mode? Check if the bitrate has been pre-defined */ |
| 267 | if (!bt->tq) |
| 268 | /* Determine bit-timing parameters */ |
| 269 | err = can_calc_bittiming(dev, bt); |
| 270 | else |
| 271 | /* Check bit-timing params and calculate proper brp */ |
| 272 | err = can_fixup_bittiming(dev, bt); |
| 273 | if (err) |
| 274 | return err; |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Local echo of CAN messages |
| 282 | * |
| 283 | * CAN network devices *should* support a local echo functionality |
| 284 | * (see Documentation/networking/can.txt). To test the handling of CAN |
| 285 | * interfaces that do not support the local echo both driver types are |
| 286 | * implemented. In the case that the driver does not support the echo |
| 287 | * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core |
| 288 | * to perform the echo as a fallback solution. |
| 289 | */ |
| 290 | static void can_flush_echo_skb(struct net_device *dev) |
| 291 | { |
| 292 | struct can_priv *priv = netdev_priv(dev); |
| 293 | struct net_device_stats *stats = &dev->stats; |
| 294 | int i; |
| 295 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 296 | for (i = 0; i < priv->echo_skb_max; i++) { |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 297 | if (priv->echo_skb[i]) { |
| 298 | kfree_skb(priv->echo_skb[i]); |
| 299 | priv->echo_skb[i] = NULL; |
| 300 | stats->tx_dropped++; |
| 301 | stats->tx_aborted_errors++; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Put the skb on the stack to be looped backed locally lateron |
| 308 | * |
| 309 | * The function is typically called in the start_xmit function |
| 310 | * of the device driver. The driver must protect access to |
| 311 | * priv->echo_skb, if necessary. |
| 312 | */ |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 313 | void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, |
| 314 | unsigned int idx) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 315 | { |
| 316 | struct can_priv *priv = netdev_priv(dev); |
| 317 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 318 | BUG_ON(idx >= priv->echo_skb_max); |
| 319 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 320 | /* check flag whether this packet has to be looped back */ |
| 321 | if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK) { |
| 322 | kfree_skb(skb); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | if (!priv->echo_skb[idx]) { |
| 327 | struct sock *srcsk = skb->sk; |
| 328 | |
| 329 | if (atomic_read(&skb->users) != 1) { |
| 330 | struct sk_buff *old_skb = skb; |
| 331 | |
| 332 | skb = skb_clone(old_skb, GFP_ATOMIC); |
| 333 | kfree_skb(old_skb); |
| 334 | if (!skb) |
| 335 | return; |
| 336 | } else |
| 337 | skb_orphan(skb); |
| 338 | |
| 339 | skb->sk = srcsk; |
| 340 | |
| 341 | /* make settings for echo to reduce code in irq context */ |
| 342 | skb->protocol = htons(ETH_P_CAN); |
| 343 | skb->pkt_type = PACKET_BROADCAST; |
| 344 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 345 | skb->dev = dev; |
| 346 | |
| 347 | /* save this skb for tx interrupt echo handling */ |
| 348 | priv->echo_skb[idx] = skb; |
| 349 | } else { |
| 350 | /* locking problem with netif_stop_queue() ?? */ |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 351 | netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 352 | kfree_skb(skb); |
| 353 | } |
| 354 | } |
| 355 | EXPORT_SYMBOL_GPL(can_put_echo_skb); |
| 356 | |
| 357 | /* |
| 358 | * Get the skb from the stack and loop it back locally |
| 359 | * |
| 360 | * The function is typically called when the TX done interrupt |
| 361 | * is handled in the device driver. The driver must protect |
| 362 | * access to priv->echo_skb, if necessary. |
| 363 | */ |
Marc Kleine-Budde | cf5046b | 2011-10-10 23:43:53 +0200 | [diff] [blame] | 364 | unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 365 | { |
| 366 | struct can_priv *priv = netdev_priv(dev); |
| 367 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 368 | BUG_ON(idx >= priv->echo_skb_max); |
| 369 | |
Wolfgang Grandegger | 39e3ab6 | 2009-09-01 05:26:12 +0000 | [diff] [blame] | 370 | if (priv->echo_skb[idx]) { |
Marc Kleine-Budde | cf5046b | 2011-10-10 23:43:53 +0200 | [diff] [blame] | 371 | struct sk_buff *skb = priv->echo_skb[idx]; |
| 372 | struct can_frame *cf = (struct can_frame *)skb->data; |
| 373 | u8 dlc = cf->can_dlc; |
| 374 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 375 | netif_rx(priv->echo_skb[idx]); |
| 376 | priv->echo_skb[idx] = NULL; |
Marc Kleine-Budde | cf5046b | 2011-10-10 23:43:53 +0200 | [diff] [blame] | 377 | |
| 378 | return dlc; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 379 | } |
Marc Kleine-Budde | cf5046b | 2011-10-10 23:43:53 +0200 | [diff] [blame] | 380 | |
| 381 | return 0; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 382 | } |
| 383 | EXPORT_SYMBOL_GPL(can_get_echo_skb); |
| 384 | |
| 385 | /* |
Wolfgang Grandegger | 39e3ab6 | 2009-09-01 05:26:12 +0000 | [diff] [blame] | 386 | * Remove the skb from the stack and free it. |
| 387 | * |
| 388 | * The function is typically called when TX failed. |
| 389 | */ |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 390 | void can_free_echo_skb(struct net_device *dev, unsigned int idx) |
Wolfgang Grandegger | 39e3ab6 | 2009-09-01 05:26:12 +0000 | [diff] [blame] | 391 | { |
| 392 | struct can_priv *priv = netdev_priv(dev); |
| 393 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 394 | BUG_ON(idx >= priv->echo_skb_max); |
| 395 | |
Wolfgang Grandegger | 39e3ab6 | 2009-09-01 05:26:12 +0000 | [diff] [blame] | 396 | if (priv->echo_skb[idx]) { |
| 397 | kfree_skb(priv->echo_skb[idx]); |
| 398 | priv->echo_skb[idx] = NULL; |
| 399 | } |
| 400 | } |
| 401 | EXPORT_SYMBOL_GPL(can_free_echo_skb); |
| 402 | |
| 403 | /* |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 404 | * CAN device restart for bus-off recovery |
| 405 | */ |
Marc Kleine-Budde | 77fc95a | 2012-02-06 22:21:13 +0100 | [diff] [blame] | 406 | static void can_restart(unsigned long data) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 407 | { |
| 408 | struct net_device *dev = (struct net_device *)data; |
| 409 | struct can_priv *priv = netdev_priv(dev); |
| 410 | struct net_device_stats *stats = &dev->stats; |
| 411 | struct sk_buff *skb; |
| 412 | struct can_frame *cf; |
| 413 | int err; |
| 414 | |
| 415 | BUG_ON(netif_carrier_ok(dev)); |
| 416 | |
| 417 | /* |
| 418 | * No synchronization needed because the device is bus-off and |
| 419 | * no messages can come in or go out. |
| 420 | */ |
| 421 | can_flush_echo_skb(dev); |
| 422 | |
| 423 | /* send restart message upstream */ |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 424 | skb = alloc_can_err_skb(dev, &cf); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 425 | if (skb == NULL) { |
| 426 | err = -ENOMEM; |
Wolfgang Grandegger | b3d0df7 | 2009-07-20 04:06:40 +0000 | [diff] [blame] | 427 | goto restart; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 428 | } |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 429 | cf->can_id |= CAN_ERR_RESTARTED; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 430 | |
| 431 | netif_rx(skb); |
| 432 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 433 | stats->rx_packets++; |
| 434 | stats->rx_bytes += cf->can_dlc; |
| 435 | |
Wolfgang Grandegger | b3d0df7 | 2009-07-20 04:06:40 +0000 | [diff] [blame] | 436 | restart: |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 437 | netdev_dbg(dev, "restarted\n"); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 438 | priv->can_stats.restarts++; |
| 439 | |
| 440 | /* Now restart the device */ |
| 441 | err = priv->do_set_mode(dev, CAN_MODE_START); |
| 442 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 443 | netif_carrier_on(dev); |
| 444 | if (err) |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 445 | netdev_err(dev, "Error %d during restart", err); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | int can_restart_now(struct net_device *dev) |
| 449 | { |
| 450 | struct can_priv *priv = netdev_priv(dev); |
| 451 | |
| 452 | /* |
| 453 | * A manual restart is only permitted if automatic restart is |
| 454 | * disabled and the device is in the bus-off state |
| 455 | */ |
| 456 | if (priv->restart_ms) |
| 457 | return -EINVAL; |
| 458 | if (priv->state != CAN_STATE_BUS_OFF) |
| 459 | return -EBUSY; |
| 460 | |
| 461 | /* Runs as soon as possible in the timer context */ |
| 462 | mod_timer(&priv->restart_timer, jiffies); |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * CAN bus-off |
| 469 | * |
| 470 | * This functions should be called when the device goes bus-off to |
| 471 | * tell the netif layer that no more packets can be sent or received. |
| 472 | * If enabled, a timer is started to trigger bus-off recovery. |
| 473 | */ |
| 474 | void can_bus_off(struct net_device *dev) |
| 475 | { |
| 476 | struct can_priv *priv = netdev_priv(dev); |
| 477 | |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 478 | netdev_dbg(dev, "bus-off\n"); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 479 | |
| 480 | netif_carrier_off(dev); |
| 481 | priv->can_stats.bus_off++; |
| 482 | |
| 483 | if (priv->restart_ms) |
| 484 | mod_timer(&priv->restart_timer, |
| 485 | jiffies + (priv->restart_ms * HZ) / 1000); |
| 486 | } |
| 487 | EXPORT_SYMBOL_GPL(can_bus_off); |
| 488 | |
| 489 | static void can_setup(struct net_device *dev) |
| 490 | { |
| 491 | dev->type = ARPHRD_CAN; |
Oliver Hartkopp | 1e0625f | 2012-06-13 20:48:21 +0200 | [diff] [blame] | 492 | dev->mtu = CAN_MTU; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 493 | dev->hard_header_len = 0; |
| 494 | dev->addr_len = 0; |
| 495 | dev->tx_queue_len = 10; |
| 496 | |
| 497 | /* New-style flags. */ |
| 498 | dev->flags = IFF_NOARP; |
Michał Mirosław | 34324dc | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 499 | dev->features = NETIF_F_HW_CSUM; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 502 | struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf) |
| 503 | { |
| 504 | struct sk_buff *skb; |
| 505 | |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 506 | skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) + |
| 507 | sizeof(struct can_frame)); |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 508 | if (unlikely(!skb)) |
| 509 | return NULL; |
| 510 | |
| 511 | skb->protocol = htons(ETH_P_CAN); |
| 512 | skb->pkt_type = PACKET_BROADCAST; |
| 513 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 514 | |
Oliver Hartkopp | 2bf3440 | 2013-01-28 08:33:33 +0000 | [diff] [blame^] | 515 | can_skb_reserve(skb); |
| 516 | can_skb_prv(skb)->ifindex = dev->ifindex; |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 517 | |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 518 | *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame)); |
| 519 | memset(*cf, 0, sizeof(struct can_frame)); |
| 520 | |
| 521 | return skb; |
| 522 | } |
| 523 | EXPORT_SYMBOL_GPL(alloc_can_skb); |
| 524 | |
| 525 | struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf) |
| 526 | { |
| 527 | struct sk_buff *skb; |
| 528 | |
| 529 | skb = alloc_can_skb(dev, cf); |
| 530 | if (unlikely(!skb)) |
| 531 | return NULL; |
| 532 | |
| 533 | (*cf)->can_id = CAN_ERR_FLAG; |
| 534 | (*cf)->can_dlc = CAN_ERR_DLC; |
| 535 | |
| 536 | return skb; |
| 537 | } |
| 538 | EXPORT_SYMBOL_GPL(alloc_can_err_skb); |
| 539 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 540 | /* |
| 541 | * Allocate and setup space for the CAN network device |
| 542 | */ |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 543 | struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 544 | { |
| 545 | struct net_device *dev; |
| 546 | struct can_priv *priv; |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 547 | int size; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 548 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 549 | if (echo_skb_max) |
| 550 | size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) + |
| 551 | echo_skb_max * sizeof(struct sk_buff *); |
| 552 | else |
| 553 | size = sizeof_priv; |
| 554 | |
| 555 | dev = alloc_netdev(size, "can%d", can_setup); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 556 | if (!dev) |
| 557 | return NULL; |
| 558 | |
| 559 | priv = netdev_priv(dev); |
| 560 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 561 | if (echo_skb_max) { |
| 562 | priv->echo_skb_max = echo_skb_max; |
| 563 | priv->echo_skb = (void *)priv + |
| 564 | ALIGN(sizeof_priv, sizeof(struct sk_buff *)); |
| 565 | } |
| 566 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 567 | priv->state = CAN_STATE_STOPPED; |
| 568 | |
| 569 | init_timer(&priv->restart_timer); |
| 570 | |
| 571 | return dev; |
| 572 | } |
| 573 | EXPORT_SYMBOL_GPL(alloc_candev); |
| 574 | |
| 575 | /* |
| 576 | * Free space of the CAN network device |
| 577 | */ |
| 578 | void free_candev(struct net_device *dev) |
| 579 | { |
| 580 | free_netdev(dev); |
| 581 | } |
| 582 | EXPORT_SYMBOL_GPL(free_candev); |
| 583 | |
| 584 | /* |
| 585 | * Common open function when the device gets opened. |
| 586 | * |
| 587 | * This function should be called in the open function of the device |
| 588 | * driver. |
| 589 | */ |
| 590 | int open_candev(struct net_device *dev) |
| 591 | { |
| 592 | struct can_priv *priv = netdev_priv(dev); |
| 593 | |
| 594 | if (!priv->bittiming.tq && !priv->bittiming.bitrate) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 595 | netdev_err(dev, "bit-timing not yet defined\n"); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 596 | return -EINVAL; |
| 597 | } |
| 598 | |
Wolfgang Grandegger | 1b0d922 | 2009-07-20 04:06:41 +0000 | [diff] [blame] | 599 | /* Switch carrier on if device was stopped while in bus-off state */ |
| 600 | if (!netif_carrier_ok(dev)) |
| 601 | netif_carrier_on(dev); |
| 602 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 603 | setup_timer(&priv->restart_timer, can_restart, (unsigned long)dev); |
| 604 | |
| 605 | return 0; |
| 606 | } |
Wolfgang Grandegger | 128ced8 | 2009-05-30 07:55:48 +0000 | [diff] [blame] | 607 | EXPORT_SYMBOL_GPL(open_candev); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 608 | |
| 609 | /* |
| 610 | * Common close function for cleanup before the device gets closed. |
| 611 | * |
| 612 | * This function should be called in the close function of the device |
| 613 | * driver. |
| 614 | */ |
| 615 | void close_candev(struct net_device *dev) |
| 616 | { |
| 617 | struct can_priv *priv = netdev_priv(dev); |
| 618 | |
Alexander Stein | ab48b03 | 2012-11-27 08:52:34 +0100 | [diff] [blame] | 619 | del_timer_sync(&priv->restart_timer); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 620 | can_flush_echo_skb(dev); |
| 621 | } |
| 622 | EXPORT_SYMBOL_GPL(close_candev); |
| 623 | |
| 624 | /* |
| 625 | * CAN netlink interface |
| 626 | */ |
| 627 | static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = { |
| 628 | [IFLA_CAN_STATE] = { .type = NLA_U32 }, |
| 629 | [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) }, |
| 630 | [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 }, |
| 631 | [IFLA_CAN_RESTART] = { .type = NLA_U32 }, |
| 632 | [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) }, |
| 633 | [IFLA_CAN_BITTIMING_CONST] |
| 634 | = { .len = sizeof(struct can_bittiming_const) }, |
| 635 | [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) }, |
Wolfgang Grandegger | 52c793f | 2010-02-22 22:21:17 +0000 | [diff] [blame] | 636 | [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) }, |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 637 | }; |
| 638 | |
| 639 | static int can_changelink(struct net_device *dev, |
| 640 | struct nlattr *tb[], struct nlattr *data[]) |
| 641 | { |
| 642 | struct can_priv *priv = netdev_priv(dev); |
| 643 | int err; |
| 644 | |
| 645 | /* We need synchronization with dev->stop() */ |
| 646 | ASSERT_RTNL(); |
| 647 | |
| 648 | if (data[IFLA_CAN_CTRLMODE]) { |
| 649 | struct can_ctrlmode *cm; |
| 650 | |
| 651 | /* Do not allow changing controller mode while running */ |
| 652 | if (dev->flags & IFF_UP) |
| 653 | return -EBUSY; |
| 654 | cm = nla_data(data[IFLA_CAN_CTRLMODE]); |
Christian Pellegrin | ad72c34 | 2010-01-14 07:08:34 +0000 | [diff] [blame] | 655 | if (cm->flags & ~priv->ctrlmode_supported) |
| 656 | return -EOPNOTSUPP; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 657 | priv->ctrlmode &= ~cm->mask; |
| 658 | priv->ctrlmode |= cm->flags; |
| 659 | } |
| 660 | |
| 661 | if (data[IFLA_CAN_BITTIMING]) { |
| 662 | struct can_bittiming bt; |
| 663 | |
| 664 | /* Do not allow changing bittiming while running */ |
| 665 | if (dev->flags & IFF_UP) |
| 666 | return -EBUSY; |
| 667 | memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt)); |
| 668 | if ((!bt.bitrate && !bt.tq) || (bt.bitrate && bt.tq)) |
| 669 | return -EINVAL; |
| 670 | err = can_get_bittiming(dev, &bt); |
| 671 | if (err) |
| 672 | return err; |
| 673 | memcpy(&priv->bittiming, &bt, sizeof(bt)); |
| 674 | |
| 675 | if (priv->do_set_bittiming) { |
| 676 | /* Finally, set the bit-timing registers */ |
| 677 | err = priv->do_set_bittiming(dev); |
| 678 | if (err) |
| 679 | return err; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | if (data[IFLA_CAN_RESTART_MS]) { |
| 684 | /* Do not allow changing restart delay while running */ |
| 685 | if (dev->flags & IFF_UP) |
| 686 | return -EBUSY; |
| 687 | priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]); |
| 688 | } |
| 689 | |
| 690 | if (data[IFLA_CAN_RESTART]) { |
| 691 | /* Do not allow a restart while not running */ |
| 692 | if (!(dev->flags & IFF_UP)) |
| 693 | return -EINVAL; |
| 694 | err = can_restart_now(dev); |
| 695 | if (err) |
| 696 | return err; |
| 697 | } |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
Wolfgang Grandegger | 53a0ef86 | 2009-11-06 23:53:13 +0000 | [diff] [blame] | 702 | static size_t can_get_size(const struct net_device *dev) |
| 703 | { |
| 704 | struct can_priv *priv = netdev_priv(dev); |
| 705 | size_t size; |
| 706 | |
| 707 | size = nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */ |
| 708 | size += sizeof(struct can_ctrlmode); /* IFLA_CAN_CTRLMODE */ |
| 709 | size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */ |
| 710 | size += sizeof(struct can_bittiming); /* IFLA_CAN_BITTIMING */ |
| 711 | size += sizeof(struct can_clock); /* IFLA_CAN_CLOCK */ |
Wolfgang Grandegger | 52c793f | 2010-02-22 22:21:17 +0000 | [diff] [blame] | 712 | if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */ |
| 713 | size += sizeof(struct can_berr_counter); |
Wolfgang Grandegger | 53a0ef86 | 2009-11-06 23:53:13 +0000 | [diff] [blame] | 714 | if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */ |
| 715 | size += sizeof(struct can_bittiming_const); |
| 716 | |
| 717 | return size; |
| 718 | } |
| 719 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 720 | static int can_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 721 | { |
| 722 | struct can_priv *priv = netdev_priv(dev); |
| 723 | struct can_ctrlmode cm = {.flags = priv->ctrlmode}; |
Wolfgang Grandegger | 52c793f | 2010-02-22 22:21:17 +0000 | [diff] [blame] | 724 | struct can_berr_counter bec; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 725 | enum can_state state = priv->state; |
| 726 | |
| 727 | if (priv->do_get_state) |
| 728 | priv->do_get_state(dev, &state); |
David S. Miller | 31e0e32 | 2012-04-01 20:21:11 -0400 | [diff] [blame] | 729 | if (nla_put_u32(skb, IFLA_CAN_STATE, state) || |
| 730 | nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) || |
| 731 | nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) || |
| 732 | nla_put(skb, IFLA_CAN_BITTIMING, |
| 733 | sizeof(priv->bittiming), &priv->bittiming) || |
| 734 | nla_put(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock) || |
| 735 | (priv->do_get_berr_counter && |
| 736 | !priv->do_get_berr_counter(dev, &bec) && |
| 737 | nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) || |
| 738 | (priv->bittiming_const && |
| 739 | nla_put(skb, IFLA_CAN_BITTIMING_CONST, |
| 740 | sizeof(*priv->bittiming_const), priv->bittiming_const))) |
| 741 | goto nla_put_failure; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 742 | return 0; |
| 743 | |
| 744 | nla_put_failure: |
| 745 | return -EMSGSIZE; |
| 746 | } |
| 747 | |
Wolfgang Grandegger | 55369c0 | 2009-11-12 05:34:05 +0000 | [diff] [blame] | 748 | static size_t can_get_xstats_size(const struct net_device *dev) |
| 749 | { |
| 750 | return sizeof(struct can_device_stats); |
| 751 | } |
| 752 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 753 | static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev) |
| 754 | { |
| 755 | struct can_priv *priv = netdev_priv(dev); |
| 756 | |
David S. Miller | 31e0e32 | 2012-04-01 20:21:11 -0400 | [diff] [blame] | 757 | if (nla_put(skb, IFLA_INFO_XSTATS, |
| 758 | sizeof(priv->can_stats), &priv->can_stats)) |
| 759 | goto nla_put_failure; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 760 | return 0; |
| 761 | |
| 762 | nla_put_failure: |
| 763 | return -EMSGSIZE; |
| 764 | } |
| 765 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 766 | static int can_newlink(struct net *src_net, struct net_device *dev, |
Oliver Hartkopp | 993e6f2 | 2009-08-11 02:41:24 +0000 | [diff] [blame] | 767 | struct nlattr *tb[], struct nlattr *data[]) |
| 768 | { |
| 769 | return -EOPNOTSUPP; |
| 770 | } |
| 771 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 772 | static struct rtnl_link_ops can_link_ops __read_mostly = { |
| 773 | .kind = "can", |
| 774 | .maxtype = IFLA_CAN_MAX, |
| 775 | .policy = can_policy, |
| 776 | .setup = can_setup, |
Oliver Hartkopp | 993e6f2 | 2009-08-11 02:41:24 +0000 | [diff] [blame] | 777 | .newlink = can_newlink, |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 778 | .changelink = can_changelink, |
Wolfgang Grandegger | 53a0ef86 | 2009-11-06 23:53:13 +0000 | [diff] [blame] | 779 | .get_size = can_get_size, |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 780 | .fill_info = can_fill_info, |
Wolfgang Grandegger | 55369c0 | 2009-11-12 05:34:05 +0000 | [diff] [blame] | 781 | .get_xstats_size = can_get_xstats_size, |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 782 | .fill_xstats = can_fill_xstats, |
| 783 | }; |
| 784 | |
| 785 | /* |
| 786 | * Register the CAN network device |
| 787 | */ |
| 788 | int register_candev(struct net_device *dev) |
| 789 | { |
| 790 | dev->rtnl_link_ops = &can_link_ops; |
| 791 | return register_netdev(dev); |
| 792 | } |
| 793 | EXPORT_SYMBOL_GPL(register_candev); |
| 794 | |
| 795 | /* |
| 796 | * Unregister the CAN network device |
| 797 | */ |
| 798 | void unregister_candev(struct net_device *dev) |
| 799 | { |
| 800 | unregister_netdev(dev); |
| 801 | } |
| 802 | EXPORT_SYMBOL_GPL(unregister_candev); |
| 803 | |
Kurt Van Dijck | bf03a53 | 2012-12-18 18:50:56 +0100 | [diff] [blame] | 804 | /* |
| 805 | * Test if a network device is a candev based device |
| 806 | * and return the can_priv* if so. |
| 807 | */ |
| 808 | struct can_priv *safe_candev_priv(struct net_device *dev) |
| 809 | { |
| 810 | if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops)) |
| 811 | return NULL; |
| 812 | |
| 813 | return netdev_priv(dev); |
| 814 | } |
| 815 | EXPORT_SYMBOL_GPL(safe_candev_priv); |
| 816 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 817 | static __init int can_dev_init(void) |
| 818 | { |
| 819 | int err; |
| 820 | |
Kurt Van Dijck | a1ef7bd | 2012-12-18 18:50:57 +0100 | [diff] [blame] | 821 | can_led_notifier_init(); |
| 822 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 823 | err = rtnl_link_register(&can_link_ops); |
| 824 | if (!err) |
| 825 | printk(KERN_INFO MOD_DESC "\n"); |
| 826 | |
| 827 | return err; |
| 828 | } |
| 829 | module_init(can_dev_init); |
| 830 | |
| 831 | static __exit void can_dev_exit(void) |
| 832 | { |
| 833 | rtnl_link_unregister(&can_link_ops); |
Kurt Van Dijck | a1ef7bd | 2012-12-18 18:50:57 +0100 | [diff] [blame] | 834 | |
| 835 | can_led_notifier_exit(); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 836 | } |
| 837 | module_exit(can_dev_exit); |
| 838 | |
| 839 | MODULE_ALIAS_RTNL_LINK("can"); |