blob: 860d4aed82743cebe1db9c871e37b23020e580b9 [file] [log] [blame]
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +00001/*
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 *
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000015 * 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
21#include <linux/module.h>
22#include <linux/timer.h>
23#include <linux/platform_device.h>
24#include <linux/netdevice.h>
Himangi Saraogi12b5c382014-05-19 22:25:11 +053025#include <linux/device.h>
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000026#include <linux/spinlock.h>
27#include <net/mac802154.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020028#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000029
Alexander Aring7e579052015-05-17 21:44:58 +020030static int numlbs = 2;
Alexander Aringe369dc82015-05-17 21:45:05 +020031
Alexander Aringb82b99f2015-05-17 21:45:03 +020032static LIST_HEAD(fakelb_phys);
Alexander Aringe369dc82015-05-17 21:45:05 +020033static DEFINE_SPINLOCK(fakelb_phys_lock);
34
35static LIST_HEAD(fakelb_ifup_phys);
36static DEFINE_RWLOCK(fakelb_ifup_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000037
Alexander Aringdb3f5d02015-05-17 21:45:00 +020038struct fakelb_phy {
Alexander Aring5a504392014-10-25 17:16:34 +020039 struct ieee802154_hw *hw;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000040
Alexander Aring12da8d92015-05-17 21:45:06 +020041 u8 page;
42 u8 channel;
43
Alexander Aring789a99e2015-06-14 16:48:44 +020044 bool suspended;
45
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000046 struct list_head list;
Alexander Aringe369dc82015-05-17 21:45:05 +020047 struct list_head list_ifup;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000048};
49
Alexander Aring39572ab2015-05-17 21:45:10 +020050static int fakelb_hw_ed(struct ieee802154_hw *hw, u8 *level)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000051{
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000052 BUG_ON(!level);
53 *level = 0xbe;
54
55 return 0;
56}
57
Alexander Aring39572ab2015-05-17 21:45:10 +020058static int fakelb_hw_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000059{
Alexander Aring12da8d92015-05-17 21:45:06 +020060 struct fakelb_phy *phy = hw->priv;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000061
Alexander Aring12da8d92015-05-17 21:45:06 +020062 write_lock_bh(&fakelb_ifup_phys_lock);
63 phy->page = page;
64 phy->channel = channel;
65 write_unlock_bh(&fakelb_ifup_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000066 return 0;
67}
68
Alexander Aring39572ab2015-05-17 21:45:10 +020069static int fakelb_hw_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000070{
Alexander Aring39572ab2015-05-17 21:45:10 +020071 struct fakelb_phy *current_phy = hw->priv, *phy;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000072
Alexander Aringe369dc82015-05-17 21:45:05 +020073 read_lock_bh(&fakelb_ifup_phys_lock);
Alexander Aring789a99e2015-06-14 16:48:44 +020074 WARN_ON(current_phy->suspended);
Alexander Aringe369dc82015-05-17 21:45:05 +020075 list_for_each_entry(phy, &fakelb_ifup_phys, list_ifup) {
Alexander Aring9f8b97f2015-05-17 21:45:01 +020076 if (current_phy == phy)
77 continue;
Alexander Aringdb3f5d02015-05-17 21:45:00 +020078
Alexander Aring12da8d92015-05-17 21:45:06 +020079 if (current_phy->page == phy->page &&
Alexander Aring98be1652015-05-17 21:45:08 +020080 current_phy->channel == phy->channel) {
81 struct sk_buff *newskb = pskb_copy(skb, GFP_ATOMIC);
82
83 if (newskb)
84 ieee802154_rx_irqsafe(phy->hw, newskb, 0xcc);
85 }
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000086 }
Alexander Aringe369dc82015-05-17 21:45:05 +020087 read_unlock_bh(&fakelb_ifup_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000088
Alexander Aringa09c07a2015-05-17 21:45:09 +020089 ieee802154_xmit_complete(hw, skb, false);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000090 return 0;
91}
92
Alexander Aring39572ab2015-05-17 21:45:10 +020093static int fakelb_hw_start(struct ieee802154_hw *hw)
94{
Alexander Aringdb3f5d02015-05-17 21:45:00 +020095 struct fakelb_phy *phy = hw->priv;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000096
Alexander Aringe369dc82015-05-17 21:45:05 +020097 write_lock_bh(&fakelb_ifup_phys_lock);
Alexander Aring789a99e2015-06-14 16:48:44 +020098 phy->suspended = false;
Alexander Aringe369dc82015-05-17 21:45:05 +020099 list_add(&phy->list_ifup, &fakelb_ifup_phys);
100 write_unlock_bh(&fakelb_ifup_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000101
Alexander Aringe369dc82015-05-17 21:45:05 +0200102 return 0;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000103}
104
Alexander Aring39572ab2015-05-17 21:45:10 +0200105static void fakelb_hw_stop(struct ieee802154_hw *hw)
106{
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200107 struct fakelb_phy *phy = hw->priv;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000108
Alexander Aringe369dc82015-05-17 21:45:05 +0200109 write_lock_bh(&fakelb_ifup_phys_lock);
Alexander Aring789a99e2015-06-14 16:48:44 +0200110 phy->suspended = true;
Alexander Aringe369dc82015-05-17 21:45:05 +0200111 list_del(&phy->list_ifup);
112 write_unlock_bh(&fakelb_ifup_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000113}
114
Alexander Aring16301862014-10-28 18:21:18 +0100115static const struct ieee802154_ops fakelb_ops = {
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000116 .owner = THIS_MODULE,
Alexander Aringa09c07a2015-05-17 21:45:09 +0200117 .xmit_async = fakelb_hw_xmit,
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000118 .ed = fakelb_hw_ed,
119 .set_channel = fakelb_hw_channel,
120 .start = fakelb_hw_start,
121 .stop = fakelb_hw_stop,
122};
123
124/* Number of dummy devices to be set up by this module. */
125module_param(numlbs, int, 0);
126MODULE_PARM_DESC(numlbs, " number of pseudo devices");
127
Alexander Aringb82b99f2015-05-17 21:45:03 +0200128static int fakelb_add_one(struct device *dev)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000129{
Alexander Aring39572ab2015-05-17 21:45:10 +0200130 struct ieee802154_hw *hw;
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200131 struct fakelb_phy *phy;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000132 int err;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000133
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200134 hw = ieee802154_alloc_hw(sizeof(*phy), &fakelb_ops);
Alexander Aring5a504392014-10-25 17:16:34 +0200135 if (!hw)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000136 return -ENOMEM;
137
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200138 phy = hw->priv;
139 phy->hw = hw;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000140
141 /* 868 MHz BPSK 802.15.4-2003 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200142 hw->phy->supported.channels[0] |= 1;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000143 /* 915 MHz BPSK 802.15.4-2003 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200144 hw->phy->supported.channels[0] |= 0x7fe;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000145 /* 2.4 GHz O-QPSK 802.15.4-2003 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200146 hw->phy->supported.channels[0] |= 0x7FFF800;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000147 /* 868 MHz ASK 802.15.4-2006 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200148 hw->phy->supported.channels[1] |= 1;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000149 /* 915 MHz ASK 802.15.4-2006 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200150 hw->phy->supported.channels[1] |= 0x7fe;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000151 /* 868 MHz O-QPSK 802.15.4-2006 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200152 hw->phy->supported.channels[2] |= 1;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000153 /* 915 MHz O-QPSK 802.15.4-2006 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200154 hw->phy->supported.channels[2] |= 0x7fe;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000155 /* 2.4 GHz CSS 802.15.4a-2007 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200156 hw->phy->supported.channels[3] |= 0x3fff;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000157 /* UWB Sub-gigahertz 802.15.4a-2007 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200158 hw->phy->supported.channels[4] |= 1;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000159 /* UWB Low band 802.15.4a-2007 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200160 hw->phy->supported.channels[4] |= 0x1e;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000161 /* UWB High band 802.15.4a-2007 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200162 hw->phy->supported.channels[4] |= 0xffe0;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000163 /* 750 MHz O-QPSK 802.15.4c-2009 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200164 hw->phy->supported.channels[5] |= 0xf;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000165 /* 750 MHz MPSK 802.15.4c-2009 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200166 hw->phy->supported.channels[5] |= 0xf0;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000167 /* 950 MHz BPSK 802.15.4d-2009 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200168 hw->phy->supported.channels[6] |= 0x3ff;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000169 /* 950 MHz GFSK 802.15.4d-2009 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200170 hw->phy->supported.channels[6] |= 0x3ffc00;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000171
Alexander Aringe214a902015-05-17 21:45:07 +0200172 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
173 /* fake phy channel 13 as default */
174 hw->phy->current_channel = 13;
175 phy->channel = hw->phy->current_channel;
176
Alexander Aring5a504392014-10-25 17:16:34 +0200177 hw->parent = dev;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000178
Alexander Aring5a504392014-10-25 17:16:34 +0200179 err = ieee802154_register_hw(hw);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000180 if (err)
181 goto err_reg;
182
Alexander Aringe369dc82015-05-17 21:45:05 +0200183 spin_lock(&fakelb_phys_lock);
Alexander Aringb82b99f2015-05-17 21:45:03 +0200184 list_add_tail(&phy->list, &fakelb_phys);
Alexander Aringe369dc82015-05-17 21:45:05 +0200185 spin_unlock(&fakelb_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000186
187 return 0;
188
189err_reg:
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200190 ieee802154_free_hw(phy->hw);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000191 return err;
192}
193
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200194static void fakelb_del(struct fakelb_phy *phy)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000195{
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200196 list_del(&phy->list);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000197
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200198 ieee802154_unregister_hw(phy->hw);
199 ieee802154_free_hw(phy->hw);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000200}
201
Bill Pembertonbb1f4602012-12-03 09:24:12 -0500202static int fakelb_probe(struct platform_device *pdev)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000203{
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200204 struct fakelb_phy *phy, *tmp;
Alexander Aring39572ab2015-05-17 21:45:10 +0200205 int err, i;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000206
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000207 for (i = 0; i < numlbs; i++) {
Alexander Aringb82b99f2015-05-17 21:45:03 +0200208 err = fakelb_add_one(&pdev->dev);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000209 if (err < 0)
210 goto err_slave;
211 }
212
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000213 dev_info(&pdev->dev, "added ieee802154 hardware\n");
214 return 0;
215
216err_slave:
Alexander Aringe369dc82015-05-17 21:45:05 +0200217 spin_lock(&fakelb_phys_lock);
Alexander Aringb82b99f2015-05-17 21:45:03 +0200218 list_for_each_entry_safe(phy, tmp, &fakelb_phys, list)
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200219 fakelb_del(phy);
Alexander Aringe369dc82015-05-17 21:45:05 +0200220 spin_unlock(&fakelb_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000221 return err;
222}
223
Bill Pembertonbb1f4602012-12-03 09:24:12 -0500224static int fakelb_remove(struct platform_device *pdev)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000225{
Alexander Aring39572ab2015-05-17 21:45:10 +0200226 struct fakelb_phy *phy, *tmp;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000227
Alexander Aringe369dc82015-05-17 21:45:05 +0200228 spin_lock(&fakelb_phys_lock);
Alexander Aring39572ab2015-05-17 21:45:10 +0200229 list_for_each_entry_safe(phy, tmp, &fakelb_phys, list)
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200230 fakelb_del(phy);
Alexander Aringe369dc82015-05-17 21:45:05 +0200231 spin_unlock(&fakelb_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000232 return 0;
233}
234
235static struct platform_device *ieee802154fake_dev;
236
237static struct platform_driver ieee802154fake_driver = {
238 .probe = fakelb_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -0500239 .remove = fakelb_remove,
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000240 .driver = {
241 .name = "ieee802154fakelb",
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000242 },
243};
244
245static __init int fakelb_init_module(void)
246{
247 ieee802154fake_dev = platform_device_register_simple(
248 "ieee802154fakelb", -1, NULL, 0);
249 return platform_driver_register(&ieee802154fake_driver);
250}
251
252static __exit void fake_remove_module(void)
253{
254 platform_driver_unregister(&ieee802154fake_driver);
255 platform_device_unregister(ieee802154fake_dev);
256}
257
258module_init(fakelb_init_module);
259module_exit(fake_remove_module);
260MODULE_LICENSE("GPL");