2019-05-27 13:55:21 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2012-05-16 03:50:20 +07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007-2012 Siemens AG
|
|
|
|
*
|
|
|
|
* Written by:
|
|
|
|
* Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
|
2012-05-16 03:50:28 +07:00
|
|
|
#include <net/netlink.h>
|
2014-11-17 14:20:49 +07:00
|
|
|
#include <net/nl802154.h>
|
2012-05-16 03:50:20 +07:00
|
|
|
#include <net/mac802154.h>
|
2014-03-15 03:23:59 +07:00
|
|
|
#include <net/ieee802154_netdev.h>
|
2012-05-16 03:50:20 +07:00
|
|
|
#include <net/route.h>
|
2014-10-25 14:41:02 +07:00
|
|
|
#include <net/cfg802154.h>
|
2012-05-16 03:50:20 +07:00
|
|
|
|
2014-10-25 14:41:00 +07:00
|
|
|
#include "ieee802154_i.h"
|
2014-11-02 10:18:36 +07:00
|
|
|
#include "cfg.h"
|
2012-05-16 03:50:20 +07:00
|
|
|
|
2014-10-27 23:13:30 +07:00
|
|
|
static void ieee802154_tasklet_handler(unsigned long data)
|
|
|
|
{
|
|
|
|
struct ieee802154_local *local = (struct ieee802154_local *)data;
|
|
|
|
struct sk_buff *skb;
|
|
|
|
|
|
|
|
while ((skb = skb_dequeue(&local->skb_queue))) {
|
|
|
|
switch (skb->pkt_type) {
|
|
|
|
case IEEE802154_RX_MSG:
|
|
|
|
/* Clear skb->pkt_type in order to not confuse kernel
|
|
|
|
* netstack.
|
|
|
|
*/
|
|
|
|
skb->pkt_type = 0;
|
2015-07-07 12:20:43 +07:00
|
|
|
ieee802154_rx(local, skb);
|
2014-10-27 23:13:30 +07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WARN(1, "mac802154: Packet is of unknown type %d\n",
|
|
|
|
skb->pkt_type);
|
|
|
|
kfree_skb(skb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-25 22:16:34 +07:00
|
|
|
struct ieee802154_hw *
|
2014-10-29 00:21:18 +07:00
|
|
|
ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
|
2012-05-16 03:50:20 +07:00
|
|
|
{
|
|
|
|
struct wpan_phy *phy;
|
2014-10-25 22:16:35 +07:00
|
|
|
struct ieee802154_local *local;
|
2012-05-16 03:50:20 +07:00
|
|
|
size_t priv_size;
|
|
|
|
|
2015-06-23 13:11:03 +07:00
|
|
|
if (WARN_ON(!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
|
|
|
|
!ops->start || !ops->stop || !ops->set_channel))
|
2012-05-16 03:50:20 +07:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Ensure 32-byte alignment of our private data and hw private data.
|
2014-10-25 22:16:35 +07:00
|
|
|
* We use the wpan_phy priv data for both our ieee802154_local and for
|
2012-05-16 03:50:20 +07:00
|
|
|
* the driver's private data
|
|
|
|
*
|
|
|
|
* in memory it'll be like this:
|
|
|
|
*
|
2014-10-25 22:16:35 +07:00
|
|
|
* +-------------------------+
|
|
|
|
* | struct wpan_phy |
|
|
|
|
* +-------------------------+
|
|
|
|
* | struct ieee802154_local |
|
|
|
|
* +-------------------------+
|
|
|
|
* | driver's private data |
|
|
|
|
* +-------------------------+
|
2012-05-16 03:50:20 +07:00
|
|
|
*
|
|
|
|
* Due to ieee802154 layer isn't aware of driver and MAC structures,
|
2014-10-25 10:25:08 +07:00
|
|
|
* so lets align them here.
|
2012-05-16 03:50:20 +07:00
|
|
|
*/
|
|
|
|
|
2014-10-25 22:16:35 +07:00
|
|
|
priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
|
2012-05-16 03:50:20 +07:00
|
|
|
|
2014-11-09 14:36:47 +07:00
|
|
|
phy = wpan_phy_new(&mac802154_config_ops, priv_size);
|
2012-05-16 03:50:20 +07:00
|
|
|
if (!phy) {
|
2013-10-30 14:28:07 +07:00
|
|
|
pr_err("failure to allocate master IEEE802.15.4 device\n");
|
2012-05-16 03:50:20 +07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-12 09:36:51 +07:00
|
|
|
phy->privid = mac802154_wpan_phy_privid;
|
|
|
|
|
2014-10-25 22:16:35 +07:00
|
|
|
local = wpan_phy_priv(phy);
|
|
|
|
local->phy = phy;
|
|
|
|
local->hw.phy = local->phy;
|
|
|
|
local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
|
|
|
|
local->ops = ops;
|
2012-05-16 03:50:20 +07:00
|
|
|
|
2014-10-25 22:16:38 +07:00
|
|
|
INIT_LIST_HEAD(&local->interfaces);
|
|
|
|
mutex_init(&local->iflist_mtx);
|
2012-05-16 03:50:20 +07:00
|
|
|
|
2014-10-27 23:13:30 +07:00
|
|
|
tasklet_init(&local->tasklet,
|
|
|
|
ieee802154_tasklet_handler,
|
|
|
|
(unsigned long)local);
|
|
|
|
|
|
|
|
skb_queue_head_init(&local->skb_queue);
|
|
|
|
|
2015-07-21 21:44:47 +07:00
|
|
|
INIT_WORK(&local->tx_work, ieee802154_xmit_worker);
|
|
|
|
|
2015-05-18 02:44:43 +07:00
|
|
|
/* init supported flags with 802.15.4 default ranges */
|
|
|
|
phy->supported.max_minbe = 8;
|
|
|
|
phy->supported.min_maxbe = 3;
|
|
|
|
phy->supported.max_maxbe = 8;
|
2015-08-11 02:15:56 +07:00
|
|
|
phy->supported.min_frame_retries = 0;
|
2015-05-18 02:44:43 +07:00
|
|
|
phy->supported.max_frame_retries = 7;
|
|
|
|
phy->supported.max_csma_backoffs = 5;
|
|
|
|
phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE;
|
|
|
|
|
2015-05-18 02:44:47 +07:00
|
|
|
/* always supported */
|
|
|
|
phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE);
|
|
|
|
|
2014-10-25 22:16:35 +07:00
|
|
|
return &local->hw;
|
2012-05-16 03:50:20 +07:00
|
|
|
}
|
2014-10-25 22:16:34 +07:00
|
|
|
EXPORT_SYMBOL(ieee802154_alloc_hw);
|
2012-05-16 03:50:20 +07:00
|
|
|
|
2014-10-25 22:16:34 +07:00
|
|
|
void ieee802154_free_hw(struct ieee802154_hw *hw)
|
2012-05-16 03:50:20 +07:00
|
|
|
{
|
2014-10-25 22:16:39 +07:00
|
|
|
struct ieee802154_local *local = hw_to_local(hw);
|
2012-05-16 03:50:20 +07:00
|
|
|
|
2014-10-25 22:16:38 +07:00
|
|
|
BUG_ON(!list_empty(&local->interfaces));
|
2012-05-16 03:50:28 +07:00
|
|
|
|
2014-10-25 22:16:38 +07:00
|
|
|
mutex_destroy(&local->iflist_mtx);
|
2012-12-14 08:03:03 +07:00
|
|
|
|
2014-10-25 22:16:35 +07:00
|
|
|
wpan_phy_free(local->phy);
|
2012-05-16 03:50:20 +07:00
|
|
|
}
|
2014-10-25 22:16:34 +07:00
|
|
|
EXPORT_SYMBOL(ieee802154_free_hw);
|
2012-05-16 03:50:20 +07:00
|
|
|
|
2014-11-13 01:51:56 +07:00
|
|
|
static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy)
|
|
|
|
{
|
|
|
|
/* TODO warn on empty symbol_duration
|
|
|
|
* Should be done when all drivers sets this value.
|
|
|
|
*/
|
|
|
|
|
|
|
|
wpan_phy->lifs_period = IEEE802154_LIFS_PERIOD *
|
|
|
|
wpan_phy->symbol_duration;
|
|
|
|
wpan_phy->sifs_period = IEEE802154_SIFS_PERIOD *
|
|
|
|
wpan_phy->symbol_duration;
|
|
|
|
}
|
|
|
|
|
2014-10-25 22:16:34 +07:00
|
|
|
int ieee802154_register_hw(struct ieee802154_hw *hw)
|
2012-05-16 03:50:20 +07:00
|
|
|
{
|
2014-10-25 22:16:39 +07:00
|
|
|
struct ieee802154_local *local = hw_to_local(hw);
|
2014-11-06 02:51:19 +07:00
|
|
|
struct net_device *dev;
|
2014-07-03 05:20:43 +07:00
|
|
|
int rc = -ENOSYS;
|
|
|
|
|
2014-10-25 22:16:41 +07:00
|
|
|
local->workqueue =
|
2014-10-25 22:16:35 +07:00
|
|
|
create_singlethread_workqueue(wpan_phy_name(local->phy));
|
2014-10-25 22:16:41 +07:00
|
|
|
if (!local->workqueue) {
|
2014-07-03 05:20:43 +07:00
|
|
|
rc = -ENOMEM;
|
2012-05-16 03:50:20 +07:00
|
|
|
goto out;
|
2014-07-03 05:20:43 +07:00
|
|
|
}
|
2012-05-16 03:50:20 +07:00
|
|
|
|
2014-11-13 01:51:56 +07:00
|
|
|
hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
|
|
|
|
local->ifs_timer.function = ieee802154_xmit_ifs_timer;
|
|
|
|
|
2014-10-25 22:16:35 +07:00
|
|
|
wpan_phy_set_dev(local->phy, local->hw.parent);
|
2012-05-16 03:50:20 +07:00
|
|
|
|
2014-11-13 01:51:56 +07:00
|
|
|
ieee802154_setup_wpan_phy_pib(local->phy);
|
|
|
|
|
2015-05-18 02:44:43 +07:00
|
|
|
if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) {
|
|
|
|
local->phy->supported.min_csma_backoffs = 4;
|
|
|
|
local->phy->supported.max_csma_backoffs = 4;
|
|
|
|
local->phy->supported.min_maxbe = 5;
|
|
|
|
local->phy->supported.max_maxbe = 5;
|
|
|
|
local->phy->supported.min_minbe = 3;
|
|
|
|
local->phy->supported.max_minbe = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) {
|
2015-08-11 02:15:56 +07:00
|
|
|
local->phy->supported.min_frame_retries = 3;
|
|
|
|
local->phy->supported.max_frame_retries = 3;
|
2015-05-18 02:44:43 +07:00
|
|
|
}
|
|
|
|
|
2015-05-18 02:44:47 +07:00
|
|
|
if (hw->flags & IEEE802154_HW_PROMISCUOUS)
|
|
|
|
local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR);
|
|
|
|
|
2014-10-25 22:16:35 +07:00
|
|
|
rc = wpan_phy_register(local->phy);
|
2012-05-16 03:50:20 +07:00
|
|
|
if (rc < 0)
|
|
|
|
goto out_wq;
|
|
|
|
|
2014-11-06 02:51:19 +07:00
|
|
|
rtnl_lock();
|
|
|
|
|
2015-04-30 22:44:57 +07:00
|
|
|
dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM,
|
|
|
|
NL802154_IFTYPE_NODE,
|
2014-11-17 14:20:52 +07:00
|
|
|
cpu_to_le64(0x0000000000000000ULL));
|
2014-11-06 02:51:19 +07:00
|
|
|
if (IS_ERR(dev)) {
|
|
|
|
rtnl_unlock();
|
|
|
|
rc = PTR_ERR(dev);
|
2015-04-30 22:44:53 +07:00
|
|
|
goto out_phy;
|
2014-11-06 02:51:19 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
rtnl_unlock();
|
|
|
|
|
2012-05-16 03:50:20 +07:00
|
|
|
return 0;
|
|
|
|
|
2015-04-30 22:44:53 +07:00
|
|
|
out_phy:
|
|
|
|
wpan_phy_unregister(local->phy);
|
2012-05-16 03:50:20 +07:00
|
|
|
out_wq:
|
2014-10-25 22:16:41 +07:00
|
|
|
destroy_workqueue(local->workqueue);
|
2012-05-16 03:50:20 +07:00
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
2014-10-25 22:16:34 +07:00
|
|
|
EXPORT_SYMBOL(ieee802154_register_hw);
|
2012-05-16 03:50:20 +07:00
|
|
|
|
2014-10-25 22:16:34 +07:00
|
|
|
void ieee802154_unregister_hw(struct ieee802154_hw *hw)
|
2012-05-16 03:50:20 +07:00
|
|
|
{
|
2014-10-25 22:16:39 +07:00
|
|
|
struct ieee802154_local *local = hw_to_local(hw);
|
2012-05-16 03:50:20 +07:00
|
|
|
|
2014-10-27 23:13:30 +07:00
|
|
|
tasklet_kill(&local->tasklet);
|
2014-10-25 22:16:41 +07:00
|
|
|
flush_workqueue(local->workqueue);
|
2012-05-16 03:50:20 +07:00
|
|
|
|
|
|
|
rtnl_lock();
|
|
|
|
|
2014-11-12 09:36:48 +07:00
|
|
|
ieee802154_remove_interfaces(local);
|
2012-05-16 03:50:28 +07:00
|
|
|
|
2012-05-16 03:50:20 +07:00
|
|
|
rtnl_unlock();
|
|
|
|
|
2016-02-10 17:49:38 +07:00
|
|
|
destroy_workqueue(local->workqueue);
|
2014-10-25 22:16:35 +07:00
|
|
|
wpan_phy_unregister(local->phy);
|
2012-05-16 03:50:20 +07:00
|
|
|
}
|
2014-10-25 22:16:34 +07:00
|
|
|
EXPORT_SYMBOL(ieee802154_unregister_hw);
|
2012-05-16 03:50:20 +07:00
|
|
|
|
2014-11-12 09:36:53 +07:00
|
|
|
static int __init ieee802154_init(void)
|
|
|
|
{
|
|
|
|
return ieee802154_iface_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit ieee802154_exit(void)
|
|
|
|
{
|
|
|
|
ieee802154_iface_exit();
|
|
|
|
|
|
|
|
rcu_barrier();
|
|
|
|
}
|
|
|
|
|
|
|
|
subsys_initcall(ieee802154_init);
|
|
|
|
module_exit(ieee802154_exit);
|
|
|
|
|
2014-11-12 09:36:52 +07:00
|
|
|
MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
|
2012-05-16 03:50:20 +07:00
|
|
|
MODULE_LICENSE("GPL v2");
|