alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2012 Siemens AG |
| 3 | * |
| 4 | * Written by: |
| 5 | * Alexander Smirnov <alex.bluesman.smirnov@gmail.com> |
| 6 | * |
| 7 | * Based on the code from 'linux-zigbee.sourceforge.net' project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 |
| 11 | * as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/netdevice.h> |
| 26 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 27 | #include <net/netlink.h> |
| 28 | #include <linux/nl802154.h> |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 29 | #include <net/mac802154.h> |
| 30 | #include <net/route.h> |
| 31 | #include <net/wpan-phy.h> |
| 32 | |
| 33 | #include "mac802154.h" |
| 34 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 35 | int mac802154_slave_open(struct net_device *dev) |
| 36 | { |
| 37 | struct mac802154_sub_if_data *priv = netdev_priv(dev); |
| 38 | struct mac802154_priv *ipriv = priv->hw; |
| 39 | int res = 0; |
| 40 | |
| 41 | if (ipriv->open_count++ == 0) { |
| 42 | res = ipriv->ops->start(&ipriv->hw); |
| 43 | WARN_ON(res); |
| 44 | if (res) |
| 45 | goto err; |
| 46 | } |
| 47 | |
| 48 | if (ipriv->ops->ieee_addr) { |
| 49 | res = ipriv->ops->ieee_addr(&ipriv->hw, dev->dev_addr); |
| 50 | WARN_ON(res); |
| 51 | if (res) |
| 52 | goto err; |
| 53 | mac802154_dev_set_ieee_addr(dev); |
| 54 | } |
| 55 | |
| 56 | netif_start_queue(dev); |
| 57 | return 0; |
| 58 | err: |
| 59 | priv->hw->open_count--; |
| 60 | |
| 61 | return res; |
| 62 | } |
| 63 | |
| 64 | int mac802154_slave_close(struct net_device *dev) |
| 65 | { |
| 66 | struct mac802154_sub_if_data *priv = netdev_priv(dev); |
| 67 | struct mac802154_priv *ipriv = priv->hw; |
| 68 | |
| 69 | netif_stop_queue(dev); |
| 70 | |
| 71 | if (!--ipriv->open_count) |
| 72 | ipriv->ops->stop(&ipriv->hw); |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int |
| 78 | mac802154_netdev_register(struct wpan_phy *phy, struct net_device *dev) |
| 79 | { |
| 80 | struct mac802154_sub_if_data *priv; |
| 81 | struct mac802154_priv *ipriv; |
| 82 | int err; |
| 83 | |
| 84 | ipriv = wpan_phy_priv(phy); |
| 85 | |
| 86 | priv = netdev_priv(dev); |
| 87 | priv->dev = dev; |
| 88 | priv->hw = ipriv; |
| 89 | |
| 90 | dev->needed_headroom = ipriv->hw.extra_tx_headroom; |
| 91 | |
| 92 | SET_NETDEV_DEV(dev, &ipriv->phy->dev); |
| 93 | |
| 94 | mutex_lock(&ipriv->slaves_mtx); |
| 95 | if (!ipriv->running) { |
| 96 | mutex_unlock(&ipriv->slaves_mtx); |
| 97 | return -ENODEV; |
| 98 | } |
| 99 | mutex_unlock(&ipriv->slaves_mtx); |
| 100 | |
| 101 | err = register_netdev(dev); |
| 102 | if (err < 0) |
| 103 | return err; |
| 104 | |
| 105 | rtnl_lock(); |
| 106 | mutex_lock(&ipriv->slaves_mtx); |
| 107 | list_add_tail_rcu(&priv->list, &ipriv->slaves); |
| 108 | mutex_unlock(&ipriv->slaves_mtx); |
| 109 | rtnl_unlock(); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static void |
| 115 | mac802154_del_iface(struct wpan_phy *phy, struct net_device *dev) |
| 116 | { |
| 117 | struct mac802154_sub_if_data *sdata; |
| 118 | ASSERT_RTNL(); |
| 119 | |
| 120 | sdata = netdev_priv(dev); |
| 121 | |
| 122 | BUG_ON(sdata->hw->phy != phy); |
| 123 | |
| 124 | mutex_lock(&sdata->hw->slaves_mtx); |
| 125 | list_del_rcu(&sdata->list); |
| 126 | mutex_unlock(&sdata->hw->slaves_mtx); |
| 127 | |
| 128 | synchronize_rcu(); |
| 129 | unregister_netdevice(sdata->dev); |
| 130 | } |
| 131 | |
| 132 | static struct net_device * |
| 133 | mac802154_add_iface(struct wpan_phy *phy, const char *name, int type) |
| 134 | { |
| 135 | struct net_device *dev; |
| 136 | int err = -ENOMEM; |
| 137 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 138 | switch (type) { |
alex.bluesman.smirnov@gmail.com | 0606069 | 2012-05-15 20:50:29 +0000 | [diff] [blame] | 139 | case IEEE802154_DEV_MONITOR: |
| 140 | dev = alloc_netdev(sizeof(struct mac802154_sub_if_data), |
| 141 | name, mac802154_monitor_setup); |
| 142 | break; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 143 | case IEEE802154_DEV_WPAN: |
| 144 | dev = alloc_netdev(sizeof(struct mac802154_sub_if_data), |
| 145 | name, mac802154_wpan_setup); |
| 146 | break; |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 147 | default: |
| 148 | dev = NULL; |
| 149 | err = -EINVAL; |
| 150 | break; |
| 151 | } |
| 152 | if (!dev) |
| 153 | goto err; |
| 154 | |
| 155 | err = mac802154_netdev_register(phy, dev); |
| 156 | if (err) |
| 157 | goto err_free; |
| 158 | |
| 159 | dev_hold(dev); /* we return an incremented device refcount */ |
| 160 | return dev; |
| 161 | |
| 162 | err_free: |
| 163 | free_netdev(dev); |
| 164 | err: |
| 165 | return ERR_PTR(err); |
| 166 | } |
| 167 | |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 168 | static int mac802154_set_txpower(struct wpan_phy *phy, int db) |
| 169 | { |
| 170 | struct mac802154_priv *priv = wpan_phy_priv(phy); |
| 171 | |
| 172 | if (!priv->ops->set_txpower) |
| 173 | return -ENOTSUPP; |
| 174 | |
| 175 | return priv->ops->set_txpower(&priv->hw, db); |
| 176 | } |
| 177 | |
Phoebe Buckheister | 84dda3c | 2014-02-17 11:34:10 +0100 | [diff] [blame] | 178 | static int mac802154_set_lbt(struct wpan_phy *phy, bool on) |
| 179 | { |
| 180 | struct mac802154_priv *priv = wpan_phy_priv(phy); |
| 181 | |
| 182 | if (!priv->ops->set_lbt) |
| 183 | return -ENOTSUPP; |
| 184 | |
| 185 | return priv->ops->set_lbt(&priv->hw, on); |
| 186 | } |
| 187 | |
Phoebe Buckheister | ba08fea | 2014-02-17 11:34:11 +0100 | [diff] [blame] | 188 | static int mac802154_set_cca_mode(struct wpan_phy *phy, u8 mode) |
| 189 | { |
| 190 | struct mac802154_priv *priv = wpan_phy_priv(phy); |
| 191 | |
| 192 | if (!priv->ops->set_cca_mode) |
| 193 | return -ENOTSUPP; |
| 194 | |
| 195 | return priv->ops->set_cca_mode(&priv->hw, mode); |
| 196 | } |
| 197 | |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 198 | static int mac802154_set_cca_ed_level(struct wpan_phy *phy, s32 level) |
| 199 | { |
| 200 | struct mac802154_priv *priv = wpan_phy_priv(phy); |
| 201 | |
| 202 | if (!priv->ops->set_cca_ed_level) |
| 203 | return -ENOTSUPP; |
| 204 | |
| 205 | return priv->ops->set_cca_ed_level(&priv->hw, level); |
| 206 | } |
| 207 | |
Phoebe Buckheister | 4244db1 | 2014-02-17 11:34:14 +0100 | [diff] [blame] | 208 | static int mac802154_set_csma_params(struct wpan_phy *phy, u8 min_be, |
| 209 | u8 max_be, u8 retries) |
| 210 | { |
| 211 | struct mac802154_priv *priv = wpan_phy_priv(phy); |
| 212 | |
| 213 | if (!priv->ops->set_csma_params) |
| 214 | return -ENOTSUPP; |
| 215 | |
| 216 | return priv->ops->set_csma_params(&priv->hw, min_be, max_be, retries); |
| 217 | } |
| 218 | |
| 219 | static int mac802154_set_frame_retries(struct wpan_phy *phy, s8 retries) |
| 220 | { |
| 221 | struct mac802154_priv *priv = wpan_phy_priv(phy); |
| 222 | |
| 223 | if (!priv->ops->set_frame_retries) |
| 224 | return -ENOTSUPP; |
| 225 | |
| 226 | return priv->ops->set_frame_retries(&priv->hw, retries); |
| 227 | } |
| 228 | |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 229 | struct ieee802154_dev * |
| 230 | ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops) |
| 231 | { |
| 232 | struct wpan_phy *phy; |
| 233 | struct mac802154_priv *priv; |
| 234 | size_t priv_size; |
| 235 | |
| 236 | if (!ops || !ops->xmit || !ops->ed || !ops->start || |
| 237 | !ops->stop || !ops->set_channel) { |
Chen Weilong | 83a1a7c | 2013-10-30 15:28:07 +0800 | [diff] [blame] | 238 | pr_err("undefined IEEE802.15.4 device operations\n"); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | /* Ensure 32-byte alignment of our private data and hw private data. |
| 243 | * We use the wpan_phy priv data for both our mac802154_priv and for |
| 244 | * the driver's private data |
| 245 | * |
| 246 | * in memory it'll be like this: |
| 247 | * |
| 248 | * +-----------------------+ |
| 249 | * | struct wpan_phy | |
| 250 | * +-----------------------+ |
| 251 | * | struct mac802154_priv | |
| 252 | * +-----------------------+ |
| 253 | * | driver's private data | |
| 254 | * +-----------------------+ |
| 255 | * |
| 256 | * Due to ieee802154 layer isn't aware of driver and MAC structures, |
| 257 | * so lets allign them here. |
| 258 | */ |
| 259 | |
| 260 | priv_size = ALIGN(sizeof(*priv), NETDEV_ALIGN) + priv_data_len; |
| 261 | |
| 262 | phy = wpan_phy_alloc(priv_size); |
| 263 | if (!phy) { |
Chen Weilong | 83a1a7c | 2013-10-30 15:28:07 +0800 | [diff] [blame] | 264 | pr_err("failure to allocate master IEEE802.15.4 device\n"); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 265 | return NULL; |
| 266 | } |
| 267 | |
| 268 | priv = wpan_phy_priv(phy); |
| 269 | priv->hw.phy = priv->phy = phy; |
| 270 | priv->hw.priv = (char *)priv + ALIGN(sizeof(*priv), NETDEV_ALIGN); |
| 271 | priv->ops = ops; |
| 272 | |
| 273 | INIT_LIST_HEAD(&priv->slaves); |
| 274 | mutex_init(&priv->slaves_mtx); |
| 275 | |
| 276 | return &priv->hw; |
| 277 | } |
| 278 | EXPORT_SYMBOL(ieee802154_alloc_device); |
| 279 | |
| 280 | void ieee802154_free_device(struct ieee802154_dev *hw) |
| 281 | { |
| 282 | struct mac802154_priv *priv = mac802154_to_priv(hw); |
| 283 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 284 | BUG_ON(!list_empty(&priv->slaves)); |
| 285 | |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 286 | mutex_destroy(&priv->slaves_mtx); |
Konstantin Khlebnikov | 1e9f954 | 2012-12-14 01:03:03 +0000 | [diff] [blame] | 287 | |
| 288 | wpan_phy_free(priv->phy); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 289 | } |
| 290 | EXPORT_SYMBOL(ieee802154_free_device); |
| 291 | |
| 292 | int ieee802154_register_device(struct ieee802154_dev *dev) |
| 293 | { |
| 294 | struct mac802154_priv *priv = mac802154_to_priv(dev); |
| 295 | int rc = -ENOMEM; |
| 296 | |
| 297 | priv->dev_workqueue = |
| 298 | create_singlethread_workqueue(wpan_phy_name(priv->phy)); |
| 299 | if (!priv->dev_workqueue) |
| 300 | goto out; |
| 301 | |
| 302 | wpan_phy_set_dev(priv->phy, priv->hw.parent); |
| 303 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 304 | priv->phy->add_iface = mac802154_add_iface; |
| 305 | priv->phy->del_iface = mac802154_del_iface; |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 306 | priv->phy->set_txpower = mac802154_set_txpower; |
Phoebe Buckheister | 84dda3c | 2014-02-17 11:34:10 +0100 | [diff] [blame] | 307 | priv->phy->set_lbt = mac802154_set_lbt; |
Phoebe Buckheister | ba08fea | 2014-02-17 11:34:11 +0100 | [diff] [blame] | 308 | priv->phy->set_cca_mode = mac802154_set_cca_mode; |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 309 | priv->phy->set_cca_ed_level = mac802154_set_cca_ed_level; |
Phoebe Buckheister | 4244db1 | 2014-02-17 11:34:14 +0100 | [diff] [blame] | 310 | priv->phy->set_csma_params = mac802154_set_csma_params; |
| 311 | priv->phy->set_frame_retries = mac802154_set_frame_retries; |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 312 | |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 313 | rc = wpan_phy_register(priv->phy); |
| 314 | if (rc < 0) |
| 315 | goto out_wq; |
| 316 | |
| 317 | rtnl_lock(); |
| 318 | |
| 319 | mutex_lock(&priv->slaves_mtx); |
| 320 | priv->running = MAC802154_DEVICE_RUN; |
| 321 | mutex_unlock(&priv->slaves_mtx); |
| 322 | |
| 323 | rtnl_unlock(); |
| 324 | |
| 325 | return 0; |
| 326 | |
| 327 | out_wq: |
| 328 | destroy_workqueue(priv->dev_workqueue); |
| 329 | out: |
| 330 | return rc; |
| 331 | } |
| 332 | EXPORT_SYMBOL(ieee802154_register_device); |
| 333 | |
| 334 | void ieee802154_unregister_device(struct ieee802154_dev *dev) |
| 335 | { |
| 336 | struct mac802154_priv *priv = mac802154_to_priv(dev); |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 337 | struct mac802154_sub_if_data *sdata, *next; |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 338 | |
| 339 | flush_workqueue(priv->dev_workqueue); |
| 340 | destroy_workqueue(priv->dev_workqueue); |
| 341 | |
| 342 | rtnl_lock(); |
| 343 | |
| 344 | mutex_lock(&priv->slaves_mtx); |
| 345 | priv->running = MAC802154_DEVICE_STOPPED; |
| 346 | mutex_unlock(&priv->slaves_mtx); |
| 347 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 348 | list_for_each_entry_safe(sdata, next, &priv->slaves, list) { |
| 349 | mutex_lock(&sdata->hw->slaves_mtx); |
| 350 | list_del(&sdata->list); |
| 351 | mutex_unlock(&sdata->hw->slaves_mtx); |
| 352 | |
| 353 | unregister_netdevice(sdata->dev); |
| 354 | } |
| 355 | |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 356 | rtnl_unlock(); |
| 357 | |
| 358 | wpan_phy_unregister(priv->phy); |
| 359 | } |
| 360 | EXPORT_SYMBOL(ieee802154_unregister_device); |
| 361 | |
| 362 | MODULE_DESCRIPTION("IEEE 802.15.4 implementation"); |
| 363 | MODULE_LICENSE("GPL v2"); |