main.c 9.0 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
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
35
	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 64 65 66
		WARN_ON(res);
		if (res)
			goto err;
	}

	netif_start_queue(dev);
	return 0;
err:
67
	sdata->local->open_count--;
68 69 70 71 72 73

	return res;
}

int mac802154_slave_close(struct net_device *dev)
{
74
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
75
	struct ieee802154_local *local = sdata->local;
76

77 78
	ASSERT_RTNL();

79 80
	netif_stop_queue(dev);

81
	mutex_lock(&sdata->local->iflist_mtx);
82
	sdata->running = false;
83
	mutex_unlock(&sdata->local->iflist_mtx);
84

85 86
	if (!--local->open_count)
		local->ops->stop(&local->hw);
87 88 89 90 91 92 93

	return 0;
}

static int
mac802154_netdev_register(struct wpan_phy *phy, struct net_device *dev)
{
94
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
95
	struct ieee802154_local *local;
96 97
	int err;

98
	local = wpan_phy_priv(phy);
99

100
	sdata->dev = dev;
101
	sdata->local = local;
102

103
	dev->needed_headroom = local->hw.extra_tx_headroom;
104

105
	SET_NETDEV_DEV(dev, &local->phy->dev);
106

107
	mutex_lock(&local->iflist_mtx);
108
	if (!local->running) {
109
		mutex_unlock(&local->iflist_mtx);
110 111
		return -ENODEV;
	}
112
	mutex_unlock(&local->iflist_mtx);
113 114 115 116 117 118

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

	rtnl_lock();
119 120 121
	mutex_lock(&local->iflist_mtx);
	list_add_tail_rcu(&sdata->list, &local->interfaces);
	mutex_unlock(&local->iflist_mtx);
122 123 124 125 126 127 128 129
	rtnl_unlock();

	return 0;
}

static void
mac802154_del_iface(struct wpan_phy *phy, struct net_device *dev)
{
130
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
131

132 133
	ASSERT_RTNL();

134
	BUG_ON(sdata->local->phy != phy);
135

136
	mutex_lock(&sdata->local->iflist_mtx);
137
	list_del_rcu(&sdata->list);
138
	mutex_unlock(&sdata->local->iflist_mtx);
139 140 141 142 143 144 145 146 147 148 149 150

	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) {
151
	case IEEE802154_DEV_MONITOR:
152
		dev = alloc_netdev(sizeof(struct ieee802154_sub_if_data),
153 154
				   name, NET_NAME_UNKNOWN,
				   mac802154_monitor_setup);
155
		break;
156
	case IEEE802154_DEV_WPAN:
157
		dev = alloc_netdev(sizeof(struct ieee802154_sub_if_data),
158 159
				   name, NET_NAME_UNKNOWN,
				   mac802154_wpan_setup);
160
		break;
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	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);
}

182 183
static int mac802154_set_txpower(struct wpan_phy *phy, int db)
{
184
	struct ieee802154_local *local = wpan_phy_priv(phy);
185

186
	return local->ops->set_txpower(&local->hw, db);
187 188
}

189 190
static int mac802154_set_lbt(struct wpan_phy *phy, bool on)
{
191
	struct ieee802154_local *local = wpan_phy_priv(phy);
192

193
	return local->ops->set_lbt(&local->hw, on);
194 195
}

196 197
static int mac802154_set_cca_mode(struct wpan_phy *phy, u8 mode)
{
198
	struct ieee802154_local *local = wpan_phy_priv(phy);
199

200
	return local->ops->set_cca_mode(&local->hw, mode);
201 202
}

203 204
static int mac802154_set_cca_ed_level(struct wpan_phy *phy, s32 level)
{
205
	struct ieee802154_local *local = wpan_phy_priv(phy);
206

207
	return local->ops->set_cca_ed_level(&local->hw, level);
208 209
}

210 211 212
static int mac802154_set_csma_params(struct wpan_phy *phy, u8 min_be,
				     u8 max_be, u8 retries)
{
213
	struct ieee802154_local *local = wpan_phy_priv(phy);
214

215
	return local->ops->set_csma_params(&local->hw, min_be, max_be, retries);
216 217 218 219
}

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

222
	return local->ops->set_frame_retries(&local->hw, retries);
223 224
}

225 226
struct ieee802154_hw *
ieee802154_alloc_hw(size_t priv_data_len, struct ieee802154_ops *ops)
227 228
{
	struct wpan_phy *phy;
229
	struct ieee802154_local *local;
230 231
	size_t priv_size;

232 233
	if (!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
	    !ops->start || !ops->stop || !ops->set_channel) {
234
		pr_err("undefined IEEE802.15.4 device operations\n");
235 236 237 238
		return NULL;
	}

	/* Ensure 32-byte alignment of our private data and hw private data.
239
	 * We use the wpan_phy priv data for both our ieee802154_local and for
240 241 242 243
	 * the driver's private data
	 *
	 * in memory it'll be like this:
	 *
244 245 246 247 248 249 250
	 * +-------------------------+
	 * | struct wpan_phy         |
	 * +-------------------------+
	 * | struct ieee802154_local |
	 * +-------------------------+
	 * | driver's private data   |
	 * +-------------------------+
251 252
	 *
	 * Due to ieee802154 layer isn't aware of driver and MAC structures,
253
	 * so lets align them here.
254 255
	 */

256
	priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
257 258 259

	phy = wpan_phy_alloc(priv_size);
	if (!phy) {
260
		pr_err("failure to allocate master IEEE802.15.4 device\n");
261 262 263
		return NULL;
	}

264 265 266 267 268
	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;
269

270 271
	INIT_LIST_HEAD(&local->interfaces);
	mutex_init(&local->iflist_mtx);
272

273
	return &local->hw;
274
}
275
EXPORT_SYMBOL(ieee802154_alloc_hw);
276

277
void ieee802154_free_hw(struct ieee802154_hw *hw)
278
{
279
	struct ieee802154_local *local = hw_to_local(hw);
280

281
	BUG_ON(!list_empty(&local->interfaces));
282

283
	mutex_destroy(&local->iflist_mtx);
284

285
	wpan_phy_free(local->phy);
286
}
287
EXPORT_SYMBOL(ieee802154_free_hw);
288

289
int ieee802154_register_hw(struct ieee802154_hw *hw)
290
{
291
	struct ieee802154_local *local = hw_to_local(hw);
292 293
	int rc = -ENOSYS;

294
	if (hw->flags & IEEE802154_HW_TXPOWER) {
295
		if (!local->ops->set_txpower)
296 297
			goto out;

298
		local->phy->set_txpower = mac802154_set_txpower;
299 300
	}

301
	if (hw->flags & IEEE802154_HW_LBT) {
302
		if (!local->ops->set_lbt)
303 304
			goto out;

305
		local->phy->set_lbt = mac802154_set_lbt;
306 307
	}

308
	if (hw->flags & IEEE802154_HW_CCA_MODE) {
309
		if (!local->ops->set_cca_mode)
310 311
			goto out;

312
		local->phy->set_cca_mode = mac802154_set_cca_mode;
313 314
	}

315
	if (hw->flags & IEEE802154_HW_CCA_ED_LEVEL) {
316
		if (!local->ops->set_cca_ed_level)
317 318
			goto out;

319
		local->phy->set_cca_ed_level = mac802154_set_cca_ed_level;
320 321
	}

322
	if (hw->flags & IEEE802154_HW_CSMA_PARAMS) {
323
		if (!local->ops->set_csma_params)
324 325
			goto out;

326
		local->phy->set_csma_params = mac802154_set_csma_params;
327 328
	}

329
	if (hw->flags & IEEE802154_HW_FRAME_RETRIES) {
330
		if (!local->ops->set_frame_retries)
331 332
			goto out;

333
		local->phy->set_frame_retries = mac802154_set_frame_retries;
334
	}
335

336
	local->workqueue =
337
		create_singlethread_workqueue(wpan_phy_name(local->phy));
338
	if (!local->workqueue) {
339
		rc = -ENOMEM;
340
		goto out;
341
	}
342

343
	wpan_phy_set_dev(local->phy, local->hw.parent);
344

345 346
	local->phy->add_iface = mac802154_add_iface;
	local->phy->del_iface = mac802154_del_iface;
347

348
	rc = wpan_phy_register(local->phy);
349 350 351 352 353
	if (rc < 0)
		goto out_wq;

	rtnl_lock();

354
	mutex_lock(&local->iflist_mtx);
355
	local->running = MAC802154_DEVICE_RUN;
356
	mutex_unlock(&local->iflist_mtx);
357 358 359 360 361 362

	rtnl_unlock();

	return 0;

out_wq:
363
	destroy_workqueue(local->workqueue);
364 365 366
out:
	return rc;
}
367
EXPORT_SYMBOL(ieee802154_register_hw);
368

369
void ieee802154_unregister_hw(struct ieee802154_hw *hw)
370
{
371
	struct ieee802154_local *local = hw_to_local(hw);
372
	struct ieee802154_sub_if_data *sdata, *next;
373

374 375
	flush_workqueue(local->workqueue);
	destroy_workqueue(local->workqueue);
376 377 378

	rtnl_lock();

379
	mutex_lock(&local->iflist_mtx);
380
	local->running = MAC802154_DEVICE_STOPPED;
381
	mutex_unlock(&local->iflist_mtx);
382

383 384
	list_for_each_entry_safe(sdata, next, &local->interfaces, list) {
		mutex_lock(&sdata->local->iflist_mtx);
385
		list_del(&sdata->list);
386
		mutex_unlock(&sdata->local->iflist_mtx);
387 388 389 390

		unregister_netdevice(sdata->dev);
	}

391 392
	rtnl_unlock();

393
	wpan_phy_unregister(local->phy);
394
}
395
EXPORT_SYMBOL(ieee802154_unregister_hw);
396 397 398

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