e6e2993b7ec6eeba4e1cc01d61c5f8fc40e69de6
[firefly-linux-kernel-4.4.55.git] / drivers / net / ieee802154 / fakelb.c
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  * 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>
25 #include <linux/device.h>
26 #include <linux/spinlock.h>
27 #include <net/mac802154.h>
28 #include <net/cfg802154.h>
29
30 static int numlbs = 1;
31
32 struct fakelb_dev_priv {
33         struct ieee802154_dev *dev;
34
35         struct list_head list;
36         struct fakelb_priv *fake;
37
38         spinlock_t lock;
39         bool working;
40 };
41
42 struct fakelb_priv {
43         struct list_head list;
44         rwlock_t lock;
45 };
46
47 static int
48 fakelb_hw_ed(struct ieee802154_dev *dev, u8 *level)
49 {
50         might_sleep();
51         BUG_ON(!level);
52         *level = 0xbe;
53
54         return 0;
55 }
56
57 static int
58 fakelb_hw_channel(struct ieee802154_dev *dev, int page, int channel)
59 {
60         pr_debug("set channel to %d\n", channel);
61
62         might_sleep();
63         dev->phy->current_page = page;
64         dev->phy->current_channel = channel;
65
66         return 0;
67 }
68
69 static void
70 fakelb_hw_deliver(struct fakelb_dev_priv *priv, struct sk_buff *skb)
71 {
72         struct sk_buff *newskb;
73
74         spin_lock(&priv->lock);
75         if (priv->working) {
76                 newskb = pskb_copy(skb, GFP_ATOMIC);
77                 ieee802154_rx_irqsafe(priv->dev, newskb, 0xcc);
78         }
79         spin_unlock(&priv->lock);
80 }
81
82 static int
83 fakelb_hw_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
84 {
85         struct fakelb_dev_priv *priv = dev->priv;
86         struct fakelb_priv *fake = priv->fake;
87
88         might_sleep();
89
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);
94         } else {
95                 struct fakelb_dev_priv *dp;
96                 list_for_each_entry(dp, &priv->fake->list, list) {
97                         if (dp != priv &&
98                             (dp->dev->phy->current_channel ==
99                              priv->dev->phy->current_channel))
100                                 fakelb_hw_deliver(dp, skb);
101                 }
102         }
103         read_unlock_bh(&fake->lock);
104
105         return 0;
106 }
107
108 static int
109 fakelb_hw_start(struct ieee802154_dev *dev) {
110         struct fakelb_dev_priv *priv = dev->priv;
111         int ret = 0;
112
113         spin_lock(&priv->lock);
114         if (priv->working)
115                 ret = -EBUSY;
116         else
117                 priv->working = 1;
118         spin_unlock(&priv->lock);
119
120         return ret;
121 }
122
123 static void
124 fakelb_hw_stop(struct ieee802154_dev *dev) {
125         struct fakelb_dev_priv *priv = dev->priv;
126
127         spin_lock(&priv->lock);
128         priv->working = 0;
129         spin_unlock(&priv->lock);
130 }
131
132 static struct ieee802154_ops fakelb_ops = {
133         .owner = THIS_MODULE,
134         .xmit = fakelb_hw_xmit,
135         .ed = fakelb_hw_ed,
136         .set_channel = fakelb_hw_channel,
137         .start = fakelb_hw_start,
138         .stop = fakelb_hw_stop,
139 };
140
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");
144
145 static int fakelb_add_one(struct device *dev, struct fakelb_priv *fake)
146 {
147         struct fakelb_dev_priv *priv;
148         int err;
149         struct ieee802154_dev *ieee;
150
151         ieee = ieee802154_alloc_device(sizeof(*priv), &fakelb_ops);
152         if (!ieee)
153                 return -ENOMEM;
154
155         priv = ieee->priv;
156         priv->dev = ieee;
157
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;
188
189         INIT_LIST_HEAD(&priv->list);
190         priv->fake = fake;
191
192         spin_lock_init(&priv->lock);
193
194         ieee->parent = dev;
195
196         err = ieee802154_register_device(ieee);
197         if (err)
198                 goto err_reg;
199
200         write_lock_bh(&fake->lock);
201         list_add_tail(&priv->list, &fake->list);
202         write_unlock_bh(&fake->lock);
203
204         return 0;
205
206 err_reg:
207         ieee802154_free_device(priv->dev);
208         return err;
209 }
210
211 static void fakelb_del(struct fakelb_dev_priv *priv)
212 {
213         write_lock_bh(&priv->fake->lock);
214         list_del(&priv->list);
215         write_unlock_bh(&priv->fake->lock);
216
217         ieee802154_unregister_device(priv->dev);
218         ieee802154_free_device(priv->dev);
219 }
220
221 static int fakelb_probe(struct platform_device *pdev)
222 {
223         struct fakelb_priv *priv;
224         struct fakelb_dev_priv *dp;
225         int err = -ENOMEM;
226         int i;
227
228         priv = devm_kzalloc(&pdev->dev, sizeof(struct fakelb_priv),
229                             GFP_KERNEL);
230         if (!priv)
231                 goto err_alloc;
232
233         INIT_LIST_HEAD(&priv->list);
234         rwlock_init(&priv->lock);
235
236         for (i = 0; i < numlbs; i++) {
237                 err = fakelb_add_one(&pdev->dev, priv);
238                 if (err < 0)
239                         goto err_slave;
240         }
241
242         platform_set_drvdata(pdev, priv);
243         dev_info(&pdev->dev, "added ieee802154 hardware\n");
244         return 0;
245
246 err_slave:
247         list_for_each_entry(dp, &priv->list, list)
248                 fakelb_del(dp);
249 err_alloc:
250         return err;
251 }
252
253 static int fakelb_remove(struct platform_device *pdev)
254 {
255         struct fakelb_priv *priv = platform_get_drvdata(pdev);
256         struct fakelb_dev_priv *dp, *temp;
257
258         list_for_each_entry_safe(dp, temp, &priv->list, list)
259                 fakelb_del(dp);
260
261         return 0;
262 }
263
264 static struct platform_device *ieee802154fake_dev;
265
266 static struct platform_driver ieee802154fake_driver = {
267         .probe = fakelb_probe,
268         .remove = fakelb_remove,
269         .driver = {
270                         .name = "ieee802154fakelb",
271                         .owner = THIS_MODULE,
272         },
273 };
274
275 static __init int fakelb_init_module(void)
276 {
277         ieee802154fake_dev = platform_device_register_simple(
278                              "ieee802154fakelb", -1, NULL, 0);
279         return platform_driver_register(&ieee802154fake_driver);
280 }
281
282 static __exit void fake_remove_module(void)
283 {
284         platform_driver_unregister(&ieee802154fake_driver);
285         platform_device_unregister(ieee802154fake_dev);
286 }
287
288 module_init(fakelb_init_module);
289 module_exit(fake_remove_module);
290 MODULE_LICENSE("GPL");