1010f540 |
1 | /* |
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 | |
62610ad2 |
27 | #include <net/netlink.h> |
28 | #include <linux/nl802154.h> |
1010f540 |
29 | #include <net/mac802154.h> |
30 | #include <net/route.h> |
31 | #include <net/wpan-phy.h> |
32 | |
33 | #include "mac802154.h" |
34 | |
62610ad2 |
35 | int 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; |
58 | err: |
59 | priv->hw->open_count--; |
60 | |
61 | return res; |
62 | } |
63 | |
64 | int 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 | |
77 | static int |
78 | mac802154_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 | |
114 | static void |
115 | mac802154_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 | |
132 | static struct net_device * |
133 | mac802154_add_iface(struct wpan_phy *phy, const char *name, int type) |
134 | { |
135 | struct net_device *dev; |
136 | int err = -ENOMEM; |
137 | |
62610ad2 |
138 | switch (type) { |
0606069d |
139 | case IEEE802154_DEV_MONITOR: |
140 | dev = alloc_netdev(sizeof(struct mac802154_sub_if_data), |
141 | name, mac802154_monitor_setup); |
142 | break; |
62610ad2 |
143 | default: |
144 | dev = NULL; |
145 | err = -EINVAL; |
146 | break; |
147 | } |
148 | if (!dev) |
149 | goto err; |
150 | |
151 | err = mac802154_netdev_register(phy, dev); |
152 | if (err) |
153 | goto err_free; |
154 | |
155 | dev_hold(dev); /* we return an incremented device refcount */ |
156 | return dev; |
157 | |
158 | err_free: |
159 | free_netdev(dev); |
160 | err: |
161 | return ERR_PTR(err); |
162 | } |
163 | |
1010f540 |
164 | struct ieee802154_dev * |
165 | ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops) |
166 | { |
167 | struct wpan_phy *phy; |
168 | struct mac802154_priv *priv; |
169 | size_t priv_size; |
170 | |
171 | if (!ops || !ops->xmit || !ops->ed || !ops->start || |
172 | !ops->stop || !ops->set_channel) { |
173 | printk(KERN_ERR |
174 | "undefined IEEE802.15.4 device operations\n"); |
175 | return NULL; |
176 | } |
177 | |
178 | /* Ensure 32-byte alignment of our private data and hw private data. |
179 | * We use the wpan_phy priv data for both our mac802154_priv and for |
180 | * the driver's private data |
181 | * |
182 | * in memory it'll be like this: |
183 | * |
184 | * +-----------------------+ |
185 | * | struct wpan_phy | |
186 | * +-----------------------+ |
187 | * | struct mac802154_priv | |
188 | * +-----------------------+ |
189 | * | driver's private data | |
190 | * +-----------------------+ |
191 | * |
192 | * Due to ieee802154 layer isn't aware of driver and MAC structures, |
193 | * so lets allign them here. |
194 | */ |
195 | |
196 | priv_size = ALIGN(sizeof(*priv), NETDEV_ALIGN) + priv_data_len; |
197 | |
198 | phy = wpan_phy_alloc(priv_size); |
199 | if (!phy) { |
200 | printk(KERN_ERR |
201 | "failure to allocate master IEEE802.15.4 device\n"); |
202 | return NULL; |
203 | } |
204 | |
205 | priv = wpan_phy_priv(phy); |
206 | priv->hw.phy = priv->phy = phy; |
207 | priv->hw.priv = (char *)priv + ALIGN(sizeof(*priv), NETDEV_ALIGN); |
208 | priv->ops = ops; |
209 | |
210 | INIT_LIST_HEAD(&priv->slaves); |
211 | mutex_init(&priv->slaves_mtx); |
212 | |
213 | return &priv->hw; |
214 | } |
215 | EXPORT_SYMBOL(ieee802154_alloc_device); |
216 | |
217 | void ieee802154_free_device(struct ieee802154_dev *hw) |
218 | { |
219 | struct mac802154_priv *priv = mac802154_to_priv(hw); |
220 | |
62610ad2 |
221 | BUG_ON(!list_empty(&priv->slaves)); |
222 | |
1010f540 |
223 | wpan_phy_free(priv->phy); |
224 | |
225 | mutex_destroy(&priv->slaves_mtx); |
226 | } |
227 | EXPORT_SYMBOL(ieee802154_free_device); |
228 | |
229 | int ieee802154_register_device(struct ieee802154_dev *dev) |
230 | { |
231 | struct mac802154_priv *priv = mac802154_to_priv(dev); |
232 | int rc = -ENOMEM; |
233 | |
234 | priv->dev_workqueue = |
235 | create_singlethread_workqueue(wpan_phy_name(priv->phy)); |
236 | if (!priv->dev_workqueue) |
237 | goto out; |
238 | |
239 | wpan_phy_set_dev(priv->phy, priv->hw.parent); |
240 | |
62610ad2 |
241 | priv->phy->add_iface = mac802154_add_iface; |
242 | priv->phy->del_iface = mac802154_del_iface; |
243 | |
1010f540 |
244 | rc = wpan_phy_register(priv->phy); |
245 | if (rc < 0) |
246 | goto out_wq; |
247 | |
248 | rtnl_lock(); |
249 | |
250 | mutex_lock(&priv->slaves_mtx); |
251 | priv->running = MAC802154_DEVICE_RUN; |
252 | mutex_unlock(&priv->slaves_mtx); |
253 | |
254 | rtnl_unlock(); |
255 | |
256 | return 0; |
257 | |
258 | out_wq: |
259 | destroy_workqueue(priv->dev_workqueue); |
260 | out: |
261 | return rc; |
262 | } |
263 | EXPORT_SYMBOL(ieee802154_register_device); |
264 | |
265 | void ieee802154_unregister_device(struct ieee802154_dev *dev) |
266 | { |
267 | struct mac802154_priv *priv = mac802154_to_priv(dev); |
62610ad2 |
268 | struct mac802154_sub_if_data *sdata, *next; |
1010f540 |
269 | |
270 | flush_workqueue(priv->dev_workqueue); |
271 | destroy_workqueue(priv->dev_workqueue); |
272 | |
273 | rtnl_lock(); |
274 | |
275 | mutex_lock(&priv->slaves_mtx); |
276 | priv->running = MAC802154_DEVICE_STOPPED; |
277 | mutex_unlock(&priv->slaves_mtx); |
278 | |
62610ad2 |
279 | list_for_each_entry_safe(sdata, next, &priv->slaves, list) { |
280 | mutex_lock(&sdata->hw->slaves_mtx); |
281 | list_del(&sdata->list); |
282 | mutex_unlock(&sdata->hw->slaves_mtx); |
283 | |
284 | unregister_netdevice(sdata->dev); |
285 | } |
286 | |
1010f540 |
287 | rtnl_unlock(); |
288 | |
289 | wpan_phy_unregister(priv->phy); |
290 | } |
291 | EXPORT_SYMBOL(ieee802154_unregister_device); |
292 | |
293 | MODULE_DESCRIPTION("IEEE 802.15.4 implementation"); |
294 | MODULE_LICENSE("GPL v2"); |