fakelb.c 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Loopback IEEE 802.15.4 interface
 *
 * Copyright 2007-2012 Siemens AG
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Written by:
 * Sergey Lapin <slapin@ossfans.org>
 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
 */

#include <linux/module.h>
#include <linux/timer.h>
#include <linux/platform_device.h>
#include <linux/netdevice.h>
25
#include <linux/device.h>
26 27
#include <linux/spinlock.h>
#include <net/mac802154.h>
28
#include <net/cfg802154.h>
29

30
static int numlbs = 2;
31

32
struct fakelb_phy {
33
	struct ieee802154_hw *hw;
34 35 36 37 38 39 40 41 42 43 44 45 46 47

	struct list_head list;
	struct fakelb_priv *fake;

	spinlock_t lock;
	bool working;
};

struct fakelb_priv {
	struct list_head list;
	rwlock_t lock;
};

static int
48
fakelb_hw_ed(struct ieee802154_hw *hw, u8 *level)
49 50 51 52 53 54 55 56
{
	BUG_ON(!level);
	*level = 0xbe;

	return 0;
}

static int
57
fakelb_hw_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
58 59 60 61 62 63 64
{
	pr_debug("set channel to %d\n", channel);

	return 0;
}

static void
65
fakelb_hw_deliver(struct fakelb_phy *phy, struct sk_buff *skb)
66 67 68
{
	struct sk_buff *newskb;

69 70
	spin_lock(&phy->lock);
	if (phy->working) {
71
		newskb = pskb_copy(skb, GFP_ATOMIC);
72
		if (newskb)
73
			ieee802154_rx_irqsafe(phy->hw, newskb, 0xcc);
74
	}
75
	spin_unlock(&phy->lock);
76 77 78
}

static int
79
fakelb_hw_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
80
{
81 82
	struct fakelb_phy *current_phy = hw->priv;
	struct fakelb_priv *fake = current_phy->fake;
83 84

	read_lock_bh(&fake->lock);
85
	if (current_phy->list.next == current_phy->list.prev) {
86
		/* we are the only one device */
87
		fakelb_hw_deliver(current_phy, skb);
88
	} else {
89 90 91 92 93 94 95
		struct fakelb_phy *phy;

		list_for_each_entry(phy, &current_phy->fake->list, list) {
			if (current_phy != phy &&
			    (phy->hw->phy->current_channel ==
			     current_phy->hw->phy->current_channel))
				fakelb_hw_deliver(phy, skb);
96 97 98 99 100 101 102 103
		}
	}
	read_unlock_bh(&fake->lock);

	return 0;
}

static int
104
fakelb_hw_start(struct ieee802154_hw *hw) {
105
	struct fakelb_phy *phy = hw->priv;
106 107
	int ret = 0;

108 109
	spin_lock(&phy->lock);
	if (phy->working)
110 111
		ret = -EBUSY;
	else
112 113
		phy->working = 1;
	spin_unlock(&phy->lock);
114 115 116 117 118

	return ret;
}

static void
119
fakelb_hw_stop(struct ieee802154_hw *hw) {
120
	struct fakelb_phy *phy = hw->priv;
121

122 123 124
	spin_lock(&phy->lock);
	phy->working = 0;
	spin_unlock(&phy->lock);
125 126
}

127
static const struct ieee802154_ops fakelb_ops = {
128
	.owner = THIS_MODULE,
129
	.xmit_sync = fakelb_hw_xmit,
130 131 132 133 134 135 136 137 138 139 140 141
	.ed = fakelb_hw_ed,
	.set_channel = fakelb_hw_channel,
	.start = fakelb_hw_start,
	.stop = fakelb_hw_stop,
};

/* Number of dummy devices to be set up by this module. */
module_param(numlbs, int, 0);
MODULE_PARM_DESC(numlbs, " number of pseudo devices");

static int fakelb_add_one(struct device *dev, struct fakelb_priv *fake)
{
142
	struct fakelb_phy *phy;
143
	int err;
144
	struct ieee802154_hw *hw;
145

146
	hw = ieee802154_alloc_hw(sizeof(*phy), &fakelb_ops);
147
	if (!hw)
148 149
		return -ENOMEM;

150 151
	phy = hw->priv;
	phy->hw = hw;
152 153

	/* 868 MHz BPSK	802.15.4-2003 */
154
	hw->phy->supported.channels[0] |= 1;
155
	/* 915 MHz BPSK	802.15.4-2003 */
156
	hw->phy->supported.channels[0] |= 0x7fe;
157
	/* 2.4 GHz O-QPSK 802.15.4-2003 */
158
	hw->phy->supported.channels[0] |= 0x7FFF800;
159
	/* 868 MHz ASK 802.15.4-2006 */
160
	hw->phy->supported.channels[1] |= 1;
161
	/* 915 MHz ASK 802.15.4-2006 */
162
	hw->phy->supported.channels[1] |= 0x7fe;
163
	/* 868 MHz O-QPSK 802.15.4-2006 */
164
	hw->phy->supported.channels[2] |= 1;
165
	/* 915 MHz O-QPSK 802.15.4-2006 */
166
	hw->phy->supported.channels[2] |= 0x7fe;
167
	/* 2.4 GHz CSS 802.15.4a-2007 */
168
	hw->phy->supported.channels[3] |= 0x3fff;
169
	/* UWB Sub-gigahertz 802.15.4a-2007 */
170
	hw->phy->supported.channels[4] |= 1;
171
	/* UWB Low band 802.15.4a-2007 */
172
	hw->phy->supported.channels[4] |= 0x1e;
173
	/* UWB High band 802.15.4a-2007 */
174
	hw->phy->supported.channels[4] |= 0xffe0;
175
	/* 750 MHz O-QPSK 802.15.4c-2009 */
176
	hw->phy->supported.channels[5] |= 0xf;
177
	/* 750 MHz MPSK 802.15.4c-2009 */
178
	hw->phy->supported.channels[5] |= 0xf0;
179
	/* 950 MHz BPSK 802.15.4d-2009 */
180
	hw->phy->supported.channels[6] |= 0x3ff;
181
	/* 950 MHz GFSK 802.15.4d-2009 */
182
	hw->phy->supported.channels[6] |= 0x3ffc00;
183

184 185
	INIT_LIST_HEAD(&phy->list);
	phy->fake = fake;
186

187
	spin_lock_init(&phy->lock);
188

189
	hw->parent = dev;
190

191
	err = ieee802154_register_hw(hw);
192 193 194 195
	if (err)
		goto err_reg;

	write_lock_bh(&fake->lock);
196
	list_add_tail(&phy->list, &fake->list);
197 198 199 200 201
	write_unlock_bh(&fake->lock);

	return 0;

err_reg:
202
	ieee802154_free_hw(phy->hw);
203 204 205
	return err;
}

206
static void fakelb_del(struct fakelb_phy *phy)
207
{
208 209 210
	write_lock_bh(&phy->fake->lock);
	list_del(&phy->list);
	write_unlock_bh(&phy->fake->lock);
211

212 213
	ieee802154_unregister_hw(phy->hw);
	ieee802154_free_hw(phy->hw);
214 215
}

216
static int fakelb_probe(struct platform_device *pdev)
217 218
{
	struct fakelb_priv *priv;
219
	struct fakelb_phy *phy, *tmp;
220 221 222
	int err = -ENOMEM;
	int i;

223 224
	priv = devm_kzalloc(&pdev->dev, sizeof(struct fakelb_priv),
			    GFP_KERNEL);
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	if (!priv)
		goto err_alloc;

	INIT_LIST_HEAD(&priv->list);
	rwlock_init(&priv->lock);

	for (i = 0; i < numlbs; i++) {
		err = fakelb_add_one(&pdev->dev, priv);
		if (err < 0)
			goto err_slave;
	}

	platform_set_drvdata(pdev, priv);
	dev_info(&pdev->dev, "added ieee802154 hardware\n");
	return 0;

err_slave:
242 243
	list_for_each_entry_safe(phy, tmp, &priv->list, list)
		fakelb_del(phy);
244 245 246 247
err_alloc:
	return err;
}

248
static int fakelb_remove(struct platform_device *pdev)
249 250
{
	struct fakelb_priv *priv = platform_get_drvdata(pdev);
251
	struct fakelb_phy *phy, *temp;
252

253 254
	list_for_each_entry_safe(phy, temp, &priv->list, list)
		fakelb_del(phy);
255 256 257 258 259 260 261 262

	return 0;
}

static struct platform_device *ieee802154fake_dev;

static struct platform_driver ieee802154fake_driver = {
	.probe = fakelb_probe,
263
	.remove = fakelb_remove,
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	.driver = {
			.name = "ieee802154fakelb",
	},
};

static __init int fakelb_init_module(void)
{
	ieee802154fake_dev = platform_device_register_simple(
			     "ieee802154fakelb", -1, NULL, 0);
	return platform_driver_register(&ieee802154fake_driver);
}

static __exit void fake_remove_module(void)
{
	platform_driver_unregister(&ieee802154fake_driver);
	platform_device_unregister(ieee802154fake_dev);
}

module_init(fakelb_init_module);
module_exit(fake_remove_module);
MODULE_LICENSE("GPL");