blob: 9f36760b6c3a128d110dceb2f2524ef75aa50faf [file] [log] [blame]
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +00001/*
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.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/netdevice.h>
26
alex.bluesman.smirnov@gmail.com62610ad2012-05-15 20:50:28 +000027#include <net/netlink.h>
28#include <linux/nl802154.h>
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000029#include <net/mac802154.h>
30#include <net/route.h>
31#include <net/wpan-phy.h>
32
33#include "mac802154.h"
34
alex.bluesman.smirnov@gmail.com62610ad2012-05-15 20:50:28 +000035int mac802154_slave_open(struct net_device *dev)
36{
37 struct mac802154_sub_if_data *priv = netdev_priv(dev);
38 struct mac802154_priv *ipriv = priv->hw;
39 int res = 0;
40
41 if (ipriv->open_count++ == 0) {
42 res = ipriv->ops->start(&ipriv->hw);
43 WARN_ON(res);
44 if (res)
45 goto err;
46 }
47
48 if (ipriv->ops->ieee_addr) {
49 res = ipriv->ops->ieee_addr(&ipriv->hw, dev->dev_addr);
50 WARN_ON(res);
51 if (res)
52 goto err;
53 mac802154_dev_set_ieee_addr(dev);
54 }
55
56 netif_start_queue(dev);
57 return 0;
58err:
59 priv->hw->open_count--;
60
61 return res;
62}
63
64int mac802154_slave_close(struct net_device *dev)
65{
66 struct mac802154_sub_if_data *priv = netdev_priv(dev);
67 struct mac802154_priv *ipriv = priv->hw;
68
69 netif_stop_queue(dev);
70
71 if (!--ipriv->open_count)
72 ipriv->ops->stop(&ipriv->hw);
73
74 return 0;
75}
76
77static int
78mac802154_netdev_register(struct wpan_phy *phy, struct net_device *dev)
79{
80 struct mac802154_sub_if_data *priv;
81 struct mac802154_priv *ipriv;
82 int err;
83
84 ipriv = wpan_phy_priv(phy);
85
86 priv = netdev_priv(dev);
87 priv->dev = dev;
88 priv->hw = ipriv;
89
90 dev->needed_headroom = ipriv->hw.extra_tx_headroom;
91
92 SET_NETDEV_DEV(dev, &ipriv->phy->dev);
93
94 mutex_lock(&ipriv->slaves_mtx);
95 if (!ipriv->running) {
96 mutex_unlock(&ipriv->slaves_mtx);
97 return -ENODEV;
98 }
99 mutex_unlock(&ipriv->slaves_mtx);
100
101 err = register_netdev(dev);
102 if (err < 0)
103 return err;
104
105 rtnl_lock();
106 mutex_lock(&ipriv->slaves_mtx);
107 list_add_tail_rcu(&priv->list, &ipriv->slaves);
108 mutex_unlock(&ipriv->slaves_mtx);
109 rtnl_unlock();
110
111 return 0;
112}
113
114static void
115mac802154_del_iface(struct wpan_phy *phy, struct net_device *dev)
116{
117 struct mac802154_sub_if_data *sdata;
118 ASSERT_RTNL();
119
120 sdata = netdev_priv(dev);
121
122 BUG_ON(sdata->hw->phy != phy);
123
124 mutex_lock(&sdata->hw->slaves_mtx);
125 list_del_rcu(&sdata->list);
126 mutex_unlock(&sdata->hw->slaves_mtx);
127
128 synchronize_rcu();
129 unregister_netdevice(sdata->dev);
130}
131
132static struct net_device *
133mac802154_add_iface(struct wpan_phy *phy, const char *name, int type)
134{
135 struct net_device *dev;
136 int err = -ENOMEM;
137
138 /* No devices is currently added */
139 switch (type) {
140 default:
141 dev = NULL;
142 err = -EINVAL;
143 break;
144 }
145 if (!dev)
146 goto err;
147
148 err = mac802154_netdev_register(phy, dev);
149 if (err)
150 goto err_free;
151
152 dev_hold(dev); /* we return an incremented device refcount */
153 return dev;
154
155err_free:
156 free_netdev(dev);
157err:
158 return ERR_PTR(err);
159}
160
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000161struct ieee802154_dev *
162ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops)
163{
164 struct wpan_phy *phy;
165 struct mac802154_priv *priv;
166 size_t priv_size;
167
168 if (!ops || !ops->xmit || !ops->ed || !ops->start ||
169 !ops->stop || !ops->set_channel) {
170 printk(KERN_ERR
171 "undefined IEEE802.15.4 device operations\n");
172 return NULL;
173 }
174
175 /* Ensure 32-byte alignment of our private data and hw private data.
176 * We use the wpan_phy priv data for both our mac802154_priv and for
177 * the driver's private data
178 *
179 * in memory it'll be like this:
180 *
181 * +-----------------------+
182 * | struct wpan_phy |
183 * +-----------------------+
184 * | struct mac802154_priv |
185 * +-----------------------+
186 * | driver's private data |
187 * +-----------------------+
188 *
189 * Due to ieee802154 layer isn't aware of driver and MAC structures,
190 * so lets allign them here.
191 */
192
193 priv_size = ALIGN(sizeof(*priv), NETDEV_ALIGN) + priv_data_len;
194
195 phy = wpan_phy_alloc(priv_size);
196 if (!phy) {
197 printk(KERN_ERR
198 "failure to allocate master IEEE802.15.4 device\n");
199 return NULL;
200 }
201
202 priv = wpan_phy_priv(phy);
203 priv->hw.phy = priv->phy = phy;
204 priv->hw.priv = (char *)priv + ALIGN(sizeof(*priv), NETDEV_ALIGN);
205 priv->ops = ops;
206
207 INIT_LIST_HEAD(&priv->slaves);
208 mutex_init(&priv->slaves_mtx);
209
210 return &priv->hw;
211}
212EXPORT_SYMBOL(ieee802154_alloc_device);
213
214void ieee802154_free_device(struct ieee802154_dev *hw)
215{
216 struct mac802154_priv *priv = mac802154_to_priv(hw);
217
alex.bluesman.smirnov@gmail.com62610ad2012-05-15 20:50:28 +0000218 BUG_ON(!list_empty(&priv->slaves));
219
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000220 wpan_phy_free(priv->phy);
221
222 mutex_destroy(&priv->slaves_mtx);
223}
224EXPORT_SYMBOL(ieee802154_free_device);
225
226int ieee802154_register_device(struct ieee802154_dev *dev)
227{
228 struct mac802154_priv *priv = mac802154_to_priv(dev);
229 int rc = -ENOMEM;
230
231 priv->dev_workqueue =
232 create_singlethread_workqueue(wpan_phy_name(priv->phy));
233 if (!priv->dev_workqueue)
234 goto out;
235
236 wpan_phy_set_dev(priv->phy, priv->hw.parent);
237
alex.bluesman.smirnov@gmail.com62610ad2012-05-15 20:50:28 +0000238 priv->phy->add_iface = mac802154_add_iface;
239 priv->phy->del_iface = mac802154_del_iface;
240
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000241 rc = wpan_phy_register(priv->phy);
242 if (rc < 0)
243 goto out_wq;
244
245 rtnl_lock();
246
247 mutex_lock(&priv->slaves_mtx);
248 priv->running = MAC802154_DEVICE_RUN;
249 mutex_unlock(&priv->slaves_mtx);
250
251 rtnl_unlock();
252
253 return 0;
254
255out_wq:
256 destroy_workqueue(priv->dev_workqueue);
257out:
258 return rc;
259}
260EXPORT_SYMBOL(ieee802154_register_device);
261
262void ieee802154_unregister_device(struct ieee802154_dev *dev)
263{
264 struct mac802154_priv *priv = mac802154_to_priv(dev);
alex.bluesman.smirnov@gmail.com62610ad2012-05-15 20:50:28 +0000265 struct mac802154_sub_if_data *sdata, *next;
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000266
267 flush_workqueue(priv->dev_workqueue);
268 destroy_workqueue(priv->dev_workqueue);
269
270 rtnl_lock();
271
272 mutex_lock(&priv->slaves_mtx);
273 priv->running = MAC802154_DEVICE_STOPPED;
274 mutex_unlock(&priv->slaves_mtx);
275
alex.bluesman.smirnov@gmail.com62610ad2012-05-15 20:50:28 +0000276 list_for_each_entry_safe(sdata, next, &priv->slaves, list) {
277 mutex_lock(&sdata->hw->slaves_mtx);
278 list_del(&sdata->list);
279 mutex_unlock(&sdata->hw->slaves_mtx);
280
281 unregister_netdevice(sdata->dev);
282 }
283
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000284 rtnl_unlock();
285
286 wpan_phy_unregister(priv->phy);
287}
288EXPORT_SYMBOL(ieee802154_unregister_device);
289
290MODULE_DESCRIPTION("IEEE 802.15.4 implementation");
291MODULE_LICENSE("GPL v2");