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