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