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