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