Root/drivers/ieee802154/fakelb.c

1/*
2 * Loopback IEEE 802.15.4 interface
3 *
4 * Copyright 2007, 2008 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 */
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/spinlock.h>
29#include <net/mac802154.h>
30#include <net/wpan-phy.h>
31
32struct fake_dev_priv {
33    struct ieee802154_dev *dev;
34
35    struct list_head list;
36    struct fake_priv *fake;
37
38    unsigned int working:1;
39};
40
41struct fake_priv {
42    struct list_head list;
43    rwlock_t lock;
44};
45
46static int
47hw_ed(struct ieee802154_dev *dev, u8 *level)
48{
49    pr_debug("%s\n", __func__);
50    might_sleep();
51    BUG_ON(!level);
52    *level = 0xbe;
53    return 0;
54}
55
56static int
57hw_channel(struct ieee802154_dev *dev, int page, int channel)
58{
59    pr_debug("%s %d\n", __func__, channel);
60    might_sleep();
61    dev->phy->current_page = page;
62    dev->phy->current_channel = channel;
63    return 0;
64}
65
66static void
67hw_deliver(struct fake_dev_priv *priv, struct sk_buff *skb)
68{
69    struct sk_buff *newskb;
70
71    if (!priv->working)
72        return;
73
74    newskb = pskb_copy(skb, GFP_ATOMIC);
75
76    ieee802154_rx_irqsafe(priv->dev, newskb, 0xcc);
77}
78
79static int
80hw_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
81{
82    struct fake_dev_priv *priv = dev->priv;
83    struct fake_priv *fake = priv->fake;
84
85    might_sleep();
86
87    read_lock_bh(&fake->lock);
88    if (priv->list.next == priv->list.prev) {
89        /* we are the only one device */
90        hw_deliver(priv, skb);
91    } else {
92        struct fake_dev_priv *dp;
93        list_for_each_entry(dp, &priv->fake->list, list)
94            if (dp != priv &&
95                dp->dev->phy->current_channel ==
96                    priv->dev->phy->current_channel)
97                hw_deliver(dp, skb);
98    }
99    read_unlock_bh(&fake->lock);
100
101    return 0;
102}
103
104static int
105hw_start(struct ieee802154_dev *dev) {
106    struct fake_dev_priv *priv = dev->priv;
107
108    if (priv->working)
109        return -EBUSY;
110
111    priv->working = 1;
112
113    return 0;
114}
115
116static void
117hw_stop(struct ieee802154_dev *dev) {
118    struct fake_dev_priv *priv = dev->priv;
119
120    priv->working = 0;
121}
122
123static struct ieee802154_ops fake_ops = {
124    .owner = THIS_MODULE,
125    .xmit = hw_xmit,
126    .ed = hw_ed,
127    .set_channel = hw_channel,
128    .start = hw_start,
129    .stop = hw_stop,
130};
131
132static int ieee802154fake_add_priv(struct device *dev, struct fake_priv *fake)
133{
134    struct fake_dev_priv *priv;
135    int err = -ENOMEM;
136    struct ieee802154_dev *ieee;
137
138    ieee = ieee802154_alloc_device(sizeof(*priv), &fake_ops);
139    if (!dev)
140        goto err_alloc_dev;
141
142    priv = ieee->priv;
143    priv->dev = ieee;
144
145    /* 868 MHz BPSK 802.15.4-2003 */
146    ieee->phy->channels_supported[0] |= 1;
147    /* 915 MHz BPSK 802.15.4-2003 */
148    ieee->phy->channels_supported[0] |= 0x7fe;
149    /* 2.4 GHz O-QPSK 802.15.4-2003 */
150    ieee->phy->channels_supported[0] |= 0x7FFF800;
151    /* 868 MHz ASK 802.15.4-2006 */
152    ieee->phy->channels_supported[1] |= 1;
153    /* 915 MHz ASK 802.15.4-2006 */
154    ieee->phy->channels_supported[1] |= 0x7fe;
155    /* 868 MHz O-QPSK 802.15.4-2006 */
156    ieee->phy->channels_supported[2] |= 1;
157    /* 915 MHz O-QPSK 802.15.4-2006 */
158    ieee->phy->channels_supported[2] |= 0x7fe;
159    /* 2.4 GHz CSS 802.15.4a-2007 */
160    ieee->phy->channels_supported[3] |= 0x3fff;
161    /* UWB Sub-gigahertz 802.15.4a-2007 */
162    ieee->phy->channels_supported[4] |= 1;
163    /* UWB Low band 802.15.4a-2007 */
164    ieee->phy->channels_supported[4] |= 0x1e;
165    /* UWB High band 802.15.4a-2007 */
166    ieee->phy->channels_supported[4] |= 0xffe0;
167    /* 750 MHz O-QPSK 802.15.4c-2009 */
168    ieee->phy->channels_supported[5] |= 0xf;
169    /* 750 MHz MPSK 802.15.4c-2009 */
170    ieee->phy->channels_supported[5] |= 0xf0;
171    /* 950 MHz BPSK 802.15.4d-2009 */
172    ieee->phy->channels_supported[6] |= 0x3ff;
173    /* 950 MHz GFSK 802.15.4d-2009 */
174    ieee->phy->channels_supported[6] |= 0x3ffc00;
175
176
177    INIT_LIST_HEAD(&priv->list);
178    priv->fake = fake;
179
180    ieee->parent = dev;
181
182    err = ieee802154_register_device(ieee);
183    if (err)
184        goto err_reg;
185
186    write_lock_bh(&fake->lock);
187    list_add_tail(&priv->list, &fake->list);
188    write_unlock_bh(&fake->lock);
189
190    return 0;
191
192err_reg:
193    ieee802154_free_device(priv->dev);
194err_alloc_dev:
195    return err;
196}
197
198static void ieee802154fake_del_priv(struct fake_dev_priv *priv)
199{
200    write_lock_bh(&priv->fake->lock);
201    list_del(&priv->list);
202    write_unlock_bh(&priv->fake->lock);
203
204    ieee802154_unregister_device(priv->dev);
205    ieee802154_free_device(priv->dev);
206}
207
208static ssize_t
209adddev_store(struct device *dev, struct device_attribute *attr,
210    const char *buf, size_t n)
211{
212    struct platform_device *pdev = to_platform_device(dev);
213    struct fake_priv *priv = platform_get_drvdata(pdev);
214    int err;
215
216    err = ieee802154fake_add_priv(dev, priv);
217    if (err)
218        return err;
219    return n;
220}
221
222static DEVICE_ATTR(adddev, 0200, NULL, adddev_store);
223
224static struct attribute *fake_attrs[] = {
225    &dev_attr_adddev.attr,
226    NULL,
227};
228
229static struct attribute_group fake_group = {
230    .name = NULL /* fake */,
231    .attrs = fake_attrs,
232};
233
234
235static int __devinit ieee802154fake_probe(struct platform_device *pdev)
236{
237    struct fake_priv *priv;
238    struct fake_dev_priv *dp;
239
240    int err = -ENOMEM;
241    priv = kzalloc(sizeof(struct fake_priv), GFP_KERNEL);
242    if (!priv)
243        goto err_alloc;
244
245    INIT_LIST_HEAD(&priv->list);
246    rwlock_init(&priv->lock);
247
248    err = sysfs_create_group(&pdev->dev.kobj, &fake_group);
249    if (err)
250        goto err_grp;
251
252    err = ieee802154fake_add_priv(&pdev->dev, priv);
253    if (err < 0)
254        goto err_slave;
255
256    platform_set_drvdata(pdev, priv);
257    dev_info(&pdev->dev, "Added ieee802154 hardware\n");
258    return 0;
259
260err_slave:
261    list_for_each_entry(dp, &priv->list, list)
262        ieee802154fake_del_priv(dp);
263    sysfs_remove_group(&pdev->dev.kobj, &fake_group);
264err_grp:
265    kfree(priv);
266err_alloc:
267    return err;
268}
269
270static int __devexit ieee802154fake_remove(struct platform_device *pdev)
271{
272    struct fake_priv *priv = platform_get_drvdata(pdev);
273    struct fake_dev_priv *dp, *temp;
274
275    list_for_each_entry_safe(dp, temp, &priv->list, list)
276        ieee802154fake_del_priv(dp);
277    sysfs_remove_group(&pdev->dev.kobj, &fake_group);
278    kfree(priv);
279    return 0;
280}
281
282static struct platform_device *ieee802154fake_dev;
283
284static struct platform_driver ieee802154fake_driver = {
285    .probe = ieee802154fake_probe,
286    .remove = __devexit_p(ieee802154fake_remove),
287    .driver = {
288            .name = "ieee802154fakelb",
289            .owner = THIS_MODULE,
290    },
291};
292
293static __init int fake_init(void)
294{
295    ieee802154fake_dev = platform_device_register_simple(
296            "ieee802154fakelb", -1, NULL, 0);
297    return platform_driver_register(&ieee802154fake_driver);
298}
299
300static __exit void fake_exit(void)
301{
302    platform_driver_unregister(&ieee802154fake_driver);
303    platform_device_unregister(ieee802154fake_dev);
304}
305
306module_init(fake_init);
307module_exit(fake_exit);
308MODULE_LICENSE("GPL");
309MODULE_AUTHOR("Dmitry Eremin-Solenikov, Sergey Lapin");
310
311
312

Archive Download this file



interactive