ieee802154: add header structs with endiannes and operations
[linux-2.6-block.git] / net / mac802154 / mib.c
CommitLineData
ef2486f5 1/*
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>
b70ab2e8 27#include <net/ieee802154_netdev.h>
ef2486f5 28#include <net/wpan-phy.h>
29
30#include "mac802154.h"
31
66b69d4d 32struct phy_chan_notify_work {
33 struct work_struct work;
34 struct net_device *dev;
35};
36
ef2486f5 37struct hw_addr_filt_notify_work {
38 struct work_struct work;
39 struct net_device *dev;
40 unsigned long changed;
41};
42
42884042 43static struct mac802154_priv *mac802154_slave_get_priv(struct net_device *dev)
ef2486f5 44{
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);
ef2486f5 66}
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);
ef2486f5 81}
82
b70ab2e8 83void mac802154_dev_set_short_addr(struct net_device *dev, __le16 val)
48e44d50 84{
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
b70ab2e8 100__le16 mac802154_dev_get_short_addr(const struct net_device *dev)
e885a47a 101{
102 struct mac802154_sub_if_data *priv = netdev_priv(dev);
b70ab2e8 103 __le16 ret;
e885a47a 104
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
ef2486f5 114void 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;
b70ab2e8 118 __le64 addr;
ef2486f5 119
b70ab2e8
PB
120 addr = ieee802154_devaddr_from_raw(dev->dev_addr);
121 priv->extended_addr = addr;
122
123 if (mac->ops->set_hw_addr_filt && mac->hw.hw_filt.ieee_addr != addr) {
124 mac->hw.hw_filt.ieee_addr = addr;
ef2486f5 125 set_hw_addr_filt(dev, IEEE802515_AFILT_IEEEADDR_CHANGED);
126 }
127}
dcbe4f93 128
b70ab2e8 129__le16 mac802154_dev_get_pan_id(const struct net_device *dev)
dcbe4f93 130{
131 struct mac802154_sub_if_data *priv = netdev_priv(dev);
b70ab2e8 132 __le16 ret;
dcbe4f93 133
134 BUG_ON(dev->type != ARPHRD_IEEE802154);
135
136 spin_lock_bh(&priv->mib_lock);
137 ret = priv->pan_id;
138 spin_unlock_bh(&priv->mib_lock);
139
140 return ret;
141}
142
b70ab2e8 143void mac802154_dev_set_pan_id(struct net_device *dev, __le16 val)
dcbe4f93 144{
145 struct mac802154_sub_if_data *priv = netdev_priv(dev);
146
147 BUG_ON(dev->type != ARPHRD_IEEE802154);
148
149 spin_lock_bh(&priv->mib_lock);
150 priv->pan_id = val;
151 spin_unlock_bh(&priv->mib_lock);
152
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);
157 }
158}
66b69d4d 159
0483546a
TC
160u8 mac802154_dev_get_dsn(const struct net_device *dev)
161{
162 struct mac802154_sub_if_data *priv = netdev_priv(dev);
163
164 BUG_ON(dev->type != ARPHRD_IEEE802154);
165
166 return priv->dsn++;
167}
168
66b69d4d 169static void phy_chan_notify(struct work_struct *work)
170{
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);
175 int res;
176
9f7f78b4 177 mutex_lock(&priv->hw->phy->pib_lock);
66b69d4d 178 res = hw->ops->set_channel(&hw->hw, priv->page, priv->chan);
179 if (res)
180 pr_debug("set_channel failed\n");
9f7f78b4
AO
181 else {
182 priv->hw->phy->current_channel = priv->chan;
183 priv->hw->phy->current_page = priv->page;
184 }
185 mutex_unlock(&priv->hw->phy->pib_lock);
66b69d4d 186
187 kfree(nw);
188}
189
190void mac802154_dev_set_page_channel(struct net_device *dev, u8 page, u8 chan)
191{
192 struct mac802154_sub_if_data *priv = netdev_priv(dev);
193 struct phy_chan_notify_work *work;
194
195 BUG_ON(dev->type != ARPHRD_IEEE802154);
196
197 spin_lock_bh(&priv->mib_lock);
198 priv->page = page;
199 priv->chan = chan;
200 spin_unlock_bh(&priv->mib_lock);
201
9f7f78b4 202 mutex_lock(&priv->hw->phy->pib_lock);
66b69d4d 203 if (priv->hw->phy->current_channel != priv->chan ||
204 priv->hw->phy->current_page != priv->page) {
9f7f78b4
AO
205 mutex_unlock(&priv->hw->phy->pib_lock);
206
66b69d4d 207 work = kzalloc(sizeof(*work), GFP_ATOMIC);
208 if (!work)
209 return;
210
211 INIT_WORK(&work->work, phy_chan_notify);
212 work->dev = dev;
213 queue_work(priv->hw->dev_workqueue, &work->work);
9f7f78b4
AO
214 } else
215 mutex_unlock(&priv->hw->phy->pib_lock);
66b69d4d 216}