Commit | Line | Data |
---|---|---|
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. | |
1010f540 | 17 | */ |
18 | ||
19 | #include <linux/kernel.h> | |
20 | #include <linux/module.h> | |
21 | #include <linux/netdevice.h> | |
22 | ||
62610ad2 | 23 | #include <net/netlink.h> |
24 | #include <linux/nl802154.h> | |
1010f540 | 25 | #include <net/mac802154.h> |
b70ab2e8 | 26 | #include <net/ieee802154_netdev.h> |
1010f540 | 27 | #include <net/route.h> |
5ad60d36 | 28 | #include <net/cfg802154.h> |
1010f540 | 29 | |
0f1556bc | 30 | #include "ieee802154_i.h" |
1010f540 | 31 | |
62610ad2 | 32 | int mac802154_slave_open(struct net_device *dev) |
33 | { | |
59d19cd7 | 34 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
036562f9 | 35 | struct ieee802154_sub_if_data *subif; |
04e850fe | 36 | struct ieee802154_local *local = sdata->local; |
62610ad2 | 37 | int res = 0; |
38 | ||
336908f6 PB |
39 | ASSERT_RTNL(); |
40 | ||
036562f9 | 41 | if (sdata->type == IEEE802154_DEV_WPAN) { |
d98be45b AA |
42 | mutex_lock(&sdata->local->iflist_mtx); |
43 | list_for_each_entry(subif, &sdata->local->interfaces, list) { | |
036562f9 | 44 | if (subif != sdata && subif->type == sdata->type && |
336908f6 | 45 | subif->running) { |
d98be45b | 46 | mutex_unlock(&sdata->local->iflist_mtx); |
336908f6 PB |
47 | return -EBUSY; |
48 | } | |
49 | } | |
d98be45b | 50 | mutex_unlock(&sdata->local->iflist_mtx); |
336908f6 PB |
51 | } |
52 | ||
d98be45b | 53 | mutex_lock(&sdata->local->iflist_mtx); |
036562f9 | 54 | sdata->running = true; |
d98be45b | 55 | mutex_unlock(&sdata->local->iflist_mtx); |
336908f6 | 56 | |
a5e1ec53 AA |
57 | if (local->open_count++ == 0) { |
58 | res = local->ops->start(&local->hw); | |
62610ad2 | 59 | WARN_ON(res); |
60 | if (res) | |
61 | goto err; | |
62 | } | |
63 | ||
62610ad2 | 64 | netif_start_queue(dev); |
65 | return 0; | |
66 | err: | |
04e850fe | 67 | sdata->local->open_count--; |
62610ad2 | 68 | |
69 | return res; | |
70 | } | |
71 | ||
72 | int mac802154_slave_close(struct net_device *dev) | |
73 | { | |
59d19cd7 | 74 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
04e850fe | 75 | struct ieee802154_local *local = sdata->local; |
62610ad2 | 76 | |
336908f6 PB |
77 | ASSERT_RTNL(); |
78 | ||
62610ad2 | 79 | netif_stop_queue(dev); |
80 | ||
d98be45b | 81 | mutex_lock(&sdata->local->iflist_mtx); |
036562f9 | 82 | sdata->running = false; |
d98be45b | 83 | mutex_unlock(&sdata->local->iflist_mtx); |
336908f6 | 84 | |
a5e1ec53 AA |
85 | if (!--local->open_count) |
86 | local->ops->stop(&local->hw); | |
62610ad2 | 87 | |
88 | return 0; | |
89 | } | |
90 | ||
91 | static int | |
92 | mac802154_netdev_register(struct wpan_phy *phy, struct net_device *dev) | |
93 | { | |
59d19cd7 | 94 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
a5e1ec53 | 95 | struct ieee802154_local *local; |
62610ad2 | 96 | int err; |
97 | ||
a5e1ec53 | 98 | local = wpan_phy_priv(phy); |
62610ad2 | 99 | |
036562f9 | 100 | sdata->dev = dev; |
04e850fe | 101 | sdata->local = local; |
62610ad2 | 102 | |
a5e1ec53 | 103 | dev->needed_headroom = local->hw.extra_tx_headroom; |
62610ad2 | 104 | |
a5e1ec53 | 105 | SET_NETDEV_DEV(dev, &local->phy->dev); |
62610ad2 | 106 | |
d98be45b | 107 | mutex_lock(&local->iflist_mtx); |
a5e1ec53 | 108 | if (!local->running) { |
d98be45b | 109 | mutex_unlock(&local->iflist_mtx); |
62610ad2 | 110 | return -ENODEV; |
111 | } | |
d98be45b | 112 | mutex_unlock(&local->iflist_mtx); |
62610ad2 | 113 | |
114 | err = register_netdev(dev); | |
115 | if (err < 0) | |
116 | return err; | |
117 | ||
118 | rtnl_lock(); | |
d98be45b AA |
119 | mutex_lock(&local->iflist_mtx); |
120 | list_add_tail_rcu(&sdata->list, &local->interfaces); | |
121 | mutex_unlock(&local->iflist_mtx); | |
62610ad2 | 122 | rtnl_unlock(); |
123 | ||
124 | return 0; | |
125 | } | |
126 | ||
127 | static void | |
128 | mac802154_del_iface(struct wpan_phy *phy, struct net_device *dev) | |
129 | { | |
59d19cd7 | 130 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
4710d806 | 131 | |
62610ad2 | 132 | ASSERT_RTNL(); |
62610ad2 | 133 | |
04e850fe | 134 | BUG_ON(sdata->local->phy != phy); |
62610ad2 | 135 | |
d98be45b | 136 | mutex_lock(&sdata->local->iflist_mtx); |
62610ad2 | 137 | list_del_rcu(&sdata->list); |
d98be45b | 138 | mutex_unlock(&sdata->local->iflist_mtx); |
62610ad2 | 139 | |
140 | synchronize_rcu(); | |
141 | unregister_netdevice(sdata->dev); | |
142 | } | |
143 | ||
144 | static struct net_device * | |
145 | mac802154_add_iface(struct wpan_phy *phy, const char *name, int type) | |
146 | { | |
147 | struct net_device *dev; | |
148 | int err = -ENOMEM; | |
149 | ||
62610ad2 | 150 | switch (type) { |
0606069d | 151 | case IEEE802154_DEV_MONITOR: |
036562f9 | 152 | dev = alloc_netdev(sizeof(struct ieee802154_sub_if_data), |
c835a677 TG |
153 | name, NET_NAME_UNKNOWN, |
154 | mac802154_monitor_setup); | |
0606069d | 155 | break; |
32bad7e3 | 156 | case IEEE802154_DEV_WPAN: |
036562f9 | 157 | dev = alloc_netdev(sizeof(struct ieee802154_sub_if_data), |
c835a677 TG |
158 | name, NET_NAME_UNKNOWN, |
159 | mac802154_wpan_setup); | |
32bad7e3 | 160 | break; |
62610ad2 | 161 | default: |
162 | dev = NULL; | |
163 | err = -EINVAL; | |
164 | break; | |
165 | } | |
166 | if (!dev) | |
167 | goto err; | |
168 | ||
169 | err = mac802154_netdev_register(phy, dev); | |
170 | if (err) | |
171 | goto err_free; | |
172 | ||
173 | dev_hold(dev); /* we return an incremented device refcount */ | |
174 | return dev; | |
175 | ||
176 | err_free: | |
177 | free_netdev(dev); | |
178 | err: | |
179 | return ERR_PTR(err); | |
180 | } | |
181 | ||
9b2777d6 PB |
182 | static int mac802154_set_txpower(struct wpan_phy *phy, int db) |
183 | { | |
a5e1ec53 | 184 | struct ieee802154_local *local = wpan_phy_priv(phy); |
9b2777d6 | 185 | |
a5e1ec53 | 186 | return local->ops->set_txpower(&local->hw, db); |
9b2777d6 PB |
187 | } |
188 | ||
84dda3c6 PB |
189 | static int mac802154_set_lbt(struct wpan_phy *phy, bool on) |
190 | { | |
a5e1ec53 | 191 | struct ieee802154_local *local = wpan_phy_priv(phy); |
84dda3c6 | 192 | |
a5e1ec53 | 193 | return local->ops->set_lbt(&local->hw, on); |
84dda3c6 PB |
194 | } |
195 | ||
ba08fea5 PB |
196 | static int mac802154_set_cca_mode(struct wpan_phy *phy, u8 mode) |
197 | { | |
a5e1ec53 | 198 | struct ieee802154_local *local = wpan_phy_priv(phy); |
ba08fea5 | 199 | |
a5e1ec53 | 200 | return local->ops->set_cca_mode(&local->hw, mode); |
ba08fea5 PB |
201 | } |
202 | ||
6ca00197 PB |
203 | static int mac802154_set_cca_ed_level(struct wpan_phy *phy, s32 level) |
204 | { | |
a5e1ec53 | 205 | struct ieee802154_local *local = wpan_phy_priv(phy); |
6ca00197 | 206 | |
a5e1ec53 | 207 | return local->ops->set_cca_ed_level(&local->hw, level); |
6ca00197 PB |
208 | } |
209 | ||
4244db1b PB |
210 | static int mac802154_set_csma_params(struct wpan_phy *phy, u8 min_be, |
211 | u8 max_be, u8 retries) | |
212 | { | |
a5e1ec53 | 213 | struct ieee802154_local *local = wpan_phy_priv(phy); |
4244db1b | 214 | |
a5e1ec53 | 215 | return local->ops->set_csma_params(&local->hw, min_be, max_be, retries); |
4244db1b PB |
216 | } |
217 | ||
218 | static int mac802154_set_frame_retries(struct wpan_phy *phy, s8 retries) | |
219 | { | |
a5e1ec53 | 220 | struct ieee802154_local *local = wpan_phy_priv(phy); |
4244db1b | 221 | |
a5e1ec53 | 222 | return local->ops->set_frame_retries(&local->hw, retries); |
4244db1b PB |
223 | } |
224 | ||
5a504397 AA |
225 | struct ieee802154_hw * |
226 | ieee802154_alloc_hw(size_t priv_data_len, struct ieee802154_ops *ops) | |
1010f540 | 227 | { |
228 | struct wpan_phy *phy; | |
a5e1ec53 | 229 | struct ieee802154_local *local; |
1010f540 | 230 | size_t priv_size; |
231 | ||
232 | if (!ops || !ops->xmit || !ops->ed || !ops->start || | |
233 | !ops->stop || !ops->set_channel) { | |
83a1a7ce | 234 | pr_err("undefined IEEE802.15.4 device operations\n"); |
1010f540 | 235 | return NULL; |
236 | } | |
237 | ||
238 | /* Ensure 32-byte alignment of our private data and hw private data. | |
a5e1ec53 | 239 | * We use the wpan_phy priv data for both our ieee802154_local and for |
1010f540 | 240 | * the driver's private data |
241 | * | |
242 | * in memory it'll be like this: | |
243 | * | |
a5e1ec53 AA |
244 | * +-------------------------+ |
245 | * | struct wpan_phy | | |
246 | * +-------------------------+ | |
247 | * | struct ieee802154_local | | |
248 | * +-------------------------+ | |
249 | * | driver's private data | | |
250 | * +-------------------------+ | |
1010f540 | 251 | * |
252 | * Due to ieee802154 layer isn't aware of driver and MAC structures, | |
139f14ad | 253 | * so lets align them here. |
1010f540 | 254 | */ |
255 | ||
a5e1ec53 | 256 | priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len; |
1010f540 | 257 | |
258 | phy = wpan_phy_alloc(priv_size); | |
259 | if (!phy) { | |
83a1a7ce | 260 | pr_err("failure to allocate master IEEE802.15.4 device\n"); |
1010f540 | 261 | return NULL; |
262 | } | |
263 | ||
a5e1ec53 AA |
264 | local = wpan_phy_priv(phy); |
265 | local->phy = phy; | |
266 | local->hw.phy = local->phy; | |
267 | local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN); | |
268 | local->ops = ops; | |
1010f540 | 269 | |
d98be45b AA |
270 | INIT_LIST_HEAD(&local->interfaces); |
271 | mutex_init(&local->iflist_mtx); | |
1010f540 | 272 | |
a5e1ec53 | 273 | return &local->hw; |
1010f540 | 274 | } |
5a504397 | 275 | EXPORT_SYMBOL(ieee802154_alloc_hw); |
1010f540 | 276 | |
5a504397 | 277 | void ieee802154_free_hw(struct ieee802154_hw *hw) |
1010f540 | 278 | { |
60741361 | 279 | struct ieee802154_local *local = hw_to_local(hw); |
1010f540 | 280 | |
d98be45b | 281 | BUG_ON(!list_empty(&local->interfaces)); |
62610ad2 | 282 | |
d98be45b | 283 | mutex_destroy(&local->iflist_mtx); |
1e9f9545 | 284 | |
a5e1ec53 | 285 | wpan_phy_free(local->phy); |
1010f540 | 286 | } |
5a504397 | 287 | EXPORT_SYMBOL(ieee802154_free_hw); |
1010f540 | 288 | |
5a504397 | 289 | int ieee802154_register_hw(struct ieee802154_hw *hw) |
1010f540 | 290 | { |
60741361 | 291 | struct ieee802154_local *local = hw_to_local(hw); |
640985ec AA |
292 | int rc = -ENOSYS; |
293 | ||
5a504397 | 294 | if (hw->flags & IEEE802154_HW_TXPOWER) { |
a5e1ec53 | 295 | if (!local->ops->set_txpower) |
640985ec AA |
296 | goto out; |
297 | ||
a5e1ec53 | 298 | local->phy->set_txpower = mac802154_set_txpower; |
640985ec AA |
299 | } |
300 | ||
5a504397 | 301 | if (hw->flags & IEEE802154_HW_LBT) { |
a5e1ec53 | 302 | if (!local->ops->set_lbt) |
640985ec AA |
303 | goto out; |
304 | ||
a5e1ec53 | 305 | local->phy->set_lbt = mac802154_set_lbt; |
640985ec AA |
306 | } |
307 | ||
5a504397 | 308 | if (hw->flags & IEEE802154_HW_CCA_MODE) { |
a5e1ec53 | 309 | if (!local->ops->set_cca_mode) |
640985ec AA |
310 | goto out; |
311 | ||
a5e1ec53 | 312 | local->phy->set_cca_mode = mac802154_set_cca_mode; |
640985ec AA |
313 | } |
314 | ||
5a504397 | 315 | if (hw->flags & IEEE802154_HW_CCA_ED_LEVEL) { |
a5e1ec53 | 316 | if (!local->ops->set_cca_ed_level) |
640985ec AA |
317 | goto out; |
318 | ||
a5e1ec53 | 319 | local->phy->set_cca_ed_level = mac802154_set_cca_ed_level; |
640985ec AA |
320 | } |
321 | ||
5a504397 | 322 | if (hw->flags & IEEE802154_HW_CSMA_PARAMS) { |
a5e1ec53 | 323 | if (!local->ops->set_csma_params) |
640985ec AA |
324 | goto out; |
325 | ||
a5e1ec53 | 326 | local->phy->set_csma_params = mac802154_set_csma_params; |
640985ec AA |
327 | } |
328 | ||
5a504397 | 329 | if (hw->flags & IEEE802154_HW_FRAME_RETRIES) { |
a5e1ec53 | 330 | if (!local->ops->set_frame_retries) |
640985ec AA |
331 | goto out; |
332 | ||
a5e1ec53 | 333 | local->phy->set_frame_retries = mac802154_set_frame_retries; |
640985ec | 334 | } |
1010f540 | 335 | |
f7730542 | 336 | local->workqueue = |
a5e1ec53 | 337 | create_singlethread_workqueue(wpan_phy_name(local->phy)); |
f7730542 | 338 | if (!local->workqueue) { |
640985ec | 339 | rc = -ENOMEM; |
1010f540 | 340 | goto out; |
640985ec | 341 | } |
1010f540 | 342 | |
a5e1ec53 | 343 | wpan_phy_set_dev(local->phy, local->hw.parent); |
1010f540 | 344 | |
a5e1ec53 AA |
345 | local->phy->add_iface = mac802154_add_iface; |
346 | local->phy->del_iface = mac802154_del_iface; | |
62610ad2 | 347 | |
a5e1ec53 | 348 | rc = wpan_phy_register(local->phy); |
1010f540 | 349 | if (rc < 0) |
350 | goto out_wq; | |
351 | ||
352 | rtnl_lock(); | |
353 | ||
d98be45b | 354 | mutex_lock(&local->iflist_mtx); |
a5e1ec53 | 355 | local->running = MAC802154_DEVICE_RUN; |
d98be45b | 356 | mutex_unlock(&local->iflist_mtx); |
1010f540 | 357 | |
358 | rtnl_unlock(); | |
359 | ||
360 | return 0; | |
361 | ||
362 | out_wq: | |
f7730542 | 363 | destroy_workqueue(local->workqueue); |
1010f540 | 364 | out: |
365 | return rc; | |
366 | } | |
5a504397 | 367 | EXPORT_SYMBOL(ieee802154_register_hw); |
1010f540 | 368 | |
5a504397 | 369 | void ieee802154_unregister_hw(struct ieee802154_hw *hw) |
1010f540 | 370 | { |
60741361 | 371 | struct ieee802154_local *local = hw_to_local(hw); |
036562f9 | 372 | struct ieee802154_sub_if_data *sdata, *next; |
1010f540 | 373 | |
f7730542 AA |
374 | flush_workqueue(local->workqueue); |
375 | destroy_workqueue(local->workqueue); | |
1010f540 | 376 | |
377 | rtnl_lock(); | |
378 | ||
d98be45b | 379 | mutex_lock(&local->iflist_mtx); |
a5e1ec53 | 380 | local->running = MAC802154_DEVICE_STOPPED; |
d98be45b | 381 | mutex_unlock(&local->iflist_mtx); |
1010f540 | 382 | |
d98be45b AA |
383 | list_for_each_entry_safe(sdata, next, &local->interfaces, list) { |
384 | mutex_lock(&sdata->local->iflist_mtx); | |
62610ad2 | 385 | list_del(&sdata->list); |
d98be45b | 386 | mutex_unlock(&sdata->local->iflist_mtx); |
62610ad2 | 387 | |
388 | unregister_netdevice(sdata->dev); | |
389 | } | |
390 | ||
1010f540 | 391 | rtnl_unlock(); |
392 | ||
a5e1ec53 | 393 | wpan_phy_unregister(local->phy); |
1010f540 | 394 | } |
5a504397 | 395 | EXPORT_SYMBOL(ieee802154_unregister_hw); |
1010f540 | 396 | |
397 | MODULE_DESCRIPTION("IEEE 802.15.4 implementation"); | |
398 | MODULE_LICENSE("GPL v2"); |