firewire: core: add CSR MAINT_UTILITY support
[firewire-audio.git] / net / ieee802154 / nl-phy.c
blob199a2d9d12f9ec7f526c981789f096f2292df33e
1 /*
2 * Netlink inteface for IEEE 802.15.4 stack
4 * Copyright 2007, 2008 Siemens AG
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * Written by:
20 * Sergey Lapin <slapin@ossfans.org>
21 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
22 * Maxim Osipov <maxim.osipov@siemens.com>
25 #include <linux/kernel.h>
26 #include <net/netlink.h>
27 #include <net/genetlink.h>
28 #include <net/wpan-phy.h>
29 #include <net/af_ieee802154.h>
30 #include <net/ieee802154_netdev.h>
31 #include <net/rtnetlink.h> /* for rtnl_{un,}lock */
32 #include <linux/nl802154.h>
34 #include "ieee802154.h"
36 static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 pid,
37 u32 seq, int flags, struct wpan_phy *phy)
39 void *hdr;
40 int i, pages = 0;
41 uint32_t *buf = kzalloc(32 * sizeof(uint32_t), GFP_KERNEL);
43 pr_debug("%s\n", __func__);
45 if (!buf)
46 goto out;
48 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
49 IEEE802154_LIST_PHY);
50 if (!hdr)
51 goto out;
53 mutex_lock(&phy->pib_lock);
54 NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
56 NLA_PUT_U8(msg, IEEE802154_ATTR_PAGE, phy->current_page);
57 NLA_PUT_U8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel);
58 for (i = 0; i < 32; i++) {
59 if (phy->channels_supported[i])
60 buf[pages++] = phy->channels_supported[i] | (i << 27);
62 if (pages)
63 NLA_PUT(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
64 pages * sizeof(uint32_t), buf);
66 mutex_unlock(&phy->pib_lock);
67 return genlmsg_end(msg, hdr);
69 nla_put_failure:
70 mutex_unlock(&phy->pib_lock);
71 genlmsg_cancel(msg, hdr);
72 out:
73 kfree(buf);
74 return -EMSGSIZE;
77 static int ieee802154_list_phy(struct sk_buff *skb,
78 struct genl_info *info)
80 /* Request for interface name, index, type, IEEE address,
81 PAN Id, short address */
82 struct sk_buff *msg;
83 struct wpan_phy *phy;
84 const char *name;
85 int rc = -ENOBUFS;
87 pr_debug("%s\n", __func__);
89 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
90 return -EINVAL;
92 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
93 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
94 return -EINVAL; /* phy name should be null-terminated */
97 phy = wpan_phy_find(name);
98 if (!phy)
99 return -ENODEV;
101 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
102 if (!msg)
103 goto out_dev;
105 rc = ieee802154_nl_fill_phy(msg, info->snd_pid, info->snd_seq,
106 0, phy);
107 if (rc < 0)
108 goto out_free;
110 wpan_phy_put(phy);
112 return genlmsg_reply(msg, info);
113 out_free:
114 nlmsg_free(msg);
115 out_dev:
116 wpan_phy_put(phy);
117 return rc;
121 struct dump_phy_data {
122 struct sk_buff *skb;
123 struct netlink_callback *cb;
124 int idx, s_idx;
127 static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
129 int rc;
130 struct dump_phy_data *data = _data;
132 pr_debug("%s\n", __func__);
134 if (data->idx++ < data->s_idx)
135 return 0;
137 rc = ieee802154_nl_fill_phy(data->skb,
138 NETLINK_CB(data->cb->skb).pid,
139 data->cb->nlh->nlmsg_seq,
140 NLM_F_MULTI,
141 phy);
143 if (rc < 0) {
144 data->idx--;
145 return rc;
148 return 0;
151 static int ieee802154_dump_phy(struct sk_buff *skb,
152 struct netlink_callback *cb)
154 struct dump_phy_data data = {
155 .cb = cb,
156 .skb = skb,
157 .s_idx = cb->args[0],
158 .idx = 0,
161 pr_debug("%s\n", __func__);
163 wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
165 cb->args[0] = data.idx;
167 return skb->len;
170 static int ieee802154_add_iface(struct sk_buff *skb,
171 struct genl_info *info)
173 struct sk_buff *msg;
174 struct wpan_phy *phy;
175 const char *name;
176 const char *devname;
177 int rc = -ENOBUFS;
178 struct net_device *dev;
180 pr_debug("%s\n", __func__);
182 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
183 return -EINVAL;
185 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
186 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
187 return -EINVAL; /* phy name should be null-terminated */
189 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
190 devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
191 if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
192 != '\0')
193 return -EINVAL; /* phy name should be null-terminated */
194 } else {
195 devname = "wpan%d";
198 if (strlen(devname) >= IFNAMSIZ)
199 return -ENAMETOOLONG;
201 phy = wpan_phy_find(name);
202 if (!phy)
203 return -ENODEV;
205 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
206 if (!msg)
207 goto out_dev;
209 if (!phy->add_iface) {
210 rc = -EINVAL;
211 goto nla_put_failure;
214 dev = phy->add_iface(phy, devname);
215 if (IS_ERR(dev)) {
216 rc = PTR_ERR(dev);
217 goto nla_put_failure;
220 NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
221 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
223 dev_put(dev);
225 wpan_phy_put(phy);
227 return ieee802154_nl_reply(msg, info);
229 nla_put_failure:
230 nlmsg_free(msg);
231 out_dev:
232 wpan_phy_put(phy);
233 return rc;
236 static int ieee802154_del_iface(struct sk_buff *skb,
237 struct genl_info *info)
239 struct sk_buff *msg;
240 struct wpan_phy *phy;
241 const char *name;
242 int rc;
243 struct net_device *dev;
245 pr_debug("%s\n", __func__);
247 if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
248 return -EINVAL;
250 name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
251 if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
252 return -EINVAL; /* name should be null-terminated */
254 dev = dev_get_by_name(genl_info_net(info), name);
255 if (!dev)
256 return -ENODEV;
258 phy = ieee802154_mlme_ops(dev)->get_phy(dev);
259 BUG_ON(!phy);
261 rc = -EINVAL;
262 /* phy name is optional, but should be checked if it's given */
263 if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
264 struct wpan_phy *phy2;
266 const char *pname =
267 nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
268 if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
269 != '\0')
270 /* name should be null-terminated */
271 goto out_dev;
273 phy2 = wpan_phy_find(pname);
274 if (!phy2)
275 goto out_dev;
277 if (phy != phy2) {
278 wpan_phy_put(phy2);
279 goto out_dev;
283 rc = -ENOBUFS;
285 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
286 if (!msg)
287 goto out_dev;
289 if (!phy->del_iface) {
290 rc = -EINVAL;
291 goto nla_put_failure;
294 rtnl_lock();
295 phy->del_iface(phy, dev);
297 /* We don't have device anymore */
298 dev_put(dev);
299 dev = NULL;
301 rtnl_unlock();
304 NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
305 NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, name);
307 wpan_phy_put(phy);
309 return ieee802154_nl_reply(msg, info);
311 nla_put_failure:
312 nlmsg_free(msg);
313 out_dev:
314 wpan_phy_put(phy);
315 if (dev)
316 dev_put(dev);
318 return rc;
321 static struct genl_ops ieee802154_phy_ops[] = {
322 IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
323 ieee802154_dump_phy),
324 IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
325 IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
329 * No need to unregister as family unregistration will do it.
331 int nl802154_phy_register(void)
333 int i;
334 int rc;
336 for (i = 0; i < ARRAY_SIZE(ieee802154_phy_ops); i++) {
337 rc = genl_register_ops(&nl802154_family,
338 &ieee802154_phy_ops[i]);
339 if (rc)
340 return rc;
343 return 0;