fakelb.c 6.4 KB

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