xref: /openbmc/linux/drivers/net/ieee802154/fakelb.c (revision 98be165b4355f750a62ca5ad280e04a135fe9bbd)
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 = 2;
31 
32 static LIST_HEAD(fakelb_phys);
33 static DEFINE_SPINLOCK(fakelb_phys_lock);
34 
35 static LIST_HEAD(fakelb_ifup_phys);
36 static DEFINE_RWLOCK(fakelb_ifup_phys_lock);
37 
38 struct fakelb_phy {
39 	struct ieee802154_hw *hw;
40 
41 	u8 page;
42 	u8 channel;
43 
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 	BUG_ON(!level);
52 	*level = 0xbe;
53 
54 	return 0;
55 }
56 
57 static int
58 fakelb_hw_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
59 {
60 	struct fakelb_phy *phy = hw->priv;
61 
62 	write_lock_bh(&fakelb_ifup_phys_lock);
63 	phy->page = page;
64 	phy->channel = channel;
65 	write_unlock_bh(&fakelb_ifup_phys_lock);
66 	return 0;
67 }
68 
69 static int
70 fakelb_hw_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
71 {
72 	struct fakelb_phy *current_phy = hw->priv;
73 	struct fakelb_phy *phy;
74 
75 	read_lock_bh(&fakelb_ifup_phys_lock);
76 	list_for_each_entry(phy, &fakelb_ifup_phys, list_ifup) {
77 		if (current_phy == phy)
78 			continue;
79 
80 		if (current_phy->page == phy->page &&
81 		    current_phy->channel == phy->channel) {
82 			struct sk_buff *newskb = pskb_copy(skb, GFP_ATOMIC);
83 
84 			if (newskb)
85 				ieee802154_rx_irqsafe(phy->hw, newskb, 0xcc);
86 		}
87 	}
88 	read_unlock_bh(&fakelb_ifup_phys_lock);
89 
90 	return 0;
91 }
92 
93 static int
94 fakelb_hw_start(struct ieee802154_hw *hw) {
95 	struct fakelb_phy *phy = hw->priv;
96 
97 	write_lock_bh(&fakelb_ifup_phys_lock);
98 	list_add(&phy->list_ifup, &fakelb_ifup_phys);
99 	write_unlock_bh(&fakelb_ifup_phys_lock);
100 
101 	return 0;
102 }
103 
104 static void
105 fakelb_hw_stop(struct ieee802154_hw *hw) {
106 	struct fakelb_phy *phy = hw->priv;
107 
108 	write_lock_bh(&fakelb_ifup_phys_lock);
109 	list_del(&phy->list_ifup);
110 	write_unlock_bh(&fakelb_ifup_phys_lock);
111 }
112 
113 static const struct ieee802154_ops fakelb_ops = {
114 	.owner = THIS_MODULE,
115 	.xmit_sync = fakelb_hw_xmit,
116 	.ed = fakelb_hw_ed,
117 	.set_channel = fakelb_hw_channel,
118 	.start = fakelb_hw_start,
119 	.stop = fakelb_hw_stop,
120 };
121 
122 /* Number of dummy devices to be set up by this module. */
123 module_param(numlbs, int, 0);
124 MODULE_PARM_DESC(numlbs, " number of pseudo devices");
125 
126 static int fakelb_add_one(struct device *dev)
127 {
128 	struct fakelb_phy *phy;
129 	int err;
130 	struct ieee802154_hw *hw;
131 
132 	hw = ieee802154_alloc_hw(sizeof(*phy), &fakelb_ops);
133 	if (!hw)
134 		return -ENOMEM;
135 
136 	phy = hw->priv;
137 	phy->hw = hw;
138 
139 	/* 868 MHz BPSK	802.15.4-2003 */
140 	hw->phy->supported.channels[0] |= 1;
141 	/* 915 MHz BPSK	802.15.4-2003 */
142 	hw->phy->supported.channels[0] |= 0x7fe;
143 	/* 2.4 GHz O-QPSK 802.15.4-2003 */
144 	hw->phy->supported.channels[0] |= 0x7FFF800;
145 	/* 868 MHz ASK 802.15.4-2006 */
146 	hw->phy->supported.channels[1] |= 1;
147 	/* 915 MHz ASK 802.15.4-2006 */
148 	hw->phy->supported.channels[1] |= 0x7fe;
149 	/* 868 MHz O-QPSK 802.15.4-2006 */
150 	hw->phy->supported.channels[2] |= 1;
151 	/* 915 MHz O-QPSK 802.15.4-2006 */
152 	hw->phy->supported.channels[2] |= 0x7fe;
153 	/* 2.4 GHz CSS 802.15.4a-2007 */
154 	hw->phy->supported.channels[3] |= 0x3fff;
155 	/* UWB Sub-gigahertz 802.15.4a-2007 */
156 	hw->phy->supported.channels[4] |= 1;
157 	/* UWB Low band 802.15.4a-2007 */
158 	hw->phy->supported.channels[4] |= 0x1e;
159 	/* UWB High band 802.15.4a-2007 */
160 	hw->phy->supported.channels[4] |= 0xffe0;
161 	/* 750 MHz O-QPSK 802.15.4c-2009 */
162 	hw->phy->supported.channels[5] |= 0xf;
163 	/* 750 MHz MPSK 802.15.4c-2009 */
164 	hw->phy->supported.channels[5] |= 0xf0;
165 	/* 950 MHz BPSK 802.15.4d-2009 */
166 	hw->phy->supported.channels[6] |= 0x3ff;
167 	/* 950 MHz GFSK 802.15.4d-2009 */
168 	hw->phy->supported.channels[6] |= 0x3ffc00;
169 
170 	ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
171 	/* fake phy channel 13 as default */
172 	hw->phy->current_channel = 13;
173 	phy->channel = hw->phy->current_channel;
174 
175 	hw->parent = dev;
176 
177 	err = ieee802154_register_hw(hw);
178 	if (err)
179 		goto err_reg;
180 
181 	spin_lock(&fakelb_phys_lock);
182 	list_add_tail(&phy->list, &fakelb_phys);
183 	spin_unlock(&fakelb_phys_lock);
184 
185 	return 0;
186 
187 err_reg:
188 	ieee802154_free_hw(phy->hw);
189 	return err;
190 }
191 
192 static void fakelb_del(struct fakelb_phy *phy)
193 {
194 	list_del(&phy->list);
195 
196 	ieee802154_unregister_hw(phy->hw);
197 	ieee802154_free_hw(phy->hw);
198 }
199 
200 static int fakelb_probe(struct platform_device *pdev)
201 {
202 	struct fakelb_phy *phy, *tmp;
203 	int err = -ENOMEM;
204 	int i;
205 
206 	for (i = 0; i < numlbs; i++) {
207 		err = fakelb_add_one(&pdev->dev);
208 		if (err < 0)
209 			goto err_slave;
210 	}
211 
212 	dev_info(&pdev->dev, "added ieee802154 hardware\n");
213 	return 0;
214 
215 err_slave:
216 	spin_lock(&fakelb_phys_lock);
217 	list_for_each_entry_safe(phy, tmp, &fakelb_phys, list)
218 		fakelb_del(phy);
219 	spin_unlock(&fakelb_phys_lock);
220 	return err;
221 }
222 
223 static int fakelb_remove(struct platform_device *pdev)
224 {
225 	struct fakelb_phy *phy, *temp;
226 
227 	spin_lock(&fakelb_phys_lock);
228 	list_for_each_entry_safe(phy, temp, &fakelb_phys, list)
229 		fakelb_del(phy);
230 	spin_unlock(&fakelb_phys_lock);
231 	return 0;
232 }
233 
234 static struct platform_device *ieee802154fake_dev;
235 
236 static struct platform_driver ieee802154fake_driver = {
237 	.probe = fakelb_probe,
238 	.remove = fakelb_remove,
239 	.driver = {
240 			.name = "ieee802154fakelb",
241 	},
242 };
243 
244 static __init int fakelb_init_module(void)
245 {
246 	ieee802154fake_dev = platform_device_register_simple(
247 			     "ieee802154fakelb", -1, NULL, 0);
248 	return platform_driver_register(&ieee802154fake_driver);
249 }
250 
251 static __exit void fake_remove_module(void)
252 {
253 	platform_driver_unregister(&ieee802154fake_driver);
254 	platform_device_unregister(ieee802154fake_dev);
255 }
256 
257 module_init(fakelb_init_module);
258 module_exit(fake_remove_module);
259 MODULE_LICENSE("GPL");
260