Linux Audio

Check our new training course

Loading...
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) 2007-2012 Siemens AG
  4 *
  5 * Written by:
  6 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
 
 
 
 
 
 
 
 
 
  7 */
  8
  9#include <linux/kernel.h>
 10#include <linux/module.h>
 11#include <linux/netdevice.h>
 12
 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>
 19
 20#include "ieee802154_i.h"
 21#include "cfg.h"
 22
 23static void ieee802154_tasklet_handler(struct tasklet_struct *t)
 24{
 25	struct ieee802154_local *local = from_tasklet(local, t, tasklet);
 26	struct sk_buff *skb;
 27
 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
 32			 * netstack.
 33			 */
 34			skb->pkt_type = 0;
 35			ieee802154_rx(local, skb);
 36			break;
 37		default:
 38			WARN(1, "mac802154: Packet is of unknown type %d\n",
 39			     skb->pkt_type);
 40			kfree_skb(skb);
 41			break;
 42		}
 43	}
 44}
 45
 46struct ieee802154_hw *
 47ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
 48{
 49	struct wpan_phy *phy;
 50	struct ieee802154_local *local;
 51	size_t priv_size;
 52
 53	if (WARN_ON(!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
 54		    !ops->start || !ops->stop || !ops->set_channel))
 55		return NULL;
 56
 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
 60	 *
 61	 * in memory it'll be like this:
 62	 *
 63	 * +-------------------------+
 64	 * | struct wpan_phy         |
 65	 * +-------------------------+
 66	 * | struct ieee802154_local |
 67	 * +-------------------------+
 68	 * | driver's private data   |
 69	 * +-------------------------+
 70	 *
 71	 * Due to ieee802154 layer isn't aware of driver and MAC structures,
 72	 * so lets align them here.
 73	 */
 74
 75	priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
 76
 77	phy = wpan_phy_new(&mac802154_config_ops, priv_size);
 78	if (!phy) {
 79		pr_err("failure to allocate master IEEE802.15.4 device\n");
 80		return NULL;
 81	}
 82
 83	phy->privid = mac802154_wpan_phy_privid;
 84
 85	local = wpan_phy_priv(phy);
 86	local->phy = phy;
 87	local->hw.phy = local->phy;
 88	local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
 89	local->ops = ops;
 90
 91	INIT_LIST_HEAD(&local->interfaces);
 92	mutex_init(&local->iflist_mtx);
 93
 94	tasklet_setup(&local->tasklet, ieee802154_tasklet_handler);
 
 
 95
 96	skb_queue_head_init(&local->skb_queue);
 97
 98	INIT_WORK(&local->tx_work, ieee802154_xmit_worker);
 99
100	/* init supported flags with 802.15.4 default ranges */
101	phy->supported.max_minbe = 8;
102	phy->supported.min_maxbe = 3;
103	phy->supported.max_maxbe = 8;
104	phy->supported.min_frame_retries = 0;
105	phy->supported.max_frame_retries = 7;
106	phy->supported.max_csma_backoffs = 5;
107	phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE;
108
109	/* always supported */
110	phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE);
111
112	return &local->hw;
113}
114EXPORT_SYMBOL(ieee802154_alloc_hw);
115
116void ieee802154_free_hw(struct ieee802154_hw *hw)
117{
118	struct ieee802154_local *local = hw_to_local(hw);
119
120	BUG_ON(!list_empty(&local->interfaces));
121
122	mutex_destroy(&local->iflist_mtx);
123
124	wpan_phy_free(local->phy);
125}
126EXPORT_SYMBOL(ieee802154_free_hw);
127
128static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy)
129{
130	/* TODO warn on empty symbol_duration
131	 * Should be done when all drivers sets this value.
132	 */
133
134	wpan_phy->lifs_period = IEEE802154_LIFS_PERIOD *
135				wpan_phy->symbol_duration;
136	wpan_phy->sifs_period = IEEE802154_SIFS_PERIOD *
137				wpan_phy->symbol_duration;
138}
139
140int ieee802154_register_hw(struct ieee802154_hw *hw)
141{
142	struct ieee802154_local *local = hw_to_local(hw);
143	struct net_device *dev;
144	int rc = -ENOSYS;
145
146	local->workqueue =
147		create_singlethread_workqueue(wpan_phy_name(local->phy));
148	if (!local->workqueue) {
149		rc = -ENOMEM;
150		goto out;
151	}
152
153	hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
154	local->ifs_timer.function = ieee802154_xmit_ifs_timer;
155
156	wpan_phy_set_dev(local->phy, local->hw.parent);
157
158	ieee802154_setup_wpan_phy_pib(local->phy);
159
160	if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) {
161		local->phy->supported.min_csma_backoffs = 4;
162		local->phy->supported.max_csma_backoffs = 4;
163		local->phy->supported.min_maxbe = 5;
164		local->phy->supported.max_maxbe = 5;
165		local->phy->supported.min_minbe = 3;
166		local->phy->supported.max_minbe = 3;
167	}
168
169	if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) {
170		local->phy->supported.min_frame_retries = 3;
171		local->phy->supported.max_frame_retries = 3;
172	}
173
174	if (hw->flags & IEEE802154_HW_PROMISCUOUS)
175		local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR);
176
177	rc = wpan_phy_register(local->phy);
178	if (rc < 0)
179		goto out_wq;
180
181	rtnl_lock();
182
183	dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM,
184				NL802154_IFTYPE_NODE,
185				cpu_to_le64(0x0000000000000000ULL));
186	if (IS_ERR(dev)) {
187		rtnl_unlock();
188		rc = PTR_ERR(dev);
189		goto out_phy;
190	}
191
192	rtnl_unlock();
193
194	return 0;
195
196out_phy:
197	wpan_phy_unregister(local->phy);
198out_wq:
199	destroy_workqueue(local->workqueue);
200out:
201	return rc;
202}
203EXPORT_SYMBOL(ieee802154_register_hw);
204
205void ieee802154_unregister_hw(struct ieee802154_hw *hw)
206{
207	struct ieee802154_local *local = hw_to_local(hw);
208
209	tasklet_kill(&local->tasklet);
210	flush_workqueue(local->workqueue);
211
212	rtnl_lock();
213
214	ieee802154_remove_interfaces(local);
215
216	rtnl_unlock();
217
218	destroy_workqueue(local->workqueue);
219	wpan_phy_unregister(local->phy);
220}
221EXPORT_SYMBOL(ieee802154_unregister_hw);
222
223static int __init ieee802154_init(void)
224{
225	return ieee802154_iface_init();
226}
227
228static void __exit ieee802154_exit(void)
229{
230	ieee802154_iface_exit();
231
232	rcu_barrier();
233}
234
235subsys_initcall(ieee802154_init);
236module_exit(ieee802154_exit);
237
238MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
239MODULE_LICENSE("GPL v2");
v4.10.11
 
  1/*
  2 * Copyright (C) 2007-2012 Siemens AG
  3 *
  4 * Written by:
  5 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  6 *
  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.
 15 */
 16
 17#include <linux/kernel.h>
 18#include <linux/module.h>
 19#include <linux/netdevice.h>
 20
 21#include <net/netlink.h>
 22#include <net/nl802154.h>
 23#include <net/mac802154.h>
 24#include <net/ieee802154_netdev.h>
 25#include <net/route.h>
 26#include <net/cfg802154.h>
 27
 28#include "ieee802154_i.h"
 29#include "cfg.h"
 30
 31static 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, 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
 54struct ieee802154_hw *
 55ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
 56{
 57	struct wpan_phy *phy;
 58	struct ieee802154_local *local;
 59	size_t priv_size;
 60
 61	if (WARN_ON(!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
 62		    !ops->start || !ops->stop || !ops->set_channel))
 63		return NULL;
 64
 65	/* Ensure 32-byte alignment of our private data and hw private data.
 66	 * We use the wpan_phy priv data for both our ieee802154_local and for
 67	 * the driver's private data
 68	 *
 69	 * in memory it'll be like this:
 70	 *
 71	 * +-------------------------+
 72	 * | struct wpan_phy         |
 73	 * +-------------------------+
 74	 * | struct ieee802154_local |
 75	 * +-------------------------+
 76	 * | driver's private data   |
 77	 * +-------------------------+
 78	 *
 79	 * Due to ieee802154 layer isn't aware of driver and MAC structures,
 80	 * so lets align them here.
 81	 */
 82
 83	priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
 84
 85	phy = wpan_phy_new(&mac802154_config_ops, priv_size);
 86	if (!phy) {
 87		pr_err("failure to allocate master IEEE802.15.4 device\n");
 88		return NULL;
 89	}
 90
 91	phy->privid = mac802154_wpan_phy_privid;
 92
 93	local = wpan_phy_priv(phy);
 94	local->phy = phy;
 95	local->hw.phy = local->phy;
 96	local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
 97	local->ops = ops;
 98
 99	INIT_LIST_HEAD(&local->interfaces);
100	mutex_init(&local->iflist_mtx);
101
102	tasklet_init(&local->tasklet,
103		     ieee802154_tasklet_handler,
104		     (unsigned long)local);
105
106	skb_queue_head_init(&local->skb_queue);
107
108	INIT_WORK(&local->tx_work, ieee802154_xmit_worker);
109
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 = 0;
115	phy->supported.max_frame_retries = 7;
116	phy->supported.max_csma_backoffs = 5;
117	phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE;
118
119	/* always supported */
120	phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE);
121
122	return &local->hw;
123}
124EXPORT_SYMBOL(ieee802154_alloc_hw);
125
126void ieee802154_free_hw(struct ieee802154_hw *hw)
127{
128	struct ieee802154_local *local = hw_to_local(hw);
129
130	BUG_ON(!list_empty(&local->interfaces));
131
132	mutex_destroy(&local->iflist_mtx);
133
134	wpan_phy_free(local->phy);
135}
136EXPORT_SYMBOL(ieee802154_free_hw);
137
138static 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
150int ieee802154_register_hw(struct ieee802154_hw *hw)
151{
152	struct ieee802154_local *local = hw_to_local(hw);
153	struct net_device *dev;
154	int rc = -ENOSYS;
155
156	local->workqueue =
157		create_singlethread_workqueue(wpan_phy_name(local->phy));
158	if (!local->workqueue) {
159		rc = -ENOMEM;
160		goto out;
161	}
162
163	hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
164	local->ifs_timer.function = ieee802154_xmit_ifs_timer;
165
166	wpan_phy_set_dev(local->phy, local->hw.parent);
167
168	ieee802154_setup_wpan_phy_pib(local->phy);
169
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		local->phy->supported.min_frame_retries = 3;
181		local->phy->supported.max_frame_retries = 3;
182	}
183
184	if (hw->flags & IEEE802154_HW_PROMISCUOUS)
185		local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR);
186
187	rc = wpan_phy_register(local->phy);
188	if (rc < 0)
189		goto out_wq;
190
191	rtnl_lock();
192
193	dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM,
194				NL802154_IFTYPE_NODE,
195				cpu_to_le64(0x0000000000000000ULL));
196	if (IS_ERR(dev)) {
197		rtnl_unlock();
198		rc = PTR_ERR(dev);
199		goto out_phy;
200	}
201
202	rtnl_unlock();
203
204	return 0;
205
206out_phy:
207	wpan_phy_unregister(local->phy);
208out_wq:
209	destroy_workqueue(local->workqueue);
210out:
211	return rc;
212}
213EXPORT_SYMBOL(ieee802154_register_hw);
214
215void ieee802154_unregister_hw(struct ieee802154_hw *hw)
216{
217	struct ieee802154_local *local = hw_to_local(hw);
218
219	tasklet_kill(&local->tasklet);
220	flush_workqueue(local->workqueue);
221
222	rtnl_lock();
223
224	ieee802154_remove_interfaces(local);
225
226	rtnl_unlock();
227
228	destroy_workqueue(local->workqueue);
229	wpan_phy_unregister(local->phy);
230}
231EXPORT_SYMBOL(ieee802154_unregister_hw);
232
233static int __init ieee802154_init(void)
234{
235	return ieee802154_iface_init();
236}
237
238static void __exit ieee802154_exit(void)
239{
240	ieee802154_iface_exit();
241
242	rcu_barrier();
243}
244
245subsys_initcall(ieee802154_init);
246module_exit(ieee802154_exit);
247
248MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
249MODULE_LICENSE("GPL v2");