blob: f0991f2344d403f3b1ed3a1f44fc2aebf50c72b4 [file] [log] [blame]
alex.bluesman.smirnov@gmail.comef2486f2012-05-15 20:50:26 +00001/*
2 * Copyright 2007-2012 Siemens AG
3 *
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.
7 *
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.
12 *
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.
16 *
17 * Written by:
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>
22 */
23
24#include <linux/if_arp.h>
25
26#include <net/mac802154.h>
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010027#include <net/ieee802154_netdev.h>
alex.bluesman.smirnov@gmail.comef2486f2012-05-15 20:50:26 +000028#include <net/wpan-phy.h>
29
30#include "mac802154.h"
31
alex.bluesman.smirnov@gmail.com66b69d42012-06-25 23:24:51 +000032struct phy_chan_notify_work {
33 struct work_struct work;
34 struct net_device *dev;
35};
36
alex.bluesman.smirnov@gmail.comef2486f2012-05-15 20:50:26 +000037struct hw_addr_filt_notify_work {
38 struct work_struct work;
39 struct net_device *dev;
40 unsigned long changed;
41};
42
alex.bluesman.smirnov@gmail.com42884042012-07-10 21:22:46 +000043static struct mac802154_priv *mac802154_slave_get_priv(struct net_device *dev)
alex.bluesman.smirnov@gmail.comef2486f2012-05-15 20:50:26 +000044{
45 struct mac802154_sub_if_data *priv = netdev_priv(dev);
46
47 BUG_ON(dev->type != ARPHRD_IEEE802154);
48
49 return priv->hw;
50}
51
52static void hw_addr_notify(struct work_struct *work)
53{
54 struct hw_addr_filt_notify_work *nw = container_of(work,
55 struct hw_addr_filt_notify_work, work);
56 struct mac802154_priv *hw = mac802154_slave_get_priv(nw->dev);
57 int res;
58
59 res = hw->ops->set_hw_addr_filt(&hw->hw,
60 &hw->hw.hw_filt,
61 nw->changed);
62 if (res)
63 pr_debug("failed changed mask %lx\n", nw->changed);
64
65 kfree(nw);
alex.bluesman.smirnov@gmail.comef2486f2012-05-15 20:50:26 +000066}
67
68static void set_hw_addr_filt(struct net_device *dev, unsigned long changed)
69{
70 struct mac802154_sub_if_data *priv = netdev_priv(dev);
71 struct hw_addr_filt_notify_work *work;
72
73 work = kzalloc(sizeof(*work), GFP_ATOMIC);
74 if (!work)
75 return;
76
77 INIT_WORK(&work->work, hw_addr_notify);
78 work->dev = dev;
79 work->changed = changed;
80 queue_work(priv->hw->dev_workqueue, &work->work);
alex.bluesman.smirnov@gmail.comef2486f2012-05-15 20:50:26 +000081}
82
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010083void mac802154_dev_set_short_addr(struct net_device *dev, __le16 val)
alex.bluesman.smirnov@gmail.com48e44d52012-06-25 23:24:50 +000084{
85 struct mac802154_sub_if_data *priv = netdev_priv(dev);
86
87 BUG_ON(dev->type != ARPHRD_IEEE802154);
88
89 spin_lock_bh(&priv->mib_lock);
90 priv->short_addr = val;
91 spin_unlock_bh(&priv->mib_lock);
92
93 if ((priv->hw->ops->set_hw_addr_filt) &&
94 (priv->hw->hw.hw_filt.short_addr != priv->short_addr)) {
95 priv->hw->hw.hw_filt.short_addr = priv->short_addr;
96 set_hw_addr_filt(dev, IEEE802515_AFILT_SADDR_CHANGED);
97 }
98}
99
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100100__le16 mac802154_dev_get_short_addr(const struct net_device *dev)
alex.bluesman.smirnov@gmail.come885a472012-07-10 21:22:44 +0000101{
102 struct mac802154_sub_if_data *priv = netdev_priv(dev);
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100103 __le16 ret;
alex.bluesman.smirnov@gmail.come885a472012-07-10 21:22:44 +0000104
105 BUG_ON(dev->type != ARPHRD_IEEE802154);
106
107 spin_lock_bh(&priv->mib_lock);
108 ret = priv->short_addr;
109 spin_unlock_bh(&priv->mib_lock);
110
111 return ret;
112}
113
alex.bluesman.smirnov@gmail.comef2486f2012-05-15 20:50:26 +0000114void mac802154_dev_set_ieee_addr(struct net_device *dev)
115{
116 struct mac802154_sub_if_data *priv = netdev_priv(dev);
117 struct mac802154_priv *mac = priv->hw;
118
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100119 priv->extended_addr = ieee802154_devaddr_from_raw(dev->dev_addr);
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100120
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100121 if (mac->ops->set_hw_addr_filt &&
122 mac->hw.hw_filt.ieee_addr != priv->extended_addr) {
123 mac->hw.hw_filt.ieee_addr = priv->extended_addr;
alex.bluesman.smirnov@gmail.comef2486f2012-05-15 20:50:26 +0000124 set_hw_addr_filt(dev, IEEE802515_AFILT_IEEEADDR_CHANGED);
125 }
126}
alex.bluesman.smirnov@gmail.comdcbe4f92012-06-25 23:24:49 +0000127
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100128__le16 mac802154_dev_get_pan_id(const struct net_device *dev)
alex.bluesman.smirnov@gmail.comdcbe4f92012-06-25 23:24:49 +0000129{
130 struct mac802154_sub_if_data *priv = netdev_priv(dev);
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100131 __le16 ret;
alex.bluesman.smirnov@gmail.comdcbe4f92012-06-25 23:24:49 +0000132
133 BUG_ON(dev->type != ARPHRD_IEEE802154);
134
135 spin_lock_bh(&priv->mib_lock);
136 ret = priv->pan_id;
137 spin_unlock_bh(&priv->mib_lock);
138
139 return ret;
140}
141
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100142void mac802154_dev_set_pan_id(struct net_device *dev, __le16 val)
alex.bluesman.smirnov@gmail.comdcbe4f92012-06-25 23:24:49 +0000143{
144 struct mac802154_sub_if_data *priv = netdev_priv(dev);
145
146 BUG_ON(dev->type != ARPHRD_IEEE802154);
147
148 spin_lock_bh(&priv->mib_lock);
149 priv->pan_id = val;
150 spin_unlock_bh(&priv->mib_lock);
151
152 if ((priv->hw->ops->set_hw_addr_filt) &&
153 (priv->hw->hw.hw_filt.pan_id != priv->pan_id)) {
154 priv->hw->hw.hw_filt.pan_id = priv->pan_id;
155 set_hw_addr_filt(dev, IEEE802515_AFILT_PANID_CHANGED);
156 }
157}
alex.bluesman.smirnov@gmail.com66b69d42012-06-25 23:24:51 +0000158
Tony Cheneau04835462013-03-25 17:59:29 +0000159u8 mac802154_dev_get_dsn(const struct net_device *dev)
160{
161 struct mac802154_sub_if_data *priv = netdev_priv(dev);
162
163 BUG_ON(dev->type != ARPHRD_IEEE802154);
164
165 return priv->dsn++;
166}
167
alex.bluesman.smirnov@gmail.com66b69d42012-06-25 23:24:51 +0000168static void phy_chan_notify(struct work_struct *work)
169{
170 struct phy_chan_notify_work *nw = container_of(work,
171 struct phy_chan_notify_work, work);
172 struct mac802154_priv *hw = mac802154_slave_get_priv(nw->dev);
173 struct mac802154_sub_if_data *priv = netdev_priv(nw->dev);
174 int res;
175
Alan Ott9f7f78b2013-04-05 13:03:10 +0000176 mutex_lock(&priv->hw->phy->pib_lock);
alex.bluesman.smirnov@gmail.com66b69d42012-06-25 23:24:51 +0000177 res = hw->ops->set_channel(&hw->hw, priv->page, priv->chan);
178 if (res)
179 pr_debug("set_channel failed\n");
Alan Ott9f7f78b2013-04-05 13:03:10 +0000180 else {
181 priv->hw->phy->current_channel = priv->chan;
182 priv->hw->phy->current_page = priv->page;
183 }
184 mutex_unlock(&priv->hw->phy->pib_lock);
alex.bluesman.smirnov@gmail.com66b69d42012-06-25 23:24:51 +0000185
186 kfree(nw);
187}
188
189void mac802154_dev_set_page_channel(struct net_device *dev, u8 page, u8 chan)
190{
191 struct mac802154_sub_if_data *priv = netdev_priv(dev);
192 struct phy_chan_notify_work *work;
193
194 BUG_ON(dev->type != ARPHRD_IEEE802154);
195
196 spin_lock_bh(&priv->mib_lock);
197 priv->page = page;
198 priv->chan = chan;
199 spin_unlock_bh(&priv->mib_lock);
200
Alan Ott9f7f78b2013-04-05 13:03:10 +0000201 mutex_lock(&priv->hw->phy->pib_lock);
alex.bluesman.smirnov@gmail.com66b69d42012-06-25 23:24:51 +0000202 if (priv->hw->phy->current_channel != priv->chan ||
203 priv->hw->phy->current_page != priv->page) {
Alan Ott9f7f78b2013-04-05 13:03:10 +0000204 mutex_unlock(&priv->hw->phy->pib_lock);
205
alex.bluesman.smirnov@gmail.com66b69d42012-06-25 23:24:51 +0000206 work = kzalloc(sizeof(*work), GFP_ATOMIC);
207 if (!work)
208 return;
209
210 INIT_WORK(&work->work, phy_chan_notify);
211 work->dev = dev;
212 queue_work(priv->hw->dev_workqueue, &work->work);
Alan Ott9f7f78b2013-04-05 13:03:10 +0000213 } else
214 mutex_unlock(&priv->hw->phy->pib_lock);
alex.bluesman.smirnov@gmail.com66b69d42012-06-25 23:24:51 +0000215}