Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Loopback IEEE 802.15.4 interface
4 *
5 * Copyright 2007-2012 Siemens AG
6 *
7 * Written by:
8 * Sergey Lapin <slapin@ossfans.org>
9 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
10 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
11 */
12
13#include <linux/module.h>
14#include <linux/timer.h>
15#include <linux/platform_device.h>
16#include <linux/netdevice.h>
17#include <linux/device.h>
18#include <linux/spinlock.h>
19#include <net/mac802154.h>
20#include <net/cfg802154.h>
21
22static int numlbs = 2;
23
24static LIST_HEAD(fakelb_phys);
25static DEFINE_MUTEX(fakelb_phys_lock);
26
27static LIST_HEAD(fakelb_ifup_phys);
28static DEFINE_RWLOCK(fakelb_ifup_phys_lock);
29
30struct fakelb_phy {
31 struct ieee802154_hw *hw;
32
33 u8 page;
34 u8 channel;
35
36 bool suspended;
37
38 struct list_head list;
39 struct list_head list_ifup;
40};
41
42static int fakelb_hw_ed(struct ieee802154_hw *hw, u8 *level)
43{
44 WARN_ON(!level);
45 *level = 0xbe;
46
47 return 0;
48}
49
50static int fakelb_hw_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
51{
52 struct fakelb_phy *phy = hw->priv;
53
54 write_lock_bh(&fakelb_ifup_phys_lock);
55 phy->page = page;
56 phy->channel = channel;
57 write_unlock_bh(&fakelb_ifup_phys_lock);
58 return 0;
59}
60
61static int fakelb_hw_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
62{
63 struct fakelb_phy *current_phy = hw->priv, *phy;
64
65 read_lock_bh(&fakelb_ifup_phys_lock);
66 WARN_ON(current_phy->suspended);
67 list_for_each_entry(phy, &fakelb_ifup_phys, list_ifup) {
68 if (current_phy == phy)
69 continue;
70
71 if (current_phy->page == phy->page &&
72 current_phy->channel == phy->channel) {
73 struct sk_buff *newskb = pskb_copy(skb, GFP_ATOMIC);
74
75 if (newskb)
76 ieee802154_rx_irqsafe(phy->hw, newskb, 0xcc);
77 }
78 }
79 read_unlock_bh(&fakelb_ifup_phys_lock);
80
81 ieee802154_xmit_complete(hw, skb, false);
82 return 0;
83}
84
85static int fakelb_hw_start(struct ieee802154_hw *hw)
86{
87 struct fakelb_phy *phy = hw->priv;
88
89 write_lock_bh(&fakelb_ifup_phys_lock);
90 phy->suspended = false;
91 list_add(&phy->list_ifup, &fakelb_ifup_phys);
92 write_unlock_bh(&fakelb_ifup_phys_lock);
93
94 return 0;
95}
96
97static void fakelb_hw_stop(struct ieee802154_hw *hw)
98{
99 struct fakelb_phy *phy = hw->priv;
100
101 write_lock_bh(&fakelb_ifup_phys_lock);
102 phy->suspended = true;
103 list_del(&phy->list_ifup);
104 write_unlock_bh(&fakelb_ifup_phys_lock);
105}
106
107static int
108fakelb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
109{
110 return 0;
111}
112
113static const struct ieee802154_ops fakelb_ops = {
114 .owner = THIS_MODULE,
115 .xmit_async = 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 .set_promiscuous_mode = fakelb_set_promiscuous_mode,
121};
122
123/* Number of dummy devices to be set up by this module. */
124module_param(numlbs, int, 0);
125MODULE_PARM_DESC(numlbs, " number of pseudo devices");
126
127static int fakelb_add_one(struct device *dev)
128{
129 struct ieee802154_hw *hw;
130 struct fakelb_phy *phy;
131 int err;
132
133 hw = ieee802154_alloc_hw(sizeof(*phy), &fakelb_ops);
134 if (!hw)
135 return -ENOMEM;
136
137 phy = hw->priv;
138 phy->hw = hw;
139
140 /* 868 MHz BPSK 802.15.4-2003 */
141 hw->phy->supported.channels[0] |= 1;
142 /* 915 MHz BPSK 802.15.4-2003 */
143 hw->phy->supported.channels[0] |= 0x7fe;
144 /* 2.4 GHz O-QPSK 802.15.4-2003 */
145 hw->phy->supported.channels[0] |= 0x7FFF800;
146 /* 868 MHz ASK 802.15.4-2006 */
147 hw->phy->supported.channels[1] |= 1;
148 /* 915 MHz ASK 802.15.4-2006 */
149 hw->phy->supported.channels[1] |= 0x7fe;
150 /* 868 MHz O-QPSK 802.15.4-2006 */
151 hw->phy->supported.channels[2] |= 1;
152 /* 915 MHz O-QPSK 802.15.4-2006 */
153 hw->phy->supported.channels[2] |= 0x7fe;
154 /* 2.4 GHz CSS 802.15.4a-2007 */
155 hw->phy->supported.channels[3] |= 0x3fff;
156 /* UWB Sub-gigahertz 802.15.4a-2007 */
157 hw->phy->supported.channels[4] |= 1;
158 /* UWB Low band 802.15.4a-2007 */
159 hw->phy->supported.channels[4] |= 0x1e;
160 /* UWB High band 802.15.4a-2007 */
161 hw->phy->supported.channels[4] |= 0xffe0;
162 /* 750 MHz O-QPSK 802.15.4c-2009 */
163 hw->phy->supported.channels[5] |= 0xf;
164 /* 750 MHz MPSK 802.15.4c-2009 */
165 hw->phy->supported.channels[5] |= 0xf0;
166 /* 950 MHz BPSK 802.15.4d-2009 */
167 hw->phy->supported.channels[6] |= 0x3ff;
168 /* 950 MHz GFSK 802.15.4d-2009 */
169 hw->phy->supported.channels[6] |= 0x3ffc00;
170
171 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
172 /* fake phy channel 13 as default */
173 hw->phy->current_channel = 13;
174 phy->channel = hw->phy->current_channel;
175
176 hw->flags = IEEE802154_HW_PROMISCUOUS;
177 hw->parent = dev;
178
179 err = ieee802154_register_hw(hw);
180 if (err)
181 goto err_reg;
182
183 mutex_lock(&fakelb_phys_lock);
184 list_add_tail(&phy->list, &fakelb_phys);
185 mutex_unlock(&fakelb_phys_lock);
186
187 return 0;
188
189err_reg:
190 ieee802154_free_hw(phy->hw);
191 return err;
192}
193
194static void fakelb_del(struct fakelb_phy *phy)
195{
196 list_del(&phy->list);
197
198 ieee802154_unregister_hw(phy->hw);
199 ieee802154_free_hw(phy->hw);
200}
201
202static int fakelb_probe(struct platform_device *pdev)
203{
204 struct fakelb_phy *phy, *tmp;
205 int err, i;
206
207 for (i = 0; i < numlbs; i++) {
208 err = fakelb_add_one(&pdev->dev);
209 if (err < 0)
210 goto err_slave;
211 }
212
213 dev_info(&pdev->dev, "added %i fake ieee802154 hardware devices\n", numlbs);
214 return 0;
215
216err_slave:
217 mutex_lock(&fakelb_phys_lock);
218 list_for_each_entry_safe(phy, tmp, &fakelb_phys, list)
219 fakelb_del(phy);
220 mutex_unlock(&fakelb_phys_lock);
221 return err;
222}
223
224static void fakelb_remove(struct platform_device *pdev)
225{
226 struct fakelb_phy *phy, *tmp;
227
228 mutex_lock(&fakelb_phys_lock);
229 list_for_each_entry_safe(phy, tmp, &fakelb_phys, list)
230 fakelb_del(phy);
231 mutex_unlock(&fakelb_phys_lock);
232}
233
234static struct platform_device *ieee802154fake_dev;
235
236static struct platform_driver ieee802154fake_driver = {
237 .probe = fakelb_probe,
238 .remove_new = fakelb_remove,
239 .driver = {
240 .name = "ieee802154fakelb",
241 },
242};
243
244static __init int fakelb_init_module(void)
245{
246 ieee802154fake_dev = platform_device_register_simple(
247 "ieee802154fakelb", -1, NULL, 0);
248
249 pr_warn("fakelb driver is marked as deprecated, please use mac802154_hwsim!\n");
250
251 return platform_driver_register(&ieee802154fake_driver);
252}
253
254static __exit void fake_remove_module(void)
255{
256 platform_driver_unregister(&ieee802154fake_driver);
257 platform_device_unregister(ieee802154fake_dev);
258}
259
260module_init(fakelb_init_module);
261module_exit(fake_remove_module);
262MODULE_DESCRIPTION("IEEE 802.15.4 loopback driver");
263MODULE_LICENSE("GPL");
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
25#include <linux/module.h>
26#include <linux/timer.h>
27#include <linux/platform_device.h>
28#include <linux/netdevice.h>
29#include <linux/spinlock.h>
30#include <net/mac802154.h>
31#include <net/wpan-phy.h>
32
33static int numlbs = 1;
34
35struct fakelb_dev_priv {
36 struct ieee802154_dev *dev;
37
38 struct list_head list;
39 struct fakelb_priv *fake;
40
41 spinlock_t lock;
42 bool working;
43};
44
45struct fakelb_priv {
46 struct list_head list;
47 rwlock_t lock;
48};
49
50static int
51fakelb_hw_ed(struct ieee802154_dev *dev, u8 *level)
52{
53 might_sleep();
54 BUG_ON(!level);
55 *level = 0xbe;
56
57 return 0;
58}
59
60static int
61fakelb_hw_channel(struct ieee802154_dev *dev, int page, int channel)
62{
63 pr_debug("set channel to %d\n", channel);
64
65 might_sleep();
66 dev->phy->current_page = page;
67 dev->phy->current_channel = channel;
68
69 return 0;
70}
71
72static void
73fakelb_hw_deliver(struct fakelb_dev_priv *priv, struct sk_buff *skb)
74{
75 struct sk_buff *newskb;
76
77 spin_lock(&priv->lock);
78 if (priv->working) {
79 newskb = pskb_copy(skb, GFP_ATOMIC);
80 ieee802154_rx_irqsafe(priv->dev, newskb, 0xcc);
81 }
82 spin_unlock(&priv->lock);
83}
84
85static int
86fakelb_hw_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
87{
88 struct fakelb_dev_priv *priv = dev->priv;
89 struct fakelb_priv *fake = priv->fake;
90
91 might_sleep();
92
93 read_lock_bh(&fake->lock);
94 if (priv->list.next == priv->list.prev) {
95 /* we are the only one device */
96 fakelb_hw_deliver(priv, skb);
97 } else {
98 struct fakelb_dev_priv *dp;
99 list_for_each_entry(dp, &priv->fake->list, list) {
100 if (dp != priv &&
101 (dp->dev->phy->current_channel ==
102 priv->dev->phy->current_channel))
103 fakelb_hw_deliver(dp, skb);
104 }
105 }
106 read_unlock_bh(&fake->lock);
107
108 return 0;
109}
110
111static int
112fakelb_hw_start(struct ieee802154_dev *dev) {
113 struct fakelb_dev_priv *priv = dev->priv;
114 int ret = 0;
115
116 spin_lock(&priv->lock);
117 if (priv->working)
118 ret = -EBUSY;
119 else
120 priv->working = 1;
121 spin_unlock(&priv->lock);
122
123 return ret;
124}
125
126static void
127fakelb_hw_stop(struct ieee802154_dev *dev) {
128 struct fakelb_dev_priv *priv = dev->priv;
129
130 spin_lock(&priv->lock);
131 priv->working = 0;
132 spin_unlock(&priv->lock);
133}
134
135static struct ieee802154_ops fakelb_ops = {
136 .owner = THIS_MODULE,
137 .xmit = fakelb_hw_xmit,
138 .ed = fakelb_hw_ed,
139 .set_channel = fakelb_hw_channel,
140 .start = fakelb_hw_start,
141 .stop = fakelb_hw_stop,
142};
143
144/* Number of dummy devices to be set up by this module. */
145module_param(numlbs, int, 0);
146MODULE_PARM_DESC(numlbs, " number of pseudo devices");
147
148static int fakelb_add_one(struct device *dev, struct fakelb_priv *fake)
149{
150 struct fakelb_dev_priv *priv;
151 int err;
152 struct ieee802154_dev *ieee;
153
154 ieee = ieee802154_alloc_device(sizeof(*priv), &fakelb_ops);
155 if (!ieee)
156 return -ENOMEM;
157
158 priv = ieee->priv;
159 priv->dev = ieee;
160
161 /* 868 MHz BPSK 802.15.4-2003 */
162 ieee->phy->channels_supported[0] |= 1;
163 /* 915 MHz BPSK 802.15.4-2003 */
164 ieee->phy->channels_supported[0] |= 0x7fe;
165 /* 2.4 GHz O-QPSK 802.15.4-2003 */
166 ieee->phy->channels_supported[0] |= 0x7FFF800;
167 /* 868 MHz ASK 802.15.4-2006 */
168 ieee->phy->channels_supported[1] |= 1;
169 /* 915 MHz ASK 802.15.4-2006 */
170 ieee->phy->channels_supported[1] |= 0x7fe;
171 /* 868 MHz O-QPSK 802.15.4-2006 */
172 ieee->phy->channels_supported[2] |= 1;
173 /* 915 MHz O-QPSK 802.15.4-2006 */
174 ieee->phy->channels_supported[2] |= 0x7fe;
175 /* 2.4 GHz CSS 802.15.4a-2007 */
176 ieee->phy->channels_supported[3] |= 0x3fff;
177 /* UWB Sub-gigahertz 802.15.4a-2007 */
178 ieee->phy->channels_supported[4] |= 1;
179 /* UWB Low band 802.15.4a-2007 */
180 ieee->phy->channels_supported[4] |= 0x1e;
181 /* UWB High band 802.15.4a-2007 */
182 ieee->phy->channels_supported[4] |= 0xffe0;
183 /* 750 MHz O-QPSK 802.15.4c-2009 */
184 ieee->phy->channels_supported[5] |= 0xf;
185 /* 750 MHz MPSK 802.15.4c-2009 */
186 ieee->phy->channels_supported[5] |= 0xf0;
187 /* 950 MHz BPSK 802.15.4d-2009 */
188 ieee->phy->channels_supported[6] |= 0x3ff;
189 /* 950 MHz GFSK 802.15.4d-2009 */
190 ieee->phy->channels_supported[6] |= 0x3ffc00;
191
192 INIT_LIST_HEAD(&priv->list);
193 priv->fake = fake;
194
195 spin_lock_init(&priv->lock);
196
197 ieee->parent = dev;
198
199 err = ieee802154_register_device(ieee);
200 if (err)
201 goto err_reg;
202
203 write_lock_bh(&fake->lock);
204 list_add_tail(&priv->list, &fake->list);
205 write_unlock_bh(&fake->lock);
206
207 return 0;
208
209err_reg:
210 ieee802154_free_device(priv->dev);
211 return err;
212}
213
214static void fakelb_del(struct fakelb_dev_priv *priv)
215{
216 write_lock_bh(&priv->fake->lock);
217 list_del(&priv->list);
218 write_unlock_bh(&priv->fake->lock);
219
220 ieee802154_unregister_device(priv->dev);
221 ieee802154_free_device(priv->dev);
222}
223
224static int fakelb_probe(struct platform_device *pdev)
225{
226 struct fakelb_priv *priv;
227 struct fakelb_dev_priv *dp;
228 int err = -ENOMEM;
229 int i;
230
231 priv = kzalloc(sizeof(struct fakelb_priv), GFP_KERNEL);
232 if (!priv)
233 goto err_alloc;
234
235 INIT_LIST_HEAD(&priv->list);
236 rwlock_init(&priv->lock);
237
238 for (i = 0; i < numlbs; i++) {
239 err = fakelb_add_one(&pdev->dev, priv);
240 if (err < 0)
241 goto err_slave;
242 }
243
244 platform_set_drvdata(pdev, priv);
245 dev_info(&pdev->dev, "added ieee802154 hardware\n");
246 return 0;
247
248err_slave:
249 list_for_each_entry(dp, &priv->list, list)
250 fakelb_del(dp);
251 kfree(priv);
252err_alloc:
253 return err;
254}
255
256static int fakelb_remove(struct platform_device *pdev)
257{
258 struct fakelb_priv *priv = platform_get_drvdata(pdev);
259 struct fakelb_dev_priv *dp, *temp;
260
261 list_for_each_entry_safe(dp, temp, &priv->list, list)
262 fakelb_del(dp);
263 kfree(priv);
264
265 return 0;
266}
267
268static struct platform_device *ieee802154fake_dev;
269
270static struct platform_driver ieee802154fake_driver = {
271 .probe = fakelb_probe,
272 .remove = fakelb_remove,
273 .driver = {
274 .name = "ieee802154fakelb",
275 .owner = THIS_MODULE,
276 },
277};
278
279static __init int fakelb_init_module(void)
280{
281 ieee802154fake_dev = platform_device_register_simple(
282 "ieee802154fakelb", -1, NULL, 0);
283 return platform_driver_register(&ieee802154fake_driver);
284}
285
286static __exit void fake_remove_module(void)
287{
288 platform_driver_unregister(&ieee802154fake_driver);
289 platform_device_unregister(ieee802154fake_dev);
290}
291
292module_init(fakelb_init_module);
293module_exit(fake_remove_module);
294MODULE_LICENSE("GPL");