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