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