fakelb: rename fakelb_dev_priv to fakelb_phy
[deliverable/linux.git] / drivers / net / ieee802154 / fakelb.c
CommitLineData
e1e49b64 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 *
e1e49b64 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>
12b5c38f 25#include <linux/device.h>
e1e49b64 26#include <linux/spinlock.h>
27#include <net/mac802154.h>
5ad60d36 28#include <net/cfg802154.h>
e1e49b64 29
7e57905b 30static int numlbs = 2;
e1e49b64 31
db3f5d0d 32struct fakelb_phy {
5a504397 33 struct ieee802154_hw *hw;
e1e49b64 34
35 struct list_head list;
36 struct fakelb_priv *fake;
37
38 spinlock_t lock;
39 bool working;
40};
41
42struct fakelb_priv {
43 struct list_head list;
44 rwlock_t lock;
45};
46
47static int
5a504397 48fakelb_hw_ed(struct ieee802154_hw *hw, u8 *level)
e1e49b64 49{
e1e49b64 50 BUG_ON(!level);
51 *level = 0xbe;
52
53 return 0;
54}
55
56static int
e37d2ec8 57fakelb_hw_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
e1e49b64 58{
59 pr_debug("set channel to %d\n", channel);
60
e1e49b64 61 return 0;
62}
63
64static void
db3f5d0d 65fakelb_hw_deliver(struct fakelb_phy *phy, struct sk_buff *skb)
e1e49b64 66{
67 struct sk_buff *newskb;
68
db3f5d0d
AA
69 spin_lock(&phy->lock);
70 if (phy->working) {
e1e49b64 71 newskb = pskb_copy(skb, GFP_ATOMIC);
cb97d9a3 72 if (newskb)
db3f5d0d 73 ieee802154_rx_irqsafe(phy->hw, newskb, 0xcc);
e1e49b64 74 }
db3f5d0d 75 spin_unlock(&phy->lock);
e1e49b64 76}
77
78static int
5a504397 79fakelb_hw_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
e1e49b64 80{
db3f5d0d
AA
81 struct fakelb_phy *current_phy = hw->priv;
82 struct fakelb_priv *fake = current_phy->fake;
e1e49b64 83
e1e49b64 84 read_lock_bh(&fake->lock);
db3f5d0d 85 if (current_phy->list.next == current_phy->list.prev) {
e1e49b64 86 /* we are the only one device */
db3f5d0d 87 fakelb_hw_deliver(current_phy, skb);
e1e49b64 88 } else {
db3f5d0d
AA
89 struct fakelb_phy *phy;
90
91 list_for_each_entry(phy, &current_phy->fake->list, list) {
92 if (current_phy != phy &&
93 (phy->hw->phy->current_channel ==
94 current_phy->hw->phy->current_channel))
95 fakelb_hw_deliver(phy, skb);
e1e49b64 96 }
97 }
98 read_unlock_bh(&fake->lock);
99
100 return 0;
101}
102
103static int
5a504397 104fakelb_hw_start(struct ieee802154_hw *hw) {
db3f5d0d 105 struct fakelb_phy *phy = hw->priv;
e1e49b64 106 int ret = 0;
107
db3f5d0d
AA
108 spin_lock(&phy->lock);
109 if (phy->working)
e1e49b64 110 ret = -EBUSY;
111 else
db3f5d0d
AA
112 phy->working = 1;
113 spin_unlock(&phy->lock);
e1e49b64 114
115 return ret;
116}
117
118static void
5a504397 119fakelb_hw_stop(struct ieee802154_hw *hw) {
db3f5d0d 120 struct fakelb_phy *phy = hw->priv;
e1e49b64 121
db3f5d0d
AA
122 spin_lock(&phy->lock);
123 phy->working = 0;
124 spin_unlock(&phy->lock);
e1e49b64 125}
126
16301861 127static const struct ieee802154_ops fakelb_ops = {
e1e49b64 128 .owner = THIS_MODULE,
ed0a5dce 129 .xmit_sync = fakelb_hw_xmit,
e1e49b64 130 .ed = fakelb_hw_ed,
131 .set_channel = fakelb_hw_channel,
132 .start = fakelb_hw_start,
133 .stop = fakelb_hw_stop,
134};
135
136/* Number of dummy devices to be set up by this module. */
137module_param(numlbs, int, 0);
138MODULE_PARM_DESC(numlbs, " number of pseudo devices");
139
140static int fakelb_add_one(struct device *dev, struct fakelb_priv *fake)
141{
db3f5d0d 142 struct fakelb_phy *phy;
e1e49b64 143 int err;
5a504397 144 struct ieee802154_hw *hw;
e1e49b64 145
db3f5d0d 146 hw = ieee802154_alloc_hw(sizeof(*phy), &fakelb_ops);
5a504397 147 if (!hw)
e1e49b64 148 return -ENOMEM;
149
db3f5d0d
AA
150 phy = hw->priv;
151 phy->hw = hw;
e1e49b64 152
153 /* 868 MHz BPSK 802.15.4-2003 */
72f655e4 154 hw->phy->supported.channels[0] |= 1;
e1e49b64 155 /* 915 MHz BPSK 802.15.4-2003 */
72f655e4 156 hw->phy->supported.channels[0] |= 0x7fe;
e1e49b64 157 /* 2.4 GHz O-QPSK 802.15.4-2003 */
72f655e4 158 hw->phy->supported.channels[0] |= 0x7FFF800;
e1e49b64 159 /* 868 MHz ASK 802.15.4-2006 */
72f655e4 160 hw->phy->supported.channels[1] |= 1;
e1e49b64 161 /* 915 MHz ASK 802.15.4-2006 */
72f655e4 162 hw->phy->supported.channels[1] |= 0x7fe;
e1e49b64 163 /* 868 MHz O-QPSK 802.15.4-2006 */
72f655e4 164 hw->phy->supported.channels[2] |= 1;
e1e49b64 165 /* 915 MHz O-QPSK 802.15.4-2006 */
72f655e4 166 hw->phy->supported.channels[2] |= 0x7fe;
e1e49b64 167 /* 2.4 GHz CSS 802.15.4a-2007 */
72f655e4 168 hw->phy->supported.channels[3] |= 0x3fff;
e1e49b64 169 /* UWB Sub-gigahertz 802.15.4a-2007 */
72f655e4 170 hw->phy->supported.channels[4] |= 1;
e1e49b64 171 /* UWB Low band 802.15.4a-2007 */
72f655e4 172 hw->phy->supported.channels[4] |= 0x1e;
e1e49b64 173 /* UWB High band 802.15.4a-2007 */
72f655e4 174 hw->phy->supported.channels[4] |= 0xffe0;
e1e49b64 175 /* 750 MHz O-QPSK 802.15.4c-2009 */
72f655e4 176 hw->phy->supported.channels[5] |= 0xf;
e1e49b64 177 /* 750 MHz MPSK 802.15.4c-2009 */
72f655e4 178 hw->phy->supported.channels[5] |= 0xf0;
e1e49b64 179 /* 950 MHz BPSK 802.15.4d-2009 */
72f655e4 180 hw->phy->supported.channels[6] |= 0x3ff;
e1e49b64 181 /* 950 MHz GFSK 802.15.4d-2009 */
72f655e4 182 hw->phy->supported.channels[6] |= 0x3ffc00;
e1e49b64 183
db3f5d0d
AA
184 INIT_LIST_HEAD(&phy->list);
185 phy->fake = fake;
e1e49b64 186
db3f5d0d 187 spin_lock_init(&phy->lock);
e1e49b64 188
5a504397 189 hw->parent = dev;
e1e49b64 190
5a504397 191 err = ieee802154_register_hw(hw);
e1e49b64 192 if (err)
193 goto err_reg;
194
195 write_lock_bh(&fake->lock);
db3f5d0d 196 list_add_tail(&phy->list, &fake->list);
e1e49b64 197 write_unlock_bh(&fake->lock);
198
199 return 0;
200
201err_reg:
db3f5d0d 202 ieee802154_free_hw(phy->hw);
e1e49b64 203 return err;
204}
205
db3f5d0d 206static void fakelb_del(struct fakelb_phy *phy)
e1e49b64 207{
db3f5d0d
AA
208 write_lock_bh(&phy->fake->lock);
209 list_del(&phy->list);
210 write_unlock_bh(&phy->fake->lock);
e1e49b64 211
db3f5d0d
AA
212 ieee802154_unregister_hw(phy->hw);
213 ieee802154_free_hw(phy->hw);
e1e49b64 214}
215
bb1f4606 216static int fakelb_probe(struct platform_device *pdev)
e1e49b64 217{
218 struct fakelb_priv *priv;
db3f5d0d 219 struct fakelb_phy *phy, *tmp;
e1e49b64 220 int err = -ENOMEM;
221 int i;
222
12b5c38f
HS
223 priv = devm_kzalloc(&pdev->dev, sizeof(struct fakelb_priv),
224 GFP_KERNEL);
e1e49b64 225 if (!priv)
226 goto err_alloc;
227
228 INIT_LIST_HEAD(&priv->list);
229 rwlock_init(&priv->lock);
230
231 for (i = 0; i < numlbs; i++) {
232 err = fakelb_add_one(&pdev->dev, priv);
233 if (err < 0)
234 goto err_slave;
235 }
236
237 platform_set_drvdata(pdev, priv);
238 dev_info(&pdev->dev, "added ieee802154 hardware\n");
239 return 0;
240
241err_slave:
db3f5d0d
AA
242 list_for_each_entry_safe(phy, tmp, &priv->list, list)
243 fakelb_del(phy);
e1e49b64 244err_alloc:
245 return err;
246}
247
bb1f4606 248static int fakelb_remove(struct platform_device *pdev)
e1e49b64 249{
250 struct fakelb_priv *priv = platform_get_drvdata(pdev);
db3f5d0d 251 struct fakelb_phy *phy, *temp;
e1e49b64 252
db3f5d0d
AA
253 list_for_each_entry_safe(phy, temp, &priv->list, list)
254 fakelb_del(phy);
e1e49b64 255
256 return 0;
257}
258
259static struct platform_device *ieee802154fake_dev;
260
261static struct platform_driver ieee802154fake_driver = {
262 .probe = fakelb_probe,
bb1f4606 263 .remove = fakelb_remove,
e1e49b64 264 .driver = {
265 .name = "ieee802154fakelb",
e1e49b64 266 },
267};
268
269static __init int fakelb_init_module(void)
270{
271 ieee802154fake_dev = platform_device_register_simple(
272 "ieee802154fakelb", -1, NULL, 0);
273 return platform_driver_register(&ieee802154fake_driver);
274}
275
276static __exit void fake_remove_module(void)
277{
278 platform_driver_unregister(&ieee802154fake_driver);
279 platform_device_unregister(ieee802154fake_dev);
280}
281
282module_init(fakelb_init_module);
283module_exit(fake_remove_module);
284MODULE_LICENSE("GPL");
This page took 0.48474 seconds and 5 git commands to generate.