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
static LIST_HEAD(fakelb_phys);
33
static DEFINE_MUTEX(fakelb_phys_lock);
34 35 36

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 45
	bool suspended;

46
	struct list_head list;
47
	struct list_head list_ifup;
48 49
};

A
Alexander Aring 已提交
50
static int fakelb_hw_ed(struct ieee802154_hw *hw, u8 *level)
51 52 53 54 55 56 57
{
	BUG_ON(!level);
	*level = 0xbe;

	return 0;
}

A
Alexander Aring 已提交
58
static int 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
	return 0;
}

A
Alexander Aring 已提交
69
static int fakelb_hw_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
70
{
A
Alexander Aring 已提交
71
	struct fakelb_phy *current_phy = hw->priv, *phy;
72

73
	read_lock_bh(&fakelb_ifup_phys_lock);
74
	WARN_ON(current_phy->suspended);
75
	list_for_each_entry(phy, &fakelb_ifup_phys, list_ifup) {
76 77 78
		if (current_phy == phy)
			continue;

79
		if (current_phy->page == phy->page &&
80 81 82 83 84 85
		    current_phy->channel == phy->channel) {
			struct sk_buff *newskb = pskb_copy(skb, GFP_ATOMIC);

			if (newskb)
				ieee802154_rx_irqsafe(phy->hw, newskb, 0xcc);
		}
86
	}
87
	read_unlock_bh(&fakelb_ifup_phys_lock);
88

89
	ieee802154_xmit_complete(hw, skb, false);
90 91 92
	return 0;
}

A
Alexander Aring 已提交
93 94
static int fakelb_hw_start(struct ieee802154_hw *hw)
{
95
	struct fakelb_phy *phy = hw->priv;
96

97
	write_lock_bh(&fakelb_ifup_phys_lock);
98
	phy->suspended = false;
99 100
	list_add(&phy->list_ifup, &fakelb_ifup_phys);
	write_unlock_bh(&fakelb_ifup_phys_lock);
101

102
	return 0;
103 104
}

A
Alexander Aring 已提交
105 106
static void fakelb_hw_stop(struct ieee802154_hw *hw)
{
107
	struct fakelb_phy *phy = hw->priv;
108

109
	write_lock_bh(&fakelb_ifup_phys_lock);
110
	phy->suspended = true;
111 112
	list_del(&phy->list_ifup);
	write_unlock_bh(&fakelb_ifup_phys_lock);
113 114
}

115 116 117 118 119 120
static int
fakelb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
{
	return 0;
}

121
static const struct ieee802154_ops fakelb_ops = {
122
	.owner = THIS_MODULE,
123
	.xmit_async = fakelb_hw_xmit,
124 125 126 127
	.ed = fakelb_hw_ed,
	.set_channel = fakelb_hw_channel,
	.start = fakelb_hw_start,
	.stop = fakelb_hw_stop,
128
	.set_promiscuous_mode = fakelb_set_promiscuous_mode,
129 130 131 132 133 134
};

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

135
static int fakelb_add_one(struct device *dev)
136
{
A
Alexander Aring 已提交
137
	struct ieee802154_hw *hw;
138
	struct fakelb_phy *phy;
139 140
	int err;

141
	hw = ieee802154_alloc_hw(sizeof(*phy), &fakelb_ops);
142
	if (!hw)
143 144
		return -ENOMEM;

145 146
	phy = hw->priv;
	phy->hw = hw;
147 148

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

179 180 181 182 183
	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;

184
	hw->flags = IEEE802154_HW_PROMISCUOUS;
185
	hw->parent = dev;
186

187
	err = ieee802154_register_hw(hw);
188 189 190
	if (err)
		goto err_reg;

191
	mutex_lock(&fakelb_phys_lock);
192
	list_add_tail(&phy->list, &fakelb_phys);
193
	mutex_unlock(&fakelb_phys_lock);
194 195 196 197

	return 0;

err_reg:
198
	ieee802154_free_hw(phy->hw);
199 200 201
	return err;
}

202
static void fakelb_del(struct fakelb_phy *phy)
203
{
204
	list_del(&phy->list);
205

206 207
	ieee802154_unregister_hw(phy->hw);
	ieee802154_free_hw(phy->hw);
208 209
}

210
static int fakelb_probe(struct platform_device *pdev)
211
{
212
	struct fakelb_phy *phy, *tmp;
A
Alexander Aring 已提交
213
	int err, i;
214 215

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

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

err_slave:
225
	mutex_lock(&fakelb_phys_lock);
226
	list_for_each_entry_safe(phy, tmp, &fakelb_phys, list)
227
		fakelb_del(phy);
228
	mutex_unlock(&fakelb_phys_lock);
229 230 231
	return err;
}

232
static int fakelb_remove(struct platform_device *pdev)
233
{
A
Alexander Aring 已提交
234
	struct fakelb_phy *phy, *tmp;
235

236
	mutex_lock(&fakelb_phys_lock);
A
Alexander Aring 已提交
237
	list_for_each_entry_safe(phy, tmp, &fakelb_phys, list)
238
		fakelb_del(phy);
239
	mutex_unlock(&fakelb_phys_lock);
240 241 242 243 244 245 246
	return 0;
}

static struct platform_device *ieee802154fake_dev;

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