1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2025 Vincent Mailhol <mailhol@kernel.org> */
#include <linux/array_size.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/units.h>
#include <linux/string_choices.h>
#include <linux/can.h>
#include <linux/can/bittiming.h>
#include <linux/can/dev.h>
#include <linux/can/skb.h>
struct dummy_can {
struct can_priv can;
struct net_device *dev;
};
static struct dummy_can *dummy_can;
static const struct can_bittiming_const dummy_can_bittiming_const = {
.name = "dummy_can CC",
.tseg1_min = 2,
.tseg1_max = 256,
.tseg2_min = 2,
.tseg2_max = 128,
.sjw_max = 128,
.brp_min = 1,
.brp_max = 512,
.brp_inc = 1
};
static const struct can_bittiming_const dummy_can_fd_databittiming_const = {
.name = "dummy_can FD",
.tseg1_min = 2,
.tseg1_max = 256,
.tseg2_min = 2,
.tseg2_max = 128,
.sjw_max = 128,
.brp_min = 1,
.brp_max = 512,
.brp_inc = 1
};
static const struct can_tdc_const dummy_can_fd_tdc_const = {
.tdcv_min = 0,
.tdcv_max = 0, /* Manual mode not supported. */
.tdco_min = 0,
.tdco_max = 127,
.tdcf_min = 0,
.tdcf_max = 127
};
static const struct can_bittiming_const dummy_can_xl_databittiming_const = {
.name = "dummy_can XL",
.tseg1_min = 2,
.tseg1_max = 256,
.tseg2_min = 2,
.tseg2_max = 128,
.sjw_max = 128,
.brp_min = 1,
.brp_max = 512,
.brp_inc = 1
};
static const struct can_tdc_const dummy_can_xl_tdc_const = {
.tdcv_min = 0,
.tdcv_max = 0, /* Manual mode not supported. */
.tdco_min = 0,
.tdco_max = 127,
.tdcf_min = 0,
.tdcf_max = 127
};
static const struct can_pwm_const dummy_can_pwm_const = {
.pwms_min = 1,
.pwms_max = 8,
.pwml_min = 2,
.pwml_max = 24,
.pwmo_min = 0,
.pwmo_max = 16,
};
static void dummy_can_print_bittiming(struct net_device *dev,
struct can_bittiming *bt)
{
netdev_dbg(dev, "\tbitrate: %u\n", bt->bitrate);
netdev_dbg(dev, "\tsample_point: %u\n", bt->sample_point);
netdev_dbg(dev, "\ttq: %u\n", bt->tq);
netdev_dbg(dev, "\tprop_seg: %u\n", bt->prop_seg);
netdev_dbg(dev, "\tphase_seg1: %u\n", bt->phase_seg1);
netdev_dbg(dev, "\tphase_seg2: %u\n", bt->phase_seg2);
netdev_dbg(dev, "\tsjw: %u\n", bt->sjw);
netdev_dbg(dev, "\tbrp: %u\n", bt->brp);
}
static void dummy_can_print_tdc(struct net_device *dev, struct can_tdc *tdc)
{
netdev_dbg(dev, "\t\ttdcv: %u\n", tdc->tdcv);
netdev_dbg(dev, "\t\ttdco: %u\n", tdc->tdco);
netdev_dbg(dev, "\t\ttdcf: %u\n", tdc->tdcf);
}
static void dummy_can_print_pwm(struct net_device *dev, struct can_pwm *pwm,
struct can_bittiming *dbt)
{
netdev_dbg(dev, "\t\tpwms: %u\n", pwm->pwms);
netdev_dbg(dev, "\t\tpwml: %u\n", pwm->pwml);
netdev_dbg(dev, "\t\tpwmo: %u\n", pwm->pwmo);
}
static void dummy_can_print_ctrlmode(struct net_device *dev)
{
struct dummy_can *priv = netdev_priv(dev);
struct can_priv *can_priv = &priv->can;
unsigned long supported = can_priv->ctrlmode_supported;
u32 enabled = can_priv->ctrlmode;
netdev_dbg(dev, "Control modes:\n");
netdev_dbg(dev, "\tsupported: 0x%08x\n", (u32)supported);
netdev_dbg(dev, "\tenabled: 0x%08x\n", enabled);
if (supported) {
int idx;
netdev_dbg(dev, "\tlist:");
for_each_set_bit(idx, &supported, BITS_PER_TYPE(u32))
netdev_dbg(dev, "\t\t%s: %s\n",
can_get_ctrlmode_str(BIT(idx)),
enabled & BIT(idx) ? "on" : "off");
}
}
static void dummy_can_print_bittiming_info(struct net_device *dev)
{
struct dummy_can *priv = netdev_priv(dev);
struct can_priv *can_priv = &priv->can;
netdev_dbg(dev, "Clock frequency: %u\n", can_priv->clock.freq);
netdev_dbg(dev, "Maximum bitrate: %u\n", can_priv->bitrate_max);
netdev_dbg(dev, "MTU: %u\n", dev->mtu);
netdev_dbg(dev, "\n");
dummy_can_print_ctrlmode(dev);
netdev_dbg(dev, "\n");
netdev_dbg(dev, "Classical CAN nominal bittiming:\n");
dummy_can_print_bittiming(dev, &can_priv->bittiming);
netdev_dbg(dev, "\n");
if (can_priv->ctrlmode & CAN_CTRLMODE_FD) {
netdev_dbg(dev, "CAN FD databittiming:\n");
dummy_can_print_bittiming(dev, &can_priv->fd.data_bittiming);
if (can_fd_tdc_is_enabled(can_priv)) {
netdev_dbg(dev, "\tCAN FD TDC:\n");
dummy_can_print_tdc(dev, &can_priv->fd.tdc);
}
}
netdev_dbg(dev, "\n");
if (can_priv->ctrlmode & CAN_CTRLMODE_XL) {
netdev_dbg(dev, "CAN XL databittiming:\n");
dummy_can_print_bittiming(dev, &can_priv->xl.data_bittiming);
if (can_xl_tdc_is_enabled(can_priv)) {
netdev_dbg(dev, "\tCAN XL TDC:\n");
dummy_can_print_tdc(dev, &can_priv->xl.tdc);
}
if (can_priv->ctrlmode & CAN_CTRLMODE_XL_TMS) {
netdev_dbg(dev, "\tCAN XL PWM:\n");
dummy_can_print_pwm(dev, &can_priv->xl.pwm,
&can_priv->xl.data_bittiming);
}
}
netdev_dbg(dev, "\n");
}
static int dummy_can_netdev_open(struct net_device *dev)
{
int ret;
struct can_priv *priv = netdev_priv(dev);
dummy_can_print_bittiming_info(dev);
netdev_dbg(dev, "error-signalling is %s\n",
str_enabled_disabled(!can_dev_in_xl_only_mode(priv)));
ret = open_candev(dev);
if (ret)
return ret;
netif_start_queue(dev);
netdev_dbg(dev, "dummy-can is up\n");
return 0;
}
static int dummy_can_netdev_close(struct net_device *dev)
{
netif_stop_queue(dev);
close_candev(dev);
netdev_dbg(dev, "dummy-can is down\n");
return 0;
}
static netdev_tx_t dummy_can_start_xmit(struct sk_buff *skb,
struct net_device *dev)
{
if (can_dev_dropped_skb(dev, skb))
return NETDEV_TX_OK;
can_put_echo_skb(skb, dev, 0, 0);
dev->stats.tx_packets++;
dev->stats.tx_bytes += can_get_echo_skb(dev, 0, NULL);
return NETDEV_TX_OK;
}
static const struct net_device_ops dummy_can_netdev_ops = {
.ndo_open = dummy_can_netdev_open,
.ndo_stop = dummy_can_netdev_close,
.ndo_start_xmit = dummy_can_start_xmit,
};
static const struct ethtool_ops dummy_can_ethtool_ops = {
.get_ts_info = ethtool_op_get_ts_info,
};
static int __init dummy_can_init(void)
{
struct net_device *dev;
struct dummy_can *priv;
int ret;
dev = alloc_candev(sizeof(*priv), 1);
if (!dev)
return -ENOMEM;
dev->netdev_ops = &dummy_can_netdev_ops;
dev->ethtool_ops = &dummy_can_ethtool_ops;
priv = netdev_priv(dev);
priv->can.bittiming_const = &dummy_can_bittiming_const;
priv->can.bitrate_max = 20 * MEGA /* BPS */;
priv->can.clock.freq = 160 * MEGA /* Hz */;
priv->can.fd.data_bittiming_const = &dummy_can_fd_databittiming_const;
priv->can.fd.tdc_const = &dummy_can_fd_tdc_const;
priv->can.xl.data_bittiming_const = &dummy_can_xl_databittiming_const;
priv->can.xl.tdc_const = &dummy_can_xl_tdc_const;
priv->can.xl.pwm_const = &dummy_can_pwm_const;
priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY |
CAN_CTRLMODE_FD | CAN_CTRLMODE_TDC_AUTO |
CAN_CTRLMODE_RESTRICTED | CAN_CTRLMODE_XL |
CAN_CTRLMODE_XL_TDC_AUTO | CAN_CTRLMODE_XL_TMS;
priv->dev = dev;
ret = register_candev(priv->dev);
if (ret) {
free_candev(priv->dev);
return ret;
}
dummy_can = priv;
netdev_dbg(dev, "dummy-can ready\n");
return 0;
}
static void __exit dummy_can_exit(void)
{
struct net_device *dev = dummy_can->dev;
netdev_dbg(dev, "dummy-can bye bye\n");
unregister_candev(dev);
free_candev(dev);
}
module_init(dummy_can_init);
module_exit(dummy_can_exit);
MODULE_DESCRIPTION("A dummy CAN driver, mainly to test the netlink interface");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Vincent Mailhol <mailhol@kernel.org>");
|