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