blob: ddcd6ff8d39c26990a9e1a5224637be9106b2cf0 [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 *
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000015 */
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/netdevice.h>
20
alex.bluesman.smirnov@gmail.com62610ad2012-05-15 20:50:28 +000021#include <net/netlink.h>
Alexander Aring944742a2014-11-17 08:20:49 +010022#include <net/nl802154.h>
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000023#include <net/mac802154.h>
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010024#include <net/ieee802154_netdev.h>
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000025#include <net/route.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020026#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000027
Alexander Aring0f1556b2014-10-25 09:41:00 +020028#include "ieee802154_i.h"
Alexander Aring1201cd22014-11-02 04:18:36 +010029#include "cfg.h"
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000030
Alexander Aringc5c47e62014-10-27 17:13:30 +010031static void ieee802154_tasklet_handler(unsigned long data)
32{
33 struct ieee802154_local *local = (struct ieee802154_local *)data;
34 struct sk_buff *skb;
35
36 while ((skb = skb_dequeue(&local->skb_queue))) {
37 switch (skb->pkt_type) {
38 case IEEE802154_RX_MSG:
39 /* Clear skb->pkt_type in order to not confuse kernel
40 * netstack.
41 */
42 skb->pkt_type = 0;
43 ieee802154_rx(&local->hw, skb);
44 break;
45 default:
46 WARN(1, "mac802154: Packet is of unknown type %d\n",
47 skb->pkt_type);
48 kfree_skb(skb);
49 break;
50 }
51 }
52}
53
Alexander Aring5a504392014-10-25 17:16:34 +020054struct ieee802154_hw *
Alexander Aring16301862014-10-28 18:21:18 +010055ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000056{
57 struct wpan_phy *phy;
Alexander Aringa5e1ec52014-10-25 17:16:35 +020058 struct ieee802154_local *local;
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000059 size_t priv_size;
60
Alexander Aringed0a5dc2014-10-26 09:37:08 +010061 if (!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
62 !ops->start || !ops->stop || !ops->set_channel) {
Chen Weilong83a1a7c2013-10-30 15:28:07 +080063 pr_err("undefined IEEE802.15.4 device operations\n");
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000064 return NULL;
65 }
66
67 /* Ensure 32-byte alignment of our private data and hw private data.
Alexander Aringa5e1ec52014-10-25 17:16:35 +020068 * We use the wpan_phy priv data for both our ieee802154_local and for
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000069 * the driver's private data
70 *
71 * in memory it'll be like this:
72 *
Alexander Aringa5e1ec52014-10-25 17:16:35 +020073 * +-------------------------+
74 * | struct wpan_phy |
75 * +-------------------------+
76 * | struct ieee802154_local |
77 * +-------------------------+
78 * | driver's private data |
79 * +-------------------------+
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000080 *
81 * Due to ieee802154 layer isn't aware of driver and MAC structures,
Alexander Aring139f14a2014-10-25 05:25:08 +020082 * so lets align them here.
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000083 */
84
Alexander Aringa5e1ec52014-10-25 17:16:35 +020085 priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000086
Alexander Aringf6013792014-11-09 08:36:47 +010087 phy = wpan_phy_new(&mac802154_config_ops, priv_size);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000088 if (!phy) {
Chen Weilong83a1a7c2013-10-30 15:28:07 +080089 pr_err("failure to allocate master IEEE802.15.4 device\n");
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000090 return NULL;
91 }
92
Alexander Aring6322d502014-11-12 03:36:51 +010093 phy->privid = mac802154_wpan_phy_privid;
94
Alexander Aringa5e1ec52014-10-25 17:16:35 +020095 local = wpan_phy_priv(phy);
96 local->phy = phy;
97 local->hw.phy = local->phy;
98 local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
99 local->ops = ops;
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000100
Alexander Aringd98be452014-10-25 17:16:38 +0200101 INIT_LIST_HEAD(&local->interfaces);
102 mutex_init(&local->iflist_mtx);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000103
Alexander Aringc5c47e62014-10-27 17:13:30 +0100104 tasklet_init(&local->tasklet,
105 ieee802154_tasklet_handler,
106 (unsigned long)local);
107
108 skb_queue_head_init(&local->skb_queue);
109
Alexander Aringfea33182015-05-17 21:44:43 +0200110 /* init supported flags with 802.15.4 default ranges */
111 phy->supported.max_minbe = 8;
112 phy->supported.min_maxbe = 3;
113 phy->supported.max_maxbe = 8;
114 phy->supported.min_frame_retries = -1;
115 phy->supported.max_frame_retries = 7;
116 phy->supported.max_csma_backoffs = 5;
117 phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE;
118
Alexander Aringa5e1ec52014-10-25 17:16:35 +0200119 return &local->hw;
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000120}
Alexander Aring5a504392014-10-25 17:16:34 +0200121EXPORT_SYMBOL(ieee802154_alloc_hw);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000122
Alexander Aring5a504392014-10-25 17:16:34 +0200123void ieee802154_free_hw(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000124{
Alexander Aring60741362014-10-25 17:16:39 +0200125 struct ieee802154_local *local = hw_to_local(hw);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000126
Alexander Aringd98be452014-10-25 17:16:38 +0200127 BUG_ON(!list_empty(&local->interfaces));
alex.bluesman.smirnov@gmail.com62610ad2012-05-15 20:50:28 +0000128
Alexander Aringd98be452014-10-25 17:16:38 +0200129 mutex_destroy(&local->iflist_mtx);
Konstantin Khlebnikov1e9f9542012-12-14 01:03:03 +0000130
Alexander Aringa5e1ec52014-10-25 17:16:35 +0200131 wpan_phy_free(local->phy);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000132}
Alexander Aring5a504392014-10-25 17:16:34 +0200133EXPORT_SYMBOL(ieee802154_free_hw);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000134
Alexander Aring61f2dcb2014-11-12 19:51:56 +0100135static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy)
136{
137 /* TODO warn on empty symbol_duration
138 * Should be done when all drivers sets this value.
139 */
140
141 wpan_phy->lifs_period = IEEE802154_LIFS_PERIOD *
142 wpan_phy->symbol_duration;
143 wpan_phy->sifs_period = IEEE802154_SIFS_PERIOD *
144 wpan_phy->symbol_duration;
145}
146
Alexander Aring5a504392014-10-25 17:16:34 +0200147int ieee802154_register_hw(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000148{
Alexander Aring60741362014-10-25 17:16:39 +0200149 struct ieee802154_local *local = hw_to_local(hw);
Alexander Aringe4962a12014-11-05 20:51:19 +0100150 struct net_device *dev;
Alexander Aring640985e2014-07-03 00:20:43 +0200151 int rc = -ENOSYS;
152
Alexander Aringf7730542014-10-25 17:16:41 +0200153 local->workqueue =
Alexander Aringa5e1ec52014-10-25 17:16:35 +0200154 create_singlethread_workqueue(wpan_phy_name(local->phy));
Alexander Aringf7730542014-10-25 17:16:41 +0200155 if (!local->workqueue) {
Alexander Aring640985e2014-07-03 00:20:43 +0200156 rc = -ENOMEM;
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000157 goto out;
Alexander Aring640985e2014-07-03 00:20:43 +0200158 }
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000159
Alexander Aring61f2dcb2014-11-12 19:51:56 +0100160 hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
161 local->ifs_timer.function = ieee802154_xmit_ifs_timer;
162
Alexander Aringa5e1ec52014-10-25 17:16:35 +0200163 wpan_phy_set_dev(local->phy, local->hw.parent);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000164
Alexander Aring61f2dcb2014-11-12 19:51:56 +0100165 ieee802154_setup_wpan_phy_pib(local->phy);
166
Alexander Aringfea33182015-05-17 21:44:43 +0200167 if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) {
168 local->phy->supported.min_csma_backoffs = 4;
169 local->phy->supported.max_csma_backoffs = 4;
170 local->phy->supported.min_maxbe = 5;
171 local->phy->supported.max_maxbe = 5;
172 local->phy->supported.min_minbe = 3;
173 local->phy->supported.max_minbe = 3;
174 }
175
176 if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) {
177 /* TODO should be 3, but our default value is -1 which means
178 * no ARET handling.
179 */
180 local->phy->supported.min_frame_retries = -1;
181 local->phy->supported.max_frame_retries = -1;
182 }
183
Alexander Aringa5e1ec52014-10-25 17:16:35 +0200184 rc = wpan_phy_register(local->phy);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000185 if (rc < 0)
186 goto out_wq;
187
Alexander Aringe4962a12014-11-05 20:51:19 +0100188 rtnl_lock();
189
Varka Bhadram5b4a10392015-04-30 17:44:57 +0200190 dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM,
191 NL802154_IFTYPE_NODE,
Alexander Aring0e575472014-11-17 08:20:52 +0100192 cpu_to_le64(0x0000000000000000ULL));
Alexander Aringe4962a12014-11-05 20:51:19 +0100193 if (IS_ERR(dev)) {
194 rtnl_unlock();
195 rc = PTR_ERR(dev);
Alexander Aring2b4d4132015-04-30 17:44:53 +0200196 goto out_phy;
Alexander Aringe4962a12014-11-05 20:51:19 +0100197 }
198
199 rtnl_unlock();
200
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000201 return 0;
202
Alexander Aring2b4d4132015-04-30 17:44:53 +0200203out_phy:
204 wpan_phy_unregister(local->phy);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000205out_wq:
Alexander Aringf7730542014-10-25 17:16:41 +0200206 destroy_workqueue(local->workqueue);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000207out:
208 return rc;
209}
Alexander Aring5a504392014-10-25 17:16:34 +0200210EXPORT_SYMBOL(ieee802154_register_hw);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000211
Alexander Aring5a504392014-10-25 17:16:34 +0200212void ieee802154_unregister_hw(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000213{
Alexander Aring60741362014-10-25 17:16:39 +0200214 struct ieee802154_local *local = hw_to_local(hw);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000215
Alexander Aringc5c47e62014-10-27 17:13:30 +0100216 tasklet_kill(&local->tasklet);
Alexander Aringf7730542014-10-25 17:16:41 +0200217 flush_workqueue(local->workqueue);
218 destroy_workqueue(local->workqueue);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000219
220 rtnl_lock();
221
Alexander Aring592dfbfc2014-11-12 03:36:48 +0100222 ieee802154_remove_interfaces(local);
alex.bluesman.smirnov@gmail.com62610ad2012-05-15 20:50:28 +0000223
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000224 rtnl_unlock();
225
Alexander Aringa5e1ec52014-10-25 17:16:35 +0200226 wpan_phy_unregister(local->phy);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000227}
Alexander Aring5a504392014-10-25 17:16:34 +0200228EXPORT_SYMBOL(ieee802154_unregister_hw);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000229
Alexander Aringbe4fd8e2014-11-12 03:36:53 +0100230static int __init ieee802154_init(void)
231{
232 return ieee802154_iface_init();
233}
234
235static void __exit ieee802154_exit(void)
236{
237 ieee802154_iface_exit();
238
239 rcu_barrier();
240}
241
242subsys_initcall(ieee802154_init);
243module_exit(ieee802154_exit);
244
Alexander Aring912f67a2014-11-12 03:36:52 +0100245MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000246MODULE_LICENSE("GPL v2");