Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 1 | /* |
| 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-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 13 | */ |
| 14 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/device.h> |
| 19 | |
Alexander Aring | 5ad60d3 | 2014-10-25 09:41:02 +0200 | [diff] [blame] | 20 | #include <net/cfg802154.h> |
Alexander Aring | f3ada64 | 2014-11-09 08:36:48 +0100 | [diff] [blame] | 21 | #include <net/rtnetlink.h> |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 22 | |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 23 | #include "ieee802154.h" |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 24 | #include "sysfs.h" |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 25 | #include "core.h" |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 26 | |
Alexander Aring | f3ada64 | 2014-11-09 08:36:48 +0100 | [diff] [blame] | 27 | /* RCU-protected (and RTNL for writers) */ |
| 28 | static LIST_HEAD(cfg802154_rdev_list); |
| 29 | static int cfg802154_rdev_list_generation; |
| 30 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 31 | static int wpan_phy_match(struct device *dev, const void *data) |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 32 | { |
| 33 | return !strcmp(dev_name(dev), (const char *)data); |
| 34 | } |
| 35 | |
| 36 | struct wpan_phy *wpan_phy_find(const char *str) |
| 37 | { |
| 38 | struct device *dev; |
| 39 | |
| 40 | if (WARN_ON(!str)) |
| 41 | return NULL; |
| 42 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 43 | dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 44 | if (!dev) |
| 45 | return NULL; |
| 46 | |
| 47 | return container_of(dev, struct wpan_phy, dev); |
| 48 | } |
| 49 | EXPORT_SYMBOL(wpan_phy_find); |
| 50 | |
Dmitry Eremin-Solenikov | 1c889f4 | 2009-09-15 16:57:04 +0400 | [diff] [blame] | 51 | struct wpan_phy_iter_data { |
| 52 | int (*fn)(struct wpan_phy *phy, void *data); |
| 53 | void *data; |
| 54 | }; |
| 55 | |
| 56 | static int wpan_phy_iter(struct device *dev, void *_data) |
| 57 | { |
| 58 | struct wpan_phy_iter_data *wpid = _data; |
| 59 | struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 60 | |
Dmitry Eremin-Solenikov | 1c889f4 | 2009-09-15 16:57:04 +0400 | [diff] [blame] | 61 | return wpid->fn(phy, wpid->data); |
| 62 | } |
| 63 | |
| 64 | int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data), |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 65 | void *data) |
Dmitry Eremin-Solenikov | 1c889f4 | 2009-09-15 16:57:04 +0400 | [diff] [blame] | 66 | { |
| 67 | struct wpan_phy_iter_data wpid = { |
| 68 | .fn = fn, |
| 69 | .data = data, |
| 70 | }; |
| 71 | |
| 72 | return class_for_each_device(&wpan_phy_class, NULL, |
| 73 | &wpid, wpan_phy_iter); |
| 74 | } |
| 75 | EXPORT_SYMBOL(wpan_phy_for_each); |
| 76 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 77 | struct wpan_phy * |
Alexander Aring | f601379 | 2014-11-09 08:36:47 +0100 | [diff] [blame] | 78 | wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size) |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 79 | { |
Alexander Aring | 53f9ee61 | 2014-11-05 20:51:12 +0100 | [diff] [blame] | 80 | static atomic_t wpan_phy_counter = ATOMIC_INIT(0); |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 81 | struct cfg802154_registered_device *rdev; |
| 82 | size_t alloc_size; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 83 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 84 | alloc_size = sizeof(*rdev) + priv_size; |
| 85 | rdev = kzalloc(alloc_size, GFP_KERNEL); |
| 86 | if (!rdev) |
| 87 | return NULL; |
| 88 | |
| 89 | rdev->ops = ops; |
| 90 | |
Alexander Aring | 53f9ee61 | 2014-11-05 20:51:12 +0100 | [diff] [blame] | 91 | rdev->wpan_phy_idx = atomic_inc_return(&wpan_phy_counter); |
| 92 | |
| 93 | if (unlikely(rdev->wpan_phy_idx < 0)) { |
| 94 | /* ugh, wrapped! */ |
| 95 | atomic_dec(&wpan_phy_counter); |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 96 | kfree(rdev); |
Alexander Aring | 53f9ee61 | 2014-11-05 20:51:12 +0100 | [diff] [blame] | 97 | return NULL; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 98 | } |
Alexander Aring | 53f9ee61 | 2014-11-05 20:51:12 +0100 | [diff] [blame] | 99 | |
| 100 | /* atomic_inc_return makes it start at 1, make it start at 0 */ |
| 101 | rdev->wpan_phy_idx--; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 102 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 103 | mutex_init(&rdev->wpan_phy.pib_lock); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 104 | |
Alexander Aring | fcf39e6 | 2014-11-09 08:36:50 +0100 | [diff] [blame^] | 105 | INIT_LIST_HEAD(&rdev->wpan_dev_list); |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 106 | device_initialize(&rdev->wpan_phy.dev); |
Alexander Aring | 53f9ee61 | 2014-11-05 20:51:12 +0100 | [diff] [blame] | 107 | dev_set_name(&rdev->wpan_phy.dev, "wpan-phy%d", rdev->wpan_phy_idx); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 108 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 109 | rdev->wpan_phy.dev.class = &wpan_phy_class; |
| 110 | rdev->wpan_phy.dev.platform_data = rdev; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 111 | |
Alexander Aring | fcf39e6 | 2014-11-09 08:36:50 +0100 | [diff] [blame^] | 112 | init_waitqueue_head(&rdev->dev_wait); |
| 113 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 114 | return &rdev->wpan_phy; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 115 | } |
Alexander Aring | f601379 | 2014-11-09 08:36:47 +0100 | [diff] [blame] | 116 | EXPORT_SYMBOL(wpan_phy_new); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 117 | |
Dmitry Eremin-Solenikov | e9cf356 | 2009-09-28 19:01:20 +0400 | [diff] [blame] | 118 | int wpan_phy_register(struct wpan_phy *phy) |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 119 | { |
Alexander Aring | f3ada64 | 2014-11-09 08:36:48 +0100 | [diff] [blame] | 120 | struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy); |
| 121 | int ret; |
| 122 | |
| 123 | rtnl_lock(); |
| 124 | ret = device_add(&phy->dev); |
| 125 | if (ret) { |
| 126 | rtnl_unlock(); |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | list_add_rcu(&rdev->list, &cfg802154_rdev_list); |
| 131 | cfg802154_rdev_list_generation++; |
| 132 | |
| 133 | /* TODO phy registered lock */ |
| 134 | rtnl_unlock(); |
| 135 | |
| 136 | /* TODO nl802154 phy notify */ |
| 137 | |
| 138 | return 0; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 139 | } |
| 140 | EXPORT_SYMBOL(wpan_phy_register); |
| 141 | |
| 142 | void wpan_phy_unregister(struct wpan_phy *phy) |
| 143 | { |
Alexander Aring | f3ada64 | 2014-11-09 08:36:48 +0100 | [diff] [blame] | 144 | struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy); |
| 145 | |
Alexander Aring | fcf39e6 | 2014-11-09 08:36:50 +0100 | [diff] [blame^] | 146 | wait_event(rdev->dev_wait, ({ |
| 147 | int __count; |
| 148 | rtnl_lock(); |
| 149 | __count = rdev->opencount; |
| 150 | rtnl_unlock(); |
| 151 | __count == 0; })); |
Alexander Aring | f3ada64 | 2014-11-09 08:36:48 +0100 | [diff] [blame] | 152 | |
| 153 | rtnl_lock(); |
| 154 | /* TODO nl802154 phy notify */ |
| 155 | /* TODO phy registered lock */ |
| 156 | |
Alexander Aring | fcf39e6 | 2014-11-09 08:36:50 +0100 | [diff] [blame^] | 157 | WARN_ON(!list_empty(&rdev->wpan_dev_list)); |
Alexander Aring | f3ada64 | 2014-11-09 08:36:48 +0100 | [diff] [blame] | 158 | |
| 159 | /* First remove the hardware from everywhere, this makes |
| 160 | * it impossible to find from userspace. |
| 161 | */ |
| 162 | list_del_rcu(&rdev->list); |
| 163 | synchronize_rcu(); |
| 164 | |
| 165 | cfg802154_rdev_list_generation++; |
| 166 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 167 | device_del(&phy->dev); |
Alexander Aring | f3ada64 | 2014-11-09 08:36:48 +0100 | [diff] [blame] | 168 | |
| 169 | rtnl_unlock(); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 170 | } |
| 171 | EXPORT_SYMBOL(wpan_phy_unregister); |
| 172 | |
| 173 | void wpan_phy_free(struct wpan_phy *phy) |
| 174 | { |
| 175 | put_device(&phy->dev); |
| 176 | } |
| 177 | EXPORT_SYMBOL(wpan_phy_free); |
| 178 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 179 | void cfg802154_dev_free(struct cfg802154_registered_device *rdev) |
| 180 | { |
| 181 | kfree(rdev); |
| 182 | } |
| 183 | |
Alexander Aring | fcf39e6 | 2014-11-09 08:36:50 +0100 | [diff] [blame^] | 184 | static void |
| 185 | cfg802154_update_iface_num(struct cfg802154_registered_device *rdev, |
| 186 | int iftype, int num) |
| 187 | { |
| 188 | ASSERT_RTNL(); |
| 189 | |
| 190 | rdev->num_running_ifaces += num; |
| 191 | } |
| 192 | |
| 193 | static int cfg802154_netdev_notifier_call(struct notifier_block *nb, |
| 194 | unsigned long state, void *ptr) |
| 195 | { |
| 196 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 197 | struct wpan_dev *wpan_dev = dev->ieee802154_ptr; |
| 198 | struct cfg802154_registered_device *rdev; |
| 199 | |
| 200 | if (!wpan_dev) |
| 201 | return NOTIFY_DONE; |
| 202 | |
| 203 | rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy); |
| 204 | |
| 205 | /* TODO WARN_ON unspec type */ |
| 206 | |
| 207 | switch (state) { |
| 208 | /* TODO NETDEV_DEVTYPE */ |
| 209 | case NETDEV_REGISTER: |
| 210 | wpan_dev->identifier = ++rdev->wpan_dev_id; |
| 211 | list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list); |
| 212 | rdev->devlist_generation++; |
| 213 | |
| 214 | wpan_dev->netdev = dev; |
| 215 | break; |
| 216 | case NETDEV_DOWN: |
| 217 | cfg802154_update_iface_num(rdev, wpan_dev->iftype, -1); |
| 218 | |
| 219 | rdev->opencount--; |
| 220 | wake_up(&rdev->dev_wait); |
| 221 | break; |
| 222 | case NETDEV_UP: |
| 223 | cfg802154_update_iface_num(rdev, wpan_dev->iftype, 1); |
| 224 | |
| 225 | rdev->opencount++; |
| 226 | break; |
| 227 | case NETDEV_UNREGISTER: |
| 228 | /* It is possible to get NETDEV_UNREGISTER |
| 229 | * multiple times. To detect that, check |
| 230 | * that the interface is still on the list |
| 231 | * of registered interfaces, and only then |
| 232 | * remove and clean it up. |
| 233 | */ |
| 234 | if (!list_empty(&wpan_dev->list)) { |
| 235 | list_del_rcu(&wpan_dev->list); |
| 236 | rdev->devlist_generation++; |
| 237 | } |
| 238 | /* synchronize (so that we won't find this netdev |
| 239 | * from other code any more) and then clear the list |
| 240 | * head so that the above code can safely check for |
| 241 | * !list_empty() to avoid double-cleanup. |
| 242 | */ |
| 243 | synchronize_rcu(); |
| 244 | INIT_LIST_HEAD(&wpan_dev->list); |
| 245 | break; |
| 246 | default: |
| 247 | return NOTIFY_DONE; |
| 248 | } |
| 249 | |
| 250 | return NOTIFY_OK; |
| 251 | } |
| 252 | |
| 253 | static struct notifier_block cfg802154_netdev_notifier = { |
| 254 | .notifier_call = cfg802154_netdev_notifier_call, |
| 255 | }; |
| 256 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 257 | static int __init wpan_phy_class_init(void) |
| 258 | { |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 259 | int rc; |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 260 | |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 261 | rc = wpan_phy_sysfs_init(); |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 262 | if (rc) |
| 263 | goto err; |
| 264 | |
Alexander Aring | fcf39e6 | 2014-11-09 08:36:50 +0100 | [diff] [blame^] | 265 | rc = register_netdevice_notifier(&cfg802154_netdev_notifier); |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 266 | if (rc) |
| 267 | goto err_nl; |
| 268 | |
Alexander Aring | fcf39e6 | 2014-11-09 08:36:50 +0100 | [diff] [blame^] | 269 | rc = ieee802154_nl_init(); |
| 270 | if (rc) |
| 271 | goto err_notifier; |
| 272 | |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 273 | return 0; |
Alexander Aring | fcf39e6 | 2014-11-09 08:36:50 +0100 | [diff] [blame^] | 274 | |
| 275 | err_notifier: |
| 276 | unregister_netdevice_notifier(&cfg802154_netdev_notifier); |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 277 | err_nl: |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 278 | wpan_phy_sysfs_exit(); |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 279 | err: |
| 280 | return rc; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 281 | } |
Dmitry Eremin-Solenikov | 282a395 | 2009-11-12 23:58:23 +0300 | [diff] [blame] | 282 | subsys_initcall(wpan_phy_class_init); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 283 | |
| 284 | static void __exit wpan_phy_class_exit(void) |
| 285 | { |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 286 | ieee802154_nl_exit(); |
Alexander Aring | fcf39e6 | 2014-11-09 08:36:50 +0100 | [diff] [blame^] | 287 | unregister_netdevice_notifier(&cfg802154_netdev_notifier); |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 288 | wpan_phy_sysfs_exit(); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 289 | } |
| 290 | module_exit(wpan_phy_class_exit); |
| 291 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 292 | MODULE_LICENSE("GPL v2"); |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 293 | MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface"); |
| 294 | MODULE_AUTHOR("Dmitry Eremin-Solenikov"); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 295 | |