cfg.c 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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.
 *
 * Authors:
 * Alexander Aring <aar@pengutronix.de>
 *
 * Based on: net/mac80211/cfg.c
 */

16
#include <net/rtnetlink.h>
17 18
#include <net/cfg802154.h>

19
#include "ieee802154_i.h"
20
#include "driver-ops.h"
21
#include "cfg.h"
22 23 24

static struct net_device *
ieee802154_add_iface_deprecated(struct wpan_phy *wpan_phy,
25 26
				const char *name,
				unsigned char name_assign_type, int type)
27
{
28
	struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
29
	struct net_device *dev;
30

31
	rtnl_lock();
32
	dev = ieee802154_if_add(local, name, name_assign_type, type,
33
				cpu_to_le64(0x0000000000000000ULL));
34 35 36
	rtnl_unlock();

	return dev;
37 38 39 40 41
}

static void ieee802154_del_iface_deprecated(struct wpan_phy *wpan_phy,
					    struct net_device *dev)
{
42 43 44
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);

	ieee802154_if_remove(sdata);
45 46
}

47 48
static int
ieee802154_add_iface(struct wpan_phy *phy, const char *name,
49
		     unsigned char name_assign_type,
50
		     enum nl802154_iftype type, __le64 extended_addr)
51 52 53 54
{
	struct ieee802154_local *local = wpan_phy_priv(phy);
	struct net_device *err;

55 56
	err = ieee802154_if_add(local, name, name_assign_type, type,
				extended_addr);
57
	return PTR_ERR_OR_ZERO(err);
58 59
}

60 61 62 63 64 65 66 67
static int
ieee802154_del_iface(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev)
{
	ieee802154_if_remove(IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev));

	return 0;
}

68
static int
69
ieee802154_set_channel(struct wpan_phy *wpan_phy, u8 page, u8 channel)
70 71 72 73 74 75
{
	struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
	int ret;

	ASSERT_RTNL();

76 77 78 79
	if (wpan_phy->current_page == page &&
	    wpan_phy->current_channel == channel)
		return 0;

80 81 82 83 84 85 86 87 88
	ret = drv_set_channel(local, page, channel);
	if (!ret) {
		wpan_phy->current_page = page;
		wpan_phy->current_channel = channel;
	}

	return ret;
}

89 90 91 92 93 94 95 96 97
static int
ieee802154_set_cca_mode(struct wpan_phy *wpan_phy,
			const struct wpan_phy_cca *cca)
{
	struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
	int ret;

	ASSERT_RTNL();

98 99 100
	if (wpan_phy_cca_cmp(&wpan_phy->cca, cca))
		return 0;

101 102 103 104 105 106 107
	ret = drv_set_cca_mode(local, cca);
	if (!ret)
		wpan_phy->cca = *cca;

	return ret;
}

108 109
static int
ieee802154_set_pan_id(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
110
		      __le16 pan_id)
111 112 113
{
	ASSERT_RTNL();

114 115 116
	if (wpan_dev->pan_id == pan_id)
		return 0;

117
	wpan_dev->pan_id = pan_id;
118 119 120
	return 0;
}

121 122 123
static int
ieee802154_set_backoff_exponent(struct wpan_phy *wpan_phy,
				struct wpan_dev *wpan_dev,
124
				u8 min_be, u8 max_be)
125 126 127
{
	ASSERT_RTNL();

128 129 130 131
	if (wpan_dev->min_be == min_be &&
	    wpan_dev->max_be == max_be)
		return 0;

132 133 134 135 136
	wpan_dev->min_be = min_be;
	wpan_dev->max_be = max_be;
	return 0;
}

137 138
static int
ieee802154_set_short_addr(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
139
			  __le16 short_addr)
140 141 142
{
	ASSERT_RTNL();

143 144 145
	if (wpan_dev->short_addr == short_addr)
		return 0;

146
	wpan_dev->short_addr = short_addr;
147 148 149
	return 0;
}

150 151 152 153
static int
ieee802154_set_max_csma_backoffs(struct wpan_phy *wpan_phy,
				 struct wpan_dev *wpan_dev,
				 u8 max_csma_backoffs)
154 155 156
{
	ASSERT_RTNL();

157 158 159
	if (wpan_dev->csma_retries == max_csma_backoffs)
		return 0;

160 161 162 163
	wpan_dev->csma_retries = max_csma_backoffs;
	return 0;
}

164 165 166 167
static int
ieee802154_set_max_frame_retries(struct wpan_phy *wpan_phy,
				 struct wpan_dev *wpan_dev,
				 s8 max_frame_retries)
168 169 170
{
	ASSERT_RTNL();

171 172 173
	if (wpan_dev->frame_retries == max_frame_retries)
		return 0;

174 175 176 177
	wpan_dev->frame_retries = max_frame_retries;
	return 0;
}

178 179 180
static int
ieee802154_set_lbt_mode(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
			bool mode)
181 182 183
{
	ASSERT_RTNL();

184 185 186
	if (wpan_dev->lbt == mode)
		return 0;

187 188 189 190
	wpan_dev->lbt = mode;
	return 0;
}

191
const struct cfg802154_ops mac802154_config_ops = {
192 193
	.add_virtual_intf_deprecated = ieee802154_add_iface_deprecated,
	.del_virtual_intf_deprecated = ieee802154_del_iface_deprecated,
194
	.add_virtual_intf = ieee802154_add_iface,
195
	.del_virtual_intf = ieee802154_del_iface,
196
	.set_channel = ieee802154_set_channel,
197
	.set_cca_mode = ieee802154_set_cca_mode,
198
	.set_pan_id = ieee802154_set_pan_id,
199
	.set_short_addr = ieee802154_set_short_addr,
200
	.set_backoff_exponent = ieee802154_set_backoff_exponent,
201
	.set_max_csma_backoffs = ieee802154_set_max_csma_backoffs,
202
	.set_max_frame_retries = ieee802154_set_max_frame_retries,
203
	.set_lbt_mode = ieee802154_set_lbt_mode,
204
};