blob: 2ee00e8a03082aaebce7f017a5ca1e7219c6aade [file] [log] [blame]
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +04001/*
2 * Copyright (C) 2007, 2008, 2009 Siemens AG
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040013 */
14
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040016#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/device.h>
19
Alexander Aring5ad60d32014-10-25 09:41:02 +020020#include <net/cfg802154.h>
Alexander Aringf3ada642014-11-09 08:36:48 +010021#include <net/rtnetlink.h>
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040022
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +040023#include "ieee802154.h"
Alexander Aring79fe1a22014-11-09 08:36:53 +010024#include "nl802154.h"
Alexander Aringe23e9ec2014-10-28 18:21:32 +010025#include "sysfs.h"
Alexander Aringa5dd1d72014-11-02 04:18:35 +010026#include "core.h"
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040027
Alexander Aringc4dd7472015-03-02 15:10:04 +010028/* name for sysfs, %d is appended */
29#define PHY_NAME "phy"
30
Alexander Aringf3ada642014-11-09 08:36:48 +010031/* RCU-protected (and RTNL for writers) */
Alexander Aring79fe1a22014-11-09 08:36:53 +010032LIST_HEAD(cfg802154_rdev_list);
Alexander Aringca20ce202014-11-09 08:36:54 +010033int cfg802154_rdev_list_generation;
Alexander Aringf3ada642014-11-09 08:36:48 +010034
Michał Mirosław9f3b7952013-02-01 20:40:17 +010035static int wpan_phy_match(struct device *dev, const void *data)
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040036{
37 return !strcmp(dev_name(dev), (const char *)data);
38}
39
40struct wpan_phy *wpan_phy_find(const char *str)
41{
42 struct device *dev;
43
44 if (WARN_ON(!str))
45 return NULL;
46
Michał Mirosław9f3b7952013-02-01 20:40:17 +010047 dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040048 if (!dev)
49 return NULL;
50
51 return container_of(dev, struct wpan_phy, dev);
52}
53EXPORT_SYMBOL(wpan_phy_find);
54
Dmitry Eremin-Solenikov1c889f42009-09-15 16:57:04 +040055struct wpan_phy_iter_data {
56 int (*fn)(struct wpan_phy *phy, void *data);
57 void *data;
58};
59
60static int wpan_phy_iter(struct device *dev, void *_data)
61{
62 struct wpan_phy_iter_data *wpid = _data;
63 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
Varka Bhadram4710d802014-07-02 09:01:09 +053064
Dmitry Eremin-Solenikov1c889f42009-09-15 16:57:04 +040065 return wpid->fn(phy, wpid->data);
66}
67
68int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
Varka Bhadram4710d802014-07-02 09:01:09 +053069 void *data)
Dmitry Eremin-Solenikov1c889f42009-09-15 16:57:04 +040070{
71 struct wpan_phy_iter_data wpid = {
72 .fn = fn,
73 .data = data,
74 };
75
76 return class_for_each_device(&wpan_phy_class, NULL,
77 &wpid, wpan_phy_iter);
78}
79EXPORT_SYMBOL(wpan_phy_for_each);
80
Alexander Aring79fe1a22014-11-09 08:36:53 +010081struct cfg802154_registered_device *
82cfg802154_rdev_by_wpan_phy_idx(int wpan_phy_idx)
83{
84 struct cfg802154_registered_device *result = NULL, *rdev;
85
86 ASSERT_RTNL();
87
88 list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
89 if (rdev->wpan_phy_idx == wpan_phy_idx) {
90 result = rdev;
91 break;
92 }
93 }
94
95 return result;
96}
97
Alexander Aringa5dd1d72014-11-02 04:18:35 +010098struct wpan_phy *
Alexander Aringf6013792014-11-09 08:36:47 +010099wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size)
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400100{
Alexander Aring53f9ee612014-11-05 20:51:12 +0100101 static atomic_t wpan_phy_counter = ATOMIC_INIT(0);
Alexander Aringa5dd1d72014-11-02 04:18:35 +0100102 struct cfg802154_registered_device *rdev;
103 size_t alloc_size;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400104
Alexander Aringa5dd1d72014-11-02 04:18:35 +0100105 alloc_size = sizeof(*rdev) + priv_size;
106 rdev = kzalloc(alloc_size, GFP_KERNEL);
107 if (!rdev)
108 return NULL;
109
110 rdev->ops = ops;
111
Alexander Aring53f9ee612014-11-05 20:51:12 +0100112 rdev->wpan_phy_idx = atomic_inc_return(&wpan_phy_counter);
113
114 if (unlikely(rdev->wpan_phy_idx < 0)) {
115 /* ugh, wrapped! */
116 atomic_dec(&wpan_phy_counter);
Alexander Aringa5dd1d72014-11-02 04:18:35 +0100117 kfree(rdev);
Alexander Aring53f9ee612014-11-05 20:51:12 +0100118 return NULL;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400119 }
Alexander Aring53f9ee612014-11-05 20:51:12 +0100120
121 /* atomic_inc_return makes it start at 1, make it start at 0 */
122 rdev->wpan_phy_idx--;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400123
Alexander Aringa5dd1d72014-11-02 04:18:35 +0100124 mutex_init(&rdev->wpan_phy.pib_lock);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400125
Alexander Aringfcf39e62014-11-09 08:36:50 +0100126 INIT_LIST_HEAD(&rdev->wpan_dev_list);
Alexander Aringa5dd1d72014-11-02 04:18:35 +0100127 device_initialize(&rdev->wpan_phy.dev);
Alexander Aringc4dd7472015-03-02 15:10:04 +0100128 dev_set_name(&rdev->wpan_phy.dev, PHY_NAME "%d", rdev->wpan_phy_idx);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400129
Alexander Aringa5dd1d72014-11-02 04:18:35 +0100130 rdev->wpan_phy.dev.class = &wpan_phy_class;
131 rdev->wpan_phy.dev.platform_data = rdev;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400132
Alexander Aringfcf39e62014-11-09 08:36:50 +0100133 init_waitqueue_head(&rdev->dev_wait);
134
Alexander Aringa5dd1d72014-11-02 04:18:35 +0100135 return &rdev->wpan_phy;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400136}
Alexander Aringf6013792014-11-09 08:36:47 +0100137EXPORT_SYMBOL(wpan_phy_new);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400138
Dmitry Eremin-Solenikove9cf3562009-09-28 19:01:20 +0400139int wpan_phy_register(struct wpan_phy *phy)
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400140{
Alexander Aringf3ada642014-11-09 08:36:48 +0100141 struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
142 int ret;
143
144 rtnl_lock();
145 ret = device_add(&phy->dev);
146 if (ret) {
147 rtnl_unlock();
148 return ret;
149 }
150
151 list_add_rcu(&rdev->list, &cfg802154_rdev_list);
152 cfg802154_rdev_list_generation++;
153
154 /* TODO phy registered lock */
155 rtnl_unlock();
156
157 /* TODO nl802154 phy notify */
158
159 return 0;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400160}
161EXPORT_SYMBOL(wpan_phy_register);
162
163void wpan_phy_unregister(struct wpan_phy *phy)
164{
Alexander Aringf3ada642014-11-09 08:36:48 +0100165 struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
166
Alexander Aringfcf39e62014-11-09 08:36:50 +0100167 wait_event(rdev->dev_wait, ({
168 int __count;
169 rtnl_lock();
170 __count = rdev->opencount;
171 rtnl_unlock();
172 __count == 0; }));
Alexander Aringf3ada642014-11-09 08:36:48 +0100173
174 rtnl_lock();
175 /* TODO nl802154 phy notify */
176 /* TODO phy registered lock */
177
Alexander Aringfcf39e62014-11-09 08:36:50 +0100178 WARN_ON(!list_empty(&rdev->wpan_dev_list));
Alexander Aringf3ada642014-11-09 08:36:48 +0100179
180 /* First remove the hardware from everywhere, this makes
181 * it impossible to find from userspace.
182 */
183 list_del_rcu(&rdev->list);
184 synchronize_rcu();
185
186 cfg802154_rdev_list_generation++;
187
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400188 device_del(&phy->dev);
Alexander Aringf3ada642014-11-09 08:36:48 +0100189
190 rtnl_unlock();
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400191}
192EXPORT_SYMBOL(wpan_phy_unregister);
193
194void wpan_phy_free(struct wpan_phy *phy)
195{
196 put_device(&phy->dev);
197}
198EXPORT_SYMBOL(wpan_phy_free);
199
Alexander Aringa5dd1d72014-11-02 04:18:35 +0100200void cfg802154_dev_free(struct cfg802154_registered_device *rdev)
201{
202 kfree(rdev);
203}
204
Alexander Aringfcf39e62014-11-09 08:36:50 +0100205static void
206cfg802154_update_iface_num(struct cfg802154_registered_device *rdev,
207 int iftype, int num)
208{
209 ASSERT_RTNL();
210
211 rdev->num_running_ifaces += num;
212}
213
214static int cfg802154_netdev_notifier_call(struct notifier_block *nb,
215 unsigned long state, void *ptr)
216{
217 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
218 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
219 struct cfg802154_registered_device *rdev;
220
221 if (!wpan_dev)
222 return NOTIFY_DONE;
223
224 rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
225
226 /* TODO WARN_ON unspec type */
227
228 switch (state) {
229 /* TODO NETDEV_DEVTYPE */
230 case NETDEV_REGISTER:
Nicolas Dichtelf9d1ce82015-02-05 18:21:30 +0100231 dev->features |= NETIF_F_NETNS_LOCAL;
Alexander Aringfcf39e62014-11-09 08:36:50 +0100232 wpan_dev->identifier = ++rdev->wpan_dev_id;
233 list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list);
234 rdev->devlist_generation++;
235
236 wpan_dev->netdev = dev;
237 break;
238 case NETDEV_DOWN:
239 cfg802154_update_iface_num(rdev, wpan_dev->iftype, -1);
240
241 rdev->opencount--;
242 wake_up(&rdev->dev_wait);
243 break;
244 case NETDEV_UP:
245 cfg802154_update_iface_num(rdev, wpan_dev->iftype, 1);
246
247 rdev->opencount++;
248 break;
249 case NETDEV_UNREGISTER:
250 /* It is possible to get NETDEV_UNREGISTER
251 * multiple times. To detect that, check
252 * that the interface is still on the list
253 * of registered interfaces, and only then
254 * remove and clean it up.
255 */
256 if (!list_empty(&wpan_dev->list)) {
257 list_del_rcu(&wpan_dev->list);
258 rdev->devlist_generation++;
259 }
260 /* synchronize (so that we won't find this netdev
261 * from other code any more) and then clear the list
262 * head so that the above code can safely check for
263 * !list_empty() to avoid double-cleanup.
264 */
265 synchronize_rcu();
266 INIT_LIST_HEAD(&wpan_dev->list);
267 break;
268 default:
269 return NOTIFY_DONE;
270 }
271
272 return NOTIFY_OK;
273}
274
275static struct notifier_block cfg802154_netdev_notifier = {
276 .notifier_call = cfg802154_netdev_notifier_call,
277};
278
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400279static int __init wpan_phy_class_init(void)
280{
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400281 int rc;
Varka Bhadram4710d802014-07-02 09:01:09 +0530282
Alexander Aringe23e9ec2014-10-28 18:21:32 +0100283 rc = wpan_phy_sysfs_init();
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400284 if (rc)
285 goto err;
286
Alexander Aringfcf39e62014-11-09 08:36:50 +0100287 rc = register_netdevice_notifier(&cfg802154_netdev_notifier);
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400288 if (rc)
289 goto err_nl;
290
Alexander Aringfcf39e62014-11-09 08:36:50 +0100291 rc = ieee802154_nl_init();
292 if (rc)
293 goto err_notifier;
294
Alexander Aring79fe1a22014-11-09 08:36:53 +0100295 rc = nl802154_init();
296 if (rc)
297 goto err_ieee802154_nl;
298
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400299 return 0;
Alexander Aringfcf39e62014-11-09 08:36:50 +0100300
Alexander Aring79fe1a22014-11-09 08:36:53 +0100301err_ieee802154_nl:
302 ieee802154_nl_exit();
303
Alexander Aringfcf39e62014-11-09 08:36:50 +0100304err_notifier:
305 unregister_netdevice_notifier(&cfg802154_netdev_notifier);
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400306err_nl:
Alexander Aringe23e9ec2014-10-28 18:21:32 +0100307 wpan_phy_sysfs_exit();
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400308err:
309 return rc;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400310}
Dmitry Eremin-Solenikov282a3952009-11-12 23:58:23 +0300311subsys_initcall(wpan_phy_class_init);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400312
313static void __exit wpan_phy_class_exit(void)
314{
Alexander Aring79fe1a22014-11-09 08:36:53 +0100315 nl802154_exit();
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400316 ieee802154_nl_exit();
Alexander Aringfcf39e62014-11-09 08:36:50 +0100317 unregister_netdevice_notifier(&cfg802154_netdev_notifier);
Alexander Aringe23e9ec2014-10-28 18:21:32 +0100318 wpan_phy_sysfs_exit();
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400319}
320module_exit(wpan_phy_class_exit);
321
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400322MODULE_LICENSE("GPL v2");
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400323MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
324MODULE_AUTHOR("Dmitry Eremin-Solenikov");
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400325