2 * Copyright 2007-2012 Siemens AG
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
19 * Sergey Lapin <slapin@ossfans.org>
20 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
21 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
24 #include <linux/if_arp.h>
26 #include <net/mac802154.h>
27 #include <net/wpan-phy.h>
29 #include "mac802154.h"
31 struct phy_chan_notify_work {
32 struct work_struct work;
33 struct net_device *dev;
36 struct hw_addr_filt_notify_work {
37 struct work_struct work;
38 struct net_device *dev;
39 unsigned long changed;
42 static struct mac802154_priv *mac802154_slave_get_priv(struct net_device *dev)
44 struct mac802154_sub_if_data *priv = netdev_priv(dev);
46 BUG_ON(dev->type != ARPHRD_IEEE802154);
51 static void hw_addr_notify(struct work_struct *work)
53 struct hw_addr_filt_notify_work *nw = container_of(work,
54 struct hw_addr_filt_notify_work, work);
55 struct mac802154_priv *hw = mac802154_slave_get_priv(nw->dev);
58 res = hw->ops->set_hw_addr_filt(&hw->hw,
62 pr_debug("failed changed mask %lx\n", nw->changed);
67 static void set_hw_addr_filt(struct net_device *dev, unsigned long changed)
69 struct mac802154_sub_if_data *priv = netdev_priv(dev);
70 struct hw_addr_filt_notify_work *work;
72 work = kzalloc(sizeof(*work), GFP_ATOMIC);
76 INIT_WORK(&work->work, hw_addr_notify);
78 work->changed = changed;
79 queue_work(priv->hw->dev_workqueue, &work->work);
82 void mac802154_dev_set_short_addr(struct net_device *dev, u16 val)
84 struct mac802154_sub_if_data *priv = netdev_priv(dev);
86 BUG_ON(dev->type != ARPHRD_IEEE802154);
88 spin_lock_bh(&priv->mib_lock);
89 priv->short_addr = val;
90 spin_unlock_bh(&priv->mib_lock);
92 if ((priv->hw->ops->set_hw_addr_filt) &&
93 (priv->hw->hw.hw_filt.short_addr != priv->short_addr)) {
94 priv->hw->hw.hw_filt.short_addr = priv->short_addr;
95 set_hw_addr_filt(dev, IEEE802515_AFILT_SADDR_CHANGED);
99 u16 mac802154_dev_get_short_addr(const struct net_device *dev)
101 struct mac802154_sub_if_data *priv = netdev_priv(dev);
104 BUG_ON(dev->type != ARPHRD_IEEE802154);
106 spin_lock_bh(&priv->mib_lock);
107 ret = priv->short_addr;
108 spin_unlock_bh(&priv->mib_lock);
113 void mac802154_dev_set_ieee_addr(struct net_device *dev)
115 struct mac802154_sub_if_data *priv = netdev_priv(dev);
116 struct mac802154_priv *mac = priv->hw;
118 if (mac->ops->set_hw_addr_filt &&
119 memcmp(mac->hw.hw_filt.ieee_addr,
120 dev->dev_addr, IEEE802154_ADDR_LEN)) {
121 memcpy(mac->hw.hw_filt.ieee_addr,
122 dev->dev_addr, IEEE802154_ADDR_LEN);
123 set_hw_addr_filt(dev, IEEE802515_AFILT_IEEEADDR_CHANGED);
127 u16 mac802154_dev_get_pan_id(const struct net_device *dev)
129 struct mac802154_sub_if_data *priv = netdev_priv(dev);
132 BUG_ON(dev->type != ARPHRD_IEEE802154);
134 spin_lock_bh(&priv->mib_lock);
136 spin_unlock_bh(&priv->mib_lock);
141 void mac802154_dev_set_pan_id(struct net_device *dev, u16 val)
143 struct mac802154_sub_if_data *priv = netdev_priv(dev);
145 BUG_ON(dev->type != ARPHRD_IEEE802154);
147 spin_lock_bh(&priv->mib_lock);
149 spin_unlock_bh(&priv->mib_lock);
151 if ((priv->hw->ops->set_hw_addr_filt) &&
152 (priv->hw->hw.hw_filt.pan_id != priv->pan_id)) {
153 priv->hw->hw.hw_filt.pan_id = priv->pan_id;
154 set_hw_addr_filt(dev, IEEE802515_AFILT_PANID_CHANGED);
158 u8 mac802154_dev_get_dsn(const struct net_device *dev)
160 struct mac802154_sub_if_data *priv = netdev_priv(dev);
162 BUG_ON(dev->type != ARPHRD_IEEE802154);
167 static void phy_chan_notify(struct work_struct *work)
169 struct phy_chan_notify_work *nw = container_of(work,
170 struct phy_chan_notify_work, work);
171 struct mac802154_priv *hw = mac802154_slave_get_priv(nw->dev);
172 struct mac802154_sub_if_data *priv = netdev_priv(nw->dev);
175 mutex_lock(&priv->hw->phy->pib_lock);
176 res = hw->ops->set_channel(&hw->hw, priv->page, priv->chan);
178 pr_debug("set_channel failed\n");
180 priv->hw->phy->current_channel = priv->chan;
181 priv->hw->phy->current_page = priv->page;
183 mutex_unlock(&priv->hw->phy->pib_lock);
188 void mac802154_dev_set_page_channel(struct net_device *dev, u8 page, u8 chan)
190 struct mac802154_sub_if_data *priv = netdev_priv(dev);
191 struct phy_chan_notify_work *work;
193 BUG_ON(dev->type != ARPHRD_IEEE802154);
195 spin_lock_bh(&priv->mib_lock);
198 spin_unlock_bh(&priv->mib_lock);
200 mutex_lock(&priv->hw->phy->pib_lock);
201 if (priv->hw->phy->current_channel != priv->chan ||
202 priv->hw->phy->current_page != priv->page) {
203 mutex_unlock(&priv->hw->phy->pib_lock);
205 work = kzalloc(sizeof(*work), GFP_ATOMIC);
209 INIT_WORK(&work->work, phy_chan_notify);
211 queue_work(priv->hw->dev_workqueue, &work->work);
213 mutex_unlock(&priv->hw->phy->pib_lock);