alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 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 | * |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 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> |
Himangi Saraogi | 12b5c38 | 2014-05-19 22:25:11 +0530 | [diff] [blame] | 25 | #include <linux/device.h> |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 26 | #include <linux/spinlock.h> |
| 27 | #include <net/mac802154.h> |
Alexander Aring | 5ad60d3 | 2014-10-25 09:41:02 +0200 | [diff] [blame] | 28 | #include <net/cfg802154.h> |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 29 | |
| 30 | static int numlbs = 1; |
| 31 | |
| 32 | struct fakelb_dev_priv { |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 33 | struct ieee802154_hw *hw; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 34 | |
| 35 | struct list_head list; |
| 36 | struct fakelb_priv *fake; |
| 37 | |
| 38 | spinlock_t lock; |
| 39 | bool working; |
| 40 | }; |
| 41 | |
| 42 | struct fakelb_priv { |
| 43 | struct list_head list; |
| 44 | rwlock_t lock; |
| 45 | }; |
| 46 | |
| 47 | static int |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 48 | fakelb_hw_ed(struct ieee802154_hw *hw, u8 *level) |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 49 | { |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 50 | BUG_ON(!level); |
| 51 | *level = 0xbe; |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int |
Alexander Aring | e37d2ec | 2014-10-28 18:21:19 +0100 | [diff] [blame] | 57 | fakelb_hw_channel(struct ieee802154_hw *hw, u8 page, u8 channel) |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 58 | { |
| 59 | pr_debug("set channel to %d\n", channel); |
| 60 | |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | fakelb_hw_deliver(struct fakelb_dev_priv *priv, struct sk_buff *skb) |
| 66 | { |
| 67 | struct sk_buff *newskb; |
| 68 | |
| 69 | spin_lock(&priv->lock); |
| 70 | if (priv->working) { |
| 71 | newskb = pskb_copy(skb, GFP_ATOMIC); |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 72 | ieee802154_rx_irqsafe(priv->hw, newskb, 0xcc); |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 73 | } |
| 74 | spin_unlock(&priv->lock); |
| 75 | } |
| 76 | |
| 77 | static int |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 78 | fakelb_hw_xmit(struct ieee802154_hw *hw, struct sk_buff *skb) |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 79 | { |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 80 | struct fakelb_dev_priv *priv = hw->priv; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 81 | struct fakelb_priv *fake = priv->fake; |
| 82 | |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 83 | read_lock_bh(&fake->lock); |
| 84 | if (priv->list.next == priv->list.prev) { |
| 85 | /* we are the only one device */ |
| 86 | fakelb_hw_deliver(priv, skb); |
| 87 | } else { |
| 88 | struct fakelb_dev_priv *dp; |
| 89 | list_for_each_entry(dp, &priv->fake->list, list) { |
| 90 | if (dp != priv && |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 91 | (dp->hw->phy->current_channel == |
| 92 | priv->hw->phy->current_channel)) |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 93 | fakelb_hw_deliver(dp, skb); |
| 94 | } |
| 95 | } |
| 96 | read_unlock_bh(&fake->lock); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 102 | fakelb_hw_start(struct ieee802154_hw *hw) { |
| 103 | struct fakelb_dev_priv *priv = hw->priv; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 104 | int ret = 0; |
| 105 | |
| 106 | spin_lock(&priv->lock); |
| 107 | if (priv->working) |
| 108 | ret = -EBUSY; |
| 109 | else |
| 110 | priv->working = 1; |
| 111 | spin_unlock(&priv->lock); |
| 112 | |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | static void |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 117 | fakelb_hw_stop(struct ieee802154_hw *hw) { |
| 118 | struct fakelb_dev_priv *priv = hw->priv; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 119 | |
| 120 | spin_lock(&priv->lock); |
| 121 | priv->working = 0; |
| 122 | spin_unlock(&priv->lock); |
| 123 | } |
| 124 | |
Alexander Aring | 1630186 | 2014-10-28 18:21:18 +0100 | [diff] [blame] | 125 | static const struct ieee802154_ops fakelb_ops = { |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 126 | .owner = THIS_MODULE, |
Alexander Aring | ed0a5dc | 2014-10-26 09:37:08 +0100 | [diff] [blame] | 127 | .xmit_sync = fakelb_hw_xmit, |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 128 | .ed = fakelb_hw_ed, |
| 129 | .set_channel = fakelb_hw_channel, |
| 130 | .start = fakelb_hw_start, |
| 131 | .stop = fakelb_hw_stop, |
| 132 | }; |
| 133 | |
| 134 | /* Number of dummy devices to be set up by this module. */ |
| 135 | module_param(numlbs, int, 0); |
| 136 | MODULE_PARM_DESC(numlbs, " number of pseudo devices"); |
| 137 | |
| 138 | static int fakelb_add_one(struct device *dev, struct fakelb_priv *fake) |
| 139 | { |
| 140 | struct fakelb_dev_priv *priv; |
| 141 | int err; |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 142 | struct ieee802154_hw *hw; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 143 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 144 | hw = ieee802154_alloc_hw(sizeof(*priv), &fakelb_ops); |
| 145 | if (!hw) |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 146 | return -ENOMEM; |
| 147 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 148 | priv = hw->priv; |
| 149 | priv->hw = hw; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 150 | |
| 151 | /* 868 MHz BPSK 802.15.4-2003 */ |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 152 | hw->phy->channels_supported[0] |= 1; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 153 | /* 915 MHz BPSK 802.15.4-2003 */ |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 154 | hw->phy->channels_supported[0] |= 0x7fe; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 155 | /* 2.4 GHz O-QPSK 802.15.4-2003 */ |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 156 | hw->phy->channels_supported[0] |= 0x7FFF800; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 157 | /* 868 MHz ASK 802.15.4-2006 */ |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 158 | hw->phy->channels_supported[1] |= 1; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 159 | /* 915 MHz ASK 802.15.4-2006 */ |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 160 | hw->phy->channels_supported[1] |= 0x7fe; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 161 | /* 868 MHz O-QPSK 802.15.4-2006 */ |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 162 | hw->phy->channels_supported[2] |= 1; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 163 | /* 915 MHz O-QPSK 802.15.4-2006 */ |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 164 | hw->phy->channels_supported[2] |= 0x7fe; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 165 | /* 2.4 GHz CSS 802.15.4a-2007 */ |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 166 | hw->phy->channels_supported[3] |= 0x3fff; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 167 | /* UWB Sub-gigahertz 802.15.4a-2007 */ |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 168 | hw->phy->channels_supported[4] |= 1; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 169 | /* UWB Low band 802.15.4a-2007 */ |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 170 | hw->phy->channels_supported[4] |= 0x1e; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 171 | /* UWB High band 802.15.4a-2007 */ |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 172 | hw->phy->channels_supported[4] |= 0xffe0; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 173 | /* 750 MHz O-QPSK 802.15.4c-2009 */ |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 174 | hw->phy->channels_supported[5] |= 0xf; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 175 | /* 750 MHz MPSK 802.15.4c-2009 */ |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 176 | hw->phy->channels_supported[5] |= 0xf0; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 177 | /* 950 MHz BPSK 802.15.4d-2009 */ |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 178 | hw->phy->channels_supported[6] |= 0x3ff; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 179 | /* 950 MHz GFSK 802.15.4d-2009 */ |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 180 | hw->phy->channels_supported[6] |= 0x3ffc00; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 181 | |
| 182 | INIT_LIST_HEAD(&priv->list); |
| 183 | priv->fake = fake; |
| 184 | |
| 185 | spin_lock_init(&priv->lock); |
| 186 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 187 | hw->parent = dev; |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 188 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 189 | err = ieee802154_register_hw(hw); |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 190 | if (err) |
| 191 | goto err_reg; |
| 192 | |
| 193 | write_lock_bh(&fake->lock); |
| 194 | list_add_tail(&priv->list, &fake->list); |
| 195 | write_unlock_bh(&fake->lock); |
| 196 | |
| 197 | return 0; |
| 198 | |
| 199 | err_reg: |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 200 | ieee802154_free_hw(priv->hw); |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 201 | return err; |
| 202 | } |
| 203 | |
| 204 | static void fakelb_del(struct fakelb_dev_priv *priv) |
| 205 | { |
| 206 | write_lock_bh(&priv->fake->lock); |
| 207 | list_del(&priv->list); |
| 208 | write_unlock_bh(&priv->fake->lock); |
| 209 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 210 | ieee802154_unregister_hw(priv->hw); |
| 211 | ieee802154_free_hw(priv->hw); |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Bill Pemberton | bb1f460 | 2012-12-03 09:24:12 -0500 | [diff] [blame] | 214 | static int fakelb_probe(struct platform_device *pdev) |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 215 | { |
| 216 | struct fakelb_priv *priv; |
| 217 | struct fakelb_dev_priv *dp; |
| 218 | int err = -ENOMEM; |
| 219 | int i; |
| 220 | |
Himangi Saraogi | 12b5c38 | 2014-05-19 22:25:11 +0530 | [diff] [blame] | 221 | priv = devm_kzalloc(&pdev->dev, sizeof(struct fakelb_priv), |
| 222 | GFP_KERNEL); |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 223 | if (!priv) |
| 224 | goto err_alloc; |
| 225 | |
| 226 | INIT_LIST_HEAD(&priv->list); |
| 227 | rwlock_init(&priv->lock); |
| 228 | |
| 229 | for (i = 0; i < numlbs; i++) { |
| 230 | err = fakelb_add_one(&pdev->dev, priv); |
| 231 | if (err < 0) |
| 232 | goto err_slave; |
| 233 | } |
| 234 | |
| 235 | platform_set_drvdata(pdev, priv); |
| 236 | dev_info(&pdev->dev, "added ieee802154 hardware\n"); |
| 237 | return 0; |
| 238 | |
| 239 | err_slave: |
| 240 | list_for_each_entry(dp, &priv->list, list) |
| 241 | fakelb_del(dp); |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 242 | err_alloc: |
| 243 | return err; |
| 244 | } |
| 245 | |
Bill Pemberton | bb1f460 | 2012-12-03 09:24:12 -0500 | [diff] [blame] | 246 | static int fakelb_remove(struct platform_device *pdev) |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 247 | { |
| 248 | struct fakelb_priv *priv = platform_get_drvdata(pdev); |
| 249 | struct fakelb_dev_priv *dp, *temp; |
| 250 | |
| 251 | list_for_each_entry_safe(dp, temp, &priv->list, list) |
| 252 | fakelb_del(dp); |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static struct platform_device *ieee802154fake_dev; |
| 258 | |
| 259 | static struct platform_driver ieee802154fake_driver = { |
| 260 | .probe = fakelb_probe, |
Bill Pemberton | bb1f460 | 2012-12-03 09:24:12 -0500 | [diff] [blame] | 261 | .remove = fakelb_remove, |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 262 | .driver = { |
| 263 | .name = "ieee802154fakelb", |
alex.bluesman.smirnov@gmail.com | e1e49b6 | 2012-05-15 20:50:30 +0000 | [diff] [blame] | 264 | }, |
| 265 | }; |
| 266 | |
| 267 | static __init int fakelb_init_module(void) |
| 268 | { |
| 269 | ieee802154fake_dev = platform_device_register_simple( |
| 270 | "ieee802154fakelb", -1, NULL, 0); |
| 271 | return platform_driver_register(&ieee802154fake_driver); |
| 272 | } |
| 273 | |
| 274 | static __exit void fake_remove_module(void) |
| 275 | { |
| 276 | platform_driver_unregister(&ieee802154fake_driver); |
| 277 | platform_device_unregister(ieee802154fake_dev); |
| 278 | } |
| 279 | |
| 280 | module_init(fakelb_init_module); |
| 281 | module_exit(fake_remove_module); |
| 282 | MODULE_LICENSE("GPL"); |