blob: 9d0da4ec3e8c91a6aab44231c9e701691a814799 [file] [log] [blame]
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +00001/*
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 *
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000015 * 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>
Himangi Saraogi12b5c382014-05-19 22:25:11 +053025#include <linux/device.h>
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000026#include <linux/spinlock.h>
27#include <net/mac802154.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020028#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000029
Alexander Aring7e579052015-05-17 21:44:58 +020030static int numlbs = 2;
Alexander Aringe369dc82015-05-17 21:45:05 +020031
Alexander Aringb82b99f2015-05-17 21:45:03 +020032static LIST_HEAD(fakelb_phys);
Alexander Aringe369dc82015-05-17 21:45:05 +020033static DEFINE_SPINLOCK(fakelb_phys_lock);
34
35static LIST_HEAD(fakelb_ifup_phys);
36static DEFINE_RWLOCK(fakelb_ifup_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000037
Alexander Aringdb3f5d02015-05-17 21:45:00 +020038struct fakelb_phy {
Alexander Aring5a504392014-10-25 17:16:34 +020039 struct ieee802154_hw *hw;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000040
Alexander Aring12da8d92015-05-17 21:45:06 +020041 u8 page;
42 u8 channel;
43
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000044 struct list_head list;
Alexander Aringe369dc82015-05-17 21:45:05 +020045 struct list_head list_ifup;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000046};
47
Alexander Aring39572ab2015-05-17 21:45:10 +020048static int fakelb_hw_ed(struct ieee802154_hw *hw, u8 *level)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000049{
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000050 BUG_ON(!level);
51 *level = 0xbe;
52
53 return 0;
54}
55
Alexander Aring39572ab2015-05-17 21:45:10 +020056static int fakelb_hw_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000057{
Alexander Aring12da8d92015-05-17 21:45:06 +020058 struct fakelb_phy *phy = hw->priv;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000059
Alexander Aring12da8d92015-05-17 21:45:06 +020060 write_lock_bh(&fakelb_ifup_phys_lock);
61 phy->page = page;
62 phy->channel = channel;
63 write_unlock_bh(&fakelb_ifup_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000064 return 0;
65}
66
Alexander Aring39572ab2015-05-17 21:45:10 +020067static int fakelb_hw_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000068{
Alexander Aring39572ab2015-05-17 21:45:10 +020069 struct fakelb_phy *current_phy = hw->priv, *phy;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000070
Alexander Aringe369dc82015-05-17 21:45:05 +020071 read_lock_bh(&fakelb_ifup_phys_lock);
72 list_for_each_entry(phy, &fakelb_ifup_phys, list_ifup) {
Alexander Aring9f8b97f2015-05-17 21:45:01 +020073 if (current_phy == phy)
74 continue;
Alexander Aringdb3f5d02015-05-17 21:45:00 +020075
Alexander Aring12da8d92015-05-17 21:45:06 +020076 if (current_phy->page == phy->page &&
Alexander Aring98be1652015-05-17 21:45:08 +020077 current_phy->channel == phy->channel) {
78 struct sk_buff *newskb = pskb_copy(skb, GFP_ATOMIC);
79
80 if (newskb)
81 ieee802154_rx_irqsafe(phy->hw, newskb, 0xcc);
82 }
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000083 }
Alexander Aringe369dc82015-05-17 21:45:05 +020084 read_unlock_bh(&fakelb_ifup_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000085
Alexander Aringa09c07a2015-05-17 21:45:09 +020086 ieee802154_xmit_complete(hw, skb, false);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000087 return 0;
88}
89
Alexander Aring39572ab2015-05-17 21:45:10 +020090static int fakelb_hw_start(struct ieee802154_hw *hw)
91{
Alexander Aringdb3f5d02015-05-17 21:45:00 +020092 struct fakelb_phy *phy = hw->priv;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000093
Alexander Aringe369dc82015-05-17 21:45:05 +020094 write_lock_bh(&fakelb_ifup_phys_lock);
95 list_add(&phy->list_ifup, &fakelb_ifup_phys);
96 write_unlock_bh(&fakelb_ifup_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000097
Alexander Aringe369dc82015-05-17 21:45:05 +020098 return 0;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +000099}
100
Alexander Aring39572ab2015-05-17 21:45:10 +0200101static void fakelb_hw_stop(struct ieee802154_hw *hw)
102{
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200103 struct fakelb_phy *phy = hw->priv;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000104
Alexander Aringe369dc82015-05-17 21:45:05 +0200105 write_lock_bh(&fakelb_ifup_phys_lock);
106 list_del(&phy->list_ifup);
107 write_unlock_bh(&fakelb_ifup_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000108}
109
Alexander Aring16301862014-10-28 18:21:18 +0100110static const struct ieee802154_ops fakelb_ops = {
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000111 .owner = THIS_MODULE,
Alexander Aringa09c07a2015-05-17 21:45:09 +0200112 .xmit_async = fakelb_hw_xmit,
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000113 .ed = fakelb_hw_ed,
114 .set_channel = fakelb_hw_channel,
115 .start = fakelb_hw_start,
116 .stop = fakelb_hw_stop,
117};
118
119/* Number of dummy devices to be set up by this module. */
120module_param(numlbs, int, 0);
121MODULE_PARM_DESC(numlbs, " number of pseudo devices");
122
Alexander Aringb82b99f2015-05-17 21:45:03 +0200123static int fakelb_add_one(struct device *dev)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000124{
Alexander Aring39572ab2015-05-17 21:45:10 +0200125 struct ieee802154_hw *hw;
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200126 struct fakelb_phy *phy;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000127 int err;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000128
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200129 hw = ieee802154_alloc_hw(sizeof(*phy), &fakelb_ops);
Alexander Aring5a504392014-10-25 17:16:34 +0200130 if (!hw)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000131 return -ENOMEM;
132
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200133 phy = hw->priv;
134 phy->hw = hw;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000135
136 /* 868 MHz BPSK 802.15.4-2003 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200137 hw->phy->supported.channels[0] |= 1;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000138 /* 915 MHz BPSK 802.15.4-2003 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200139 hw->phy->supported.channels[0] |= 0x7fe;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000140 /* 2.4 GHz O-QPSK 802.15.4-2003 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200141 hw->phy->supported.channels[0] |= 0x7FFF800;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000142 /* 868 MHz ASK 802.15.4-2006 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200143 hw->phy->supported.channels[1] |= 1;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000144 /* 915 MHz ASK 802.15.4-2006 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200145 hw->phy->supported.channels[1] |= 0x7fe;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000146 /* 868 MHz O-QPSK 802.15.4-2006 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200147 hw->phy->supported.channels[2] |= 1;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000148 /* 915 MHz O-QPSK 802.15.4-2006 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200149 hw->phy->supported.channels[2] |= 0x7fe;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000150 /* 2.4 GHz CSS 802.15.4a-2007 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200151 hw->phy->supported.channels[3] |= 0x3fff;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000152 /* UWB Sub-gigahertz 802.15.4a-2007 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200153 hw->phy->supported.channels[4] |= 1;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000154 /* UWB Low band 802.15.4a-2007 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200155 hw->phy->supported.channels[4] |= 0x1e;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000156 /* UWB High band 802.15.4a-2007 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200157 hw->phy->supported.channels[4] |= 0xffe0;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000158 /* 750 MHz O-QPSK 802.15.4c-2009 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200159 hw->phy->supported.channels[5] |= 0xf;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000160 /* 750 MHz MPSK 802.15.4c-2009 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200161 hw->phy->supported.channels[5] |= 0xf0;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000162 /* 950 MHz BPSK 802.15.4d-2009 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200163 hw->phy->supported.channels[6] |= 0x3ff;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000164 /* 950 MHz GFSK 802.15.4d-2009 */
Alexander Aring72f655e2015-05-17 21:44:42 +0200165 hw->phy->supported.channels[6] |= 0x3ffc00;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000166
Alexander Aringe214a902015-05-17 21:45:07 +0200167 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
168 /* fake phy channel 13 as default */
169 hw->phy->current_channel = 13;
170 phy->channel = hw->phy->current_channel;
171
Alexander Aring5a504392014-10-25 17:16:34 +0200172 hw->parent = dev;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000173
Alexander Aring5a504392014-10-25 17:16:34 +0200174 err = ieee802154_register_hw(hw);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000175 if (err)
176 goto err_reg;
177
Alexander Aringe369dc82015-05-17 21:45:05 +0200178 spin_lock(&fakelb_phys_lock);
Alexander Aringb82b99f2015-05-17 21:45:03 +0200179 list_add_tail(&phy->list, &fakelb_phys);
Alexander Aringe369dc82015-05-17 21:45:05 +0200180 spin_unlock(&fakelb_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000181
182 return 0;
183
184err_reg:
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200185 ieee802154_free_hw(phy->hw);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000186 return err;
187}
188
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200189static void fakelb_del(struct fakelb_phy *phy)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000190{
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200191 list_del(&phy->list);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000192
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200193 ieee802154_unregister_hw(phy->hw);
194 ieee802154_free_hw(phy->hw);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000195}
196
Bill Pembertonbb1f4602012-12-03 09:24:12 -0500197static int fakelb_probe(struct platform_device *pdev)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000198{
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200199 struct fakelb_phy *phy, *tmp;
Alexander Aring39572ab2015-05-17 21:45:10 +0200200 int err, i;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000201
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000202 for (i = 0; i < numlbs; i++) {
Alexander Aringb82b99f2015-05-17 21:45:03 +0200203 err = fakelb_add_one(&pdev->dev);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000204 if (err < 0)
205 goto err_slave;
206 }
207
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000208 dev_info(&pdev->dev, "added ieee802154 hardware\n");
209 return 0;
210
211err_slave:
Alexander Aringe369dc82015-05-17 21:45:05 +0200212 spin_lock(&fakelb_phys_lock);
Alexander Aringb82b99f2015-05-17 21:45:03 +0200213 list_for_each_entry_safe(phy, tmp, &fakelb_phys, list)
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200214 fakelb_del(phy);
Alexander Aringe369dc82015-05-17 21:45:05 +0200215 spin_unlock(&fakelb_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000216 return err;
217}
218
Bill Pembertonbb1f4602012-12-03 09:24:12 -0500219static int fakelb_remove(struct platform_device *pdev)
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000220{
Alexander Aring39572ab2015-05-17 21:45:10 +0200221 struct fakelb_phy *phy, *tmp;
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000222
Alexander Aringe369dc82015-05-17 21:45:05 +0200223 spin_lock(&fakelb_phys_lock);
Alexander Aring39572ab2015-05-17 21:45:10 +0200224 list_for_each_entry_safe(phy, tmp, &fakelb_phys, list)
Alexander Aringdb3f5d02015-05-17 21:45:00 +0200225 fakelb_del(phy);
Alexander Aringe369dc82015-05-17 21:45:05 +0200226 spin_unlock(&fakelb_phys_lock);
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000227 return 0;
228}
229
230static struct platform_device *ieee802154fake_dev;
231
232static struct platform_driver ieee802154fake_driver = {
233 .probe = fakelb_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -0500234 .remove = fakelb_remove,
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000235 .driver = {
236 .name = "ieee802154fakelb",
alex.bluesman.smirnov@gmail.come1e49b62012-05-15 20:50:30 +0000237 },
238};
239
240static __init int fakelb_init_module(void)
241{
242 ieee802154fake_dev = platform_device_register_simple(
243 "ieee802154fakelb", -1, NULL, 0);
244 return platform_driver_register(&ieee802154fake_driver);
245}
246
247static __exit void fake_remove_module(void)
248{
249 platform_driver_unregister(&ieee802154fake_driver);
250 platform_device_unregister(ieee802154fake_dev);
251}
252
253module_init(fakelb_init_module);
254module_exit(fake_remove_module);
255MODULE_LICENSE("GPL");