dummy_can.c 7.3 KB

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