2 * Loopback IEEE 802.15.4 interface
4 * Copyright 2007-2012 Siemens AG
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.
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.
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.
20 * Sergey Lapin <slapin@ossfans.org>
21 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
22 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
25 #include <linux/module.h>
26 #include <linux/timer.h>
27 #include <linux/platform_device.h>
28 #include <linux/netdevice.h>
29 #include <linux/device.h>
30 #include <linux/spinlock.h>
31 #include <net/mac802154.h>
32 #include <net/wpan-phy.h>
34 static int numlbs = 1;
36 struct fakelb_dev_priv {
37 struct ieee802154_dev *dev;
39 struct list_head list;
40 struct fakelb_priv *fake;
47 struct list_head list;
52 fakelb_hw_ed(struct ieee802154_dev *dev, u8 *level)
62 fakelb_hw_channel(struct ieee802154_dev *dev, int page, int channel)
64 pr_debug("set channel to %d\n", channel);
67 dev->phy->current_page = page;
68 dev->phy->current_channel = channel;
74 fakelb_hw_deliver(struct fakelb_dev_priv *priv, struct sk_buff *skb)
76 struct sk_buff *newskb;
78 spin_lock(&priv->lock);
80 newskb = pskb_copy(skb, GFP_ATOMIC);
81 ieee802154_rx_irqsafe(priv->dev, newskb, 0xcc);
83 spin_unlock(&priv->lock);
87 fakelb_hw_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
89 struct fakelb_dev_priv *priv = dev->priv;
90 struct fakelb_priv *fake = priv->fake;
94 read_lock_bh(&fake->lock);
95 if (priv->list.next == priv->list.prev) {
96 /* we are the only one device */
97 fakelb_hw_deliver(priv, skb);
99 struct fakelb_dev_priv *dp;
100 list_for_each_entry(dp, &priv->fake->list, list) {
102 (dp->dev->phy->current_channel ==
103 priv->dev->phy->current_channel))
104 fakelb_hw_deliver(dp, skb);
107 read_unlock_bh(&fake->lock);
113 fakelb_hw_start(struct ieee802154_dev *dev) {
114 struct fakelb_dev_priv *priv = dev->priv;
117 spin_lock(&priv->lock);
122 spin_unlock(&priv->lock);
128 fakelb_hw_stop(struct ieee802154_dev *dev) {
129 struct fakelb_dev_priv *priv = dev->priv;
131 spin_lock(&priv->lock);
133 spin_unlock(&priv->lock);
136 static struct ieee802154_ops fakelb_ops = {
137 .owner = THIS_MODULE,
138 .xmit = fakelb_hw_xmit,
140 .set_channel = fakelb_hw_channel,
141 .start = fakelb_hw_start,
142 .stop = fakelb_hw_stop,
145 /* Number of dummy devices to be set up by this module. */
146 module_param(numlbs, int, 0);
147 MODULE_PARM_DESC(numlbs, " number of pseudo devices");
149 static int fakelb_add_one(struct device *dev, struct fakelb_priv *fake)
151 struct fakelb_dev_priv *priv;
153 struct ieee802154_dev *ieee;
155 ieee = ieee802154_alloc_device(sizeof(*priv), &fakelb_ops);
162 /* 868 MHz BPSK 802.15.4-2003 */
163 ieee->phy->channels_supported[0] |= 1;
164 /* 915 MHz BPSK 802.15.4-2003 */
165 ieee->phy->channels_supported[0] |= 0x7fe;
166 /* 2.4 GHz O-QPSK 802.15.4-2003 */
167 ieee->phy->channels_supported[0] |= 0x7FFF800;
168 /* 868 MHz ASK 802.15.4-2006 */
169 ieee->phy->channels_supported[1] |= 1;
170 /* 915 MHz ASK 802.15.4-2006 */
171 ieee->phy->channels_supported[1] |= 0x7fe;
172 /* 868 MHz O-QPSK 802.15.4-2006 */
173 ieee->phy->channels_supported[2] |= 1;
174 /* 915 MHz O-QPSK 802.15.4-2006 */
175 ieee->phy->channels_supported[2] |= 0x7fe;
176 /* 2.4 GHz CSS 802.15.4a-2007 */
177 ieee->phy->channels_supported[3] |= 0x3fff;
178 /* UWB Sub-gigahertz 802.15.4a-2007 */
179 ieee->phy->channels_supported[4] |= 1;
180 /* UWB Low band 802.15.4a-2007 */
181 ieee->phy->channels_supported[4] |= 0x1e;
182 /* UWB High band 802.15.4a-2007 */
183 ieee->phy->channels_supported[4] |= 0xffe0;
184 /* 750 MHz O-QPSK 802.15.4c-2009 */
185 ieee->phy->channels_supported[5] |= 0xf;
186 /* 750 MHz MPSK 802.15.4c-2009 */
187 ieee->phy->channels_supported[5] |= 0xf0;
188 /* 950 MHz BPSK 802.15.4d-2009 */
189 ieee->phy->channels_supported[6] |= 0x3ff;
190 /* 950 MHz GFSK 802.15.4d-2009 */
191 ieee->phy->channels_supported[6] |= 0x3ffc00;
193 INIT_LIST_HEAD(&priv->list);
196 spin_lock_init(&priv->lock);
200 err = ieee802154_register_device(ieee);
204 write_lock_bh(&fake->lock);
205 list_add_tail(&priv->list, &fake->list);
206 write_unlock_bh(&fake->lock);
211 ieee802154_free_device(priv->dev);
215 static void fakelb_del(struct fakelb_dev_priv *priv)
217 write_lock_bh(&priv->fake->lock);
218 list_del(&priv->list);
219 write_unlock_bh(&priv->fake->lock);
221 ieee802154_unregister_device(priv->dev);
222 ieee802154_free_device(priv->dev);
225 static int fakelb_probe(struct platform_device *pdev)
227 struct fakelb_priv *priv;
228 struct fakelb_dev_priv *dp;
232 priv = devm_kzalloc(&pdev->dev, sizeof(struct fakelb_priv),
237 INIT_LIST_HEAD(&priv->list);
238 rwlock_init(&priv->lock);
240 for (i = 0; i < numlbs; i++) {
241 err = fakelb_add_one(&pdev->dev, priv);
246 platform_set_drvdata(pdev, priv);
247 dev_info(&pdev->dev, "added ieee802154 hardware\n");
251 list_for_each_entry(dp, &priv->list, list)
257 static int fakelb_remove(struct platform_device *pdev)
259 struct fakelb_priv *priv = platform_get_drvdata(pdev);
260 struct fakelb_dev_priv *dp, *temp;
262 list_for_each_entry_safe(dp, temp, &priv->list, list)
268 static struct platform_device *ieee802154fake_dev;
270 static struct platform_driver ieee802154fake_driver = {
271 .probe = fakelb_probe,
272 .remove = fakelb_remove,
274 .name = "ieee802154fakelb",
275 .owner = THIS_MODULE,
279 static __init int fakelb_init_module(void)
281 ieee802154fake_dev = platform_device_register_simple(
282 "ieee802154fakelb", -1, NULL, 0);
283 return platform_driver_register(&ieee802154fake_driver);
286 static __exit void fake_remove_module(void)
288 platform_driver_unregister(&ieee802154fake_driver);
289 platform_device_unregister(ieee802154fake_dev);
292 module_init(fakelb_init_module);
293 module_exit(fake_remove_module);
294 MODULE_LICENSE("GPL");