mac802154: allow only one WPAN to be up at any given time
[deliverable/linux.git] / net / mac802154 / ieee802154_dev.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.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/netdevice.h>
26
62610ad2 27#include <net/netlink.h>
28#include <linux/nl802154.h>
1010f540 29#include <net/mac802154.h>
b70ab2e8 30#include <net/ieee802154_netdev.h>
1010f540 31#include <net/route.h>
32#include <net/wpan-phy.h>
33
34#include "mac802154.h"
35
62610ad2 36int mac802154_slave_open(struct net_device *dev)
37{
38 struct mac802154_sub_if_data *priv = netdev_priv(dev);
336908f6 39 struct mac802154_sub_if_data *subif;
62610ad2 40 struct mac802154_priv *ipriv = priv->hw;
41 int res = 0;
42
336908f6
PB
43 ASSERT_RTNL();
44
45 if (priv->type == IEEE802154_DEV_WPAN) {
46 mutex_lock(&priv->hw->slaves_mtx);
47 list_for_each_entry(subif, &priv->hw->slaves, list) {
48 if (subif != priv && subif->type == priv->type &&
49 subif->running) {
50 mutex_unlock(&priv->hw->slaves_mtx);
51 return -EBUSY;
52 }
53 }
54 mutex_unlock(&priv->hw->slaves_mtx);
55 }
56
57 mutex_lock(&priv->hw->slaves_mtx);
58 priv->running = true;
59 mutex_unlock(&priv->hw->slaves_mtx);
60
62610ad2 61 if (ipriv->open_count++ == 0) {
62 res = ipriv->ops->start(&ipriv->hw);
63 WARN_ON(res);
64 if (res)
65 goto err;
66 }
67
68 if (ipriv->ops->ieee_addr) {
b70ab2e8
PB
69 __le64 addr = ieee802154_devaddr_from_raw(dev->dev_addr);
70
71 res = ipriv->ops->ieee_addr(&ipriv->hw, addr);
62610ad2 72 WARN_ON(res);
73 if (res)
74 goto err;
75 mac802154_dev_set_ieee_addr(dev);
76 }
77
78 netif_start_queue(dev);
79 return 0;
80err:
81 priv->hw->open_count--;
82
83 return res;
84}
85
86int mac802154_slave_close(struct net_device *dev)
87{
88 struct mac802154_sub_if_data *priv = netdev_priv(dev);
89 struct mac802154_priv *ipriv = priv->hw;
90
336908f6
PB
91 ASSERT_RTNL();
92
62610ad2 93 netif_stop_queue(dev);
94
336908f6
PB
95 mutex_lock(&priv->hw->slaves_mtx);
96 priv->running = false;
97 mutex_unlock(&priv->hw->slaves_mtx);
98
62610ad2 99 if (!--ipriv->open_count)
100 ipriv->ops->stop(&ipriv->hw);
101
102 return 0;
103}
104
105static int
106mac802154_netdev_register(struct wpan_phy *phy, struct net_device *dev)
107{
108 struct mac802154_sub_if_data *priv;
109 struct mac802154_priv *ipriv;
110 int err;
111
112 ipriv = wpan_phy_priv(phy);
113
114 priv = netdev_priv(dev);
115 priv->dev = dev;
116 priv->hw = ipriv;
117
118 dev->needed_headroom = ipriv->hw.extra_tx_headroom;
119
120 SET_NETDEV_DEV(dev, &ipriv->phy->dev);
121
122 mutex_lock(&ipriv->slaves_mtx);
123 if (!ipriv->running) {
124 mutex_unlock(&ipriv->slaves_mtx);
125 return -ENODEV;
126 }
127 mutex_unlock(&ipriv->slaves_mtx);
128
129 err = register_netdev(dev);
130 if (err < 0)
131 return err;
132
133 rtnl_lock();
134 mutex_lock(&ipriv->slaves_mtx);
135 list_add_tail_rcu(&priv->list, &ipriv->slaves);
136 mutex_unlock(&ipriv->slaves_mtx);
137 rtnl_unlock();
138
139 return 0;
140}
141
142static void
143mac802154_del_iface(struct wpan_phy *phy, struct net_device *dev)
144{
145 struct mac802154_sub_if_data *sdata;
146 ASSERT_RTNL();
147
148 sdata = netdev_priv(dev);
149
150 BUG_ON(sdata->hw->phy != phy);
151
152 mutex_lock(&sdata->hw->slaves_mtx);
153 list_del_rcu(&sdata->list);
154 mutex_unlock(&sdata->hw->slaves_mtx);
155
156 synchronize_rcu();
157 unregister_netdevice(sdata->dev);
158}
159
160static struct net_device *
161mac802154_add_iface(struct wpan_phy *phy, const char *name, int type)
162{
163 struct net_device *dev;
164 int err = -ENOMEM;
165
62610ad2 166 switch (type) {
0606069d 167 case IEEE802154_DEV_MONITOR:
168 dev = alloc_netdev(sizeof(struct mac802154_sub_if_data),
169 name, mac802154_monitor_setup);
170 break;
32bad7e3 171 case IEEE802154_DEV_WPAN:
172 dev = alloc_netdev(sizeof(struct mac802154_sub_if_data),
173 name, mac802154_wpan_setup);
174 break;
62610ad2 175 default:
176 dev = NULL;
177 err = -EINVAL;
178 break;
179 }
180 if (!dev)
181 goto err;
182
183 err = mac802154_netdev_register(phy, dev);
184 if (err)
185 goto err_free;
186
187 dev_hold(dev); /* we return an incremented device refcount */
188 return dev;
189
190err_free:
191 free_netdev(dev);
192err:
193 return ERR_PTR(err);
194}
195
9b2777d6
PB
196static int mac802154_set_txpower(struct wpan_phy *phy, int db)
197{
198 struct mac802154_priv *priv = wpan_phy_priv(phy);
199
200 if (!priv->ops->set_txpower)
201 return -ENOTSUPP;
202
203 return priv->ops->set_txpower(&priv->hw, db);
204}
205
84dda3c6
PB
206static int mac802154_set_lbt(struct wpan_phy *phy, bool on)
207{
208 struct mac802154_priv *priv = wpan_phy_priv(phy);
209
210 if (!priv->ops->set_lbt)
211 return -ENOTSUPP;
212
213 return priv->ops->set_lbt(&priv->hw, on);
214}
215
ba08fea5
PB
216static int mac802154_set_cca_mode(struct wpan_phy *phy, u8 mode)
217{
218 struct mac802154_priv *priv = wpan_phy_priv(phy);
219
220 if (!priv->ops->set_cca_mode)
221 return -ENOTSUPP;
222
223 return priv->ops->set_cca_mode(&priv->hw, mode);
224}
225
6ca00197
PB
226static int mac802154_set_cca_ed_level(struct wpan_phy *phy, s32 level)
227{
228 struct mac802154_priv *priv = wpan_phy_priv(phy);
229
230 if (!priv->ops->set_cca_ed_level)
231 return -ENOTSUPP;
232
233 return priv->ops->set_cca_ed_level(&priv->hw, level);
234}
235
4244db1b
PB
236static int mac802154_set_csma_params(struct wpan_phy *phy, u8 min_be,
237 u8 max_be, u8 retries)
238{
239 struct mac802154_priv *priv = wpan_phy_priv(phy);
240
241 if (!priv->ops->set_csma_params)
242 return -ENOTSUPP;
243
244 return priv->ops->set_csma_params(&priv->hw, min_be, max_be, retries);
245}
246
247static int mac802154_set_frame_retries(struct wpan_phy *phy, s8 retries)
248{
249 struct mac802154_priv *priv = wpan_phy_priv(phy);
250
251 if (!priv->ops->set_frame_retries)
252 return -ENOTSUPP;
253
254 return priv->ops->set_frame_retries(&priv->hw, retries);
255}
256
1010f540 257struct ieee802154_dev *
258ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops)
259{
260 struct wpan_phy *phy;
261 struct mac802154_priv *priv;
262 size_t priv_size;
263
264 if (!ops || !ops->xmit || !ops->ed || !ops->start ||
265 !ops->stop || !ops->set_channel) {
83a1a7ce 266 pr_err("undefined IEEE802.15.4 device operations\n");
1010f540 267 return NULL;
268 }
269
270 /* Ensure 32-byte alignment of our private data and hw private data.
271 * We use the wpan_phy priv data for both our mac802154_priv and for
272 * the driver's private data
273 *
274 * in memory it'll be like this:
275 *
276 * +-----------------------+
277 * | struct wpan_phy |
278 * +-----------------------+
279 * | struct mac802154_priv |
280 * +-----------------------+
281 * | driver's private data |
282 * +-----------------------+
283 *
284 * Due to ieee802154 layer isn't aware of driver and MAC structures,
285 * so lets allign them here.
286 */
287
288 priv_size = ALIGN(sizeof(*priv), NETDEV_ALIGN) + priv_data_len;
289
290 phy = wpan_phy_alloc(priv_size);
291 if (!phy) {
83a1a7ce 292 pr_err("failure to allocate master IEEE802.15.4 device\n");
1010f540 293 return NULL;
294 }
295
296 priv = wpan_phy_priv(phy);
297 priv->hw.phy = priv->phy = phy;
298 priv->hw.priv = (char *)priv + ALIGN(sizeof(*priv), NETDEV_ALIGN);
299 priv->ops = ops;
300
301 INIT_LIST_HEAD(&priv->slaves);
302 mutex_init(&priv->slaves_mtx);
303
304 return &priv->hw;
305}
306EXPORT_SYMBOL(ieee802154_alloc_device);
307
308void ieee802154_free_device(struct ieee802154_dev *hw)
309{
310 struct mac802154_priv *priv = mac802154_to_priv(hw);
311
62610ad2 312 BUG_ON(!list_empty(&priv->slaves));
313
1010f540 314 mutex_destroy(&priv->slaves_mtx);
1e9f9545
KK
315
316 wpan_phy_free(priv->phy);
1010f540 317}
318EXPORT_SYMBOL(ieee802154_free_device);
319
320int ieee802154_register_device(struct ieee802154_dev *dev)
321{
322 struct mac802154_priv *priv = mac802154_to_priv(dev);
323 int rc = -ENOMEM;
324
325 priv->dev_workqueue =
326 create_singlethread_workqueue(wpan_phy_name(priv->phy));
327 if (!priv->dev_workqueue)
328 goto out;
329
330 wpan_phy_set_dev(priv->phy, priv->hw.parent);
331
62610ad2 332 priv->phy->add_iface = mac802154_add_iface;
333 priv->phy->del_iface = mac802154_del_iface;
9b2777d6 334 priv->phy->set_txpower = mac802154_set_txpower;
84dda3c6 335 priv->phy->set_lbt = mac802154_set_lbt;
ba08fea5 336 priv->phy->set_cca_mode = mac802154_set_cca_mode;
6ca00197 337 priv->phy->set_cca_ed_level = mac802154_set_cca_ed_level;
4244db1b
PB
338 priv->phy->set_csma_params = mac802154_set_csma_params;
339 priv->phy->set_frame_retries = mac802154_set_frame_retries;
62610ad2 340
1010f540 341 rc = wpan_phy_register(priv->phy);
342 if (rc < 0)
343 goto out_wq;
344
345 rtnl_lock();
346
347 mutex_lock(&priv->slaves_mtx);
348 priv->running = MAC802154_DEVICE_RUN;
349 mutex_unlock(&priv->slaves_mtx);
350
351 rtnl_unlock();
352
353 return 0;
354
355out_wq:
356 destroy_workqueue(priv->dev_workqueue);
357out:
358 return rc;
359}
360EXPORT_SYMBOL(ieee802154_register_device);
361
362void ieee802154_unregister_device(struct ieee802154_dev *dev)
363{
364 struct mac802154_priv *priv = mac802154_to_priv(dev);
62610ad2 365 struct mac802154_sub_if_data *sdata, *next;
1010f540 366
367 flush_workqueue(priv->dev_workqueue);
368 destroy_workqueue(priv->dev_workqueue);
369
370 rtnl_lock();
371
372 mutex_lock(&priv->slaves_mtx);
373 priv->running = MAC802154_DEVICE_STOPPED;
374 mutex_unlock(&priv->slaves_mtx);
375
62610ad2 376 list_for_each_entry_safe(sdata, next, &priv->slaves, list) {
377 mutex_lock(&sdata->hw->slaves_mtx);
378 list_del(&sdata->list);
379 mutex_unlock(&sdata->hw->slaves_mtx);
380
381 unregister_netdevice(sdata->dev);
382 }
383
1010f540 384 rtnl_unlock();
385
386 wpan_phy_unregister(priv->phy);
387}
388EXPORT_SYMBOL(ieee802154_unregister_device);
389
390MODULE_DESCRIPTION("IEEE 802.15.4 implementation");
391MODULE_LICENSE("GPL v2");
This page took 0.144409 seconds and 5 git commands to generate.