0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/netdevice.h>
0012
0013 #include <net/netlink.h>
0014 #include <net/nl802154.h>
0015 #include <net/mac802154.h>
0016 #include <net/ieee802154_netdev.h>
0017 #include <net/route.h>
0018 #include <net/cfg802154.h>
0019
0020 #include "ieee802154_i.h"
0021 #include "cfg.h"
0022
0023 static void ieee802154_tasklet_handler(struct tasklet_struct *t)
0024 {
0025 struct ieee802154_local *local = from_tasklet(local, t, tasklet);
0026 struct sk_buff *skb;
0027
0028 while ((skb = skb_dequeue(&local->skb_queue))) {
0029 switch (skb->pkt_type) {
0030 case IEEE802154_RX_MSG:
0031
0032
0033
0034 skb->pkt_type = 0;
0035 ieee802154_rx(local, skb);
0036 break;
0037 default:
0038 WARN(1, "mac802154: Packet is of unknown type %d\n",
0039 skb->pkt_type);
0040 kfree_skb(skb);
0041 break;
0042 }
0043 }
0044 }
0045
0046 struct ieee802154_hw *
0047 ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
0048 {
0049 struct wpan_phy *phy;
0050 struct ieee802154_local *local;
0051 size_t priv_size;
0052
0053 if (WARN_ON(!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
0054 !ops->start || !ops->stop || !ops->set_channel))
0055 return NULL;
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075 priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
0076
0077 phy = wpan_phy_new(&mac802154_config_ops, priv_size);
0078 if (!phy) {
0079 pr_err("failure to allocate master IEEE802.15.4 device\n");
0080 return NULL;
0081 }
0082
0083 phy->privid = mac802154_wpan_phy_privid;
0084
0085 local = wpan_phy_priv(phy);
0086 local->phy = phy;
0087 local->hw.phy = local->phy;
0088 local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
0089 local->ops = ops;
0090
0091 INIT_LIST_HEAD(&local->interfaces);
0092 mutex_init(&local->iflist_mtx);
0093
0094 tasklet_setup(&local->tasklet, ieee802154_tasklet_handler);
0095
0096 skb_queue_head_init(&local->skb_queue);
0097
0098 INIT_WORK(&local->tx_work, ieee802154_xmit_worker);
0099
0100
0101 phy->supported.max_minbe = 8;
0102 phy->supported.min_maxbe = 3;
0103 phy->supported.max_maxbe = 8;
0104 phy->supported.min_frame_retries = 0;
0105 phy->supported.max_frame_retries = 7;
0106 phy->supported.max_csma_backoffs = 5;
0107 phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE;
0108
0109
0110 phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE);
0111
0112 return &local->hw;
0113 }
0114 EXPORT_SYMBOL(ieee802154_alloc_hw);
0115
0116 void ieee802154_configure_durations(struct wpan_phy *phy)
0117 {
0118 u32 duration = 0;
0119
0120 switch (phy->current_page) {
0121 case 0:
0122 if (BIT(phy->current_channel) & 0x1)
0123
0124 duration = 50 * NSEC_PER_USEC;
0125 else if (BIT(phy->current_channel) & 0x7FE)
0126
0127 duration = 25 * NSEC_PER_USEC;
0128 else if (BIT(phy->current_channel) & 0x7FFF800)
0129
0130 duration = 16 * NSEC_PER_USEC;
0131 break;
0132 case 2:
0133 if (BIT(phy->current_channel) & 0x1)
0134
0135 duration = 40 * NSEC_PER_USEC;
0136 else if (BIT(phy->current_channel) & 0x7FE)
0137
0138 duration = 16 * NSEC_PER_USEC;
0139 break;
0140 case 3:
0141 if (BIT(phy->current_channel) & 0x3FFF)
0142
0143 duration = 6 * NSEC_PER_USEC;
0144 break;
0145 default:
0146 break;
0147 }
0148
0149 if (!duration) {
0150 pr_debug("Unknown PHY symbol duration\n");
0151 return;
0152 }
0153
0154 phy->symbol_duration = duration;
0155 phy->lifs_period = (IEEE802154_LIFS_PERIOD * phy->symbol_duration) / NSEC_PER_SEC;
0156 phy->sifs_period = (IEEE802154_SIFS_PERIOD * phy->symbol_duration) / NSEC_PER_SEC;
0157 }
0158 EXPORT_SYMBOL(ieee802154_configure_durations);
0159
0160 void ieee802154_free_hw(struct ieee802154_hw *hw)
0161 {
0162 struct ieee802154_local *local = hw_to_local(hw);
0163
0164 BUG_ON(!list_empty(&local->interfaces));
0165
0166 mutex_destroy(&local->iflist_mtx);
0167
0168 wpan_phy_free(local->phy);
0169 }
0170 EXPORT_SYMBOL(ieee802154_free_hw);
0171
0172 static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy)
0173 {
0174
0175
0176
0177
0178 wpan_phy->lifs_period =
0179 (IEEE802154_LIFS_PERIOD * wpan_phy->symbol_duration) / 1000;
0180 wpan_phy->sifs_period =
0181 (IEEE802154_SIFS_PERIOD * wpan_phy->symbol_duration) / 1000;
0182 }
0183
0184 int ieee802154_register_hw(struct ieee802154_hw *hw)
0185 {
0186 struct ieee802154_local *local = hw_to_local(hw);
0187 struct net_device *dev;
0188 int rc = -ENOSYS;
0189
0190 local->workqueue =
0191 create_singlethread_workqueue(wpan_phy_name(local->phy));
0192 if (!local->workqueue) {
0193 rc = -ENOMEM;
0194 goto out;
0195 }
0196
0197 hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
0198 local->ifs_timer.function = ieee802154_xmit_ifs_timer;
0199
0200 wpan_phy_set_dev(local->phy, local->hw.parent);
0201
0202 ieee802154_setup_wpan_phy_pib(local->phy);
0203
0204 ieee802154_configure_durations(local->phy);
0205
0206 if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) {
0207 local->phy->supported.min_csma_backoffs = 4;
0208 local->phy->supported.max_csma_backoffs = 4;
0209 local->phy->supported.min_maxbe = 5;
0210 local->phy->supported.max_maxbe = 5;
0211 local->phy->supported.min_minbe = 3;
0212 local->phy->supported.max_minbe = 3;
0213 }
0214
0215 if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) {
0216 local->phy->supported.min_frame_retries = 3;
0217 local->phy->supported.max_frame_retries = 3;
0218 }
0219
0220 if (hw->flags & IEEE802154_HW_PROMISCUOUS)
0221 local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR);
0222
0223 rc = wpan_phy_register(local->phy);
0224 if (rc < 0)
0225 goto out_wq;
0226
0227 rtnl_lock();
0228
0229 dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM,
0230 NL802154_IFTYPE_NODE,
0231 cpu_to_le64(0x0000000000000000ULL));
0232 if (IS_ERR(dev)) {
0233 rtnl_unlock();
0234 rc = PTR_ERR(dev);
0235 goto out_phy;
0236 }
0237
0238 rtnl_unlock();
0239
0240 return 0;
0241
0242 out_phy:
0243 wpan_phy_unregister(local->phy);
0244 out_wq:
0245 destroy_workqueue(local->workqueue);
0246 out:
0247 return rc;
0248 }
0249 EXPORT_SYMBOL(ieee802154_register_hw);
0250
0251 void ieee802154_unregister_hw(struct ieee802154_hw *hw)
0252 {
0253 struct ieee802154_local *local = hw_to_local(hw);
0254
0255 tasklet_kill(&local->tasklet);
0256 flush_workqueue(local->workqueue);
0257
0258 rtnl_lock();
0259
0260 ieee802154_remove_interfaces(local);
0261
0262 rtnl_unlock();
0263
0264 destroy_workqueue(local->workqueue);
0265 wpan_phy_unregister(local->phy);
0266 }
0267 EXPORT_SYMBOL(ieee802154_unregister_hw);
0268
0269 static int __init ieee802154_init(void)
0270 {
0271 return ieee802154_iface_init();
0272 }
0273
0274 static void __exit ieee802154_exit(void)
0275 {
0276 ieee802154_iface_exit();
0277
0278 rcu_barrier();
0279 }
0280
0281 subsys_initcall(ieee802154_init);
0282 module_exit(ieee802154_exit);
0283
0284 MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
0285 MODULE_LICENSE("GPL v2");