fakelb.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 = 1;
  29. struct fakelb_dev_priv {
  30. struct ieee802154_hw *hw;
  31. struct list_head list;
  32. struct fakelb_priv *fake;
  33. spinlock_t lock;
  34. bool working;
  35. };
  36. struct fakelb_priv {
  37. struct list_head list;
  38. rwlock_t lock;
  39. };
  40. static int
  41. fakelb_hw_ed(struct ieee802154_hw *hw, u8 *level)
  42. {
  43. BUG_ON(!level);
  44. *level = 0xbe;
  45. return 0;
  46. }
  47. static int
  48. fakelb_hw_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
  49. {
  50. pr_debug("set channel to %d\n", channel);
  51. return 0;
  52. }
  53. static void
  54. fakelb_hw_deliver(struct fakelb_dev_priv *priv, struct sk_buff *skb)
  55. {
  56. struct sk_buff *newskb;
  57. spin_lock(&priv->lock);
  58. if (priv->working) {
  59. newskb = pskb_copy(skb, GFP_ATOMIC);
  60. ieee802154_rx_irqsafe(priv->hw, newskb, 0xcc);
  61. }
  62. spin_unlock(&priv->lock);
  63. }
  64. static int
  65. fakelb_hw_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
  66. {
  67. struct fakelb_dev_priv *priv = hw->priv;
  68. struct fakelb_priv *fake = priv->fake;
  69. read_lock_bh(&fake->lock);
  70. if (priv->list.next == priv->list.prev) {
  71. /* we are the only one device */
  72. fakelb_hw_deliver(priv, skb);
  73. } else {
  74. struct fakelb_dev_priv *dp;
  75. list_for_each_entry(dp, &priv->fake->list, list) {
  76. if (dp != priv &&
  77. (dp->hw->phy->current_channel ==
  78. priv->hw->phy->current_channel))
  79. fakelb_hw_deliver(dp, skb);
  80. }
  81. }
  82. read_unlock_bh(&fake->lock);
  83. return 0;
  84. }
  85. static int
  86. fakelb_hw_start(struct ieee802154_hw *hw) {
  87. struct fakelb_dev_priv *priv = hw->priv;
  88. int ret = 0;
  89. spin_lock(&priv->lock);
  90. if (priv->working)
  91. ret = -EBUSY;
  92. else
  93. priv->working = 1;
  94. spin_unlock(&priv->lock);
  95. return ret;
  96. }
  97. static void
  98. fakelb_hw_stop(struct ieee802154_hw *hw) {
  99. struct fakelb_dev_priv *priv = hw->priv;
  100. spin_lock(&priv->lock);
  101. priv->working = 0;
  102. spin_unlock(&priv->lock);
  103. }
  104. static const struct ieee802154_ops fakelb_ops = {
  105. .owner = THIS_MODULE,
  106. .xmit_sync = fakelb_hw_xmit,
  107. .ed = fakelb_hw_ed,
  108. .set_channel = fakelb_hw_channel,
  109. .start = fakelb_hw_start,
  110. .stop = fakelb_hw_stop,
  111. };
  112. /* Number of dummy devices to be set up by this module. */
  113. module_param(numlbs, int, 0);
  114. MODULE_PARM_DESC(numlbs, " number of pseudo devices");
  115. static int fakelb_add_one(struct device *dev, struct fakelb_priv *fake)
  116. {
  117. struct fakelb_dev_priv *priv;
  118. int err;
  119. struct ieee802154_hw *hw;
  120. hw = ieee802154_alloc_hw(sizeof(*priv), &fakelb_ops);
  121. if (!hw)
  122. return -ENOMEM;
  123. priv = hw->priv;
  124. priv->hw = hw;
  125. /* 868 MHz BPSK 802.15.4-2003 */
  126. hw->phy->channels_supported[0] |= 1;
  127. /* 915 MHz BPSK 802.15.4-2003 */
  128. hw->phy->channels_supported[0] |= 0x7fe;
  129. /* 2.4 GHz O-QPSK 802.15.4-2003 */
  130. hw->phy->channels_supported[0] |= 0x7FFF800;
  131. /* 868 MHz ASK 802.15.4-2006 */
  132. hw->phy->channels_supported[1] |= 1;
  133. /* 915 MHz ASK 802.15.4-2006 */
  134. hw->phy->channels_supported[1] |= 0x7fe;
  135. /* 868 MHz O-QPSK 802.15.4-2006 */
  136. hw->phy->channels_supported[2] |= 1;
  137. /* 915 MHz O-QPSK 802.15.4-2006 */
  138. hw->phy->channels_supported[2] |= 0x7fe;
  139. /* 2.4 GHz CSS 802.15.4a-2007 */
  140. hw->phy->channels_supported[3] |= 0x3fff;
  141. /* UWB Sub-gigahertz 802.15.4a-2007 */
  142. hw->phy->channels_supported[4] |= 1;
  143. /* UWB Low band 802.15.4a-2007 */
  144. hw->phy->channels_supported[4] |= 0x1e;
  145. /* UWB High band 802.15.4a-2007 */
  146. hw->phy->channels_supported[4] |= 0xffe0;
  147. /* 750 MHz O-QPSK 802.15.4c-2009 */
  148. hw->phy->channels_supported[5] |= 0xf;
  149. /* 750 MHz MPSK 802.15.4c-2009 */
  150. hw->phy->channels_supported[5] |= 0xf0;
  151. /* 950 MHz BPSK 802.15.4d-2009 */
  152. hw->phy->channels_supported[6] |= 0x3ff;
  153. /* 950 MHz GFSK 802.15.4d-2009 */
  154. hw->phy->channels_supported[6] |= 0x3ffc00;
  155. INIT_LIST_HEAD(&priv->list);
  156. priv->fake = fake;
  157. spin_lock_init(&priv->lock);
  158. hw->parent = dev;
  159. err = ieee802154_register_hw(hw);
  160. if (err)
  161. goto err_reg;
  162. write_lock_bh(&fake->lock);
  163. list_add_tail(&priv->list, &fake->list);
  164. write_unlock_bh(&fake->lock);
  165. return 0;
  166. err_reg:
  167. ieee802154_free_hw(priv->hw);
  168. return err;
  169. }
  170. static void fakelb_del(struct fakelb_dev_priv *priv)
  171. {
  172. write_lock_bh(&priv->fake->lock);
  173. list_del(&priv->list);
  174. write_unlock_bh(&priv->fake->lock);
  175. ieee802154_unregister_hw(priv->hw);
  176. ieee802154_free_hw(priv->hw);
  177. }
  178. static int fakelb_probe(struct platform_device *pdev)
  179. {
  180. struct fakelb_priv *priv;
  181. struct fakelb_dev_priv *dp;
  182. int err = -ENOMEM;
  183. int i;
  184. priv = devm_kzalloc(&pdev->dev, sizeof(struct fakelb_priv),
  185. GFP_KERNEL);
  186. if (!priv)
  187. goto err_alloc;
  188. INIT_LIST_HEAD(&priv->list);
  189. rwlock_init(&priv->lock);
  190. for (i = 0; i < numlbs; i++) {
  191. err = fakelb_add_one(&pdev->dev, priv);
  192. if (err < 0)
  193. goto err_slave;
  194. }
  195. platform_set_drvdata(pdev, priv);
  196. dev_info(&pdev->dev, "added ieee802154 hardware\n");
  197. return 0;
  198. err_slave:
  199. list_for_each_entry(dp, &priv->list, list)
  200. fakelb_del(dp);
  201. err_alloc:
  202. return err;
  203. }
  204. static int fakelb_remove(struct platform_device *pdev)
  205. {
  206. struct fakelb_priv *priv = platform_get_drvdata(pdev);
  207. struct fakelb_dev_priv *dp, *temp;
  208. list_for_each_entry_safe(dp, temp, &priv->list, list)
  209. fakelb_del(dp);
  210. return 0;
  211. }
  212. static struct platform_device *ieee802154fake_dev;
  213. static struct platform_driver ieee802154fake_driver = {
  214. .probe = fakelb_probe,
  215. .remove = fakelb_remove,
  216. .driver = {
  217. .name = "ieee802154fakelb",
  218. },
  219. };
  220. static __init int fakelb_init_module(void)
  221. {
  222. ieee802154fake_dev = platform_device_register_simple(
  223. "ieee802154fakelb", -1, NULL, 0);
  224. return platform_driver_register(&ieee802154fake_driver);
  225. }
  226. static __exit void fake_remove_module(void)
  227. {
  228. platform_driver_unregister(&ieee802154fake_driver);
  229. platform_device_unregister(ieee802154fake_dev);
  230. }
  231. module_init(fakelb_init_module);
  232. module_exit(fake_remove_module);
  233. MODULE_LICENSE("GPL");