2012-05-16 04:50:20 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007-2012 Siemens AG
|
|
|
|
*
|
|
|
|
* Written by:
|
|
|
|
* Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
|
|
|
|
*
|
|
|
|
* Based on the code from 'linux-zigbee.sourceforge.net' project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
|
2012-05-16 04:50:28 +08:00
|
|
|
#include <net/netlink.h>
|
|
|
|
#include <linux/nl802154.h>
|
2012-05-16 04:50:20 +08:00
|
|
|
#include <net/mac802154.h>
|
2014-03-15 04:23:59 +08:00
|
|
|
#include <net/ieee802154_netdev.h>
|
2012-05-16 04:50:20 +08:00
|
|
|
#include <net/route.h>
|
2014-10-25 15:41:02 +08:00
|
|
|
#include <net/cfg802154.h>
|
2012-05-16 04:50:20 +08:00
|
|
|
|
2014-10-25 15:41:00 +08:00
|
|
|
#include "ieee802154_i.h"
|
2014-11-02 11:18:36 +08:00
|
|
|
#include "cfg.h"
|
2012-05-16 04:50:20 +08:00
|
|
|
|
2014-10-28 00:13:30 +08: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;
|
|
|
|
ieee802154_rx(&local->hw, skb);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WARN(1, "mac802154: Packet is of unknown type %d\n",
|
|
|
|
skb->pkt_type);
|
|
|
|
kfree_skb(skb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-25 23:16:34 +08:00
|
|
|
struct ieee802154_hw *
|
2014-10-29 01:21:18 +08:00
|
|
|
ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
|
2012-05-16 04:50:20 +08:00
|
|
|
{
|
|
|
|
struct wpan_phy *phy;
|
2014-10-25 23:16:35 +08:00
|
|
|
struct ieee802154_local *local;
|
2012-05-16 04:50:20 +08:00
|
|
|
size_t priv_size;
|
|
|
|
|
2014-10-26 16:37:08 +08:00
|
|
|
if (!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
|
|
|
|
!ops->start || !ops->stop || !ops->set_channel) {
|
2013-10-30 15:28:07 +08:00
|
|
|
pr_err("undefined IEEE802.15.4 device operations\n");
|
2012-05-16 04:50:20 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure 32-byte alignment of our private data and hw private data.
|
2014-10-25 23:16:35 +08:00
|
|
|
* We use the wpan_phy priv data for both our ieee802154_local and for
|
2012-05-16 04:50:20 +08:00
|
|
|
* the driver's private data
|
|
|
|
*
|
|
|
|
* in memory it'll be like this:
|
|
|
|
*
|
2014-10-25 23:16:35 +08:00
|
|
|
* +-------------------------+
|
|
|
|
* | struct wpan_phy |
|
|
|
|
* +-------------------------+
|
|
|
|
* | struct ieee802154_local |
|
|
|
|
* +-------------------------+
|
|
|
|
* | driver's private data |
|
|
|
|
* +-------------------------+
|
2012-05-16 04:50:20 +08:00
|
|
|
*
|
|
|
|
* Due to ieee802154 layer isn't aware of driver and MAC structures,
|
2014-10-25 11:25:08 +08:00
|
|
|
* so lets align them here.
|
2012-05-16 04:50:20 +08:00
|
|
|
*/
|
|
|
|
|
2014-10-25 23:16:35 +08:00
|
|
|
priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
|
2012-05-16 04:50:20 +08:00
|
|
|
|
2014-11-09 15:36:47 +08:00
|
|
|
phy = wpan_phy_new(&mac802154_config_ops, priv_size);
|
2012-05-16 04:50:20 +08:00
|
|
|
if (!phy) {
|
2013-10-30 15:28:07 +08:00
|
|
|
pr_err("failure to allocate master IEEE802.15.4 device\n");
|
2012-05-16 04:50:20 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-25 23:16:35 +08: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 04:50:20 +08:00
|
|
|
|
2014-10-25 23:16:38 +08:00
|
|
|
INIT_LIST_HEAD(&local->interfaces);
|
|
|
|
mutex_init(&local->iflist_mtx);
|
2012-05-16 04:50:20 +08:00
|
|
|
|
2014-10-28 00:13:30 +08:00
|
|
|
tasklet_init(&local->tasklet,
|
|
|
|
ieee802154_tasklet_handler,
|
|
|
|
(unsigned long)local);
|
|
|
|
|
|
|
|
skb_queue_head_init(&local->skb_queue);
|
|
|
|
|
2014-10-25 23:16:35 +08:00
|
|
|
return &local->hw;
|
2012-05-16 04:50:20 +08:00
|
|
|
}
|
2014-10-25 23:16:34 +08:00
|
|
|
EXPORT_SYMBOL(ieee802154_alloc_hw);
|
2012-05-16 04:50:20 +08:00
|
|
|
|
2014-10-25 23:16:34 +08:00
|
|
|
void ieee802154_free_hw(struct ieee802154_hw *hw)
|
2012-05-16 04:50:20 +08:00
|
|
|
{
|
2014-10-25 23:16:39 +08:00
|
|
|
struct ieee802154_local *local = hw_to_local(hw);
|
2012-05-16 04:50:20 +08:00
|
|
|
|
2014-10-25 23:16:38 +08:00
|
|
|
BUG_ON(!list_empty(&local->interfaces));
|
2012-05-16 04:50:28 +08:00
|
|
|
|
2014-10-25 23:16:38 +08:00
|
|
|
mutex_destroy(&local->iflist_mtx);
|
2012-12-14 09:03:03 +08:00
|
|
|
|
2014-10-25 23:16:35 +08:00
|
|
|
wpan_phy_free(local->phy);
|
2012-05-16 04:50:20 +08:00
|
|
|
}
|
2014-10-25 23:16:34 +08:00
|
|
|
EXPORT_SYMBOL(ieee802154_free_hw);
|
2012-05-16 04:50:20 +08:00
|
|
|
|
2014-10-25 23:16:34 +08:00
|
|
|
int ieee802154_register_hw(struct ieee802154_hw *hw)
|
2012-05-16 04:50:20 +08:00
|
|
|
{
|
2014-10-25 23:16:39 +08:00
|
|
|
struct ieee802154_local *local = hw_to_local(hw);
|
2014-11-06 03:51:19 +08:00
|
|
|
struct net_device *dev;
|
2014-07-03 06:20:43 +08:00
|
|
|
int rc = -ENOSYS;
|
|
|
|
|
2014-10-25 23:16:41 +08:00
|
|
|
local->workqueue =
|
2014-10-25 23:16:35 +08:00
|
|
|
create_singlethread_workqueue(wpan_phy_name(local->phy));
|
2014-10-25 23:16:41 +08:00
|
|
|
if (!local->workqueue) {
|
2014-07-03 06:20:43 +08:00
|
|
|
rc = -ENOMEM;
|
2012-05-16 04:50:20 +08:00
|
|
|
goto out;
|
2014-07-03 06:20:43 +08:00
|
|
|
}
|
2012-05-16 04:50:20 +08:00
|
|
|
|
2014-10-25 23:16:35 +08:00
|
|
|
wpan_phy_set_dev(local->phy, local->hw.parent);
|
2012-05-16 04:50:20 +08:00
|
|
|
|
2014-10-25 23:16:35 +08:00
|
|
|
rc = wpan_phy_register(local->phy);
|
2012-05-16 04:50:20 +08:00
|
|
|
if (rc < 0)
|
|
|
|
goto out_wq;
|
|
|
|
|
2014-11-06 03:51:19 +08:00
|
|
|
rtnl_lock();
|
|
|
|
|
|
|
|
dev = ieee802154_if_add(local, "wpan%d", NULL, IEEE802154_DEV_WPAN);
|
|
|
|
if (IS_ERR(dev)) {
|
|
|
|
rtnl_unlock();
|
|
|
|
rc = PTR_ERR(dev);
|
|
|
|
goto out_wq;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtnl_unlock();
|
|
|
|
|
2012-05-16 04:50:20 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_wq:
|
2014-10-25 23:16:41 +08:00
|
|
|
destroy_workqueue(local->workqueue);
|
2012-05-16 04:50:20 +08:00
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
2014-10-25 23:16:34 +08:00
|
|
|
EXPORT_SYMBOL(ieee802154_register_hw);
|
2012-05-16 04:50:20 +08:00
|
|
|
|
2014-10-25 23:16:34 +08:00
|
|
|
void ieee802154_unregister_hw(struct ieee802154_hw *hw)
|
2012-05-16 04:50:20 +08:00
|
|
|
{
|
2014-10-25 23:16:39 +08:00
|
|
|
struct ieee802154_local *local = hw_to_local(hw);
|
2014-10-25 23:16:36 +08:00
|
|
|
struct ieee802154_sub_if_data *sdata, *next;
|
2012-05-16 04:50:20 +08:00
|
|
|
|
2014-10-28 00:13:30 +08:00
|
|
|
tasklet_kill(&local->tasklet);
|
2014-10-25 23:16:41 +08:00
|
|
|
flush_workqueue(local->workqueue);
|
|
|
|
destroy_workqueue(local->workqueue);
|
2012-05-16 04:50:20 +08:00
|
|
|
|
|
|
|
rtnl_lock();
|
|
|
|
|
2014-10-25 23:16:38 +08:00
|
|
|
list_for_each_entry_safe(sdata, next, &local->interfaces, list) {
|
|
|
|
mutex_lock(&sdata->local->iflist_mtx);
|
2012-05-16 04:50:28 +08:00
|
|
|
list_del(&sdata->list);
|
2014-10-25 23:16:38 +08:00
|
|
|
mutex_unlock(&sdata->local->iflist_mtx);
|
2012-05-16 04:50:28 +08:00
|
|
|
|
|
|
|
unregister_netdevice(sdata->dev);
|
|
|
|
}
|
|
|
|
|
2012-05-16 04:50:20 +08:00
|
|
|
rtnl_unlock();
|
|
|
|
|
2014-10-25 23:16:35 +08:00
|
|
|
wpan_phy_unregister(local->phy);
|
2012-05-16 04:50:20 +08:00
|
|
|
}
|
2014-10-25 23:16:34 +08:00
|
|
|
EXPORT_SYMBOL(ieee802154_unregister_hw);
|
2012-05-16 04:50:20 +08:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION("IEEE 802.15.4 implementation");
|
|
|
|
MODULE_LICENSE("GPL v2");
|