2 * Copyright (C) 2007-2012 Siemens AG
5 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
7 * Based on the code from 'linux-zigbee.sourceforge.net' project.
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.
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.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/netdevice.h>
23 #include <net/netlink.h>
24 #include <linux/nl802154.h>
25 #include <net/mac802154.h>
26 #include <net/ieee802154_netdev.h>
27 #include <net/route.h>
28 #include <net/cfg802154.h>
30 #include "ieee802154_i.h"
33 static void ieee802154_tasklet_handler(unsigned long data
)
35 struct ieee802154_local
*local
= (struct ieee802154_local
*)data
;
38 while ((skb
= skb_dequeue(&local
->skb_queue
))) {
39 switch (skb
->pkt_type
) {
40 case IEEE802154_RX_MSG
:
41 /* Clear skb->pkt_type in order to not confuse kernel
45 ieee802154_rx(&local
->hw
, skb
);
48 WARN(1, "mac802154: Packet is of unknown type %d\n",
56 struct ieee802154_hw
*
57 ieee802154_alloc_hw(size_t priv_data_len
, const struct ieee802154_ops
*ops
)
60 struct ieee802154_local
*local
;
63 if (!ops
|| !(ops
->xmit_async
|| ops
->xmit_sync
) || !ops
->ed
||
64 !ops
->start
|| !ops
->stop
|| !ops
->set_channel
) {
65 pr_err("undefined IEEE802.15.4 device operations\n");
69 /* Ensure 32-byte alignment of our private data and hw private data.
70 * We use the wpan_phy priv data for both our ieee802154_local and for
71 * the driver's private data
73 * in memory it'll be like this:
75 * +-------------------------+
77 * +-------------------------+
78 * | struct ieee802154_local |
79 * +-------------------------+
80 * | driver's private data |
81 * +-------------------------+
83 * Due to ieee802154 layer isn't aware of driver and MAC structures,
84 * so lets align them here.
87 priv_size
= ALIGN(sizeof(*local
), NETDEV_ALIGN
) + priv_data_len
;
89 phy
= wpan_phy_alloc(&mac802154_config_ops
, priv_size
);
91 pr_err("failure to allocate master IEEE802.15.4 device\n");
95 local
= wpan_phy_priv(phy
);
97 local
->hw
.phy
= local
->phy
;
98 local
->hw
.priv
= (char *)local
+ ALIGN(sizeof(*local
), NETDEV_ALIGN
);
101 INIT_LIST_HEAD(&local
->interfaces
);
102 mutex_init(&local
->iflist_mtx
);
104 tasklet_init(&local
->tasklet
,
105 ieee802154_tasklet_handler
,
106 (unsigned long)local
);
108 skb_queue_head_init(&local
->skb_queue
);
112 EXPORT_SYMBOL(ieee802154_alloc_hw
);
114 void ieee802154_free_hw(struct ieee802154_hw
*hw
)
116 struct ieee802154_local
*local
= hw_to_local(hw
);
118 BUG_ON(!list_empty(&local
->interfaces
));
120 mutex_destroy(&local
->iflist_mtx
);
122 wpan_phy_free(local
->phy
);
124 EXPORT_SYMBOL(ieee802154_free_hw
);
126 int ieee802154_register_hw(struct ieee802154_hw
*hw
)
128 struct ieee802154_local
*local
= hw_to_local(hw
);
132 create_singlethread_workqueue(wpan_phy_name(local
->phy
));
133 if (!local
->workqueue
) {
138 wpan_phy_set_dev(local
->phy
, local
->hw
.parent
);
140 rc
= wpan_phy_register(local
->phy
);
147 destroy_workqueue(local
->workqueue
);
151 EXPORT_SYMBOL(ieee802154_register_hw
);
153 void ieee802154_unregister_hw(struct ieee802154_hw
*hw
)
155 struct ieee802154_local
*local
= hw_to_local(hw
);
156 struct ieee802154_sub_if_data
*sdata
, *next
;
158 tasklet_kill(&local
->tasklet
);
159 flush_workqueue(local
->workqueue
);
160 destroy_workqueue(local
->workqueue
);
164 list_for_each_entry_safe(sdata
, next
, &local
->interfaces
, list
) {
165 mutex_lock(&sdata
->local
->iflist_mtx
);
166 list_del(&sdata
->list
);
167 mutex_unlock(&sdata
->local
->iflist_mtx
);
169 unregister_netdevice(sdata
->dev
);
174 wpan_phy_unregister(local
->phy
);
176 EXPORT_SYMBOL(ieee802154_unregister_hw
);
178 MODULE_DESCRIPTION("IEEE 802.15.4 implementation");
179 MODULE_LICENSE("GPL v2");