]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/mac802154/main.c
mac802154: remove mac_params in sdata
[mirror_ubuntu-bionic-kernel.git] / net / mac802154 / main.c
CommitLineData
1010f540 1/*
2 * Copyright (C) 2007-2012 Siemens AG
3 *
4 * Written by:
5 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
6 *
7 * Based on the code from 'linux-zigbee.sourceforge.net' project.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
1010f540 17 */
18
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/netdevice.h>
22
62610ad2 23#include <net/netlink.h>
24#include <linux/nl802154.h>
1010f540 25#include <net/mac802154.h>
b70ab2e8 26#include <net/ieee802154_netdev.h>
1010f540 27#include <net/route.h>
5ad60d36 28#include <net/cfg802154.h>
1010f540 29
0f1556bc 30#include "ieee802154_i.h"
1201cd22 31#include "cfg.h"
1010f540 32
c5c47e67
AA
33static void ieee802154_tasklet_handler(unsigned long data)
34{
35 struct ieee802154_local *local = (struct ieee802154_local *)data;
36 struct sk_buff *skb;
37
38 while ((skb = skb_dequeue(&local->skb_queue))) {
39 switch (skb->pkt_type) {
40 case IEEE802154_RX_MSG:
41 /* Clear skb->pkt_type in order to not confuse kernel
42 * netstack.
43 */
44 skb->pkt_type = 0;
45 ieee802154_rx(&local->hw, skb);
46 break;
47 default:
48 WARN(1, "mac802154: Packet is of unknown type %d\n",
49 skb->pkt_type);
50 kfree_skb(skb);
51 break;
52 }
53 }
54}
55
5a504397 56struct ieee802154_hw *
16301861 57ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
1010f540 58{
59 struct wpan_phy *phy;
a5e1ec53 60 struct ieee802154_local *local;
1010f540 61 size_t priv_size;
62
ed0a5dce
AA
63 if (!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
64 !ops->start || !ops->stop || !ops->set_channel) {
83a1a7ce 65 pr_err("undefined IEEE802.15.4 device operations\n");
1010f540 66 return NULL;
67 }
68
69 /* Ensure 32-byte alignment of our private data and hw private data.
a5e1ec53 70 * We use the wpan_phy priv data for both our ieee802154_local and for
1010f540 71 * the driver's private data
72 *
73 * in memory it'll be like this:
74 *
a5e1ec53
AA
75 * +-------------------------+
76 * | struct wpan_phy |
77 * +-------------------------+
78 * | struct ieee802154_local |
79 * +-------------------------+
80 * | driver's private data |
81 * +-------------------------+
1010f540 82 *
83 * Due to ieee802154 layer isn't aware of driver and MAC structures,
139f14ad 84 * so lets align them here.
1010f540 85 */
86
a5e1ec53 87 priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
1010f540 88
1201cd22 89 phy = wpan_phy_alloc(&mac802154_config_ops, priv_size);
1010f540 90 if (!phy) {
83a1a7ce 91 pr_err("failure to allocate master IEEE802.15.4 device\n");
1010f540 92 return NULL;
93 }
94
a5e1ec53
AA
95 local = wpan_phy_priv(phy);
96 local->phy = phy;
97 local->hw.phy = local->phy;
98 local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
99 local->ops = ops;
1010f540 100
d98be45b
AA
101 INIT_LIST_HEAD(&local->interfaces);
102 mutex_init(&local->iflist_mtx);
1010f540 103
c5c47e67
AA
104 tasklet_init(&local->tasklet,
105 ieee802154_tasklet_handler,
106 (unsigned long)local);
107
108 skb_queue_head_init(&local->skb_queue);
109
a5e1ec53 110 return &local->hw;
1010f540 111}
5a504397 112EXPORT_SYMBOL(ieee802154_alloc_hw);
1010f540 113
5a504397 114void ieee802154_free_hw(struct ieee802154_hw *hw)
1010f540 115{
60741361 116 struct ieee802154_local *local = hw_to_local(hw);
1010f540 117
d98be45b 118 BUG_ON(!list_empty(&local->interfaces));
62610ad2 119
d98be45b 120 mutex_destroy(&local->iflist_mtx);
1e9f9545 121
a5e1ec53 122 wpan_phy_free(local->phy);
1010f540 123}
5a504397 124EXPORT_SYMBOL(ieee802154_free_hw);
1010f540 125
5a504397 126int ieee802154_register_hw(struct ieee802154_hw *hw)
1010f540 127{
60741361 128 struct ieee802154_local *local = hw_to_local(hw);
e4962a14 129 struct net_device *dev;
640985ec
AA
130 int rc = -ENOSYS;
131
f7730542 132 local->workqueue =
a5e1ec53 133 create_singlethread_workqueue(wpan_phy_name(local->phy));
f7730542 134 if (!local->workqueue) {
640985ec 135 rc = -ENOMEM;
1010f540 136 goto out;
640985ec 137 }
1010f540 138
a5e1ec53 139 wpan_phy_set_dev(local->phy, local->hw.parent);
1010f540 140
a5e1ec53 141 rc = wpan_phy_register(local->phy);
1010f540 142 if (rc < 0)
143 goto out_wq;
144
e4962a14
AA
145 rtnl_lock();
146
147 dev = ieee802154_if_add(local, "wpan%d", NULL, IEEE802154_DEV_WPAN);
148 if (IS_ERR(dev)) {
149 rtnl_unlock();
150 rc = PTR_ERR(dev);
151 goto out_wq;
152 }
153
154 rtnl_unlock();
155
1010f540 156 return 0;
157
158out_wq:
f7730542 159 destroy_workqueue(local->workqueue);
1010f540 160out:
161 return rc;
162}
5a504397 163EXPORT_SYMBOL(ieee802154_register_hw);
1010f540 164
5a504397 165void ieee802154_unregister_hw(struct ieee802154_hw *hw)
1010f540 166{
60741361 167 struct ieee802154_local *local = hw_to_local(hw);
036562f9 168 struct ieee802154_sub_if_data *sdata, *next;
1010f540 169
c5c47e67 170 tasklet_kill(&local->tasklet);
f7730542
AA
171 flush_workqueue(local->workqueue);
172 destroy_workqueue(local->workqueue);
1010f540 173
174 rtnl_lock();
175
d98be45b
AA
176 list_for_each_entry_safe(sdata, next, &local->interfaces, list) {
177 mutex_lock(&sdata->local->iflist_mtx);
62610ad2 178 list_del(&sdata->list);
d98be45b 179 mutex_unlock(&sdata->local->iflist_mtx);
62610ad2 180
181 unregister_netdevice(sdata->dev);
182 }
183
1010f540 184 rtnl_unlock();
185
a5e1ec53 186 wpan_phy_unregister(local->phy);
1010f540 187}
5a504397 188EXPORT_SYMBOL(ieee802154_unregister_hw);
1010f540 189
190MODULE_DESCRIPTION("IEEE 802.15.4 implementation");
191MODULE_LICENSE("GPL v2");