cfg802154: introduce cfg802154_registered_device
[linux-2.6-block.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"
1010f540 31
62610ad2 32static int
33mac802154_netdev_register(struct wpan_phy *phy, struct net_device *dev)
34{
59d19cd7 35 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
a5e1ec53 36 struct ieee802154_local *local;
62610ad2 37 int err;
38
a5e1ec53 39 local = wpan_phy_priv(phy);
62610ad2 40
036562f9 41 sdata->dev = dev;
04e850fe 42 sdata->local = local;
62610ad2 43
a5e1ec53 44 dev->needed_headroom = local->hw.extra_tx_headroom;
62610ad2 45
a5e1ec53 46 SET_NETDEV_DEV(dev, &local->phy->dev);
62610ad2 47
62610ad2 48 err = register_netdev(dev);
49 if (err < 0)
50 return err;
51
52 rtnl_lock();
d98be45b
AA
53 mutex_lock(&local->iflist_mtx);
54 list_add_tail_rcu(&sdata->list, &local->interfaces);
55 mutex_unlock(&local->iflist_mtx);
62610ad2 56 rtnl_unlock();
57
58 return 0;
59}
60
61static void
62mac802154_del_iface(struct wpan_phy *phy, struct net_device *dev)
63{
59d19cd7 64 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
4710d806 65
62610ad2 66 ASSERT_RTNL();
62610ad2 67
04e850fe 68 BUG_ON(sdata->local->phy != phy);
62610ad2 69
d98be45b 70 mutex_lock(&sdata->local->iflist_mtx);
62610ad2 71 list_del_rcu(&sdata->list);
d98be45b 72 mutex_unlock(&sdata->local->iflist_mtx);
62610ad2 73
74 synchronize_rcu();
75 unregister_netdevice(sdata->dev);
76}
77
78static struct net_device *
79mac802154_add_iface(struct wpan_phy *phy, const char *name, int type)
80{
81 struct net_device *dev;
82 int err = -ENOMEM;
83
62610ad2 84 switch (type) {
0606069d 85 case IEEE802154_DEV_MONITOR:
036562f9 86 dev = alloc_netdev(sizeof(struct ieee802154_sub_if_data),
c835a677
TG
87 name, NET_NAME_UNKNOWN,
88 mac802154_monitor_setup);
0606069d 89 break;
32bad7e3 90 case IEEE802154_DEV_WPAN:
036562f9 91 dev = alloc_netdev(sizeof(struct ieee802154_sub_if_data),
c835a677
TG
92 name, NET_NAME_UNKNOWN,
93 mac802154_wpan_setup);
32bad7e3 94 break;
62610ad2 95 default:
96 dev = NULL;
97 err = -EINVAL;
98 break;
99 }
100 if (!dev)
101 goto err;
102
103 err = mac802154_netdev_register(phy, dev);
104 if (err)
105 goto err_free;
106
107 dev_hold(dev); /* we return an incremented device refcount */
108 return dev;
109
110err_free:
111 free_netdev(dev);
112err:
113 return ERR_PTR(err);
114}
115
c5c47e67
AA
116static void ieee802154_tasklet_handler(unsigned long data)
117{
118 struct ieee802154_local *local = (struct ieee802154_local *)data;
119 struct sk_buff *skb;
120
121 while ((skb = skb_dequeue(&local->skb_queue))) {
122 switch (skb->pkt_type) {
123 case IEEE802154_RX_MSG:
124 /* Clear skb->pkt_type in order to not confuse kernel
125 * netstack.
126 */
127 skb->pkt_type = 0;
128 ieee802154_rx(&local->hw, skb);
129 break;
130 default:
131 WARN(1, "mac802154: Packet is of unknown type %d\n",
132 skb->pkt_type);
133 kfree_skb(skb);
134 break;
135 }
136 }
137}
138
5a504397 139struct ieee802154_hw *
16301861 140ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
1010f540 141{
142 struct wpan_phy *phy;
a5e1ec53 143 struct ieee802154_local *local;
1010f540 144 size_t priv_size;
145
ed0a5dce
AA
146 if (!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
147 !ops->start || !ops->stop || !ops->set_channel) {
83a1a7ce 148 pr_err("undefined IEEE802.15.4 device operations\n");
1010f540 149 return NULL;
150 }
151
152 /* Ensure 32-byte alignment of our private data and hw private data.
a5e1ec53 153 * We use the wpan_phy priv data for both our ieee802154_local and for
1010f540 154 * the driver's private data
155 *
156 * in memory it'll be like this:
157 *
a5e1ec53
AA
158 * +-------------------------+
159 * | struct wpan_phy |
160 * +-------------------------+
161 * | struct ieee802154_local |
162 * +-------------------------+
163 * | driver's private data |
164 * +-------------------------+
1010f540 165 *
166 * Due to ieee802154 layer isn't aware of driver and MAC structures,
139f14ad 167 * so lets align them here.
1010f540 168 */
169
a5e1ec53 170 priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
1010f540 171
a5dd1d72 172 phy = wpan_phy_alloc(NULL, priv_size);
1010f540 173 if (!phy) {
83a1a7ce 174 pr_err("failure to allocate master IEEE802.15.4 device\n");
1010f540 175 return NULL;
176 }
177
a5e1ec53
AA
178 local = wpan_phy_priv(phy);
179 local->phy = phy;
180 local->hw.phy = local->phy;
181 local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
182 local->ops = ops;
1010f540 183
d98be45b
AA
184 INIT_LIST_HEAD(&local->interfaces);
185 mutex_init(&local->iflist_mtx);
1010f540 186
c5c47e67
AA
187 tasklet_init(&local->tasklet,
188 ieee802154_tasklet_handler,
189 (unsigned long)local);
190
191 skb_queue_head_init(&local->skb_queue);
192
a5e1ec53 193 return &local->hw;
1010f540 194}
5a504397 195EXPORT_SYMBOL(ieee802154_alloc_hw);
1010f540 196
5a504397 197void ieee802154_free_hw(struct ieee802154_hw *hw)
1010f540 198{
60741361 199 struct ieee802154_local *local = hw_to_local(hw);
1010f540 200
d98be45b 201 BUG_ON(!list_empty(&local->interfaces));
62610ad2 202
d98be45b 203 mutex_destroy(&local->iflist_mtx);
1e9f9545 204
a5e1ec53 205 wpan_phy_free(local->phy);
1010f540 206}
5a504397 207EXPORT_SYMBOL(ieee802154_free_hw);
1010f540 208
5a504397 209int ieee802154_register_hw(struct ieee802154_hw *hw)
1010f540 210{
60741361 211 struct ieee802154_local *local = hw_to_local(hw);
640985ec
AA
212 int rc = -ENOSYS;
213
f7730542 214 local->workqueue =
a5e1ec53 215 create_singlethread_workqueue(wpan_phy_name(local->phy));
f7730542 216 if (!local->workqueue) {
640985ec 217 rc = -ENOMEM;
1010f540 218 goto out;
640985ec 219 }
1010f540 220
a5e1ec53 221 wpan_phy_set_dev(local->phy, local->hw.parent);
1010f540 222
a5e1ec53
AA
223 local->phy->add_iface = mac802154_add_iface;
224 local->phy->del_iface = mac802154_del_iface;
62610ad2 225
a5e1ec53 226 rc = wpan_phy_register(local->phy);
1010f540 227 if (rc < 0)
228 goto out_wq;
229
1010f540 230 return 0;
231
232out_wq:
f7730542 233 destroy_workqueue(local->workqueue);
1010f540 234out:
235 return rc;
236}
5a504397 237EXPORT_SYMBOL(ieee802154_register_hw);
1010f540 238
5a504397 239void ieee802154_unregister_hw(struct ieee802154_hw *hw)
1010f540 240{
60741361 241 struct ieee802154_local *local = hw_to_local(hw);
036562f9 242 struct ieee802154_sub_if_data *sdata, *next;
1010f540 243
c5c47e67 244 tasklet_kill(&local->tasklet);
f7730542
AA
245 flush_workqueue(local->workqueue);
246 destroy_workqueue(local->workqueue);
1010f540 247
248 rtnl_lock();
249
d98be45b
AA
250 list_for_each_entry_safe(sdata, next, &local->interfaces, list) {
251 mutex_lock(&sdata->local->iflist_mtx);
62610ad2 252 list_del(&sdata->list);
d98be45b 253 mutex_unlock(&sdata->local->iflist_mtx);
62610ad2 254
255 unregister_netdevice(sdata->dev);
256 }
257
1010f540 258 rtnl_unlock();
259
a5e1ec53 260 wpan_phy_unregister(local->phy);
1010f540 261}
5a504397 262EXPORT_SYMBOL(ieee802154_unregister_hw);
1010f540 263
264MODULE_DESCRIPTION("IEEE 802.15.4 implementation");
265MODULE_LICENSE("GPL v2");