Skip to content

Commit 1010f54

Browse files
alex-bluesmandavem330
authored andcommitted
mac802154: allocation of ieee802154 device
An interface to allocate and register ieee802154 compatible device. The allocated device has the following representation in memory: +-----------------------+ | struct wpan_phy | +-----------------------+ | struct mac802154_priv | +-----------------------+ | driver's private data | +-----------------------+ Used by device drivers to register new instance in the stack. Signed-off-by: Alexander Smirnov <alex.bluesman.smirnov@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 0afd7ad commit 1010f54

7 files changed

Lines changed: 239 additions & 0 deletions

File tree

‎include/net/mac802154.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,11 @@ struct ieee802154_ops {
123123
u8 addr[IEEE802154_ADDR_LEN]);
124124
};
125125

126+
/* Basic interface to register ieee802154 device */
127+
struct ieee802154_dev *
128+
ieee802154_alloc_device(size_t priv_data_lex, struct ieee802154_ops *ops);
129+
void ieee802154_free_device(struct ieee802154_dev *dev);
130+
int ieee802154_register_device(struct ieee802154_dev *dev);
131+
void ieee802154_unregister_device(struct ieee802154_dev *dev);
132+
126133
#endif /* NET_MAC802154_H */

‎net/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ source "net/econet/Kconfig"
211211
source "net/wanrouter/Kconfig"
212212
source "net/phonet/Kconfig"
213213
source "net/ieee802154/Kconfig"
214+
source "net/mac802154/Kconfig"
214215
source "net/sched/Kconfig"
215216
source "net/dcb/Kconfig"
216217
source "net/dns_resolver/Kconfig"

‎net/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ ifneq ($(CONFIG_DCB),)
6060
obj-y += dcb/
6161
endif
6262
obj-$(CONFIG_IEEE802154) += ieee802154/
63+
obj-$(CONFIG_MAC802154) += mac802154/
6364

6465
ifeq ($(CONFIG_NET),y)
6566
obj-$(CONFIG_SYSCTL) += sysctl_net.o

‎net/mac802154/Kconfig‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
config MAC802154
2+
tristate "Generic IEEE 802.15.4 Soft Networking Stack (mac802154)"
3+
depends on IEEE802154 && EXPERIMENTAL
4+
select CRC_CCITT
5+
---help---
6+
This option enables the hardware independent IEEE 802.15.4
7+
networking stack for SoftMAC devices (the ones implementing
8+
only PHY level of IEEE 802.15.4 standard).
9+
10+
Note: this implementation is neither certified, nor feature
11+
complete! Compatibility with other implementations hasn't
12+
been tested yet!
13+
14+
If you plan to use HardMAC IEEE 802.15.4 devices, you can
15+
say N here. Alternatievly you can say M to compile it as
16+
module.

‎net/mac802154/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
obj-$(CONFIG_MAC802154) += mac802154.o
2+
mac802154-objs := ieee802154_dev.o

‎net/mac802154/ieee802154_dev.c‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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.
17+
*
18+
* You should have received a copy of the GNU General Public License along
19+
* with this program; if not, write to the Free Software Foundation, Inc.,
20+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21+
*/
22+
23+
#include <linux/kernel.h>
24+
#include <linux/module.h>
25+
#include <linux/netdevice.h>
26+
27+
#include <net/mac802154.h>
28+
#include <net/route.h>
29+
#include <net/wpan-phy.h>
30+
31+
#include "mac802154.h"
32+
33+
struct ieee802154_dev *
34+
ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops)
35+
{
36+
struct wpan_phy *phy;
37+
struct mac802154_priv *priv;
38+
size_t priv_size;
39+
40+
if (!ops || !ops->xmit || !ops->ed || !ops->start ||
41+
!ops->stop || !ops->set_channel) {
42+
printk(KERN_ERR
43+
"undefined IEEE802.15.4 device operations\n");
44+
return NULL;
45+
}
46+
47+
/* Ensure 32-byte alignment of our private data and hw private data.
48+
* We use the wpan_phy priv data for both our mac802154_priv and for
49+
* the driver's private data
50+
*
51+
* in memory it'll be like this:
52+
*
53+
* +-----------------------+
54+
* | struct wpan_phy |
55+
* +-----------------------+
56+
* | struct mac802154_priv |
57+
* +-----------------------+
58+
* | driver's private data |
59+
* +-----------------------+
60+
*
61+
* Due to ieee802154 layer isn't aware of driver and MAC structures,
62+
* so lets allign them here.
63+
*/
64+
65+
priv_size = ALIGN(sizeof(*priv), NETDEV_ALIGN) + priv_data_len;
66+
67+
phy = wpan_phy_alloc(priv_size);
68+
if (!phy) {
69+
printk(KERN_ERR
70+
"failure to allocate master IEEE802.15.4 device\n");
71+
return NULL;
72+
}
73+
74+
priv = wpan_phy_priv(phy);
75+
priv->hw.phy = priv->phy = phy;
76+
priv->hw.priv = (char *)priv + ALIGN(sizeof(*priv), NETDEV_ALIGN);
77+
priv->ops = ops;
78+
79+
INIT_LIST_HEAD(&priv->slaves);
80+
mutex_init(&priv->slaves_mtx);
81+
82+
return &priv->hw;
83+
}
84+
EXPORT_SYMBOL(ieee802154_alloc_device);
85+
86+
void ieee802154_free_device(struct ieee802154_dev *hw)
87+
{
88+
struct mac802154_priv *priv = mac802154_to_priv(hw);
89+
90+
wpan_phy_free(priv->phy);
91+
92+
mutex_destroy(&priv->slaves_mtx);
93+
}
94+
EXPORT_SYMBOL(ieee802154_free_device);
95+
96+
int ieee802154_register_device(struct ieee802154_dev *dev)
97+
{
98+
struct mac802154_priv *priv = mac802154_to_priv(dev);
99+
int rc = -ENOMEM;
100+
101+
priv->dev_workqueue =
102+
create_singlethread_workqueue(wpan_phy_name(priv->phy));
103+
if (!priv->dev_workqueue)
104+
goto out;
105+
106+
wpan_phy_set_dev(priv->phy, priv->hw.parent);
107+
108+
rc = wpan_phy_register(priv->phy);
109+
if (rc < 0)
110+
goto out_wq;
111+
112+
rtnl_lock();
113+
114+
mutex_lock(&priv->slaves_mtx);
115+
priv->running = MAC802154_DEVICE_RUN;
116+
mutex_unlock(&priv->slaves_mtx);
117+
118+
rtnl_unlock();
119+
120+
return 0;
121+
122+
out_wq:
123+
destroy_workqueue(priv->dev_workqueue);
124+
out:
125+
return rc;
126+
}
127+
EXPORT_SYMBOL(ieee802154_register_device);
128+
129+
void ieee802154_unregister_device(struct ieee802154_dev *dev)
130+
{
131+
struct mac802154_priv *priv = mac802154_to_priv(dev);
132+
133+
flush_workqueue(priv->dev_workqueue);
134+
destroy_workqueue(priv->dev_workqueue);
135+
136+
rtnl_lock();
137+
138+
mutex_lock(&priv->slaves_mtx);
139+
priv->running = MAC802154_DEVICE_STOPPED;
140+
mutex_unlock(&priv->slaves_mtx);
141+
142+
rtnl_unlock();
143+
144+
wpan_phy_unregister(priv->phy);
145+
}
146+
EXPORT_SYMBOL(ieee802154_unregister_device);
147+
148+
MODULE_DESCRIPTION("IEEE 802.15.4 implementation");
149+
MODULE_LICENSE("GPL v2");

‎net/mac802154/mac802154.h‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (C) 2007-2012 Siemens AG
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License version 2
6+
* as published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License along
14+
* with this program; if not, write to the Free Software Foundation, Inc.,
15+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16+
*
17+
* Written by:
18+
* Pavel Smolenskiy <pavel.smolenskiy@gmail.com>
19+
* Maxim Gorbachyov <maxim.gorbachev@siemens.com>
20+
* Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
21+
* Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
22+
*/
23+
#ifndef MAC802154_H
24+
#define MAC802154_H
25+
26+
/* mac802154 device private data */
27+
struct mac802154_priv {
28+
struct ieee802154_dev hw;
29+
struct ieee802154_ops *ops;
30+
31+
/* ieee802154 phy */
32+
struct wpan_phy *phy;
33+
34+
int open_count;
35+
36+
/* As in mac80211 slaves list is modified:
37+
* 1) under the RTNL
38+
* 2) protected by slaves_mtx;
39+
* 3) in an RCU manner
40+
*
41+
* So atomic readers can use any of this protection methods.
42+
*/
43+
struct list_head slaves;
44+
struct mutex slaves_mtx;
45+
46+
/* This one is used for scanning and other jobs not to be interfered
47+
* with serial driver.
48+
*/
49+
struct workqueue_struct *dev_workqueue;
50+
51+
/* SoftMAC device is registered and running. One can add subinterfaces.
52+
* This flag should be modified under slaves_mtx and RTNL, so you can
53+
* read them using any of protection methods.
54+
*/
55+
bool running;
56+
};
57+
58+
#define MAC802154_DEVICE_STOPPED 0x00
59+
#define MAC802154_DEVICE_RUN 0x01
60+
61+
#define mac802154_to_priv(_hw) container_of(_hw, struct mac802154_priv, hw)
62+
63+
#endif /* MAC802154_H */

0 commit comments

Comments
 (0)