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> |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 30 | #include <net/ieee802154_netdev.h> |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 31 | #include <net/route.h> |
| 32 | #include <net/wpan-phy.h> |
| 33 | |
| 34 | #include "mac802154.h" |
| 35 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 36 | int mac802154_slave_open(struct net_device *dev) |
| 37 | { |
| 38 | struct mac802154_sub_if_data *priv = netdev_priv(dev); |
Phoebe Buckheister | 336908f | 2014-03-31 21:37:45 +0200 | [diff] [blame] | 39 | struct mac802154_sub_if_data *subif; |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 40 | struct mac802154_priv *ipriv = priv->hw; |
| 41 | int res = 0; |
| 42 | |
Phoebe Buckheister | 336908f | 2014-03-31 21:37:45 +0200 | [diff] [blame] | 43 | ASSERT_RTNL(); |
| 44 | |
| 45 | if (priv->type == IEEE802154_DEV_WPAN) { |
| 46 | mutex_lock(&priv->hw->slaves_mtx); |
| 47 | list_for_each_entry(subif, &priv->hw->slaves, list) { |
| 48 | if (subif != priv && subif->type == priv->type && |
| 49 | subif->running) { |
| 50 | mutex_unlock(&priv->hw->slaves_mtx); |
| 51 | return -EBUSY; |
| 52 | } |
| 53 | } |
| 54 | mutex_unlock(&priv->hw->slaves_mtx); |
| 55 | } |
| 56 | |
| 57 | mutex_lock(&priv->hw->slaves_mtx); |
| 58 | priv->running = true; |
| 59 | mutex_unlock(&priv->hw->slaves_mtx); |
| 60 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 61 | if (ipriv->open_count++ == 0) { |
| 62 | res = ipriv->ops->start(&ipriv->hw); |
| 63 | WARN_ON(res); |
| 64 | if (res) |
| 65 | goto err; |
| 66 | } |
| 67 | |
| 68 | if (ipriv->ops->ieee_addr) { |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 69 | __le64 addr = ieee802154_devaddr_from_raw(dev->dev_addr); |
| 70 | |
| 71 | res = ipriv->ops->ieee_addr(&ipriv->hw, addr); |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 72 | WARN_ON(res); |
| 73 | if (res) |
| 74 | goto err; |
| 75 | mac802154_dev_set_ieee_addr(dev); |
| 76 | } |
| 77 | |
| 78 | netif_start_queue(dev); |
| 79 | return 0; |
| 80 | err: |
| 81 | priv->hw->open_count--; |
| 82 | |
| 83 | return res; |
| 84 | } |
| 85 | |
| 86 | int mac802154_slave_close(struct net_device *dev) |
| 87 | { |
| 88 | struct mac802154_sub_if_data *priv = netdev_priv(dev); |
| 89 | struct mac802154_priv *ipriv = priv->hw; |
| 90 | |
Phoebe Buckheister | 336908f | 2014-03-31 21:37:45 +0200 | [diff] [blame] | 91 | ASSERT_RTNL(); |
| 92 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 93 | netif_stop_queue(dev); |
| 94 | |
Phoebe Buckheister | 336908f | 2014-03-31 21:37:45 +0200 | [diff] [blame] | 95 | mutex_lock(&priv->hw->slaves_mtx); |
| 96 | priv->running = false; |
| 97 | mutex_unlock(&priv->hw->slaves_mtx); |
| 98 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 99 | if (!--ipriv->open_count) |
| 100 | ipriv->ops->stop(&ipriv->hw); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int |
| 106 | mac802154_netdev_register(struct wpan_phy *phy, struct net_device *dev) |
| 107 | { |
| 108 | struct mac802154_sub_if_data *priv; |
| 109 | struct mac802154_priv *ipriv; |
| 110 | int err; |
| 111 | |
| 112 | ipriv = wpan_phy_priv(phy); |
| 113 | |
| 114 | priv = netdev_priv(dev); |
| 115 | priv->dev = dev; |
| 116 | priv->hw = ipriv; |
| 117 | |
| 118 | dev->needed_headroom = ipriv->hw.extra_tx_headroom; |
| 119 | |
| 120 | SET_NETDEV_DEV(dev, &ipriv->phy->dev); |
| 121 | |
| 122 | mutex_lock(&ipriv->slaves_mtx); |
| 123 | if (!ipriv->running) { |
| 124 | mutex_unlock(&ipriv->slaves_mtx); |
| 125 | return -ENODEV; |
| 126 | } |
| 127 | mutex_unlock(&ipriv->slaves_mtx); |
| 128 | |
| 129 | err = register_netdev(dev); |
| 130 | if (err < 0) |
| 131 | return err; |
| 132 | |
| 133 | rtnl_lock(); |
| 134 | mutex_lock(&ipriv->slaves_mtx); |
| 135 | list_add_tail_rcu(&priv->list, &ipriv->slaves); |
| 136 | mutex_unlock(&ipriv->slaves_mtx); |
| 137 | rtnl_unlock(); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static void |
| 143 | mac802154_del_iface(struct wpan_phy *phy, struct net_device *dev) |
| 144 | { |
| 145 | struct mac802154_sub_if_data *sdata; |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 146 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 147 | ASSERT_RTNL(); |
| 148 | |
| 149 | sdata = netdev_priv(dev); |
| 150 | |
| 151 | BUG_ON(sdata->hw->phy != phy); |
| 152 | |
| 153 | mutex_lock(&sdata->hw->slaves_mtx); |
| 154 | list_del_rcu(&sdata->list); |
| 155 | mutex_unlock(&sdata->hw->slaves_mtx); |
| 156 | |
| 157 | synchronize_rcu(); |
| 158 | unregister_netdevice(sdata->dev); |
| 159 | } |
| 160 | |
| 161 | static struct net_device * |
| 162 | mac802154_add_iface(struct wpan_phy *phy, const char *name, int type) |
| 163 | { |
| 164 | struct net_device *dev; |
| 165 | int err = -ENOMEM; |
| 166 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 167 | switch (type) { |
alex.bluesman.smirnov@gmail.com | 0606069 | 2012-05-15 20:50:29 +0000 | [diff] [blame] | 168 | case IEEE802154_DEV_MONITOR: |
| 169 | dev = alloc_netdev(sizeof(struct mac802154_sub_if_data), |
| 170 | name, mac802154_monitor_setup); |
| 171 | break; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 172 | case IEEE802154_DEV_WPAN: |
| 173 | dev = alloc_netdev(sizeof(struct mac802154_sub_if_data), |
| 174 | name, mac802154_wpan_setup); |
| 175 | break; |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 176 | default: |
| 177 | dev = NULL; |
| 178 | err = -EINVAL; |
| 179 | break; |
| 180 | } |
| 181 | if (!dev) |
| 182 | goto err; |
| 183 | |
| 184 | err = mac802154_netdev_register(phy, dev); |
| 185 | if (err) |
| 186 | goto err_free; |
| 187 | |
| 188 | dev_hold(dev); /* we return an incremented device refcount */ |
| 189 | return dev; |
| 190 | |
| 191 | err_free: |
| 192 | free_netdev(dev); |
| 193 | err: |
| 194 | return ERR_PTR(err); |
| 195 | } |
| 196 | |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 197 | static int mac802154_set_txpower(struct wpan_phy *phy, int db) |
| 198 | { |
| 199 | struct mac802154_priv *priv = wpan_phy_priv(phy); |
| 200 | |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 201 | return priv->ops->set_txpower(&priv->hw, db); |
| 202 | } |
| 203 | |
Phoebe Buckheister | 84dda3c | 2014-02-17 11:34:10 +0100 | [diff] [blame] | 204 | static int mac802154_set_lbt(struct wpan_phy *phy, bool on) |
| 205 | { |
| 206 | struct mac802154_priv *priv = wpan_phy_priv(phy); |
| 207 | |
Phoebe Buckheister | 84dda3c | 2014-02-17 11:34:10 +0100 | [diff] [blame] | 208 | return priv->ops->set_lbt(&priv->hw, on); |
| 209 | } |
| 210 | |
Phoebe Buckheister | ba08fea | 2014-02-17 11:34:11 +0100 | [diff] [blame] | 211 | static int mac802154_set_cca_mode(struct wpan_phy *phy, u8 mode) |
| 212 | { |
| 213 | struct mac802154_priv *priv = wpan_phy_priv(phy); |
| 214 | |
Phoebe Buckheister | ba08fea | 2014-02-17 11:34:11 +0100 | [diff] [blame] | 215 | return priv->ops->set_cca_mode(&priv->hw, mode); |
| 216 | } |
| 217 | |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 218 | static int mac802154_set_cca_ed_level(struct wpan_phy *phy, s32 level) |
| 219 | { |
| 220 | struct mac802154_priv *priv = wpan_phy_priv(phy); |
| 221 | |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 222 | return priv->ops->set_cca_ed_level(&priv->hw, level); |
| 223 | } |
| 224 | |
Phoebe Buckheister | 4244db1 | 2014-02-17 11:34:14 +0100 | [diff] [blame] | 225 | static int mac802154_set_csma_params(struct wpan_phy *phy, u8 min_be, |
| 226 | u8 max_be, u8 retries) |
| 227 | { |
| 228 | struct mac802154_priv *priv = wpan_phy_priv(phy); |
| 229 | |
Phoebe Buckheister | 4244db1 | 2014-02-17 11:34:14 +0100 | [diff] [blame] | 230 | return priv->ops->set_csma_params(&priv->hw, min_be, max_be, retries); |
| 231 | } |
| 232 | |
| 233 | static int mac802154_set_frame_retries(struct wpan_phy *phy, s8 retries) |
| 234 | { |
| 235 | struct mac802154_priv *priv = wpan_phy_priv(phy); |
| 236 | |
Phoebe Buckheister | 4244db1 | 2014-02-17 11:34:14 +0100 | [diff] [blame] | 237 | return priv->ops->set_frame_retries(&priv->hw, retries); |
| 238 | } |
| 239 | |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 240 | struct ieee802154_dev * |
| 241 | ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops) |
| 242 | { |
| 243 | struct wpan_phy *phy; |
| 244 | struct mac802154_priv *priv; |
| 245 | size_t priv_size; |
| 246 | |
| 247 | if (!ops || !ops->xmit || !ops->ed || !ops->start || |
| 248 | !ops->stop || !ops->set_channel) { |
Chen Weilong | 83a1a7c | 2013-10-30 15:28:07 +0800 | [diff] [blame] | 249 | pr_err("undefined IEEE802.15.4 device operations\n"); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 250 | return NULL; |
| 251 | } |
| 252 | |
| 253 | /* Ensure 32-byte alignment of our private data and hw private data. |
| 254 | * We use the wpan_phy priv data for both our mac802154_priv and for |
| 255 | * the driver's private data |
| 256 | * |
| 257 | * in memory it'll be like this: |
| 258 | * |
| 259 | * +-----------------------+ |
| 260 | * | struct wpan_phy | |
| 261 | * +-----------------------+ |
| 262 | * | struct mac802154_priv | |
| 263 | * +-----------------------+ |
| 264 | * | driver's private data | |
| 265 | * +-----------------------+ |
| 266 | * |
| 267 | * Due to ieee802154 layer isn't aware of driver and MAC structures, |
| 268 | * so lets allign them here. |
| 269 | */ |
| 270 | |
| 271 | priv_size = ALIGN(sizeof(*priv), NETDEV_ALIGN) + priv_data_len; |
| 272 | |
| 273 | phy = wpan_phy_alloc(priv_size); |
| 274 | if (!phy) { |
Chen Weilong | 83a1a7c | 2013-10-30 15:28:07 +0800 | [diff] [blame] | 275 | 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] | 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | priv = wpan_phy_priv(phy); |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 280 | priv->phy = phy; |
| 281 | priv->hw.phy = priv->phy; |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 282 | priv->hw.priv = (char *)priv + ALIGN(sizeof(*priv), NETDEV_ALIGN); |
| 283 | priv->ops = ops; |
| 284 | |
| 285 | INIT_LIST_HEAD(&priv->slaves); |
| 286 | mutex_init(&priv->slaves_mtx); |
| 287 | |
| 288 | return &priv->hw; |
| 289 | } |
| 290 | EXPORT_SYMBOL(ieee802154_alloc_device); |
| 291 | |
| 292 | void ieee802154_free_device(struct ieee802154_dev *hw) |
| 293 | { |
| 294 | struct mac802154_priv *priv = mac802154_to_priv(hw); |
| 295 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 296 | BUG_ON(!list_empty(&priv->slaves)); |
| 297 | |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 298 | mutex_destroy(&priv->slaves_mtx); |
Konstantin Khlebnikov | 1e9f954 | 2012-12-14 01:03:03 +0000 | [diff] [blame] | 299 | |
| 300 | wpan_phy_free(priv->phy); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 301 | } |
| 302 | EXPORT_SYMBOL(ieee802154_free_device); |
| 303 | |
| 304 | int ieee802154_register_device(struct ieee802154_dev *dev) |
| 305 | { |
| 306 | struct mac802154_priv *priv = mac802154_to_priv(dev); |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame^] | 307 | int rc = -ENOSYS; |
| 308 | |
| 309 | if (dev->flags & IEEE802154_HW_TXPOWER) { |
| 310 | if (!priv->ops->set_txpower) |
| 311 | goto out; |
| 312 | |
| 313 | priv->phy->set_txpower = mac802154_set_txpower; |
| 314 | } |
| 315 | |
| 316 | if (dev->flags & IEEE802154_HW_LBT) { |
| 317 | if (!priv->ops->set_lbt) |
| 318 | goto out; |
| 319 | |
| 320 | priv->phy->set_lbt = mac802154_set_lbt; |
| 321 | } |
| 322 | |
| 323 | if (dev->flags & IEEE802154_HW_CCA_MODE) { |
| 324 | if (!priv->ops->set_cca_mode) |
| 325 | goto out; |
| 326 | |
| 327 | priv->phy->set_cca_mode = mac802154_set_cca_mode; |
| 328 | } |
| 329 | |
| 330 | if (dev->flags & IEEE802154_HW_CCA_ED_LEVEL) { |
| 331 | if (!priv->ops->set_cca_ed_level) |
| 332 | goto out; |
| 333 | |
| 334 | priv->phy->set_cca_ed_level = mac802154_set_cca_ed_level; |
| 335 | } |
| 336 | |
| 337 | if (dev->flags & IEEE802154_HW_CSMA_PARAMS) { |
| 338 | if (!priv->ops->set_csma_params) |
| 339 | goto out; |
| 340 | |
| 341 | priv->phy->set_csma_params = mac802154_set_csma_params; |
| 342 | } |
| 343 | |
| 344 | if (dev->flags & IEEE802154_HW_FRAME_RETRIES) { |
| 345 | if (!priv->ops->set_frame_retries) |
| 346 | goto out; |
| 347 | |
| 348 | priv->phy->set_frame_retries = mac802154_set_frame_retries; |
| 349 | } |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 350 | |
| 351 | priv->dev_workqueue = |
| 352 | create_singlethread_workqueue(wpan_phy_name(priv->phy)); |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame^] | 353 | if (!priv->dev_workqueue) { |
| 354 | rc = -ENOMEM; |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 355 | goto out; |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame^] | 356 | } |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 357 | |
| 358 | wpan_phy_set_dev(priv->phy, priv->hw.parent); |
| 359 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 360 | priv->phy->add_iface = mac802154_add_iface; |
| 361 | priv->phy->del_iface = mac802154_del_iface; |
| 362 | |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 363 | rc = wpan_phy_register(priv->phy); |
| 364 | if (rc < 0) |
| 365 | goto out_wq; |
| 366 | |
| 367 | rtnl_lock(); |
| 368 | |
| 369 | mutex_lock(&priv->slaves_mtx); |
| 370 | priv->running = MAC802154_DEVICE_RUN; |
| 371 | mutex_unlock(&priv->slaves_mtx); |
| 372 | |
| 373 | rtnl_unlock(); |
| 374 | |
| 375 | return 0; |
| 376 | |
| 377 | out_wq: |
| 378 | destroy_workqueue(priv->dev_workqueue); |
| 379 | out: |
| 380 | return rc; |
| 381 | } |
| 382 | EXPORT_SYMBOL(ieee802154_register_device); |
| 383 | |
| 384 | void ieee802154_unregister_device(struct ieee802154_dev *dev) |
| 385 | { |
| 386 | struct mac802154_priv *priv = mac802154_to_priv(dev); |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 387 | struct mac802154_sub_if_data *sdata, *next; |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 388 | |
| 389 | flush_workqueue(priv->dev_workqueue); |
| 390 | destroy_workqueue(priv->dev_workqueue); |
| 391 | |
| 392 | rtnl_lock(); |
| 393 | |
| 394 | mutex_lock(&priv->slaves_mtx); |
| 395 | priv->running = MAC802154_DEVICE_STOPPED; |
| 396 | mutex_unlock(&priv->slaves_mtx); |
| 397 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 398 | list_for_each_entry_safe(sdata, next, &priv->slaves, list) { |
| 399 | mutex_lock(&sdata->hw->slaves_mtx); |
| 400 | list_del(&sdata->list); |
| 401 | mutex_unlock(&sdata->hw->slaves_mtx); |
| 402 | |
| 403 | unregister_netdevice(sdata->dev); |
| 404 | } |
| 405 | |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 406 | rtnl_unlock(); |
| 407 | |
| 408 | wpan_phy_unregister(priv->phy); |
| 409 | } |
| 410 | EXPORT_SYMBOL(ieee802154_unregister_device); |
| 411 | |
| 412 | MODULE_DESCRIPTION("IEEE 802.15.4 implementation"); |
| 413 | MODULE_LICENSE("GPL v2"); |