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.
16 * Sergey Lapin <slapin@ossfans.org>
17 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
18 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
21 #include <linux/module.h>
22 #include <linux/timer.h>
23 #include <linux/platform_device.h>
24 #include <linux/netdevice.h>
25 #include <linux/device.h>
26 #include <linux/spinlock.h>
27 #include <net/mac802154.h>
28 #include <net/cfg802154.h>
30 static int numlbs = 1;
32 struct fakelb_dev_priv {
33 struct ieee802154_dev *dev;
35 struct list_head list;
36 struct fakelb_priv *fake;
43 struct list_head list;
48 fakelb_hw_ed(struct ieee802154_dev *dev, u8 *level)
58 fakelb_hw_channel(struct ieee802154_dev *dev, int page, int channel)
60 pr_debug("set channel to %d\n", channel);
63 dev->phy->current_page = page;
64 dev->phy->current_channel = channel;
70 fakelb_hw_deliver(struct fakelb_dev_priv *priv, struct sk_buff *skb)
72 struct sk_buff *newskb;
74 spin_lock(&priv->lock);
76 newskb = pskb_copy(skb, GFP_ATOMIC);
77 ieee802154_rx_irqsafe(priv->dev, newskb, 0xcc);
79 spin_unlock(&priv->lock);
83 fakelb_hw_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
85 struct fakelb_dev_priv *priv = dev->priv;
86 struct fakelb_priv *fake = priv->fake;
90 read_lock_bh(&fake->lock);
91 if (priv->list.next == priv->list.prev) {
92 /* we are the only one device */
93 fakelb_hw_deliver(priv, skb);
95 struct fakelb_dev_priv *dp;
96 list_for_each_entry(dp, &priv->fake->list, list) {
98 (dp->dev->phy->current_channel ==
99 priv->dev->phy->current_channel))
100 fakelb_hw_deliver(dp, skb);
103 read_unlock_bh(&fake->lock);
109 fakelb_hw_start(struct ieee802154_dev *dev) {
110 struct fakelb_dev_priv *priv = dev->priv;
113 spin_lock(&priv->lock);
118 spin_unlock(&priv->lock);
124 fakelb_hw_stop(struct ieee802154_dev *dev) {
125 struct fakelb_dev_priv *priv = dev->priv;
127 spin_lock(&priv->lock);
129 spin_unlock(&priv->lock);
132 static struct ieee802154_ops fakelb_ops = {
133 .owner = THIS_MODULE,
134 .xmit = fakelb_hw_xmit,
136 .set_channel = fakelb_hw_channel,
137 .start = fakelb_hw_start,
138 .stop = fakelb_hw_stop,
141 /* Number of dummy devices to be set up by this module. */
142 module_param(numlbs, int, 0);
143 MODULE_PARM_DESC(numlbs, " number of pseudo devices");
145 static int fakelb_add_one(struct device *dev, struct fakelb_priv *fake)
147 struct fakelb_dev_priv *priv;
149 struct ieee802154_dev *ieee;
151 ieee = ieee802154_alloc_device(sizeof(*priv), &fakelb_ops);
158 /* 868 MHz BPSK 802.15.4-2003 */
159 ieee->phy->channels_supported[0] |= 1;
160 /* 915 MHz BPSK 802.15.4-2003 */
161 ieee->phy->channels_supported[0] |= 0x7fe;
162 /* 2.4 GHz O-QPSK 802.15.4-2003 */
163 ieee->phy->channels_supported[0] |= 0x7FFF800;
164 /* 868 MHz ASK 802.15.4-2006 */
165 ieee->phy->channels_supported[1] |= 1;
166 /* 915 MHz ASK 802.15.4-2006 */
167 ieee->phy->channels_supported[1] |= 0x7fe;
168 /* 868 MHz O-QPSK 802.15.4-2006 */
169 ieee->phy->channels_supported[2] |= 1;
170 /* 915 MHz O-QPSK 802.15.4-2006 */
171 ieee->phy->channels_supported[2] |= 0x7fe;
172 /* 2.4 GHz CSS 802.15.4a-2007 */
173 ieee->phy->channels_supported[3] |= 0x3fff;
174 /* UWB Sub-gigahertz 802.15.4a-2007 */
175 ieee->phy->channels_supported[4] |= 1;
176 /* UWB Low band 802.15.4a-2007 */
177 ieee->phy->channels_supported[4] |= 0x1e;
178 /* UWB High band 802.15.4a-2007 */
179 ieee->phy->channels_supported[4] |= 0xffe0;
180 /* 750 MHz O-QPSK 802.15.4c-2009 */
181 ieee->phy->channels_supported[5] |= 0xf;
182 /* 750 MHz MPSK 802.15.4c-2009 */
183 ieee->phy->channels_supported[5] |= 0xf0;
184 /* 950 MHz BPSK 802.15.4d-2009 */
185 ieee->phy->channels_supported[6] |= 0x3ff;
186 /* 950 MHz GFSK 802.15.4d-2009 */
187 ieee->phy->channels_supported[6] |= 0x3ffc00;
189 INIT_LIST_HEAD(&priv->list);
192 spin_lock_init(&priv->lock);
196 err = ieee802154_register_device(ieee);
200 write_lock_bh(&fake->lock);
201 list_add_tail(&priv->list, &fake->list);
202 write_unlock_bh(&fake->lock);
207 ieee802154_free_device(priv->dev);
211 static void fakelb_del(struct fakelb_dev_priv *priv)
213 write_lock_bh(&priv->fake->lock);
214 list_del(&priv->list);
215 write_unlock_bh(&priv->fake->lock);
217 ieee802154_unregister_device(priv->dev);
218 ieee802154_free_device(priv->dev);
221 static int fakelb_probe(struct platform_device *pdev)
223 struct fakelb_priv *priv;
224 struct fakelb_dev_priv *dp;
228 priv = devm_kzalloc(&pdev->dev, sizeof(struct fakelb_priv),
233 INIT_LIST_HEAD(&priv->list);
234 rwlock_init(&priv->lock);
236 for (i = 0; i < numlbs; i++) {
237 err = fakelb_add_one(&pdev->dev, priv);
242 platform_set_drvdata(pdev, priv);
243 dev_info(&pdev->dev, "added ieee802154 hardware\n");
247 list_for_each_entry(dp, &priv->list, list)
253 static int fakelb_remove(struct platform_device *pdev)
255 struct fakelb_priv *priv = platform_get_drvdata(pdev);
256 struct fakelb_dev_priv *dp, *temp;
258 list_for_each_entry_safe(dp, temp, &priv->list, list)
264 static struct platform_device *ieee802154fake_dev;
266 static struct platform_driver ieee802154fake_driver = {
267 .probe = fakelb_probe,
268 .remove = fakelb_remove,
270 .name = "ieee802154fakelb",
271 .owner = THIS_MODULE,
275 static __init int fakelb_init_module(void)
277 ieee802154fake_dev = platform_device_register_simple(
278 "ieee802154fakelb", -1, NULL, 0);
279 return platform_driver_register(&ieee802154fake_driver);
282 static __exit void fake_remove_module(void)
284 platform_driver_unregister(&ieee802154fake_driver);
285 platform_device_unregister(ieee802154fake_dev);
288 module_init(fakelb_init_module);
289 module_exit(fake_remove_module);
290 MODULE_LICENSE("GPL");