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