fakelb.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Loopback IEEE 802.15.4 interface
  4. *
  5. * Copyright 2007-2012 Siemens AG
  6. *
  7. * Written by:
  8. * Sergey Lapin <[email protected]>
  9. * Dmitry Eremin-Solenikov <[email protected]>
  10. * Alexander Smirnov <[email protected]>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/timer.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/device.h>
  17. #include <linux/spinlock.h>
  18. #include <net/mac802154.h>
  19. #include <net/cfg802154.h>
  20. static int numlbs = 2;
  21. static LIST_HEAD(fakelb_phys);
  22. static DEFINE_MUTEX(fakelb_phys_lock);
  23. static LIST_HEAD(fakelb_ifup_phys);
  24. static DEFINE_RWLOCK(fakelb_ifup_phys_lock);
  25. struct fakelb_phy {
  26. struct ieee802154_hw *hw;
  27. u8 page;
  28. u8 channel;
  29. bool suspended;
  30. struct list_head list;
  31. struct list_head list_ifup;
  32. };
  33. static int fakelb_hw_ed(struct ieee802154_hw *hw, u8 *level)
  34. {
  35. WARN_ON(!level);
  36. *level = 0xbe;
  37. return 0;
  38. }
  39. static int fakelb_hw_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
  40. {
  41. struct fakelb_phy *phy = hw->priv;
  42. write_lock_bh(&fakelb_ifup_phys_lock);
  43. phy->page = page;
  44. phy->channel = channel;
  45. write_unlock_bh(&fakelb_ifup_phys_lock);
  46. return 0;
  47. }
  48. static int fakelb_hw_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
  49. {
  50. struct fakelb_phy *current_phy = hw->priv, *phy;
  51. read_lock_bh(&fakelb_ifup_phys_lock);
  52. WARN_ON(current_phy->suspended);
  53. list_for_each_entry(phy, &fakelb_ifup_phys, list_ifup) {
  54. if (current_phy == phy)
  55. continue;
  56. if (current_phy->page == phy->page &&
  57. current_phy->channel == phy->channel) {
  58. struct sk_buff *newskb = pskb_copy(skb, GFP_ATOMIC);
  59. if (newskb)
  60. ieee802154_rx_irqsafe(phy->hw, newskb, 0xcc);
  61. }
  62. }
  63. read_unlock_bh(&fakelb_ifup_phys_lock);
  64. ieee802154_xmit_complete(hw, skb, false);
  65. return 0;
  66. }
  67. static int fakelb_hw_start(struct ieee802154_hw *hw)
  68. {
  69. struct fakelb_phy *phy = hw->priv;
  70. write_lock_bh(&fakelb_ifup_phys_lock);
  71. phy->suspended = false;
  72. list_add(&phy->list_ifup, &fakelb_ifup_phys);
  73. write_unlock_bh(&fakelb_ifup_phys_lock);
  74. return 0;
  75. }
  76. static void fakelb_hw_stop(struct ieee802154_hw *hw)
  77. {
  78. struct fakelb_phy *phy = hw->priv;
  79. write_lock_bh(&fakelb_ifup_phys_lock);
  80. phy->suspended = true;
  81. list_del(&phy->list_ifup);
  82. write_unlock_bh(&fakelb_ifup_phys_lock);
  83. }
  84. static int
  85. fakelb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
  86. {
  87. return 0;
  88. }
  89. static const struct ieee802154_ops fakelb_ops = {
  90. .owner = THIS_MODULE,
  91. .xmit_async = fakelb_hw_xmit,
  92. .ed = fakelb_hw_ed,
  93. .set_channel = fakelb_hw_channel,
  94. .start = fakelb_hw_start,
  95. .stop = fakelb_hw_stop,
  96. .set_promiscuous_mode = fakelb_set_promiscuous_mode,
  97. };
  98. /* Number of dummy devices to be set up by this module. */
  99. module_param(numlbs, int, 0);
  100. MODULE_PARM_DESC(numlbs, " number of pseudo devices");
  101. static int fakelb_add_one(struct device *dev)
  102. {
  103. struct ieee802154_hw *hw;
  104. struct fakelb_phy *phy;
  105. int err;
  106. hw = ieee802154_alloc_hw(sizeof(*phy), &fakelb_ops);
  107. if (!hw)
  108. return -ENOMEM;
  109. phy = hw->priv;
  110. phy->hw = hw;
  111. /* 868 MHz BPSK 802.15.4-2003 */
  112. hw->phy->supported.channels[0] |= 1;
  113. /* 915 MHz BPSK 802.15.4-2003 */
  114. hw->phy->supported.channels[0] |= 0x7fe;
  115. /* 2.4 GHz O-QPSK 802.15.4-2003 */
  116. hw->phy->supported.channels[0] |= 0x7FFF800;
  117. /* 868 MHz ASK 802.15.4-2006 */
  118. hw->phy->supported.channels[1] |= 1;
  119. /* 915 MHz ASK 802.15.4-2006 */
  120. hw->phy->supported.channels[1] |= 0x7fe;
  121. /* 868 MHz O-QPSK 802.15.4-2006 */
  122. hw->phy->supported.channels[2] |= 1;
  123. /* 915 MHz O-QPSK 802.15.4-2006 */
  124. hw->phy->supported.channels[2] |= 0x7fe;
  125. /* 2.4 GHz CSS 802.15.4a-2007 */
  126. hw->phy->supported.channels[3] |= 0x3fff;
  127. /* UWB Sub-gigahertz 802.15.4a-2007 */
  128. hw->phy->supported.channels[4] |= 1;
  129. /* UWB Low band 802.15.4a-2007 */
  130. hw->phy->supported.channels[4] |= 0x1e;
  131. /* UWB High band 802.15.4a-2007 */
  132. hw->phy->supported.channels[4] |= 0xffe0;
  133. /* 750 MHz O-QPSK 802.15.4c-2009 */
  134. hw->phy->supported.channels[5] |= 0xf;
  135. /* 750 MHz MPSK 802.15.4c-2009 */
  136. hw->phy->supported.channels[5] |= 0xf0;
  137. /* 950 MHz BPSK 802.15.4d-2009 */
  138. hw->phy->supported.channels[6] |= 0x3ff;
  139. /* 950 MHz GFSK 802.15.4d-2009 */
  140. hw->phy->supported.channels[6] |= 0x3ffc00;
  141. ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
  142. /* fake phy channel 13 as default */
  143. hw->phy->current_channel = 13;
  144. phy->channel = hw->phy->current_channel;
  145. hw->flags = IEEE802154_HW_PROMISCUOUS;
  146. hw->parent = dev;
  147. err = ieee802154_register_hw(hw);
  148. if (err)
  149. goto err_reg;
  150. mutex_lock(&fakelb_phys_lock);
  151. list_add_tail(&phy->list, &fakelb_phys);
  152. mutex_unlock(&fakelb_phys_lock);
  153. return 0;
  154. err_reg:
  155. ieee802154_free_hw(phy->hw);
  156. return err;
  157. }
  158. static void fakelb_del(struct fakelb_phy *phy)
  159. {
  160. list_del(&phy->list);
  161. ieee802154_unregister_hw(phy->hw);
  162. ieee802154_free_hw(phy->hw);
  163. }
  164. static int fakelb_probe(struct platform_device *pdev)
  165. {
  166. struct fakelb_phy *phy, *tmp;
  167. int err, i;
  168. for (i = 0; i < numlbs; i++) {
  169. err = fakelb_add_one(&pdev->dev);
  170. if (err < 0)
  171. goto err_slave;
  172. }
  173. dev_info(&pdev->dev, "added %i fake ieee802154 hardware devices\n", numlbs);
  174. return 0;
  175. err_slave:
  176. mutex_lock(&fakelb_phys_lock);
  177. list_for_each_entry_safe(phy, tmp, &fakelb_phys, list)
  178. fakelb_del(phy);
  179. mutex_unlock(&fakelb_phys_lock);
  180. return err;
  181. }
  182. static int fakelb_remove(struct platform_device *pdev)
  183. {
  184. struct fakelb_phy *phy, *tmp;
  185. mutex_lock(&fakelb_phys_lock);
  186. list_for_each_entry_safe(phy, tmp, &fakelb_phys, list)
  187. fakelb_del(phy);
  188. mutex_unlock(&fakelb_phys_lock);
  189. return 0;
  190. }
  191. static struct platform_device *ieee802154fake_dev;
  192. static struct platform_driver ieee802154fake_driver = {
  193. .probe = fakelb_probe,
  194. .remove = fakelb_remove,
  195. .driver = {
  196. .name = "ieee802154fakelb",
  197. },
  198. };
  199. static __init int fakelb_init_module(void)
  200. {
  201. ieee802154fake_dev = platform_device_register_simple(
  202. "ieee802154fakelb", -1, NULL, 0);
  203. pr_warn("fakelb driver is marked as deprecated, please use mac802154_hwsim!\n");
  204. return platform_driver_register(&ieee802154fake_driver);
  205. }
  206. static __exit void fake_remove_module(void)
  207. {
  208. platform_driver_unregister(&ieee802154fake_driver);
  209. platform_device_unregister(ieee802154fake_dev);
  210. }
  211. module_init(fakelb_init_module);
  212. module_exit(fake_remove_module);
  213. MODULE_LICENSE("GPL");