blob: a371eb5fa0539ac9d8ed375b7c878ae3f6321c29 [file] [log] [blame]
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +00001/*
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.
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000017 */
18
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/netdevice.h>
22
alex.bluesman.smirnov@gmail.com62610ad2012-05-15 20:50:28 +000023#include <net/netlink.h>
24#include <linux/nl802154.h>
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000025#include <net/mac802154.h>
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010026#include <net/ieee802154_netdev.h>
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000027#include <net/route.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020028#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000029
Alexander Aring0f1556b2014-10-25 09:41:00 +020030#include "ieee802154_i.h"
Alexander Aring1201cd22014-11-02 04:18:36 +010031#include "cfg.h"
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000032
Alexander Aringc5c47e62014-10-27 17:13:30 +010033static void ieee802154_tasklet_handler(unsigned long data)
34{
35 struct ieee802154_local *local = (struct ieee802154_local *)data;
36 struct sk_buff *skb;
37
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
42 * netstack.
43 */
44 skb->pkt_type = 0;
45 ieee802154_rx(&local->hw, skb);
46 break;
47 default:
48 WARN(1, "mac802154: Packet is of unknown type %d\n",
49 skb->pkt_type);
50 kfree_skb(skb);
51 break;
52 }
53 }
54}
55
Alexander Aring5a504392014-10-25 17:16:34 +020056struct ieee802154_hw *
Alexander Aring16301862014-10-28 18:21:18 +010057ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000058{
59 struct wpan_phy *phy;
Alexander Aringa5e1ec52014-10-25 17:16:35 +020060 struct ieee802154_local *local;
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000061 size_t priv_size;
62
Alexander Aringed0a5dc2014-10-26 09:37:08 +010063 if (!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
64 !ops->start || !ops->stop || !ops->set_channel) {
Chen Weilong83a1a7c2013-10-30 15:28:07 +080065 pr_err("undefined IEEE802.15.4 device operations\n");
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000066 return NULL;
67 }
68
69 /* Ensure 32-byte alignment of our private data and hw private data.
Alexander Aringa5e1ec52014-10-25 17:16:35 +020070 * We use the wpan_phy priv data for both our ieee802154_local and for
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000071 * the driver's private data
72 *
73 * in memory it'll be like this:
74 *
Alexander Aringa5e1ec52014-10-25 17:16:35 +020075 * +-------------------------+
76 * | struct wpan_phy |
77 * +-------------------------+
78 * | struct ieee802154_local |
79 * +-------------------------+
80 * | driver's private data |
81 * +-------------------------+
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000082 *
83 * Due to ieee802154 layer isn't aware of driver and MAC structures,
Alexander Aring139f14a2014-10-25 05:25:08 +020084 * so lets align them here.
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000085 */
86
Alexander Aringa5e1ec52014-10-25 17:16:35 +020087 priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000088
Alexander Aring1201cd22014-11-02 04:18:36 +010089 phy = wpan_phy_alloc(&mac802154_config_ops, priv_size);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000090 if (!phy) {
Chen Weilong83a1a7c2013-10-30 15:28:07 +080091 pr_err("failure to allocate master IEEE802.15.4 device\n");
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +000092 return NULL;
93 }
94
Alexander Aringa5e1ec52014-10-25 17:16:35 +020095 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;
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000100
Alexander Aringd98be452014-10-25 17:16:38 +0200101 INIT_LIST_HEAD(&local->interfaces);
102 mutex_init(&local->iflist_mtx);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000103
Alexander Aringc5c47e62014-10-27 17:13:30 +0100104 tasklet_init(&local->tasklet,
105 ieee802154_tasklet_handler,
106 (unsigned long)local);
107
108 skb_queue_head_init(&local->skb_queue);
109
Alexander Aringa5e1ec52014-10-25 17:16:35 +0200110 return &local->hw;
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000111}
Alexander Aring5a504392014-10-25 17:16:34 +0200112EXPORT_SYMBOL(ieee802154_alloc_hw);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000113
Alexander Aring5a504392014-10-25 17:16:34 +0200114void ieee802154_free_hw(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000115{
Alexander Aring60741362014-10-25 17:16:39 +0200116 struct ieee802154_local *local = hw_to_local(hw);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000117
Alexander Aringd98be452014-10-25 17:16:38 +0200118 BUG_ON(!list_empty(&local->interfaces));
alex.bluesman.smirnov@gmail.com62610ad2012-05-15 20:50:28 +0000119
Alexander Aringd98be452014-10-25 17:16:38 +0200120 mutex_destroy(&local->iflist_mtx);
Konstantin Khlebnikov1e9f9542012-12-14 01:03:03 +0000121
Alexander Aringa5e1ec52014-10-25 17:16:35 +0200122 wpan_phy_free(local->phy);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000123}
Alexander Aring5a504392014-10-25 17:16:34 +0200124EXPORT_SYMBOL(ieee802154_free_hw);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000125
Alexander Aring5a504392014-10-25 17:16:34 +0200126int ieee802154_register_hw(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000127{
Alexander Aring60741362014-10-25 17:16:39 +0200128 struct ieee802154_local *local = hw_to_local(hw);
Alexander Aring640985e2014-07-03 00:20:43 +0200129 int rc = -ENOSYS;
130
Alexander Aringf7730542014-10-25 17:16:41 +0200131 local->workqueue =
Alexander Aringa5e1ec52014-10-25 17:16:35 +0200132 create_singlethread_workqueue(wpan_phy_name(local->phy));
Alexander Aringf7730542014-10-25 17:16:41 +0200133 if (!local->workqueue) {
Alexander Aring640985e2014-07-03 00:20:43 +0200134 rc = -ENOMEM;
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000135 goto out;
Alexander Aring640985e2014-07-03 00:20:43 +0200136 }
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000137
Alexander Aringa5e1ec52014-10-25 17:16:35 +0200138 wpan_phy_set_dev(local->phy, local->hw.parent);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000139
Alexander Aringa5e1ec52014-10-25 17:16:35 +0200140 rc = wpan_phy_register(local->phy);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000141 if (rc < 0)
142 goto out_wq;
143
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000144 return 0;
145
146out_wq:
Alexander Aringf7730542014-10-25 17:16:41 +0200147 destroy_workqueue(local->workqueue);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000148out:
149 return rc;
150}
Alexander Aring5a504392014-10-25 17:16:34 +0200151EXPORT_SYMBOL(ieee802154_register_hw);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000152
Alexander Aring5a504392014-10-25 17:16:34 +0200153void ieee802154_unregister_hw(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000154{
Alexander Aring60741362014-10-25 17:16:39 +0200155 struct ieee802154_local *local = hw_to_local(hw);
Alexander Aring036562f2014-10-25 17:16:36 +0200156 struct ieee802154_sub_if_data *sdata, *next;
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000157
Alexander Aringc5c47e62014-10-27 17:13:30 +0100158 tasklet_kill(&local->tasklet);
Alexander Aringf7730542014-10-25 17:16:41 +0200159 flush_workqueue(local->workqueue);
160 destroy_workqueue(local->workqueue);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000161
162 rtnl_lock();
163
Alexander Aringd98be452014-10-25 17:16:38 +0200164 list_for_each_entry_safe(sdata, next, &local->interfaces, list) {
165 mutex_lock(&sdata->local->iflist_mtx);
alex.bluesman.smirnov@gmail.com62610ad2012-05-15 20:50:28 +0000166 list_del(&sdata->list);
Alexander Aringd98be452014-10-25 17:16:38 +0200167 mutex_unlock(&sdata->local->iflist_mtx);
alex.bluesman.smirnov@gmail.com62610ad2012-05-15 20:50:28 +0000168
169 unregister_netdevice(sdata->dev);
170 }
171
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000172 rtnl_unlock();
173
Alexander Aringa5e1ec52014-10-25 17:16:35 +0200174 wpan_phy_unregister(local->phy);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000175}
Alexander Aring5a504392014-10-25 17:16:34 +0200176EXPORT_SYMBOL(ieee802154_unregister_hw);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000177
178MODULE_DESCRIPTION("IEEE 802.15.4 implementation");
179MODULE_LICENSE("GPL v2");