fakelb.c 6.4 KB

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