fakelb.c 6.7 KB

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