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/ieee802154_netdev.h>
28 #include <net/wpan-phy.h>
29 #include <net/ieee802154_netdev.h>
31 #include "mac802154.h"
33 struct phy_chan_notify_work {
34 struct work_struct work;
35 struct net_device *dev;
38 struct hw_addr_filt_notify_work {
39 struct work_struct work;
40 struct net_device *dev;
41 unsigned long changed;
44 static struct mac802154_priv *mac802154_slave_get_priv(struct net_device *dev)
46 struct mac802154_sub_if_data *priv = netdev_priv(dev);
48 BUG_ON(dev->type != ARPHRD_IEEE802154);
53 static void hw_addr_notify(struct work_struct *work)
55 struct hw_addr_filt_notify_work *nw = container_of(work,
56 struct hw_addr_filt_notify_work, work);
57 struct mac802154_priv *hw = mac802154_slave_get_priv(nw->dev);
60 res = hw->ops->set_hw_addr_filt(&hw->hw,
64 pr_debug("failed changed mask %lx\n", nw->changed);
69 static void set_hw_addr_filt(struct net_device *dev, unsigned long changed)
71 struct mac802154_sub_if_data *priv = netdev_priv(dev);
72 struct hw_addr_filt_notify_work *work;
74 work = kzalloc(sizeof(*work), GFP_ATOMIC);
78 INIT_WORK(&work->work, hw_addr_notify);
80 work->changed = changed;
81 queue_work(priv->hw->dev_workqueue, &work->work);
84 void mac802154_dev_set_short_addr(struct net_device *dev, __le16 val)
86 struct mac802154_sub_if_data *priv = netdev_priv(dev);
88 BUG_ON(dev->type != ARPHRD_IEEE802154);
90 spin_lock_bh(&priv->mib_lock);
91 priv->short_addr = val;
92 spin_unlock_bh(&priv->mib_lock);
94 if ((priv->hw->ops->set_hw_addr_filt) &&
95 (priv->hw->hw.hw_filt.short_addr != priv->short_addr)) {
96 priv->hw->hw.hw_filt.short_addr = priv->short_addr;
97 set_hw_addr_filt(dev, IEEE802515_AFILT_SADDR_CHANGED);
101 __le16 mac802154_dev_get_short_addr(const struct net_device *dev)
103 struct mac802154_sub_if_data *priv = netdev_priv(dev);
106 BUG_ON(dev->type != ARPHRD_IEEE802154);
108 spin_lock_bh(&priv->mib_lock);
109 ret = priv->short_addr;
110 spin_unlock_bh(&priv->mib_lock);
115 void mac802154_dev_set_ieee_addr(struct net_device *dev)
117 struct mac802154_sub_if_data *priv = netdev_priv(dev);
118 struct mac802154_priv *mac = priv->hw;
120 priv->extended_addr = ieee802154_devaddr_from_raw(dev->dev_addr);
122 if (mac->ops->set_hw_addr_filt &&
123 mac->hw.hw_filt.ieee_addr != priv->extended_addr) {
124 mac->hw.hw_filt.ieee_addr = priv->extended_addr;
125 set_hw_addr_filt(dev, IEEE802515_AFILT_IEEEADDR_CHANGED);
129 __le16 mac802154_dev_get_pan_id(const struct net_device *dev)
131 struct mac802154_sub_if_data *priv = netdev_priv(dev);
134 BUG_ON(dev->type != ARPHRD_IEEE802154);
136 spin_lock_bh(&priv->mib_lock);
138 spin_unlock_bh(&priv->mib_lock);
143 void mac802154_dev_set_pan_id(struct net_device *dev, __le16 val)
145 struct mac802154_sub_if_data *priv = netdev_priv(dev);
147 BUG_ON(dev->type != ARPHRD_IEEE802154);
149 spin_lock_bh(&priv->mib_lock);
151 spin_unlock_bh(&priv->mib_lock);
153 if ((priv->hw->ops->set_hw_addr_filt) &&
154 (priv->hw->hw.hw_filt.pan_id != priv->pan_id)) {
155 priv->hw->hw.hw_filt.pan_id = priv->pan_id;
156 set_hw_addr_filt(dev, IEEE802515_AFILT_PANID_CHANGED);
160 u8 mac802154_dev_get_dsn(const struct net_device *dev)
162 struct mac802154_sub_if_data *priv = netdev_priv(dev);
164 BUG_ON(dev->type != ARPHRD_IEEE802154);
169 static void phy_chan_notify(struct work_struct *work)
171 struct phy_chan_notify_work *nw = container_of(work,
172 struct phy_chan_notify_work, work);
173 struct mac802154_priv *hw = mac802154_slave_get_priv(nw->dev);
174 struct mac802154_sub_if_data *priv = netdev_priv(nw->dev);
177 mutex_lock(&priv->hw->phy->pib_lock);
178 res = hw->ops->set_channel(&hw->hw, priv->page, priv->chan);
180 pr_debug("set_channel failed\n");
182 priv->hw->phy->current_channel = priv->chan;
183 priv->hw->phy->current_page = priv->page;
185 mutex_unlock(&priv->hw->phy->pib_lock);
190 void mac802154_dev_set_page_channel(struct net_device *dev, u8 page, u8 chan)
192 struct mac802154_sub_if_data *priv = netdev_priv(dev);
193 struct phy_chan_notify_work *work;
195 BUG_ON(dev->type != ARPHRD_IEEE802154);
197 spin_lock_bh(&priv->mib_lock);
200 spin_unlock_bh(&priv->mib_lock);
202 mutex_lock(&priv->hw->phy->pib_lock);
203 if (priv->hw->phy->current_channel != priv->chan ||
204 priv->hw->phy->current_page != priv->page) {
205 mutex_unlock(&priv->hw->phy->pib_lock);
207 work = kzalloc(sizeof(*work), GFP_ATOMIC);
211 INIT_WORK(&work->work, phy_chan_notify);
213 queue_work(priv->hw->dev_workqueue, &work->work);
215 mutex_unlock(&priv->hw->phy->pib_lock);