blob: 5d9f68c75e5f8f68c5884d467e5a9d16db0edaac [file] [log] [blame]
Alexander Aring1201cd22014-11-02 04:18:36 +01001/* This program is free software; you can redistribute it and/or modify
2 * it under the terms of the GNU General Public License version 2
3 * as published by the Free Software Foundation.
4 *
5 * This program is distributed in the hope that it will be useful,
6 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8 * GNU General Public License for more details.
9 *
10 * Authors:
11 * Alexander Aring <aar@pengutronix.de>
12 *
13 * Based on: net/mac80211/cfg.c
14 */
15
Alexander Aringd5ae67b2014-11-05 20:51:17 +010016#include <net/rtnetlink.h>
Alexander Aring1201cd22014-11-02 04:18:36 +010017#include <net/cfg802154.h>
18
Alexander Aring4a9a8162014-11-02 04:18:38 +010019#include "ieee802154_i.h"
Alexander Aringab0bd562014-11-12 03:36:55 +010020#include "driver-ops.h"
Alexander Aringfdd20682014-11-02 21:43:05 +010021#include "cfg.h"
Alexander Aring4a9a8162014-11-02 04:18:38 +010022
23static struct net_device *
24ieee802154_add_iface_deprecated(struct wpan_phy *wpan_phy,
25 const char *name, int type)
26{
Alexander Aring986a8ab2014-11-05 20:51:15 +010027 struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
Alexander Aringd5ae67b2014-11-05 20:51:17 +010028 struct net_device *dev;
Alexander Aring986a8ab2014-11-05 20:51:15 +010029
Alexander Aringd5ae67b2014-11-05 20:51:17 +010030 rtnl_lock();
Alexander Aring0e575472014-11-17 08:20:52 +010031 dev = ieee802154_if_add(local, name, type,
32 cpu_to_le64(0x0000000000000000ULL));
Alexander Aringd5ae67b2014-11-05 20:51:17 +010033 rtnl_unlock();
34
35 return dev;
Alexander Aring4a9a8162014-11-02 04:18:38 +010036}
37
38static void ieee802154_del_iface_deprecated(struct wpan_phy *wpan_phy,
39 struct net_device *dev)
40{
Alexander Aringb210b182014-11-05 20:51:14 +010041 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
42
43 ieee802154_if_remove(sdata);
Alexander Aring4a9a8162014-11-02 04:18:38 +010044}
45
Alexander Aringab0bd562014-11-12 03:36:55 +010046static int
Alexander Aringf3ea5e42014-11-17 08:20:51 +010047ieee802154_add_iface(struct wpan_phy *phy, const char *name,
Alexander Aring0e575472014-11-17 08:20:52 +010048 enum nl802154_iftype type, __le64 extended_addr)
Alexander Aringf3ea5e42014-11-17 08:20:51 +010049{
50 struct ieee802154_local *local = wpan_phy_priv(phy);
51 struct net_device *err;
52
Alexander Aring0e575472014-11-17 08:20:52 +010053 err = ieee802154_if_add(local, name, type, extended_addr);
Alexander Aring36cf9422015-01-02 15:49:41 +010054 return PTR_ERR_OR_ZERO(err);
Alexander Aringf3ea5e42014-11-17 08:20:51 +010055}
56
57static int
Alexander Aringb821ecd2014-11-17 08:20:53 +010058ieee802154_del_iface(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev)
59{
60 ieee802154_if_remove(IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev));
61
62 return 0;
63}
64
65static int
Alexander Aring6d5fb872014-11-17 08:20:46 +010066ieee802154_set_channel(struct wpan_phy *wpan_phy, u8 page, u8 channel)
Alexander Aringab0bd562014-11-12 03:36:55 +010067{
68 struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
69 int ret;
70
71 ASSERT_RTNL();
72
73 /* check if phy support this setting */
74 if (!(wpan_phy->channels_supported[page] & BIT(channel)))
75 return -EINVAL;
76
77 ret = drv_set_channel(local, page, channel);
78 if (!ret) {
79 wpan_phy->current_page = page;
80 wpan_phy->current_channel = channel;
81 }
82
83 return ret;
84}
85
Alexander Aring6d5fb872014-11-17 08:20:46 +010086static int
Alexander Aringba2a9502014-12-10 15:33:13 +010087ieee802154_set_cca_mode(struct wpan_phy *wpan_phy,
88 const struct wpan_phy_cca *cca)
89{
90 struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
91 int ret;
92
93 ASSERT_RTNL();
94
95 /* check if phy support this setting */
96 if (!(local->hw.flags & IEEE802154_HW_CCA_MODE))
97 return -EOPNOTSUPP;
98
99 ret = drv_set_cca_mode(local, cca);
100 if (!ret)
101 wpan_phy->cca = *cca;
102
103 return ret;
104}
105
106static int
Alexander Aring6d5fb872014-11-17 08:20:46 +0100107ieee802154_set_pan_id(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
Alexander Aringee7b9052014-11-17 08:20:55 +0100108 __le16 pan_id)
Alexander Aring702bf372014-11-12 03:36:57 +0100109{
110 ASSERT_RTNL();
111
112 /* TODO
113 * I am not sure about to check here on broadcast pan_id.
114 * Broadcast is a valid setting, comment from 802.15.4:
115 * If this value is 0xffff, the device is not associated.
116 *
117 * This could useful to simple deassociate an device.
118 */
Alexander Aringee7b9052014-11-17 08:20:55 +0100119 if (pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST))
Alexander Aring702bf372014-11-12 03:36:57 +0100120 return -EINVAL;
121
Alexander Aringee7b9052014-11-17 08:20:55 +0100122 wpan_dev->pan_id = pan_id;
Alexander Aring702bf372014-11-12 03:36:57 +0100123 return 0;
124}
125
Alexander Aring9830c622014-11-12 03:36:58 +0100126static int
Alexander Aring656a9992014-11-12 03:36:59 +0100127ieee802154_set_backoff_exponent(struct wpan_phy *wpan_phy,
128 struct wpan_dev *wpan_dev,
Alexander Aring6d5fb872014-11-17 08:20:46 +0100129 u8 min_be, u8 max_be)
Alexander Aring656a9992014-11-12 03:36:59 +0100130{
131 struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
132
133 ASSERT_RTNL();
134
135 if (!(local->hw.flags & IEEE802154_HW_CSMA_PARAMS))
136 return -EOPNOTSUPP;
137
138 wpan_dev->min_be = min_be;
139 wpan_dev->max_be = max_be;
140 return 0;
141}
142
143static int
Alexander Aring9830c622014-11-12 03:36:58 +0100144ieee802154_set_short_addr(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
Alexander Aringee7b9052014-11-17 08:20:55 +0100145 __le16 short_addr)
Alexander Aring9830c622014-11-12 03:36:58 +0100146{
147 ASSERT_RTNL();
148
149 /* TODO
150 * I am not sure about to check here on broadcast short_addr.
151 * Broadcast is a valid setting, comment from 802.15.4:
152 * A value of 0xfffe indicates that the device has
153 * associated but has not been allocated an address. A
154 * value of 0xffff indicates that the device does not
155 * have a short address.
156 *
157 * I think we should allow to set these settings but
158 * don't allow to allow socket communication with it.
159 */
Alexander Aringee7b9052014-11-17 08:20:55 +0100160 if (short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC) ||
161 short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST))
Alexander Aring9830c622014-11-12 03:36:58 +0100162 return -EINVAL;
163
Alexander Aringee7b9052014-11-17 08:20:55 +0100164 wpan_dev->short_addr = short_addr;
Alexander Aring9830c622014-11-12 03:36:58 +0100165 return 0;
166}
167
Alexander Aring6d5fb872014-11-17 08:20:46 +0100168static int
169ieee802154_set_max_csma_backoffs(struct wpan_phy *wpan_phy,
170 struct wpan_dev *wpan_dev,
171 u8 max_csma_backoffs)
Alexander Aringa01ba762014-11-12 03:37:01 +0100172{
173 struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
174
175 ASSERT_RTNL();
176
177 if (!(local->hw.flags & IEEE802154_HW_CSMA_PARAMS))
178 return -EOPNOTSUPP;
179
180 wpan_dev->csma_retries = max_csma_backoffs;
181 return 0;
182}
183
Alexander Aring6d5fb872014-11-17 08:20:46 +0100184static int
185ieee802154_set_max_frame_retries(struct wpan_phy *wpan_phy,
186 struct wpan_dev *wpan_dev,
187 s8 max_frame_retries)
Alexander Aring17a3a462014-11-12 03:37:03 +0100188{
189 struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
190
191 ASSERT_RTNL();
192
193 if (!(local->hw.flags & IEEE802154_HW_FRAME_RETRIES))
194 return -EOPNOTSUPP;
195
196 wpan_dev->frame_retries = max_frame_retries;
197 return 0;
198}
199
Alexander Aring6d5fb872014-11-17 08:20:46 +0100200static int
201ieee802154_set_lbt_mode(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
202 bool mode)
Alexander Aringc8937a1d2014-11-12 03:37:05 +0100203{
204 struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
205
206 ASSERT_RTNL();
207
208 if (!(local->hw.flags & IEEE802154_HW_LBT))
209 return -EOPNOTSUPP;
210
211 wpan_dev->lbt = mode;
212 return 0;
213}
214
Alexander Aring1201cd22014-11-02 04:18:36 +0100215const struct cfg802154_ops mac802154_config_ops = {
Alexander Aring4a9a8162014-11-02 04:18:38 +0100216 .add_virtual_intf_deprecated = ieee802154_add_iface_deprecated,
217 .del_virtual_intf_deprecated = ieee802154_del_iface_deprecated,
Alexander Aringf3ea5e42014-11-17 08:20:51 +0100218 .add_virtual_intf = ieee802154_add_iface,
Alexander Aringb821ecd2014-11-17 08:20:53 +0100219 .del_virtual_intf = ieee802154_del_iface,
Alexander Aringab0bd562014-11-12 03:36:55 +0100220 .set_channel = ieee802154_set_channel,
Alexander Aringba2a9502014-12-10 15:33:13 +0100221 .set_cca_mode = ieee802154_set_cca_mode,
Alexander Aring702bf372014-11-12 03:36:57 +0100222 .set_pan_id = ieee802154_set_pan_id,
Alexander Aring9830c622014-11-12 03:36:58 +0100223 .set_short_addr = ieee802154_set_short_addr,
Alexander Aring656a9992014-11-12 03:36:59 +0100224 .set_backoff_exponent = ieee802154_set_backoff_exponent,
Alexander Aringa01ba762014-11-12 03:37:01 +0100225 .set_max_csma_backoffs = ieee802154_set_max_csma_backoffs,
Alexander Aring17a3a462014-11-12 03:37:03 +0100226 .set_max_frame_retries = ieee802154_set_max_frame_retries,
Alexander Aringc8937a1d2014-11-12 03:37:05 +0100227 .set_lbt_mode = ieee802154_set_lbt_mode,
Alexander Aring1201cd22014-11-02 04:18:36 +0100228};