fakelb.c 6.2 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
static LIST_HEAD(fakelb_phys);
33 34 35 36
static DEFINE_SPINLOCK(fakelb_phys_lock);

static LIST_HEAD(fakelb_ifup_phys);
static DEFINE_RWLOCK(fakelb_ifup_phys_lock);
37

38
struct fakelb_phy {
39
	struct ieee802154_hw *hw;
40

41 42 43
	u8 page;
	u8 channel;

44
	struct list_head list;
45
	struct list_head list_ifup;
46 47 48
};

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

	return 0;
}

static int
58
fakelb_hw_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
59
{
60
	struct fakelb_phy *phy = hw->priv;
61

62 63 64 65
	write_lock_bh(&fakelb_ifup_phys_lock);
	phy->page = page;
	phy->channel = channel;
	write_unlock_bh(&fakelb_ifup_phys_lock);
66 67 68 69
	return 0;
}

static void
70
fakelb_hw_deliver(struct fakelb_phy *phy, struct sk_buff *skb)
71 72 73
{
	struct sk_buff *newskb;

74 75 76
	newskb = pskb_copy(skb, GFP_ATOMIC);
	if (newskb)
		ieee802154_rx_irqsafe(phy->hw, newskb, 0xcc);
77 78 79
}

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

85 86
	read_lock_bh(&fakelb_ifup_phys_lock);
	list_for_each_entry(phy, &fakelb_ifup_phys, list_ifup) {
87 88 89
		if (current_phy == phy)
			continue;

90 91
		if (current_phy->page == phy->page &&
		    current_phy->channel == phy->channel)
92
			fakelb_hw_deliver(phy, skb);
93
	}
94
	read_unlock_bh(&fakelb_ifup_phys_lock);
95 96 97 98 99

	return 0;
}

static int
100
fakelb_hw_start(struct ieee802154_hw *hw) {
101
	struct fakelb_phy *phy = hw->priv;
102

103 104 105
	write_lock_bh(&fakelb_ifup_phys_lock);
	list_add(&phy->list_ifup, &fakelb_ifup_phys);
	write_unlock_bh(&fakelb_ifup_phys_lock);
106

107
	return 0;
108 109 110
}

static void
111
fakelb_hw_stop(struct ieee802154_hw *hw) {
112
	struct fakelb_phy *phy = hw->priv;
113

114 115 116
	write_lock_bh(&fakelb_ifup_phys_lock);
	list_del(&phy->list_ifup);
	write_unlock_bh(&fakelb_ifup_phys_lock);
117 118
}

119
static const struct ieee802154_ops fakelb_ops = {
120
	.owner = THIS_MODULE,
121
	.xmit_sync = fakelb_hw_xmit,
122 123 124 125 126 127 128 129 130 131
	.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");

132
static int fakelb_add_one(struct device *dev)
133
{
134
	struct fakelb_phy *phy;
135
	int err;
136
	struct ieee802154_hw *hw;
137

138
	hw = ieee802154_alloc_hw(sizeof(*phy), &fakelb_ops);
139
	if (!hw)
140 141
		return -ENOMEM;

142 143
	phy = hw->priv;
	phy->hw = hw;
144 145

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

176 177 178 179 180
	ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
	/* fake phy channel 13 as default */
	hw->phy->current_channel = 13;
	phy->channel = hw->phy->current_channel;

181
	hw->parent = dev;
182

183
	err = ieee802154_register_hw(hw);
184 185 186
	if (err)
		goto err_reg;

187
	spin_lock(&fakelb_phys_lock);
188
	list_add_tail(&phy->list, &fakelb_phys);
189
	spin_unlock(&fakelb_phys_lock);
190 191 192 193

	return 0;

err_reg:
194
	ieee802154_free_hw(phy->hw);
195 196 197
	return err;
}

198
static void fakelb_del(struct fakelb_phy *phy)
199
{
200
	list_del(&phy->list);
201

202 203
	ieee802154_unregister_hw(phy->hw);
	ieee802154_free_hw(phy->hw);
204 205
}

206
static int fakelb_probe(struct platform_device *pdev)
207
{
208
	struct fakelb_phy *phy, *tmp;
209 210 211 212
	int err = -ENOMEM;
	int i;

	for (i = 0; i < numlbs; i++) {
213
		err = fakelb_add_one(&pdev->dev);
214 215 216 217 218 219 220 221
		if (err < 0)
			goto err_slave;
	}

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

err_slave:
222
	spin_lock(&fakelb_phys_lock);
223
	list_for_each_entry_safe(phy, tmp, &fakelb_phys, list)
224
		fakelb_del(phy);
225
	spin_unlock(&fakelb_phys_lock);
226 227 228
	return err;
}

229
static int fakelb_remove(struct platform_device *pdev)
230
{
231
	struct fakelb_phy *phy, *temp;
232

233
	spin_lock(&fakelb_phys_lock);
234
	list_for_each_entry_safe(phy, temp, &fakelb_phys, list)
235
		fakelb_del(phy);
236
	spin_unlock(&fakelb_phys_lock);
237 238 239 240 241 242 243
	return 0;
}

static struct platform_device *ieee802154fake_dev;

static struct platform_driver ieee802154fake_driver = {
	.probe = fakelb_probe,
244
	.remove = fakelb_remove,
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	.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");