main.c 9.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (C) 2007-2012 Siemens AG
 *
 * Written by:
 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
 *
 * Based on the code from 'linux-zigbee.sourceforge.net' project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netdevice.h>

23 24
#include <net/netlink.h>
#include <linux/nl802154.h>
25
#include <net/mac802154.h>
26
#include <net/ieee802154_netdev.h>
27
#include <net/route.h>
28
#include <net/cfg802154.h>
29

30
#include "ieee802154_i.h"
31

32 33
int mac802154_slave_open(struct net_device *dev)
{
34 35
	struct ieee802154_sub_if_data *sdata = netdev_priv(dev);
	struct ieee802154_sub_if_data *subif;
36
	struct ieee802154_local *local = sdata->local;
37 38
	int res = 0;

39 40
	ASSERT_RTNL();

41
	if (sdata->type == IEEE802154_DEV_WPAN) {
42 43
		mutex_lock(&sdata->local->iflist_mtx);
		list_for_each_entry(subif, &sdata->local->interfaces, list) {
44
			if (subif != sdata && subif->type == sdata->type &&
45
			    subif->running) {
46
				mutex_unlock(&sdata->local->iflist_mtx);
47 48 49
				return -EBUSY;
			}
		}
50
		mutex_unlock(&sdata->local->iflist_mtx);
51 52
	}

53
	mutex_lock(&sdata->local->iflist_mtx);
54
	sdata->running = true;
55
	mutex_unlock(&sdata->local->iflist_mtx);
56

57 58
	if (local->open_count++ == 0) {
		res = local->ops->start(&local->hw);
59 60 61 62 63
		WARN_ON(res);
		if (res)
			goto err;
	}

64
	if (local->ops->ieee_addr) {
65 66
		__le64 addr = ieee802154_devaddr_from_raw(dev->dev_addr);

67
		res = local->ops->ieee_addr(&local->hw, addr);
68 69 70 71 72 73 74 75 76
		WARN_ON(res);
		if (res)
			goto err;
		mac802154_dev_set_ieee_addr(dev);
	}

	netif_start_queue(dev);
	return 0;
err:
77
	sdata->local->open_count--;
78 79 80 81 82 83

	return res;
}

int mac802154_slave_close(struct net_device *dev)
{
84
	struct ieee802154_sub_if_data *sdata = netdev_priv(dev);
85
	struct ieee802154_local *local = sdata->local;
86

87 88
	ASSERT_RTNL();

89 90
	netif_stop_queue(dev);

91
	mutex_lock(&sdata->local->iflist_mtx);
92
	sdata->running = false;
93
	mutex_unlock(&sdata->local->iflist_mtx);
94

95 96
	if (!--local->open_count)
		local->ops->stop(&local->hw);
97 98 99 100 101 102 103

	return 0;
}

static int
mac802154_netdev_register(struct wpan_phy *phy, struct net_device *dev)
{
104
	struct ieee802154_sub_if_data *sdata;
105
	struct ieee802154_local *local;
106 107
	int err;

108
	local = wpan_phy_priv(phy);
109

110 111
	sdata = netdev_priv(dev);
	sdata->dev = dev;
112
	sdata->local = local;
113

114
	dev->needed_headroom = local->hw.extra_tx_headroom;
115

116
	SET_NETDEV_DEV(dev, &local->phy->dev);
117

118
	mutex_lock(&local->iflist_mtx);
119
	if (!local->running) {
120
		mutex_unlock(&local->iflist_mtx);
121 122
		return -ENODEV;
	}
123
	mutex_unlock(&local->iflist_mtx);
124 125 126 127 128 129

	err = register_netdev(dev);
	if (err < 0)
		return err;

	rtnl_lock();
130 131 132
	mutex_lock(&local->iflist_mtx);
	list_add_tail_rcu(&sdata->list, &local->interfaces);
	mutex_unlock(&local->iflist_mtx);
133 134 135 136 137 138 139 140
	rtnl_unlock();

	return 0;
}

static void
mac802154_del_iface(struct wpan_phy *phy, struct net_device *dev)
{
141
	struct ieee802154_sub_if_data *sdata;
142

143 144 145 146
	ASSERT_RTNL();

	sdata = netdev_priv(dev);

147
	BUG_ON(sdata->local->phy != phy);
148

149
	mutex_lock(&sdata->local->iflist_mtx);
150
	list_del_rcu(&sdata->list);
151
	mutex_unlock(&sdata->local->iflist_mtx);
152 153 154 155 156 157 158 159 160 161 162 163

	synchronize_rcu();
	unregister_netdevice(sdata->dev);
}

static struct net_device *
mac802154_add_iface(struct wpan_phy *phy, const char *name, int type)
{
	struct net_device *dev;
	int err = -ENOMEM;

	switch (type) {
164
	case IEEE802154_DEV_MONITOR:
165
		dev = alloc_netdev(sizeof(struct ieee802154_sub_if_data),
166 167
				   name, NET_NAME_UNKNOWN,
				   mac802154_monitor_setup);
168
		break;
169
	case IEEE802154_DEV_WPAN:
170
		dev = alloc_netdev(sizeof(struct ieee802154_sub_if_data),
171 172
				   name, NET_NAME_UNKNOWN,
				   mac802154_wpan_setup);
173
		break;
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	default:
		dev = NULL;
		err = -EINVAL;
		break;
	}
	if (!dev)
		goto err;

	err = mac802154_netdev_register(phy, dev);
	if (err)
		goto err_free;

	dev_hold(dev); /* we return an incremented device refcount */
	return dev;

err_free:
	free_netdev(dev);
err:
	return ERR_PTR(err);
}

195 196
static int mac802154_set_txpower(struct wpan_phy *phy, int db)
{
197
	struct ieee802154_local *local = wpan_phy_priv(phy);
198

199
	return local->ops->set_txpower(&local->hw, db);
200 201
}

202 203
static int mac802154_set_lbt(struct wpan_phy *phy, bool on)
{
204
	struct ieee802154_local *local = wpan_phy_priv(phy);
205

206
	return local->ops->set_lbt(&local->hw, on);
207 208
}

209 210
static int mac802154_set_cca_mode(struct wpan_phy *phy, u8 mode)
{
211
	struct ieee802154_local *local = wpan_phy_priv(phy);
212

213
	return local->ops->set_cca_mode(&local->hw, mode);
214 215
}

216 217
static int mac802154_set_cca_ed_level(struct wpan_phy *phy, s32 level)
{
218
	struct ieee802154_local *local = wpan_phy_priv(phy);
219

220
	return local->ops->set_cca_ed_level(&local->hw, level);
221 222
}

223 224 225
static int mac802154_set_csma_params(struct wpan_phy *phy, u8 min_be,
				     u8 max_be, u8 retries)
{
226
	struct ieee802154_local *local = wpan_phy_priv(phy);
227

228
	return local->ops->set_csma_params(&local->hw, min_be, max_be, retries);
229 230 231 232
}

static int mac802154_set_frame_retries(struct wpan_phy *phy, s8 retries)
{
233
	struct ieee802154_local *local = wpan_phy_priv(phy);
234

235
	return local->ops->set_frame_retries(&local->hw, retries);
236 237
}

238 239
struct ieee802154_hw *
ieee802154_alloc_hw(size_t priv_data_len, struct ieee802154_ops *ops)
240 241
{
	struct wpan_phy *phy;
242
	struct ieee802154_local *local;
243 244 245 246
	size_t priv_size;

	if (!ops || !ops->xmit || !ops->ed || !ops->start ||
	    !ops->stop || !ops->set_channel) {
247
		pr_err("undefined IEEE802.15.4 device operations\n");
248 249 250 251
		return NULL;
	}

	/* Ensure 32-byte alignment of our private data and hw private data.
252
	 * We use the wpan_phy priv data for both our ieee802154_local and for
253 254 255 256
	 * the driver's private data
	 *
	 * in memory it'll be like this:
	 *
257 258 259 260 261 262 263
	 * +-------------------------+
	 * | struct wpan_phy         |
	 * +-------------------------+
	 * | struct ieee802154_local |
	 * +-------------------------+
	 * | driver's private data   |
	 * +-------------------------+
264 265
	 *
	 * Due to ieee802154 layer isn't aware of driver and MAC structures,
266
	 * so lets align them here.
267 268
	 */

269
	priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
270 271 272

	phy = wpan_phy_alloc(priv_size);
	if (!phy) {
273
		pr_err("failure to allocate master IEEE802.15.4 device\n");
274 275 276
		return NULL;
	}

277 278 279 280 281
	local = wpan_phy_priv(phy);
	local->phy = phy;
	local->hw.phy = local->phy;
	local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
	local->ops = ops;
282

283 284
	INIT_LIST_HEAD(&local->interfaces);
	mutex_init(&local->iflist_mtx);
285

286
	return &local->hw;
287
}
288
EXPORT_SYMBOL(ieee802154_alloc_hw);
289

290
void ieee802154_free_hw(struct ieee802154_hw *hw)
291
{
292
	struct ieee802154_local *local = mac802154_to_priv(hw);
293

294
	BUG_ON(!list_empty(&local->interfaces));
295

296
	mutex_destroy(&local->iflist_mtx);
297

298
	wpan_phy_free(local->phy);
299
}
300
EXPORT_SYMBOL(ieee802154_free_hw);
301

302
int ieee802154_register_hw(struct ieee802154_hw *hw)
303
{
304
	struct ieee802154_local *local = mac802154_to_priv(hw);
305 306
	int rc = -ENOSYS;

307
	if (hw->flags & IEEE802154_HW_TXPOWER) {
308
		if (!local->ops->set_txpower)
309 310
			goto out;

311
		local->phy->set_txpower = mac802154_set_txpower;
312 313
	}

314
	if (hw->flags & IEEE802154_HW_LBT) {
315
		if (!local->ops->set_lbt)
316 317
			goto out;

318
		local->phy->set_lbt = mac802154_set_lbt;
319 320
	}

321
	if (hw->flags & IEEE802154_HW_CCA_MODE) {
322
		if (!local->ops->set_cca_mode)
323 324
			goto out;

325
		local->phy->set_cca_mode = mac802154_set_cca_mode;
326 327
	}

328
	if (hw->flags & IEEE802154_HW_CCA_ED_LEVEL) {
329
		if (!local->ops->set_cca_ed_level)
330 331
			goto out;

332
		local->phy->set_cca_ed_level = mac802154_set_cca_ed_level;
333 334
	}

335
	if (hw->flags & IEEE802154_HW_CSMA_PARAMS) {
336
		if (!local->ops->set_csma_params)
337 338
			goto out;

339
		local->phy->set_csma_params = mac802154_set_csma_params;
340 341
	}

342
	if (hw->flags & IEEE802154_HW_FRAME_RETRIES) {
343
		if (!local->ops->set_frame_retries)
344 345
			goto out;

346
		local->phy->set_frame_retries = mac802154_set_frame_retries;
347
	}
348

349 350 351
	local->dev_workqueue =
		create_singlethread_workqueue(wpan_phy_name(local->phy));
	if (!local->dev_workqueue) {
352
		rc = -ENOMEM;
353
		goto out;
354
	}
355

356
	wpan_phy_set_dev(local->phy, local->hw.parent);
357

358 359
	local->phy->add_iface = mac802154_add_iface;
	local->phy->del_iface = mac802154_del_iface;
360

361
	rc = wpan_phy_register(local->phy);
362 363 364 365 366
	if (rc < 0)
		goto out_wq;

	rtnl_lock();

367
	mutex_lock(&local->iflist_mtx);
368
	local->running = MAC802154_DEVICE_RUN;
369
	mutex_unlock(&local->iflist_mtx);
370 371 372 373 374 375

	rtnl_unlock();

	return 0;

out_wq:
376
	destroy_workqueue(local->dev_workqueue);
377 378 379
out:
	return rc;
}
380
EXPORT_SYMBOL(ieee802154_register_hw);
381

382
void ieee802154_unregister_hw(struct ieee802154_hw *hw)
383
{
384
	struct ieee802154_local *local = mac802154_to_priv(hw);
385
	struct ieee802154_sub_if_data *sdata, *next;
386

387 388
	flush_workqueue(local->dev_workqueue);
	destroy_workqueue(local->dev_workqueue);
389 390 391

	rtnl_lock();

392
	mutex_lock(&local->iflist_mtx);
393
	local->running = MAC802154_DEVICE_STOPPED;
394
	mutex_unlock(&local->iflist_mtx);
395

396 397
	list_for_each_entry_safe(sdata, next, &local->interfaces, list) {
		mutex_lock(&sdata->local->iflist_mtx);
398
		list_del(&sdata->list);
399
		mutex_unlock(&sdata->local->iflist_mtx);
400 401 402 403

		unregister_netdevice(sdata->dev);
	}

404 405
	rtnl_unlock();

406
	wpan_phy_unregister(local->phy);
407
}
408
EXPORT_SYMBOL(ieee802154_unregister_hw);
409 410 411

MODULE_DESCRIPTION("IEEE 802.15.4 implementation");
MODULE_LICENSE("GPL v2");