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> |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 24 | #include <linux/workqueue.h> |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 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 */ |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 73 | #define CAN_CALC_SYNC_SEG 1 |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 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 | */ |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 88 | static 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) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 92 | { |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 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 | } |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 116 | } |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 117 | |
| 118 | if (sample_point_error_ptr) |
| 119 | *sample_point_error_ptr = best_sample_point_error; |
| 120 | |
| 121 | return best_sample_point; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Oliver Hartkopp | 08da7da | 2014-02-28 16:36:22 +0100 | [diff] [blame] | 124 | static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, |
| 125 | const struct can_bittiming_const *btc) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 126 | { |
| 127 | struct can_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 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; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 137 | u64 v64; |
| 138 | |
Roman Fietze | 67b5909 | 2014-10-20 10:32:42 +0200 | [diff] [blame] | 139 | /* Use CiA recommended sample points */ |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 140 | if (bt->sample_point) { |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 141 | sample_point_nominal = bt->sample_point; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 142 | } else { |
| 143 | if (bt->bitrate > 800000) |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 144 | sample_point_nominal = 750; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 145 | else if (bt->bitrate > 500000) |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 146 | sample_point_nominal = 800; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 147 | else |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 148 | sample_point_nominal = 875; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 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--) { |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 154 | tsegall = CAN_CALC_SYNC_SEG + tseg / 2; |
| 155 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 156 | /* Compute all possible tseg choices (tseg=tseg1+tseg2) */ |
| 157 | brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2; |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 158 | |
| 159 | /* choose brp step which is possible in system */ |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 160 | brp = (brp / btc->brp_inc) * btc->brp_inc; |
| 161 | if ((brp < btc->brp_min) || (brp > btc->brp_max)) |
| 162 | continue; |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 163 | |
| 164 | bitrate = priv->clock.freq / (brp * tsegall); |
| 165 | bitrate_error = abs(bt->bitrate - bitrate); |
| 166 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 167 | /* tseg brp biterror */ |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 168 | if (bitrate_error > best_bitrate_error) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 169 | continue; |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 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; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 181 | best_tseg = tseg / 2; |
| 182 | best_brp = brp; |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 183 | |
| 184 | if (bitrate_error == 0 && sample_point_error == 0) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 185 | break; |
| 186 | } |
| 187 | |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 188 | if (best_bitrate_error) { |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 189 | /* Error in one-tenth of a percent */ |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 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) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 194 | netdev_err(dev, |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 195 | "bitrate error %d.%d%% too high\n", |
| 196 | bitrate_error / 10, bitrate_error % 10); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 197 | return -EDOM; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 198 | } |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 199 | netdev_warn(dev, "bitrate error %d.%d%%\n", |
| 200 | bitrate_error / 10, bitrate_error % 10); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* real sample point */ |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 204 | bt->sample_point = can_update_sample_point(btc, sample_point_nominal, best_tseg, |
| 205 | &tseg1, &tseg2, NULL); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 206 | |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 207 | v64 = (u64)best_brp * 1000 * 1000 * 1000; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 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; |
Oliver Hartkopp | 2e11437 | 2011-09-28 02:50:11 +0000 | [diff] [blame] | 213 | |
| 214 | /* check for sjw user settings */ |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 215 | if (!bt->sjw || !btc->sjw_max) { |
Oliver Hartkopp | 2e11437 | 2011-09-28 02:50:11 +0000 | [diff] [blame] | 216 | bt->sjw = 1; |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 217 | } else { |
Oliver Hartkopp | 2e11437 | 2011-09-28 02:50:11 +0000 | [diff] [blame] | 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 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 226 | bt->brp = best_brp; |
Marc Kleine-Budde | 7da29f9 | 2016-05-09 14:13:06 +0200 | [diff] [blame] | 227 | |
| 228 | /* real bitrate */ |
| 229 | bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2)); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | #else /* !CONFIG_CAN_CALC_BITTIMING */ |
Oliver Hartkopp | 08da7da | 2014-02-28 16:36:22 +0100 | [diff] [blame] | 234 | static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, |
| 235 | const struct can_bittiming_const *btc) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 236 | { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 237 | netdev_err(dev, "bit-timing calculation not available\n"); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 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 | */ |
Oliver Hartkopp | 08da7da | 2014-02-28 16:36:22 +0100 | [diff] [blame] | 248 | static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt, |
| 249 | const struct can_bittiming_const *btc) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 250 | { |
| 251 | struct can_priv *priv = netdev_priv(dev); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 252 | int tseg1, alltseg; |
| 253 | u64 brp64; |
| 254 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 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 | |
Oliver Hartkopp | 08da7da | 2014-02-28 16:36:22 +0100 | [diff] [blame] | 282 | static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt, |
| 283 | const struct can_bittiming_const *btc) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 284 | { |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 285 | int err; |
| 286 | |
| 287 | /* Check if the CAN device has bit-timing parameters */ |
Oliver Hartkopp | 08da7da | 2014-02-28 16:36:22 +0100 | [diff] [blame] | 288 | if (!btc) |
Oliver Hartkopp | d482443 | 2014-04-23 22:10:55 +0200 | [diff] [blame] | 289 | return -EOPNOTSUPP; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 290 | |
Oliver Hartkopp | d5298df | 2014-02-28 16:36:21 +0100 | [diff] [blame] | 291 | /* |
| 292 | * Depending on the given can_bittiming parameter structure the CAN |
| 293 | * timing parameters are calculated based on the provided bitrate OR |
| 294 | * alternatively the CAN timing parameters (tq, prop_seg, etc.) are |
| 295 | * provided directly which are then checked and fixed up. |
| 296 | */ |
| 297 | if (!bt->tq && bt->bitrate) |
Oliver Hartkopp | 08da7da | 2014-02-28 16:36:22 +0100 | [diff] [blame] | 298 | err = can_calc_bittiming(dev, bt, btc); |
Oliver Hartkopp | d5298df | 2014-02-28 16:36:21 +0100 | [diff] [blame] | 299 | else if (bt->tq && !bt->bitrate) |
Oliver Hartkopp | 08da7da | 2014-02-28 16:36:22 +0100 | [diff] [blame] | 300 | err = can_fixup_bittiming(dev, bt, btc); |
Oliver Hartkopp | d5298df | 2014-02-28 16:36:21 +0100 | [diff] [blame] | 301 | else |
| 302 | err = -EINVAL; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 303 | |
Oliver Hartkopp | d5298df | 2014-02-28 16:36:21 +0100 | [diff] [blame] | 304 | return err; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Andri Yngvason | bac78aa | 2014-12-03 17:54:13 +0000 | [diff] [blame] | 307 | static void can_update_state_error_stats(struct net_device *dev, |
| 308 | enum can_state new_state) |
| 309 | { |
| 310 | struct can_priv *priv = netdev_priv(dev); |
| 311 | |
| 312 | if (new_state <= priv->state) |
| 313 | return; |
| 314 | |
| 315 | switch (new_state) { |
| 316 | case CAN_STATE_ERROR_WARNING: |
| 317 | priv->can_stats.error_warning++; |
| 318 | break; |
| 319 | case CAN_STATE_ERROR_PASSIVE: |
| 320 | priv->can_stats.error_passive++; |
| 321 | break; |
| 322 | case CAN_STATE_BUS_OFF: |
Andri Yngvason | be38a6f | 2015-01-16 14:30:28 +0000 | [diff] [blame] | 323 | priv->can_stats.bus_off++; |
| 324 | break; |
Andri Yngvason | bac78aa | 2014-12-03 17:54:13 +0000 | [diff] [blame] | 325 | default: |
| 326 | break; |
kbuild test robot | 5b5ba2a | 2015-01-16 16:52:21 +0800 | [diff] [blame] | 327 | } |
Andri Yngvason | bac78aa | 2014-12-03 17:54:13 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static int can_tx_state_to_frame(struct net_device *dev, enum can_state state) |
| 331 | { |
| 332 | switch (state) { |
| 333 | case CAN_STATE_ERROR_ACTIVE: |
| 334 | return CAN_ERR_CRTL_ACTIVE; |
| 335 | case CAN_STATE_ERROR_WARNING: |
| 336 | return CAN_ERR_CRTL_TX_WARNING; |
| 337 | case CAN_STATE_ERROR_PASSIVE: |
| 338 | return CAN_ERR_CRTL_TX_PASSIVE; |
| 339 | default: |
| 340 | return 0; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | static int can_rx_state_to_frame(struct net_device *dev, enum can_state state) |
| 345 | { |
| 346 | switch (state) { |
| 347 | case CAN_STATE_ERROR_ACTIVE: |
| 348 | return CAN_ERR_CRTL_ACTIVE; |
| 349 | case CAN_STATE_ERROR_WARNING: |
| 350 | return CAN_ERR_CRTL_RX_WARNING; |
| 351 | case CAN_STATE_ERROR_PASSIVE: |
| 352 | return CAN_ERR_CRTL_RX_PASSIVE; |
| 353 | default: |
| 354 | return 0; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void can_change_state(struct net_device *dev, struct can_frame *cf, |
| 359 | enum can_state tx_state, enum can_state rx_state) |
| 360 | { |
| 361 | struct can_priv *priv = netdev_priv(dev); |
| 362 | enum can_state new_state = max(tx_state, rx_state); |
| 363 | |
| 364 | if (unlikely(new_state == priv->state)) { |
| 365 | netdev_warn(dev, "%s: oops, state did not change", __func__); |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | netdev_dbg(dev, "New error state: %d\n", new_state); |
| 370 | |
| 371 | can_update_state_error_stats(dev, new_state); |
| 372 | priv->state = new_state; |
| 373 | |
| 374 | if (unlikely(new_state == CAN_STATE_BUS_OFF)) { |
| 375 | cf->can_id |= CAN_ERR_BUSOFF; |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | cf->can_id |= CAN_ERR_CRTL; |
| 380 | cf->data[1] |= tx_state >= rx_state ? |
| 381 | can_tx_state_to_frame(dev, tx_state) : 0; |
| 382 | cf->data[1] |= tx_state <= rx_state ? |
| 383 | can_rx_state_to_frame(dev, rx_state) : 0; |
| 384 | } |
| 385 | EXPORT_SYMBOL_GPL(can_change_state); |
| 386 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 387 | /* |
| 388 | * Local echo of CAN messages |
| 389 | * |
| 390 | * CAN network devices *should* support a local echo functionality |
| 391 | * (see Documentation/networking/can.txt). To test the handling of CAN |
| 392 | * interfaces that do not support the local echo both driver types are |
| 393 | * implemented. In the case that the driver does not support the echo |
| 394 | * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core |
| 395 | * to perform the echo as a fallback solution. |
| 396 | */ |
| 397 | static void can_flush_echo_skb(struct net_device *dev) |
| 398 | { |
| 399 | struct can_priv *priv = netdev_priv(dev); |
| 400 | struct net_device_stats *stats = &dev->stats; |
| 401 | int i; |
| 402 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 403 | for (i = 0; i < priv->echo_skb_max; i++) { |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 404 | if (priv->echo_skb[i]) { |
| 405 | kfree_skb(priv->echo_skb[i]); |
| 406 | priv->echo_skb[i] = NULL; |
| 407 | stats->tx_dropped++; |
| 408 | stats->tx_aborted_errors++; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Put the skb on the stack to be looped backed locally lateron |
| 415 | * |
| 416 | * The function is typically called in the start_xmit function |
| 417 | * of the device driver. The driver must protect access to |
| 418 | * priv->echo_skb, if necessary. |
| 419 | */ |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 420 | void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, |
| 421 | unsigned int idx) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 422 | { |
| 423 | struct can_priv *priv = netdev_priv(dev); |
| 424 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 425 | BUG_ON(idx >= priv->echo_skb_max); |
| 426 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 427 | /* check flag whether this packet has to be looped back */ |
Oliver Hartkopp | a94bc9c | 2014-02-28 16:36:19 +0100 | [diff] [blame] | 428 | if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK || |
| 429 | (skb->protocol != htons(ETH_P_CAN) && |
| 430 | skb->protocol != htons(ETH_P_CANFD))) { |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 431 | kfree_skb(skb); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | if (!priv->echo_skb[idx]) { |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 436 | |
Oliver Hartkopp | 0ae89be | 2014-01-30 10:11:28 +0100 | [diff] [blame] | 437 | skb = can_create_echo_skb(skb); |
| 438 | if (!skb) |
| 439 | return; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 440 | |
| 441 | /* make settings for echo to reduce code in irq context */ |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 442 | skb->pkt_type = PACKET_BROADCAST; |
| 443 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 444 | skb->dev = dev; |
| 445 | |
| 446 | /* save this skb for tx interrupt echo handling */ |
| 447 | priv->echo_skb[idx] = skb; |
| 448 | } else { |
| 449 | /* locking problem with netif_stop_queue() ?? */ |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 450 | netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 451 | kfree_skb(skb); |
| 452 | } |
| 453 | } |
| 454 | EXPORT_SYMBOL_GPL(can_put_echo_skb); |
| 455 | |
| 456 | /* |
| 457 | * Get the skb from the stack and loop it back locally |
| 458 | * |
| 459 | * The function is typically called when the TX done interrupt |
| 460 | * is handled in the device driver. The driver must protect |
| 461 | * access to priv->echo_skb, if necessary. |
| 462 | */ |
Marc Kleine-Budde | cf5046b | 2011-10-10 23:43:53 +0200 | [diff] [blame] | 463 | 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] | 464 | { |
| 465 | struct can_priv *priv = netdev_priv(dev); |
| 466 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 467 | BUG_ON(idx >= priv->echo_skb_max); |
| 468 | |
Wolfgang Grandegger | 39e3ab6 | 2009-09-01 05:26:12 +0000 | [diff] [blame] | 469 | if (priv->echo_skb[idx]) { |
Marc Kleine-Budde | cf5046b | 2011-10-10 23:43:53 +0200 | [diff] [blame] | 470 | struct sk_buff *skb = priv->echo_skb[idx]; |
| 471 | struct can_frame *cf = (struct can_frame *)skb->data; |
| 472 | u8 dlc = cf->can_dlc; |
| 473 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 474 | netif_rx(priv->echo_skb[idx]); |
| 475 | priv->echo_skb[idx] = NULL; |
Marc Kleine-Budde | cf5046b | 2011-10-10 23:43:53 +0200 | [diff] [blame] | 476 | |
| 477 | return dlc; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 478 | } |
Marc Kleine-Budde | cf5046b | 2011-10-10 23:43:53 +0200 | [diff] [blame] | 479 | |
| 480 | return 0; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 481 | } |
| 482 | EXPORT_SYMBOL_GPL(can_get_echo_skb); |
| 483 | |
| 484 | /* |
Wolfgang Grandegger | 39e3ab6 | 2009-09-01 05:26:12 +0000 | [diff] [blame] | 485 | * Remove the skb from the stack and free it. |
| 486 | * |
| 487 | * The function is typically called when TX failed. |
| 488 | */ |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 489 | void can_free_echo_skb(struct net_device *dev, unsigned int idx) |
Wolfgang Grandegger | 39e3ab6 | 2009-09-01 05:26:12 +0000 | [diff] [blame] | 490 | { |
| 491 | struct can_priv *priv = netdev_priv(dev); |
| 492 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 493 | BUG_ON(idx >= priv->echo_skb_max); |
| 494 | |
Wolfgang Grandegger | 39e3ab6 | 2009-09-01 05:26:12 +0000 | [diff] [blame] | 495 | if (priv->echo_skb[idx]) { |
Thomas Körper | 5247a58 | 2014-10-31 07:33:54 +0100 | [diff] [blame] | 496 | dev_kfree_skb_any(priv->echo_skb[idx]); |
Wolfgang Grandegger | 39e3ab6 | 2009-09-01 05:26:12 +0000 | [diff] [blame] | 497 | priv->echo_skb[idx] = NULL; |
| 498 | } |
| 499 | } |
| 500 | EXPORT_SYMBOL_GPL(can_free_echo_skb); |
| 501 | |
| 502 | /* |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 503 | * CAN device restart for bus-off recovery |
| 504 | */ |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 505 | static void can_restart(struct net_device *dev) |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 506 | { |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 507 | struct can_priv *priv = netdev_priv(dev); |
| 508 | struct net_device_stats *stats = &dev->stats; |
| 509 | struct sk_buff *skb; |
| 510 | struct can_frame *cf; |
| 511 | int err; |
| 512 | |
| 513 | BUG_ON(netif_carrier_ok(dev)); |
| 514 | |
| 515 | /* |
| 516 | * No synchronization needed because the device is bus-off and |
| 517 | * no messages can come in or go out. |
| 518 | */ |
| 519 | can_flush_echo_skb(dev); |
| 520 | |
| 521 | /* send restart message upstream */ |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 522 | skb = alloc_can_err_skb(dev, &cf); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 523 | if (skb == NULL) { |
| 524 | err = -ENOMEM; |
Wolfgang Grandegger | b3d0df7 | 2009-07-20 04:06:40 +0000 | [diff] [blame] | 525 | goto restart; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 526 | } |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 527 | cf->can_id |= CAN_ERR_RESTARTED; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 528 | |
| 529 | netif_rx(skb); |
| 530 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 531 | stats->rx_packets++; |
| 532 | stats->rx_bytes += cf->can_dlc; |
| 533 | |
Wolfgang Grandegger | b3d0df7 | 2009-07-20 04:06:40 +0000 | [diff] [blame] | 534 | restart: |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 535 | netdev_dbg(dev, "restarted\n"); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 536 | priv->can_stats.restarts++; |
| 537 | |
| 538 | /* Now restart the device */ |
| 539 | err = priv->do_set_mode(dev, CAN_MODE_START); |
| 540 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 541 | netif_carrier_on(dev); |
| 542 | if (err) |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 543 | netdev_err(dev, "Error %d during restart", err); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 546 | static void can_restart_work(struct work_struct *work) |
| 547 | { |
| 548 | struct delayed_work *dwork = to_delayed_work(work); |
| 549 | struct can_priv *priv = container_of(dwork, struct can_priv, restart_work); |
| 550 | |
| 551 | can_restart(priv->dev); |
| 552 | } |
| 553 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 554 | int can_restart_now(struct net_device *dev) |
| 555 | { |
| 556 | struct can_priv *priv = netdev_priv(dev); |
| 557 | |
| 558 | /* |
| 559 | * A manual restart is only permitted if automatic restart is |
| 560 | * disabled and the device is in the bus-off state |
| 561 | */ |
| 562 | if (priv->restart_ms) |
| 563 | return -EINVAL; |
| 564 | if (priv->state != CAN_STATE_BUS_OFF) |
| 565 | return -EBUSY; |
| 566 | |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 567 | cancel_delayed_work_sync(&priv->restart_work); |
| 568 | can_restart(dev); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * CAN bus-off |
| 575 | * |
| 576 | * This functions should be called when the device goes bus-off to |
| 577 | * tell the netif layer that no more packets can be sent or received. |
| 578 | * If enabled, a timer is started to trigger bus-off recovery. |
| 579 | */ |
| 580 | void can_bus_off(struct net_device *dev) |
| 581 | { |
| 582 | struct can_priv *priv = netdev_priv(dev); |
| 583 | |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 584 | netdev_dbg(dev, "bus-off\n"); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 585 | |
| 586 | netif_carrier_off(dev); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 587 | |
| 588 | if (priv->restart_ms) |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 589 | schedule_delayed_work(&priv->restart_work, |
| 590 | msecs_to_jiffies(priv->restart_ms)); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 591 | } |
| 592 | EXPORT_SYMBOL_GPL(can_bus_off); |
| 593 | |
| 594 | static void can_setup(struct net_device *dev) |
| 595 | { |
| 596 | dev->type = ARPHRD_CAN; |
Oliver Hartkopp | 1e0625f | 2012-06-13 20:48:21 +0200 | [diff] [blame] | 597 | dev->mtu = CAN_MTU; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 598 | dev->hard_header_len = 0; |
| 599 | dev->addr_len = 0; |
| 600 | dev->tx_queue_len = 10; |
| 601 | |
| 602 | /* New-style flags. */ |
| 603 | dev->flags = IFF_NOARP; |
Michał Mirosław | 34324dc | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 604 | dev->features = NETIF_F_HW_CSUM; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 607 | struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf) |
| 608 | { |
| 609 | struct sk_buff *skb; |
| 610 | |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 611 | skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) + |
| 612 | sizeof(struct can_frame)); |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 613 | if (unlikely(!skb)) |
| 614 | return NULL; |
| 615 | |
| 616 | skb->protocol = htons(ETH_P_CAN); |
| 617 | skb->pkt_type = PACKET_BROADCAST; |
| 618 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 619 | |
Oliver Hartkopp | 969439016 | 2015-02-23 20:37:54 +0100 | [diff] [blame] | 620 | skb_reset_mac_header(skb); |
| 621 | skb_reset_network_header(skb); |
| 622 | skb_reset_transport_header(skb); |
| 623 | |
Oliver Hartkopp | 2bf3440 | 2013-01-28 08:33:33 +0000 | [diff] [blame] | 624 | can_skb_reserve(skb); |
| 625 | can_skb_prv(skb)->ifindex = dev->ifindex; |
Oliver Hartkopp | d3b58c4 | 2015-06-26 11:58:19 +0200 | [diff] [blame] | 626 | can_skb_prv(skb)->skbcnt = 0; |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 627 | |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 628 | *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame)); |
| 629 | memset(*cf, 0, sizeof(struct can_frame)); |
| 630 | |
| 631 | return skb; |
| 632 | } |
| 633 | EXPORT_SYMBOL_GPL(alloc_can_skb); |
| 634 | |
Stephane Grosjean | cb2518c | 2014-01-15 09:50:13 +0100 | [diff] [blame] | 635 | struct sk_buff *alloc_canfd_skb(struct net_device *dev, |
| 636 | struct canfd_frame **cfd) |
| 637 | { |
| 638 | struct sk_buff *skb; |
| 639 | |
| 640 | skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) + |
| 641 | sizeof(struct canfd_frame)); |
| 642 | if (unlikely(!skb)) |
| 643 | return NULL; |
| 644 | |
| 645 | skb->protocol = htons(ETH_P_CANFD); |
| 646 | skb->pkt_type = PACKET_BROADCAST; |
| 647 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 648 | |
Oliver Hartkopp | 969439016 | 2015-02-23 20:37:54 +0100 | [diff] [blame] | 649 | skb_reset_mac_header(skb); |
| 650 | skb_reset_network_header(skb); |
| 651 | skb_reset_transport_header(skb); |
| 652 | |
Stephane Grosjean | cb2518c | 2014-01-15 09:50:13 +0100 | [diff] [blame] | 653 | can_skb_reserve(skb); |
| 654 | can_skb_prv(skb)->ifindex = dev->ifindex; |
Oliver Hartkopp | d3b58c4 | 2015-06-26 11:58:19 +0200 | [diff] [blame] | 655 | can_skb_prv(skb)->skbcnt = 0; |
Stephane Grosjean | cb2518c | 2014-01-15 09:50:13 +0100 | [diff] [blame] | 656 | |
| 657 | *cfd = (struct canfd_frame *)skb_put(skb, sizeof(struct canfd_frame)); |
| 658 | memset(*cfd, 0, sizeof(struct canfd_frame)); |
| 659 | |
| 660 | return skb; |
| 661 | } |
| 662 | EXPORT_SYMBOL_GPL(alloc_canfd_skb); |
| 663 | |
Wolfgang Grandegger | 7b6856a0 | 2009-10-20 00:08:01 -0700 | [diff] [blame] | 664 | struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf) |
| 665 | { |
| 666 | struct sk_buff *skb; |
| 667 | |
| 668 | skb = alloc_can_skb(dev, cf); |
| 669 | if (unlikely(!skb)) |
| 670 | return NULL; |
| 671 | |
| 672 | (*cf)->can_id = CAN_ERR_FLAG; |
| 673 | (*cf)->can_dlc = CAN_ERR_DLC; |
| 674 | |
| 675 | return skb; |
| 676 | } |
| 677 | EXPORT_SYMBOL_GPL(alloc_can_err_skb); |
| 678 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 679 | /* |
| 680 | * Allocate and setup space for the CAN network device |
| 681 | */ |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 682 | 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] | 683 | { |
| 684 | struct net_device *dev; |
| 685 | struct can_priv *priv; |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 686 | int size; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 687 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 688 | if (echo_skb_max) |
| 689 | size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) + |
| 690 | echo_skb_max * sizeof(struct sk_buff *); |
| 691 | else |
| 692 | size = sizeof_priv; |
| 693 | |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 694 | dev = alloc_netdev(size, "can%d", NET_NAME_UNKNOWN, can_setup); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 695 | if (!dev) |
| 696 | return NULL; |
| 697 | |
| 698 | priv = netdev_priv(dev); |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 699 | priv->dev = dev; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 700 | |
Wolfgang Grandegger | a6e4bc5 | 2009-10-08 22:17:11 +0000 | [diff] [blame] | 701 | if (echo_skb_max) { |
| 702 | priv->echo_skb_max = echo_skb_max; |
| 703 | priv->echo_skb = (void *)priv + |
| 704 | ALIGN(sizeof_priv, sizeof(struct sk_buff *)); |
| 705 | } |
| 706 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 707 | priv->state = CAN_STATE_STOPPED; |
| 708 | |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 709 | INIT_DELAYED_WORK(&priv->restart_work, can_restart_work); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 710 | |
| 711 | return dev; |
| 712 | } |
| 713 | EXPORT_SYMBOL_GPL(alloc_candev); |
| 714 | |
| 715 | /* |
| 716 | * Free space of the CAN network device |
| 717 | */ |
| 718 | void free_candev(struct net_device *dev) |
| 719 | { |
| 720 | free_netdev(dev); |
| 721 | } |
| 722 | EXPORT_SYMBOL_GPL(free_candev); |
| 723 | |
| 724 | /* |
Oliver Hartkopp | bc05a89 | 2014-02-28 16:36:24 +0100 | [diff] [blame] | 725 | * changing MTU and control mode for CAN/CANFD devices |
| 726 | */ |
| 727 | int can_change_mtu(struct net_device *dev, int new_mtu) |
| 728 | { |
| 729 | struct can_priv *priv = netdev_priv(dev); |
| 730 | |
| 731 | /* Do not allow changing the MTU while running */ |
| 732 | if (dev->flags & IFF_UP) |
| 733 | return -EBUSY; |
| 734 | |
| 735 | /* allow change of MTU according to the CANFD ability of the device */ |
| 736 | switch (new_mtu) { |
| 737 | case CAN_MTU: |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 738 | /* 'CANFD-only' controllers can not switch to CAN_MTU */ |
| 739 | if (priv->ctrlmode_static & CAN_CTRLMODE_FD) |
| 740 | return -EINVAL; |
| 741 | |
Oliver Hartkopp | bc05a89 | 2014-02-28 16:36:24 +0100 | [diff] [blame] | 742 | priv->ctrlmode &= ~CAN_CTRLMODE_FD; |
| 743 | break; |
| 744 | |
| 745 | case CANFD_MTU: |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 746 | /* check for potential CANFD ability */ |
| 747 | if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) && |
| 748 | !(priv->ctrlmode_static & CAN_CTRLMODE_FD)) |
Oliver Hartkopp | bc05a89 | 2014-02-28 16:36:24 +0100 | [diff] [blame] | 749 | return -EINVAL; |
| 750 | |
| 751 | priv->ctrlmode |= CAN_CTRLMODE_FD; |
| 752 | break; |
| 753 | |
| 754 | default: |
| 755 | return -EINVAL; |
| 756 | } |
| 757 | |
| 758 | dev->mtu = new_mtu; |
| 759 | return 0; |
| 760 | } |
| 761 | EXPORT_SYMBOL_GPL(can_change_mtu); |
| 762 | |
| 763 | /* |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 764 | * Common open function when the device gets opened. |
| 765 | * |
| 766 | * This function should be called in the open function of the device |
| 767 | * driver. |
| 768 | */ |
| 769 | int open_candev(struct net_device *dev) |
| 770 | { |
| 771 | struct can_priv *priv = netdev_priv(dev); |
| 772 | |
Oliver Hartkopp | b30749f | 2014-02-28 16:36:20 +0100 | [diff] [blame] | 773 | if (!priv->bittiming.bitrate) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 774 | netdev_err(dev, "bit-timing not yet defined\n"); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 775 | return -EINVAL; |
| 776 | } |
| 777 | |
Oliver Hartkopp | dd22586 | 2014-02-28 16:36:25 +0100 | [diff] [blame] | 778 | /* For CAN FD the data bitrate has to be >= the arbitration bitrate */ |
| 779 | if ((priv->ctrlmode & CAN_CTRLMODE_FD) && |
| 780 | (!priv->data_bittiming.bitrate || |
| 781 | (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) { |
| 782 | netdev_err(dev, "incorrect/missing data bit-timing\n"); |
| 783 | return -EINVAL; |
| 784 | } |
| 785 | |
Wolfgang Grandegger | 1b0d922 | 2009-07-20 04:06:41 +0000 | [diff] [blame] | 786 | /* Switch carrier on if device was stopped while in bus-off state */ |
| 787 | if (!netif_carrier_ok(dev)) |
| 788 | netif_carrier_on(dev); |
| 789 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 790 | return 0; |
| 791 | } |
Wolfgang Grandegger | 128ced8 | 2009-05-30 07:55:48 +0000 | [diff] [blame] | 792 | EXPORT_SYMBOL_GPL(open_candev); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 793 | |
| 794 | /* |
| 795 | * Common close function for cleanup before the device gets closed. |
| 796 | * |
| 797 | * This function should be called in the close function of the device |
| 798 | * driver. |
| 799 | */ |
| 800 | void close_candev(struct net_device *dev) |
| 801 | { |
| 802 | struct can_priv *priv = netdev_priv(dev); |
| 803 | |
Sergei Miroshnichenko | 9abefcb | 2016-09-07 16:51:12 +0300 | [diff] [blame] | 804 | cancel_delayed_work_sync(&priv->restart_work); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 805 | can_flush_echo_skb(dev); |
| 806 | } |
| 807 | EXPORT_SYMBOL_GPL(close_candev); |
| 808 | |
| 809 | /* |
| 810 | * CAN netlink interface |
| 811 | */ |
| 812 | static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = { |
| 813 | [IFLA_CAN_STATE] = { .type = NLA_U32 }, |
| 814 | [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) }, |
| 815 | [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 }, |
| 816 | [IFLA_CAN_RESTART] = { .type = NLA_U32 }, |
| 817 | [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) }, |
| 818 | [IFLA_CAN_BITTIMING_CONST] |
| 819 | = { .len = sizeof(struct can_bittiming_const) }, |
| 820 | [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) }, |
Wolfgang Grandegger | 52c793f | 2010-02-22 22:21:17 +0000 | [diff] [blame] | 821 | [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) }, |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 822 | [IFLA_CAN_DATA_BITTIMING] |
| 823 | = { .len = sizeof(struct can_bittiming) }, |
| 824 | [IFLA_CAN_DATA_BITTIMING_CONST] |
| 825 | = { .len = sizeof(struct can_bittiming_const) }, |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 826 | }; |
| 827 | |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 828 | static int can_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 829 | { |
| 830 | bool is_can_fd = false; |
| 831 | |
| 832 | /* Make sure that valid CAN FD configurations always consist of |
| 833 | * - nominal/arbitration bittiming |
| 834 | * - data bittiming |
| 835 | * - control mode with CAN_CTRLMODE_FD set |
| 836 | */ |
| 837 | |
Oliver Hartkopp | bce271f | 2016-06-21 12:14:07 +0200 | [diff] [blame] | 838 | if (!data) |
| 839 | return 0; |
| 840 | |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 841 | if (data[IFLA_CAN_CTRLMODE]) { |
| 842 | struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]); |
| 843 | |
| 844 | is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD; |
| 845 | } |
| 846 | |
| 847 | if (is_can_fd) { |
| 848 | if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING]) |
| 849 | return -EOPNOTSUPP; |
| 850 | } |
| 851 | |
| 852 | if (data[IFLA_CAN_DATA_BITTIMING]) { |
| 853 | if (!is_can_fd || !data[IFLA_CAN_BITTIMING]) |
| 854 | return -EOPNOTSUPP; |
| 855 | } |
| 856 | |
| 857 | return 0; |
| 858 | } |
| 859 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 860 | static int can_changelink(struct net_device *dev, |
| 861 | struct nlattr *tb[], struct nlattr *data[]) |
| 862 | { |
| 863 | struct can_priv *priv = netdev_priv(dev); |
| 864 | int err; |
| 865 | |
| 866 | /* We need synchronization with dev->stop() */ |
| 867 | ASSERT_RTNL(); |
| 868 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 869 | if (data[IFLA_CAN_BITTIMING]) { |
| 870 | struct can_bittiming bt; |
| 871 | |
| 872 | /* Do not allow changing bittiming while running */ |
| 873 | if (dev->flags & IFF_UP) |
| 874 | return -EBUSY; |
| 875 | memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt)); |
Oliver Hartkopp | 08da7da | 2014-02-28 16:36:22 +0100 | [diff] [blame] | 876 | err = can_get_bittiming(dev, &bt, priv->bittiming_const); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 877 | if (err) |
| 878 | return err; |
| 879 | memcpy(&priv->bittiming, &bt, sizeof(bt)); |
| 880 | |
| 881 | if (priv->do_set_bittiming) { |
| 882 | /* Finally, set the bit-timing registers */ |
| 883 | err = priv->do_set_bittiming(dev); |
| 884 | if (err) |
| 885 | return err; |
| 886 | } |
| 887 | } |
| 888 | |
Marc Kleine-Budde | 49cb5c0 | 2013-10-04 15:40:22 +0200 | [diff] [blame] | 889 | if (data[IFLA_CAN_CTRLMODE]) { |
| 890 | struct can_ctrlmode *cm; |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 891 | u32 ctrlstatic; |
| 892 | u32 maskedflags; |
Marc Kleine-Budde | 49cb5c0 | 2013-10-04 15:40:22 +0200 | [diff] [blame] | 893 | |
| 894 | /* Do not allow changing controller mode while running */ |
| 895 | if (dev->flags & IFF_UP) |
| 896 | return -EBUSY; |
| 897 | cm = nla_data(data[IFLA_CAN_CTRLMODE]); |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 898 | ctrlstatic = priv->ctrlmode_static; |
| 899 | maskedflags = cm->flags & cm->mask; |
Oliver Hartkopp | 9b1087a | 2015-01-05 18:40:15 +0100 | [diff] [blame] | 900 | |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 901 | /* check whether provided bits are allowed to be passed */ |
| 902 | if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic)) |
| 903 | return -EOPNOTSUPP; |
| 904 | |
| 905 | /* do not check for static fd-non-iso if 'fd' is disabled */ |
| 906 | if (!(maskedflags & CAN_CTRLMODE_FD)) |
| 907 | ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO; |
| 908 | |
| 909 | /* make sure static options are provided by configuration */ |
| 910 | if ((maskedflags & ctrlstatic) != ctrlstatic) |
Marc Kleine-Budde | 49cb5c0 | 2013-10-04 15:40:22 +0200 | [diff] [blame] | 911 | return -EOPNOTSUPP; |
Oliver Hartkopp | 9b1087a | 2015-01-05 18:40:15 +0100 | [diff] [blame] | 912 | |
| 913 | /* clear bits to be modified and copy the flag values */ |
Marc Kleine-Budde | 49cb5c0 | 2013-10-04 15:40:22 +0200 | [diff] [blame] | 914 | priv->ctrlmode &= ~cm->mask; |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 915 | priv->ctrlmode |= maskedflags; |
Oliver Hartkopp | bc05a89 | 2014-02-28 16:36:24 +0100 | [diff] [blame] | 916 | |
| 917 | /* CAN_CTRLMODE_FD can only be set when driver supports FD */ |
| 918 | if (priv->ctrlmode & CAN_CTRLMODE_FD) |
| 919 | dev->mtu = CANFD_MTU; |
| 920 | else |
| 921 | dev->mtu = CAN_MTU; |
Marc Kleine-Budde | 49cb5c0 | 2013-10-04 15:40:22 +0200 | [diff] [blame] | 922 | } |
| 923 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 924 | if (data[IFLA_CAN_RESTART_MS]) { |
| 925 | /* Do not allow changing restart delay while running */ |
| 926 | if (dev->flags & IFF_UP) |
| 927 | return -EBUSY; |
| 928 | priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]); |
| 929 | } |
| 930 | |
| 931 | if (data[IFLA_CAN_RESTART]) { |
| 932 | /* Do not allow a restart while not running */ |
| 933 | if (!(dev->flags & IFF_UP)) |
| 934 | return -EINVAL; |
| 935 | err = can_restart_now(dev); |
| 936 | if (err) |
| 937 | return err; |
| 938 | } |
| 939 | |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 940 | if (data[IFLA_CAN_DATA_BITTIMING]) { |
| 941 | struct can_bittiming dbt; |
| 942 | |
| 943 | /* Do not allow changing bittiming while running */ |
| 944 | if (dev->flags & IFF_UP) |
| 945 | return -EBUSY; |
| 946 | memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]), |
| 947 | sizeof(dbt)); |
| 948 | err = can_get_bittiming(dev, &dbt, priv->data_bittiming_const); |
| 949 | if (err) |
| 950 | return err; |
| 951 | memcpy(&priv->data_bittiming, &dbt, sizeof(dbt)); |
| 952 | |
| 953 | if (priv->do_set_data_bittiming) { |
| 954 | /* Finally, set the bit-timing registers */ |
| 955 | err = priv->do_set_data_bittiming(dev); |
| 956 | if (err) |
| 957 | return err; |
| 958 | } |
| 959 | } |
| 960 | |
Oliver Hartkopp | 12a6075 | 2017-01-10 18:52:06 +0100 | [diff] [blame^] | 961 | if (data[IFLA_CAN_TERMINATION]) { |
| 962 | const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]); |
| 963 | const unsigned int num_term = priv->termination_const_cnt; |
| 964 | unsigned int i; |
| 965 | |
| 966 | if (!priv->do_set_termination) |
| 967 | return -EOPNOTSUPP; |
| 968 | |
| 969 | /* check whether given value is supported by the interface */ |
| 970 | for (i = 0; i < num_term; i++) { |
| 971 | if (termval == priv->termination_const[i]) |
| 972 | break; |
| 973 | } |
| 974 | if (i >= num_term) |
| 975 | return -EINVAL; |
| 976 | |
| 977 | /* Finally, set the termination value */ |
| 978 | err = priv->do_set_termination(dev, termval); |
| 979 | if (err) |
| 980 | return err; |
| 981 | |
| 982 | priv->termination = termval; |
| 983 | } |
| 984 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 985 | return 0; |
| 986 | } |
| 987 | |
Wolfgang Grandegger | 53a0ef86 | 2009-11-06 23:53:13 +0000 | [diff] [blame] | 988 | static size_t can_get_size(const struct net_device *dev) |
| 989 | { |
| 990 | struct can_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | c13c64d | 2013-10-05 20:34:47 +0200 | [diff] [blame] | 991 | size_t size = 0; |
Wolfgang Grandegger | 53a0ef86 | 2009-11-06 23:53:13 +0000 | [diff] [blame] | 992 | |
Oliver Hartkopp | b30749f | 2014-02-28 16:36:20 +0100 | [diff] [blame] | 993 | if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */ |
| 994 | size += nla_total_size(sizeof(struct can_bittiming)); |
Marc Kleine-Budde | c13c64d | 2013-10-05 20:34:47 +0200 | [diff] [blame] | 995 | if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */ |
Marc Kleine-Budde | fe119a0 | 2013-10-05 21:25:17 +0200 | [diff] [blame] | 996 | size += nla_total_size(sizeof(struct can_bittiming_const)); |
Marc Kleine-Budde | c13c64d | 2013-10-05 20:34:47 +0200 | [diff] [blame] | 997 | size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */ |
| 998 | size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */ |
| 999 | size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */ |
| 1000 | size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */ |
| 1001 | if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */ |
| 1002 | size += nla_total_size(sizeof(struct can_berr_counter)); |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1003 | if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */ |
| 1004 | size += nla_total_size(sizeof(struct can_bittiming)); |
| 1005 | if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */ |
| 1006 | size += nla_total_size(sizeof(struct can_bittiming_const)); |
Oliver Hartkopp | 12a6075 | 2017-01-10 18:52:06 +0100 | [diff] [blame^] | 1007 | if (priv->termination_const) { |
| 1008 | size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */ |
| 1009 | size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */ |
| 1010 | priv->termination_const_cnt); |
| 1011 | } |
Wolfgang Grandegger | 53a0ef86 | 2009-11-06 23:53:13 +0000 | [diff] [blame] | 1012 | |
| 1013 | return size; |
| 1014 | } |
| 1015 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1016 | static int can_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 1017 | { |
| 1018 | struct can_priv *priv = netdev_priv(dev); |
| 1019 | struct can_ctrlmode cm = {.flags = priv->ctrlmode}; |
Wolfgang Grandegger | 52c793f | 2010-02-22 22:21:17 +0000 | [diff] [blame] | 1020 | struct can_berr_counter bec; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1021 | enum can_state state = priv->state; |
| 1022 | |
| 1023 | if (priv->do_get_state) |
| 1024 | priv->do_get_state(dev, &state); |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1025 | |
Oliver Hartkopp | b30749f | 2014-02-28 16:36:20 +0100 | [diff] [blame] | 1026 | if ((priv->bittiming.bitrate && |
| 1027 | nla_put(skb, IFLA_CAN_BITTIMING, |
| 1028 | sizeof(priv->bittiming), &priv->bittiming)) || |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1029 | |
David S. Miller | 31e0e32 | 2012-04-01 20:21:11 -0400 | [diff] [blame] | 1030 | (priv->bittiming_const && |
| 1031 | nla_put(skb, IFLA_CAN_BITTIMING_CONST, |
Marc Kleine-Budde | 57a59b9 | 2013-10-04 13:35:57 +0200 | [diff] [blame] | 1032 | sizeof(*priv->bittiming_const), priv->bittiming_const)) || |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1033 | |
Marek Vasut | 562b103 | 2015-10-30 13:48:19 +0100 | [diff] [blame] | 1034 | nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) || |
Marc Kleine-Budde | 57a59b9 | 2013-10-04 13:35:57 +0200 | [diff] [blame] | 1035 | nla_put_u32(skb, IFLA_CAN_STATE, state) || |
| 1036 | nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) || |
| 1037 | nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) || |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1038 | |
Marc Kleine-Budde | 57a59b9 | 2013-10-04 13:35:57 +0200 | [diff] [blame] | 1039 | (priv->do_get_berr_counter && |
| 1040 | !priv->do_get_berr_counter(dev, &bec) && |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1041 | nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) || |
| 1042 | |
| 1043 | (priv->data_bittiming.bitrate && |
| 1044 | nla_put(skb, IFLA_CAN_DATA_BITTIMING, |
| 1045 | sizeof(priv->data_bittiming), &priv->data_bittiming)) || |
| 1046 | |
| 1047 | (priv->data_bittiming_const && |
| 1048 | nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST, |
| 1049 | sizeof(*priv->data_bittiming_const), |
Oliver Hartkopp | 12a6075 | 2017-01-10 18:52:06 +0100 | [diff] [blame^] | 1050 | priv->data_bittiming_const)) || |
| 1051 | |
| 1052 | (priv->termination_const && |
| 1053 | (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) || |
| 1054 | nla_put(skb, IFLA_CAN_TERMINATION_CONST, |
| 1055 | sizeof(*priv->termination_const) * |
| 1056 | priv->termination_const_cnt, |
| 1057 | priv->termination_const)))) |
| 1058 | |
Marc Kleine-Budde | 57a59b9 | 2013-10-04 13:35:57 +0200 | [diff] [blame] | 1059 | return -EMSGSIZE; |
Oliver Hartkopp | 9859ccd | 2014-02-28 16:36:23 +0100 | [diff] [blame] | 1060 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1061 | return 0; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Wolfgang Grandegger | 55369c0 | 2009-11-12 05:34:05 +0000 | [diff] [blame] | 1064 | static size_t can_get_xstats_size(const struct net_device *dev) |
| 1065 | { |
| 1066 | return sizeof(struct can_device_stats); |
| 1067 | } |
| 1068 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1069 | static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev) |
| 1070 | { |
| 1071 | struct can_priv *priv = netdev_priv(dev); |
| 1072 | |
David S. Miller | 31e0e32 | 2012-04-01 20:21:11 -0400 | [diff] [blame] | 1073 | if (nla_put(skb, IFLA_INFO_XSTATS, |
| 1074 | sizeof(priv->can_stats), &priv->can_stats)) |
| 1075 | goto nla_put_failure; |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1076 | return 0; |
| 1077 | |
| 1078 | nla_put_failure: |
| 1079 | return -EMSGSIZE; |
| 1080 | } |
| 1081 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1082 | static int can_newlink(struct net *src_net, struct net_device *dev, |
Oliver Hartkopp | 993e6f2 | 2009-08-11 02:41:24 +0000 | [diff] [blame] | 1083 | struct nlattr *tb[], struct nlattr *data[]) |
| 1084 | { |
| 1085 | return -EOPNOTSUPP; |
| 1086 | } |
| 1087 | |
Oliver Hartkopp | 25e1ed6 | 2016-06-21 15:45:47 +0200 | [diff] [blame] | 1088 | static void can_dellink(struct net_device *dev, struct list_head *head) |
| 1089 | { |
| 1090 | return; |
| 1091 | } |
| 1092 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1093 | static struct rtnl_link_ops can_link_ops __read_mostly = { |
| 1094 | .kind = "can", |
| 1095 | .maxtype = IFLA_CAN_MAX, |
| 1096 | .policy = can_policy, |
| 1097 | .setup = can_setup, |
Oliver Hartkopp | bb208f1 | 2016-03-21 20:18:21 +0100 | [diff] [blame] | 1098 | .validate = can_validate, |
Oliver Hartkopp | 993e6f2 | 2009-08-11 02:41:24 +0000 | [diff] [blame] | 1099 | .newlink = can_newlink, |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1100 | .changelink = can_changelink, |
Oliver Hartkopp | 25e1ed6 | 2016-06-21 15:45:47 +0200 | [diff] [blame] | 1101 | .dellink = can_dellink, |
Wolfgang Grandegger | 53a0ef86 | 2009-11-06 23:53:13 +0000 | [diff] [blame] | 1102 | .get_size = can_get_size, |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1103 | .fill_info = can_fill_info, |
Wolfgang Grandegger | 55369c0 | 2009-11-12 05:34:05 +0000 | [diff] [blame] | 1104 | .get_xstats_size = can_get_xstats_size, |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1105 | .fill_xstats = can_fill_xstats, |
| 1106 | }; |
| 1107 | |
| 1108 | /* |
| 1109 | * Register the CAN network device |
| 1110 | */ |
| 1111 | int register_candev(struct net_device *dev) |
| 1112 | { |
Oliver Hartkopp | 12a6075 | 2017-01-10 18:52:06 +0100 | [diff] [blame^] | 1113 | struct can_priv *priv = netdev_priv(dev); |
| 1114 | |
| 1115 | /* Ensure termination_const, termination_const_cnt and |
| 1116 | * do_set_termination consistency. All must be either set or |
| 1117 | * unset. |
| 1118 | */ |
| 1119 | if ((!priv->termination_const != !priv->termination_const_cnt) || |
| 1120 | (!priv->termination_const != !priv->do_set_termination)) |
| 1121 | return -EINVAL; |
| 1122 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1123 | dev->rtnl_link_ops = &can_link_ops; |
| 1124 | return register_netdev(dev); |
| 1125 | } |
| 1126 | EXPORT_SYMBOL_GPL(register_candev); |
| 1127 | |
| 1128 | /* |
| 1129 | * Unregister the CAN network device |
| 1130 | */ |
| 1131 | void unregister_candev(struct net_device *dev) |
| 1132 | { |
| 1133 | unregister_netdev(dev); |
| 1134 | } |
| 1135 | EXPORT_SYMBOL_GPL(unregister_candev); |
| 1136 | |
Kurt Van Dijck | bf03a53 | 2012-12-18 18:50:56 +0100 | [diff] [blame] | 1137 | /* |
| 1138 | * Test if a network device is a candev based device |
| 1139 | * and return the can_priv* if so. |
| 1140 | */ |
| 1141 | struct can_priv *safe_candev_priv(struct net_device *dev) |
| 1142 | { |
| 1143 | if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops)) |
| 1144 | return NULL; |
| 1145 | |
| 1146 | return netdev_priv(dev); |
| 1147 | } |
| 1148 | EXPORT_SYMBOL_GPL(safe_candev_priv); |
| 1149 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1150 | static __init int can_dev_init(void) |
| 1151 | { |
| 1152 | int err; |
| 1153 | |
Kurt Van Dijck | a1ef7bd | 2012-12-18 18:50:57 +0100 | [diff] [blame] | 1154 | can_led_notifier_init(); |
| 1155 | |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1156 | err = rtnl_link_register(&can_link_ops); |
| 1157 | if (!err) |
| 1158 | printk(KERN_INFO MOD_DESC "\n"); |
| 1159 | |
| 1160 | return err; |
| 1161 | } |
| 1162 | module_init(can_dev_init); |
| 1163 | |
| 1164 | static __exit void can_dev_exit(void) |
| 1165 | { |
| 1166 | rtnl_link_unregister(&can_link_ops); |
Kurt Van Dijck | a1ef7bd | 2012-12-18 18:50:57 +0100 | [diff] [blame] | 1167 | |
| 1168 | can_led_notifier_exit(); |
Wolfgang Grandegger | 39549ee | 2009-05-15 23:39:29 +0000 | [diff] [blame] | 1169 | } |
| 1170 | module_exit(can_dev_exit); |
| 1171 | |
| 1172 | MODULE_ALIAS_RTNL_LINK("can"); |