main.c 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 2007-2012 Siemens AG
 *
 * Written by:
 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
 *
 * 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>

21
#include <net/netlink.h>
22
#include <net/nl802154.h>
23
#include <net/mac802154.h>
24
#include <net/ieee802154_netdev.h>
25
#include <net/route.h>
26
#include <net/cfg802154.h>
27

28
#include "ieee802154_i.h"
29
#include "cfg.h"
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
static void ieee802154_tasklet_handler(unsigned long data)
{
	struct ieee802154_local *local = (struct ieee802154_local *)data;
	struct sk_buff *skb;

	while ((skb = skb_dequeue(&local->skb_queue))) {
		switch (skb->pkt_type) {
		case IEEE802154_RX_MSG:
			/* Clear skb->pkt_type in order to not confuse kernel
			 * netstack.
			 */
			skb->pkt_type = 0;
			ieee802154_rx(&local->hw, skb);
			break;
		default:
			WARN(1, "mac802154: Packet is of unknown type %d\n",
			     skb->pkt_type);
			kfree_skb(skb);
			break;
		}
	}
}

54
struct ieee802154_hw *
55
ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
56 57
{
	struct wpan_phy *phy;
58
	struct ieee802154_local *local;
59 60
	size_t priv_size;

V
Varka Bhadram 已提交
61 62
	if (WARN_ON(!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
		    !ops->start || !ops->stop || !ops->set_channel))
63 64 65
		return NULL;

	/* Ensure 32-byte alignment of our private data and hw private data.
66
	 * We use the wpan_phy priv data for both our ieee802154_local and for
67 68 69 70
	 * the driver's private data
	 *
	 * in memory it'll be like this:
	 *
71 72 73 74 75 76 77
	 * +-------------------------+
	 * | struct wpan_phy         |
	 * +-------------------------+
	 * | struct ieee802154_local |
	 * +-------------------------+
	 * | driver's private data   |
	 * +-------------------------+
78 79
	 *
	 * Due to ieee802154 layer isn't aware of driver and MAC structures,
80
	 * so lets align them here.
81 82
	 */

83
	priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
84

85
	phy = wpan_phy_new(&mac802154_config_ops, priv_size);
86
	if (!phy) {
87
		pr_err("failure to allocate master IEEE802.15.4 device\n");
88 89 90
		return NULL;
	}

A
Alexander Aring 已提交
91 92
	phy->privid = mac802154_wpan_phy_privid;

93 94 95 96 97
	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;
98

99 100
	INIT_LIST_HEAD(&local->interfaces);
	mutex_init(&local->iflist_mtx);
101

102 103 104 105 106 107
	tasklet_init(&local->tasklet,
		     ieee802154_tasklet_handler,
		     (unsigned long)local);

	skb_queue_head_init(&local->skb_queue);

108 109 110 111 112 113 114 115 116
	/* init supported flags with 802.15.4 default ranges */
	phy->supported.max_minbe = 8;
	phy->supported.min_maxbe = 3;
	phy->supported.max_maxbe = 8;
	phy->supported.min_frame_retries = -1;
	phy->supported.max_frame_retries = 7;
	phy->supported.max_csma_backoffs = 5;
	phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE;

117 118 119
	/* always supported */
	phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE);

120
	return &local->hw;
121
}
122
EXPORT_SYMBOL(ieee802154_alloc_hw);
123

124
void ieee802154_free_hw(struct ieee802154_hw *hw)
125
{
126
	struct ieee802154_local *local = hw_to_local(hw);
127

128
	BUG_ON(!list_empty(&local->interfaces));
129

130
	mutex_destroy(&local->iflist_mtx);
131

132
	wpan_phy_free(local->phy);
133
}
134
EXPORT_SYMBOL(ieee802154_free_hw);
135

136 137 138 139 140 141 142 143 144 145 146 147
static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy)
{
	/* TODO warn on empty symbol_duration
	 * Should be done when all drivers sets this value.
	 */

	wpan_phy->lifs_period = IEEE802154_LIFS_PERIOD *
				wpan_phy->symbol_duration;
	wpan_phy->sifs_period = IEEE802154_SIFS_PERIOD *
				wpan_phy->symbol_duration;
}

148
int ieee802154_register_hw(struct ieee802154_hw *hw)
149
{
150
	struct ieee802154_local *local = hw_to_local(hw);
151
	struct net_device *dev;
152 153
	int rc = -ENOSYS;

154
	local->workqueue =
155
		create_singlethread_workqueue(wpan_phy_name(local->phy));
156
	if (!local->workqueue) {
157
		rc = -ENOMEM;
158
		goto out;
159
	}
160

161 162 163
	hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	local->ifs_timer.function = ieee802154_xmit_ifs_timer;

164
	wpan_phy_set_dev(local->phy, local->hw.parent);
165

166 167
	ieee802154_setup_wpan_phy_pib(local->phy);

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) {
		local->phy->supported.min_csma_backoffs = 4;
		local->phy->supported.max_csma_backoffs = 4;
		local->phy->supported.min_maxbe = 5;
		local->phy->supported.max_maxbe = 5;
		local->phy->supported.min_minbe = 3;
		local->phy->supported.max_minbe = 3;
	}

	if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) {
		/* TODO should be 3, but our default value is -1 which means
		 * no ARET handling.
		 */
		local->phy->supported.min_frame_retries = -1;
		local->phy->supported.max_frame_retries = -1;
	}

185 186 187
	if (hw->flags & IEEE802154_HW_PROMISCUOUS)
		local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR);

188
	rc = wpan_phy_register(local->phy);
189 190 191
	if (rc < 0)
		goto out_wq;

192 193
	rtnl_lock();

194 195
	dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM,
				NL802154_IFTYPE_NODE,
196
				cpu_to_le64(0x0000000000000000ULL));
197 198 199
	if (IS_ERR(dev)) {
		rtnl_unlock();
		rc = PTR_ERR(dev);
200
		goto out_phy;
201 202 203 204
	}

	rtnl_unlock();

205 206
	return 0;

207 208
out_phy:
	wpan_phy_unregister(local->phy);
209
out_wq:
210
	destroy_workqueue(local->workqueue);
211 212 213
out:
	return rc;
}
214
EXPORT_SYMBOL(ieee802154_register_hw);
215

216
void ieee802154_unregister_hw(struct ieee802154_hw *hw)
217
{
218
	struct ieee802154_local *local = hw_to_local(hw);
219

220
	tasklet_kill(&local->tasklet);
221 222
	flush_workqueue(local->workqueue);
	destroy_workqueue(local->workqueue);
223 224 225

	rtnl_lock();

226
	ieee802154_remove_interfaces(local);
227

228 229
	rtnl_unlock();

230
	wpan_phy_unregister(local->phy);
231
}
232
EXPORT_SYMBOL(ieee802154_unregister_hw);
233

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
static int __init ieee802154_init(void)
{
	return ieee802154_iface_init();
}

static void __exit ieee802154_exit(void)
{
	ieee802154_iface_exit();

	rcu_barrier();
}

subsys_initcall(ieee802154_init);
module_exit(ieee802154_exit);

249
MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
250
MODULE_LICENSE("GPL v2");