1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007-2012 Siemens AG
6 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
13 #include <net/netlink.h>
14 #include <net/nl802154.h>
15 #include <net/mac802154.h>
16 #include <net/ieee802154_netdev.h>
17 #include <net/route.h>
18 #include <net/cfg802154.h>
20 #include "ieee802154_i.h"
23 static void ieee802154_tasklet_handler(struct tasklet_struct *t)
25 struct ieee802154_local *local = from_tasklet(local, t, tasklet);
28 while ((skb = skb_dequeue(&local->skb_queue))) {
29 switch (skb->pkt_type) {
30 case IEEE802154_RX_MSG:
31 /* Clear skb->pkt_type in order to not confuse kernel
35 ieee802154_rx(local, skb);
38 WARN(1, "mac802154: Packet is of unknown type %d\n",
46 struct ieee802154_hw *
47 ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
50 struct ieee802154_local *local;
53 if (WARN_ON(!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
54 !ops->start || !ops->stop || !ops->set_channel))
57 /* Ensure 32-byte alignment of our private data and hw private data.
58 * We use the wpan_phy priv data for both our ieee802154_local and for
59 * the driver's private data
61 * in memory it'll be like this:
63 * +-------------------------+
65 * +-------------------------+
66 * | struct ieee802154_local |
67 * +-------------------------+
68 * | driver's private data |
69 * +-------------------------+
71 * Due to ieee802154 layer isn't aware of driver and MAC structures,
72 * so lets align them here.
75 priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
77 phy = wpan_phy_new(&mac802154_config_ops, priv_size);
79 pr_err("failure to allocate master IEEE802.15.4 device\n");
83 phy->privid = mac802154_wpan_phy_privid;
85 local = wpan_phy_priv(phy);
87 local->hw.phy = local->phy;
88 local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
91 INIT_LIST_HEAD(&local->interfaces);
92 INIT_LIST_HEAD(&local->rx_beacon_list);
93 INIT_LIST_HEAD(&local->rx_mac_cmd_list);
94 mutex_init(&local->iflist_mtx);
96 tasklet_setup(&local->tasklet, ieee802154_tasklet_handler);
98 skb_queue_head_init(&local->skb_queue);
100 INIT_WORK(&local->sync_tx_work, ieee802154_xmit_sync_worker);
101 INIT_DELAYED_WORK(&local->scan_work, mac802154_scan_worker);
102 INIT_WORK(&local->rx_beacon_work, mac802154_rx_beacon_worker);
103 INIT_DELAYED_WORK(&local->beacon_work, mac802154_beacon_worker);
104 INIT_WORK(&local->rx_mac_cmd_work, mac802154_rx_mac_cmd_worker);
106 /* init supported flags with 802.15.4 default ranges */
107 phy->supported.max_minbe = 8;
108 phy->supported.min_maxbe = 3;
109 phy->supported.max_maxbe = 8;
110 phy->supported.min_frame_retries = 0;
111 phy->supported.max_frame_retries = 7;
112 phy->supported.max_csma_backoffs = 5;
113 phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE;
115 /* always supported */
116 phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE) | BIT(NL802154_IFTYPE_COORD);
120 EXPORT_SYMBOL(ieee802154_alloc_hw);
122 void ieee802154_configure_durations(struct wpan_phy *phy,
123 unsigned int page, unsigned int channel)
129 if (BIT(channel) & 0x1)
130 /* 868 MHz BPSK 802.15.4-2003: 20 ksym/s */
131 duration = 50 * NSEC_PER_USEC;
132 else if (BIT(channel) & 0x7FE)
133 /* 915 MHz BPSK 802.15.4-2003: 40 ksym/s */
134 duration = 25 * NSEC_PER_USEC;
135 else if (BIT(channel) & 0x7FFF800)
136 /* 2400 MHz O-QPSK 802.15.4-2006: 62.5 ksym/s */
137 duration = 16 * NSEC_PER_USEC;
140 if (BIT(channel) & 0x1)
141 /* 868 MHz O-QPSK 802.15.4-2006: 25 ksym/s */
142 duration = 40 * NSEC_PER_USEC;
143 else if (BIT(channel) & 0x7FE)
144 /* 915 MHz O-QPSK 802.15.4-2006: 62.5 ksym/s */
145 duration = 16 * NSEC_PER_USEC;
148 if (BIT(channel) & 0x3FFF)
149 /* 2.4 GHz CSS 802.15.4a-2007: 1/6 Msym/s */
150 duration = 6 * NSEC_PER_USEC;
157 pr_debug("Unknown PHY symbol duration\n");
161 phy->symbol_duration = duration;
162 phy->lifs_period = (IEEE802154_LIFS_PERIOD * phy->symbol_duration) / NSEC_PER_SEC;
163 phy->sifs_period = (IEEE802154_SIFS_PERIOD * phy->symbol_duration) / NSEC_PER_SEC;
165 EXPORT_SYMBOL(ieee802154_configure_durations);
167 void ieee802154_free_hw(struct ieee802154_hw *hw)
169 struct ieee802154_local *local = hw_to_local(hw);
171 BUG_ON(!list_empty(&local->interfaces));
173 mutex_destroy(&local->iflist_mtx);
175 wpan_phy_free(local->phy);
177 EXPORT_SYMBOL(ieee802154_free_hw);
179 static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy)
181 /* TODO warn on empty symbol_duration
182 * Should be done when all drivers sets this value.
185 wpan_phy->lifs_period =
186 (IEEE802154_LIFS_PERIOD * wpan_phy->symbol_duration) / 1000;
187 wpan_phy->sifs_period =
188 (IEEE802154_SIFS_PERIOD * wpan_phy->symbol_duration) / 1000;
191 int ieee802154_register_hw(struct ieee802154_hw *hw)
193 struct ieee802154_local *local = hw_to_local(hw);
194 char mac_wq_name[IFNAMSIZ + 10] = {};
195 struct net_device *dev;
199 create_singlethread_workqueue(wpan_phy_name(local->phy));
200 if (!local->workqueue) {
205 snprintf(mac_wq_name, IFNAMSIZ + 10, "%s-mac-cmds", wpan_phy_name(local->phy));
206 local->mac_wq = create_singlethread_workqueue(mac_wq_name);
207 if (!local->mac_wq) {
212 hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
213 local->ifs_timer.function = ieee802154_xmit_ifs_timer;
215 wpan_phy_set_dev(local->phy, local->hw.parent);
217 ieee802154_setup_wpan_phy_pib(local->phy);
219 ieee802154_configure_durations(local->phy, local->phy->current_page,
220 local->phy->current_channel);
222 if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) {
223 local->phy->supported.min_csma_backoffs = 4;
224 local->phy->supported.max_csma_backoffs = 4;
225 local->phy->supported.min_maxbe = 5;
226 local->phy->supported.max_maxbe = 5;
227 local->phy->supported.min_minbe = 3;
228 local->phy->supported.max_minbe = 3;
231 if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) {
232 local->phy->supported.min_frame_retries = 3;
233 local->phy->supported.max_frame_retries = 3;
236 if (hw->flags & IEEE802154_HW_PROMISCUOUS)
237 local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR);
239 rc = wpan_phy_register(local->phy);
245 dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM,
246 NL802154_IFTYPE_NODE,
247 cpu_to_le64(0x0000000000000000ULL));
259 wpan_phy_unregister(local->phy);
261 destroy_workqueue(local->mac_wq);
263 destroy_workqueue(local->workqueue);
267 EXPORT_SYMBOL(ieee802154_register_hw);
269 void ieee802154_unregister_hw(struct ieee802154_hw *hw)
271 struct ieee802154_local *local = hw_to_local(hw);
273 tasklet_kill(&local->tasklet);
274 flush_workqueue(local->workqueue);
278 ieee802154_remove_interfaces(local);
282 destroy_workqueue(local->mac_wq);
283 destroy_workqueue(local->workqueue);
284 wpan_phy_unregister(local->phy);
286 EXPORT_SYMBOL(ieee802154_unregister_hw);
288 static int __init ieee802154_init(void)
290 return ieee802154_iface_init();
293 static void __exit ieee802154_exit(void)
295 ieee802154_iface_exit();
300 subsys_initcall(ieee802154_init);
301 module_exit(ieee802154_exit);
303 MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
304 MODULE_LICENSE("GPL v2");