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 | * | |
1010f540 | 7 | * 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. | |
1010f540 | 15 | */ |
16 | ||
17 | #include <linux/kernel.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/netdevice.h> | |
20 | ||
62610ad2 | 21 | #include <net/netlink.h> |
944742a3 | 22 | #include <net/nl802154.h> |
1010f540 | 23 | #include <net/mac802154.h> |
b70ab2e8 | 24 | #include <net/ieee802154_netdev.h> |
1010f540 | 25 | #include <net/route.h> |
5ad60d36 | 26 | #include <net/cfg802154.h> |
1010f540 | 27 | |
0f1556bc | 28 | #include "ieee802154_i.h" |
1201cd22 | 29 | #include "cfg.h" |
1010f540 | 30 | |
c5c47e67 AA |
31 | static 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 | ||
5a504397 | 54 | struct ieee802154_hw * |
16301861 | 55 | ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops) |
1010f540 | 56 | { |
57 | struct wpan_phy *phy; | |
a5e1ec53 | 58 | struct ieee802154_local *local; |
1010f540 | 59 | size_t priv_size; |
60 | ||
ed0a5dce AA |
61 | if (!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed || |
62 | !ops->start || !ops->stop || !ops->set_channel) { | |
83a1a7ce | 63 | pr_err("undefined IEEE802.15.4 device operations\n"); |
1010f540 | 64 | return NULL; |
65 | } | |
66 | ||
67 | /* Ensure 32-byte alignment of our private data and hw private data. | |
a5e1ec53 | 68 | * We use the wpan_phy priv data for both our ieee802154_local and for |
1010f540 | 69 | * the driver's private data |
70 | * | |
71 | * in memory it'll be like this: | |
72 | * | |
a5e1ec53 AA |
73 | * +-------------------------+ |
74 | * | struct wpan_phy | | |
75 | * +-------------------------+ | |
76 | * | struct ieee802154_local | | |
77 | * +-------------------------+ | |
78 | * | driver's private data | | |
79 | * +-------------------------+ | |
1010f540 | 80 | * |
81 | * Due to ieee802154 layer isn't aware of driver and MAC structures, | |
139f14ad | 82 | * so lets align them here. |
1010f540 | 83 | */ |
84 | ||
a5e1ec53 | 85 | priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len; |
1010f540 | 86 | |
f601379f | 87 | phy = wpan_phy_new(&mac802154_config_ops, priv_size); |
1010f540 | 88 | if (!phy) { |
83a1a7ce | 89 | pr_err("failure to allocate master IEEE802.15.4 device\n"); |
1010f540 | 90 | return NULL; |
91 | } | |
92 | ||
6322d50d AA |
93 | phy->privid = mac802154_wpan_phy_privid; |
94 | ||
a5e1ec53 AA |
95 | 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; | |
1010f540 | 100 | |
d98be45b AA |
101 | INIT_LIST_HEAD(&local->interfaces); |
102 | mutex_init(&local->iflist_mtx); | |
1010f540 | 103 | |
c5c47e67 AA |
104 | tasklet_init(&local->tasklet, |
105 | ieee802154_tasklet_handler, | |
106 | (unsigned long)local); | |
107 | ||
108 | skb_queue_head_init(&local->skb_queue); | |
109 | ||
fea3318d AA |
110 | /* 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 | ||
65318680 AA |
119 | /* always supported */ |
120 | phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE); | |
121 | ||
a5e1ec53 | 122 | return &local->hw; |
1010f540 | 123 | } |
5a504397 | 124 | EXPORT_SYMBOL(ieee802154_alloc_hw); |
1010f540 | 125 | |
5a504397 | 126 | void ieee802154_free_hw(struct ieee802154_hw *hw) |
1010f540 | 127 | { |
60741361 | 128 | struct ieee802154_local *local = hw_to_local(hw); |
1010f540 | 129 | |
d98be45b | 130 | BUG_ON(!list_empty(&local->interfaces)); |
62610ad2 | 131 | |
d98be45b | 132 | mutex_destroy(&local->iflist_mtx); |
1e9f9545 | 133 | |
a5e1ec53 | 134 | wpan_phy_free(local->phy); |
1010f540 | 135 | } |
5a504397 | 136 | EXPORT_SYMBOL(ieee802154_free_hw); |
1010f540 | 137 | |
61f2dcba AA |
138 | static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy) |
139 | { | |
140 | /* TODO warn on empty symbol_duration | |
141 | * Should be done when all drivers sets this value. | |
142 | */ | |
143 | ||
144 | wpan_phy->lifs_period = IEEE802154_LIFS_PERIOD * | |
145 | wpan_phy->symbol_duration; | |
146 | wpan_phy->sifs_period = IEEE802154_SIFS_PERIOD * | |
147 | wpan_phy->symbol_duration; | |
148 | } | |
149 | ||
5a504397 | 150 | int ieee802154_register_hw(struct ieee802154_hw *hw) |
1010f540 | 151 | { |
60741361 | 152 | struct ieee802154_local *local = hw_to_local(hw); |
e4962a14 | 153 | struct net_device *dev; |
640985ec AA |
154 | int rc = -ENOSYS; |
155 | ||
f7730542 | 156 | local->workqueue = |
a5e1ec53 | 157 | create_singlethread_workqueue(wpan_phy_name(local->phy)); |
f7730542 | 158 | if (!local->workqueue) { |
640985ec | 159 | rc = -ENOMEM; |
1010f540 | 160 | goto out; |
640985ec | 161 | } |
1010f540 | 162 | |
61f2dcba AA |
163 | hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
164 | local->ifs_timer.function = ieee802154_xmit_ifs_timer; | |
165 | ||
a5e1ec53 | 166 | wpan_phy_set_dev(local->phy, local->hw.parent); |
1010f540 | 167 | |
61f2dcba AA |
168 | ieee802154_setup_wpan_phy_pib(local->phy); |
169 | ||
fea3318d AA |
170 | if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) { |
171 | local->phy->supported.min_csma_backoffs = 4; | |
172 | local->phy->supported.max_csma_backoffs = 4; | |
173 | local->phy->supported.min_maxbe = 5; | |
174 | local->phy->supported.max_maxbe = 5; | |
175 | local->phy->supported.min_minbe = 3; | |
176 | local->phy->supported.max_minbe = 3; | |
177 | } | |
178 | ||
179 | if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) { | |
180 | /* TODO should be 3, but our default value is -1 which means | |
181 | * no ARET handling. | |
182 | */ | |
183 | local->phy->supported.min_frame_retries = -1; | |
184 | local->phy->supported.max_frame_retries = -1; | |
185 | } | |
186 | ||
65318680 AA |
187 | if (hw->flags & IEEE802154_HW_PROMISCUOUS) |
188 | local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR); | |
189 | ||
a5e1ec53 | 190 | rc = wpan_phy_register(local->phy); |
1010f540 | 191 | if (rc < 0) |
192 | goto out_wq; | |
193 | ||
e4962a14 AA |
194 | rtnl_lock(); |
195 | ||
5b4a1039 VB |
196 | dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM, |
197 | NL802154_IFTYPE_NODE, | |
0e57547e | 198 | cpu_to_le64(0x0000000000000000ULL)); |
e4962a14 AA |
199 | if (IS_ERR(dev)) { |
200 | rtnl_unlock(); | |
201 | rc = PTR_ERR(dev); | |
2b4d413c | 202 | goto out_phy; |
e4962a14 AA |
203 | } |
204 | ||
205 | rtnl_unlock(); | |
206 | ||
1010f540 | 207 | return 0; |
208 | ||
2b4d413c AA |
209 | out_phy: |
210 | wpan_phy_unregister(local->phy); | |
1010f540 | 211 | out_wq: |
f7730542 | 212 | destroy_workqueue(local->workqueue); |
1010f540 | 213 | out: |
214 | return rc; | |
215 | } | |
5a504397 | 216 | EXPORT_SYMBOL(ieee802154_register_hw); |
1010f540 | 217 | |
5a504397 | 218 | void ieee802154_unregister_hw(struct ieee802154_hw *hw) |
1010f540 | 219 | { |
60741361 | 220 | struct ieee802154_local *local = hw_to_local(hw); |
1010f540 | 221 | |
c5c47e67 | 222 | tasklet_kill(&local->tasklet); |
f7730542 AA |
223 | flush_workqueue(local->workqueue); |
224 | destroy_workqueue(local->workqueue); | |
1010f540 | 225 | |
226 | rtnl_lock(); | |
227 | ||
592dfbfc | 228 | ieee802154_remove_interfaces(local); |
62610ad2 | 229 | |
1010f540 | 230 | rtnl_unlock(); |
231 | ||
a5e1ec53 | 232 | wpan_phy_unregister(local->phy); |
1010f540 | 233 | } |
5a504397 | 234 | EXPORT_SYMBOL(ieee802154_unregister_hw); |
1010f540 | 235 | |
be4fd8e5 AA |
236 | static int __init ieee802154_init(void) |
237 | { | |
238 | return ieee802154_iface_init(); | |
239 | } | |
240 | ||
241 | static void __exit ieee802154_exit(void) | |
242 | { | |
243 | ieee802154_iface_exit(); | |
244 | ||
245 | rcu_barrier(); | |
246 | } | |
247 | ||
248 | subsys_initcall(ieee802154_init); | |
249 | module_exit(ieee802154_exit); | |
250 | ||
912f67ae | 251 | MODULE_DESCRIPTION("IEEE 802.15.4 subsystem"); |
1010f540 | 252 | MODULE_LICENSE("GPL v2"); |