Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Loopback IEEE 802.15.4 interface
0004  *
0005  * Copyright 2007-2012 Siemens AG
0006  *
0007  * Written by:
0008  * Sergey Lapin <slapin@ossfans.org>
0009  * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
0010  * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
0011  */
0012 
0013 #include <linux/module.h>
0014 #include <linux/timer.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/netdevice.h>
0017 #include <linux/device.h>
0018 #include <linux/spinlock.h>
0019 #include <net/mac802154.h>
0020 #include <net/cfg802154.h>
0021 
0022 static int numlbs = 2;
0023 
0024 static LIST_HEAD(fakelb_phys);
0025 static DEFINE_MUTEX(fakelb_phys_lock);
0026 
0027 static LIST_HEAD(fakelb_ifup_phys);
0028 static DEFINE_RWLOCK(fakelb_ifup_phys_lock);
0029 
0030 struct fakelb_phy {
0031     struct ieee802154_hw *hw;
0032 
0033     u8 page;
0034     u8 channel;
0035 
0036     bool suspended;
0037 
0038     struct list_head list;
0039     struct list_head list_ifup;
0040 };
0041 
0042 static int fakelb_hw_ed(struct ieee802154_hw *hw, u8 *level)
0043 {
0044     WARN_ON(!level);
0045     *level = 0xbe;
0046 
0047     return 0;
0048 }
0049 
0050 static int fakelb_hw_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
0051 {
0052     struct fakelb_phy *phy = hw->priv;
0053 
0054     write_lock_bh(&fakelb_ifup_phys_lock);
0055     phy->page = page;
0056     phy->channel = channel;
0057     write_unlock_bh(&fakelb_ifup_phys_lock);
0058     return 0;
0059 }
0060 
0061 static int fakelb_hw_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
0062 {
0063     struct fakelb_phy *current_phy = hw->priv, *phy;
0064 
0065     read_lock_bh(&fakelb_ifup_phys_lock);
0066     WARN_ON(current_phy->suspended);
0067     list_for_each_entry(phy, &fakelb_ifup_phys, list_ifup) {
0068         if (current_phy == phy)
0069             continue;
0070 
0071         if (current_phy->page == phy->page &&
0072             current_phy->channel == phy->channel) {
0073             struct sk_buff *newskb = pskb_copy(skb, GFP_ATOMIC);
0074 
0075             if (newskb)
0076                 ieee802154_rx_irqsafe(phy->hw, newskb, 0xcc);
0077         }
0078     }
0079     read_unlock_bh(&fakelb_ifup_phys_lock);
0080 
0081     ieee802154_xmit_complete(hw, skb, false);
0082     return 0;
0083 }
0084 
0085 static int fakelb_hw_start(struct ieee802154_hw *hw)
0086 {
0087     struct fakelb_phy *phy = hw->priv;
0088 
0089     write_lock_bh(&fakelb_ifup_phys_lock);
0090     phy->suspended = false;
0091     list_add(&phy->list_ifup, &fakelb_ifup_phys);
0092     write_unlock_bh(&fakelb_ifup_phys_lock);
0093 
0094     return 0;
0095 }
0096 
0097 static void fakelb_hw_stop(struct ieee802154_hw *hw)
0098 {
0099     struct fakelb_phy *phy = hw->priv;
0100 
0101     write_lock_bh(&fakelb_ifup_phys_lock);
0102     phy->suspended = true;
0103     list_del(&phy->list_ifup);
0104     write_unlock_bh(&fakelb_ifup_phys_lock);
0105 }
0106 
0107 static int
0108 fakelb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
0109 {
0110     return 0;
0111 }
0112 
0113 static const struct ieee802154_ops fakelb_ops = {
0114     .owner = THIS_MODULE,
0115     .xmit_async = fakelb_hw_xmit,
0116     .ed = fakelb_hw_ed,
0117     .set_channel = fakelb_hw_channel,
0118     .start = fakelb_hw_start,
0119     .stop = fakelb_hw_stop,
0120     .set_promiscuous_mode = fakelb_set_promiscuous_mode,
0121 };
0122 
0123 /* Number of dummy devices to be set up by this module. */
0124 module_param(numlbs, int, 0);
0125 MODULE_PARM_DESC(numlbs, " number of pseudo devices");
0126 
0127 static int fakelb_add_one(struct device *dev)
0128 {
0129     struct ieee802154_hw *hw;
0130     struct fakelb_phy *phy;
0131     int err;
0132 
0133     hw = ieee802154_alloc_hw(sizeof(*phy), &fakelb_ops);
0134     if (!hw)
0135         return -ENOMEM;
0136 
0137     phy = hw->priv;
0138     phy->hw = hw;
0139 
0140     /* 868 MHz BPSK 802.15.4-2003 */
0141     hw->phy->supported.channels[0] |= 1;
0142     /* 915 MHz BPSK 802.15.4-2003 */
0143     hw->phy->supported.channels[0] |= 0x7fe;
0144     /* 2.4 GHz O-QPSK 802.15.4-2003 */
0145     hw->phy->supported.channels[0] |= 0x7FFF800;
0146     /* 868 MHz ASK 802.15.4-2006 */
0147     hw->phy->supported.channels[1] |= 1;
0148     /* 915 MHz ASK 802.15.4-2006 */
0149     hw->phy->supported.channels[1] |= 0x7fe;
0150     /* 868 MHz O-QPSK 802.15.4-2006 */
0151     hw->phy->supported.channels[2] |= 1;
0152     /* 915 MHz O-QPSK 802.15.4-2006 */
0153     hw->phy->supported.channels[2] |= 0x7fe;
0154     /* 2.4 GHz CSS 802.15.4a-2007 */
0155     hw->phy->supported.channels[3] |= 0x3fff;
0156     /* UWB Sub-gigahertz 802.15.4a-2007 */
0157     hw->phy->supported.channels[4] |= 1;
0158     /* UWB Low band 802.15.4a-2007 */
0159     hw->phy->supported.channels[4] |= 0x1e;
0160     /* UWB High band 802.15.4a-2007 */
0161     hw->phy->supported.channels[4] |= 0xffe0;
0162     /* 750 MHz O-QPSK 802.15.4c-2009 */
0163     hw->phy->supported.channels[5] |= 0xf;
0164     /* 750 MHz MPSK 802.15.4c-2009 */
0165     hw->phy->supported.channels[5] |= 0xf0;
0166     /* 950 MHz BPSK 802.15.4d-2009 */
0167     hw->phy->supported.channels[6] |= 0x3ff;
0168     /* 950 MHz GFSK 802.15.4d-2009 */
0169     hw->phy->supported.channels[6] |= 0x3ffc00;
0170 
0171     ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
0172     /* fake phy channel 13 as default */
0173     hw->phy->current_channel = 13;
0174     phy->channel = hw->phy->current_channel;
0175 
0176     hw->flags = IEEE802154_HW_PROMISCUOUS;
0177     hw->parent = dev;
0178 
0179     err = ieee802154_register_hw(hw);
0180     if (err)
0181         goto err_reg;
0182 
0183     mutex_lock(&fakelb_phys_lock);
0184     list_add_tail(&phy->list, &fakelb_phys);
0185     mutex_unlock(&fakelb_phys_lock);
0186 
0187     return 0;
0188 
0189 err_reg:
0190     ieee802154_free_hw(phy->hw);
0191     return err;
0192 }
0193 
0194 static void fakelb_del(struct fakelb_phy *phy)
0195 {
0196     list_del(&phy->list);
0197 
0198     ieee802154_unregister_hw(phy->hw);
0199     ieee802154_free_hw(phy->hw);
0200 }
0201 
0202 static int fakelb_probe(struct platform_device *pdev)
0203 {
0204     struct fakelb_phy *phy, *tmp;
0205     int err, i;
0206 
0207     for (i = 0; i < numlbs; i++) {
0208         err = fakelb_add_one(&pdev->dev);
0209         if (err < 0)
0210             goto err_slave;
0211     }
0212 
0213     dev_info(&pdev->dev, "added %i fake ieee802154 hardware devices\n", numlbs);
0214     return 0;
0215 
0216 err_slave:
0217     mutex_lock(&fakelb_phys_lock);
0218     list_for_each_entry_safe(phy, tmp, &fakelb_phys, list)
0219         fakelb_del(phy);
0220     mutex_unlock(&fakelb_phys_lock);
0221     return err;
0222 }
0223 
0224 static int fakelb_remove(struct platform_device *pdev)
0225 {
0226     struct fakelb_phy *phy, *tmp;
0227 
0228     mutex_lock(&fakelb_phys_lock);
0229     list_for_each_entry_safe(phy, tmp, &fakelb_phys, list)
0230         fakelb_del(phy);
0231     mutex_unlock(&fakelb_phys_lock);
0232     return 0;
0233 }
0234 
0235 static struct platform_device *ieee802154fake_dev;
0236 
0237 static struct platform_driver ieee802154fake_driver = {
0238     .probe = fakelb_probe,
0239     .remove = fakelb_remove,
0240     .driver = {
0241             .name = "ieee802154fakelb",
0242     },
0243 };
0244 
0245 static __init int fakelb_init_module(void)
0246 {
0247     ieee802154fake_dev = platform_device_register_simple(
0248                  "ieee802154fakelb", -1, NULL, 0);
0249 
0250     pr_warn("fakelb driver is marked as deprecated, please use mac802154_hwsim!\n");
0251 
0252     return platform_driver_register(&ieee802154fake_driver);
0253 }
0254 
0255 static __exit void fake_remove_module(void)
0256 {
0257     platform_driver_unregister(&ieee802154fake_driver);
0258     platform_device_unregister(ieee802154fake_dev);
0259 }
0260 
0261 module_init(fakelb_init_module);
0262 module_exit(fake_remove_module);
0263 MODULE_LICENSE("GPL");