mac802154: add interframe spacing time handling
[linux-2.6-block.git] / net / mac802154 / main.c
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
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/netdevice.h>
22
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>
29
30 #include "ieee802154_i.h"
31 #include "cfg.h"
32
33 static 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
56 struct ieee802154_hw *
57 ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
58 {
59         struct wpan_phy *phy;
60         struct ieee802154_local *local;
61         size_t priv_size;
62
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");
66                 return NULL;
67         }
68
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
72          *
73          * in memory it'll be like this:
74          *
75          * +-------------------------+
76          * | struct wpan_phy         |
77          * +-------------------------+
78          * | struct ieee802154_local |
79          * +-------------------------+
80          * | driver's private data   |
81          * +-------------------------+
82          *
83          * Due to ieee802154 layer isn't aware of driver and MAC structures,
84          * so lets align them here.
85          */
86
87         priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
88
89         phy = wpan_phy_new(&mac802154_config_ops, priv_size);
90         if (!phy) {
91                 pr_err("failure to allocate master IEEE802.15.4 device\n");
92                 return NULL;
93         }
94
95         phy->privid = mac802154_wpan_phy_privid;
96
97         local = wpan_phy_priv(phy);
98         local->phy = phy;
99         local->hw.phy = local->phy;
100         local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
101         local->ops = ops;
102
103         INIT_LIST_HEAD(&local->interfaces);
104         mutex_init(&local->iflist_mtx);
105
106         tasklet_init(&local->tasklet,
107                      ieee802154_tasklet_handler,
108                      (unsigned long)local);
109
110         skb_queue_head_init(&local->skb_queue);
111
112         return &local->hw;
113 }
114 EXPORT_SYMBOL(ieee802154_alloc_hw);
115
116 void 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 }
126 EXPORT_SYMBOL(ieee802154_free_hw);
127
128 static 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
140 int 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         rc = wpan_phy_register(local->phy);
161         if (rc < 0)
162                 goto out_wq;
163
164         rtnl_lock();
165
166         dev = ieee802154_if_add(local, "wpan%d", NULL, IEEE802154_DEV_WPAN);
167         if (IS_ERR(dev)) {
168                 rtnl_unlock();
169                 rc = PTR_ERR(dev);
170                 goto out_wq;
171         }
172
173         rtnl_unlock();
174
175         return 0;
176
177 out_wq:
178         destroy_workqueue(local->workqueue);
179 out:
180         return rc;
181 }
182 EXPORT_SYMBOL(ieee802154_register_hw);
183
184 void ieee802154_unregister_hw(struct ieee802154_hw *hw)
185 {
186         struct ieee802154_local *local = hw_to_local(hw);
187
188         tasklet_kill(&local->tasklet);
189         flush_workqueue(local->workqueue);
190         destroy_workqueue(local->workqueue);
191
192         rtnl_lock();
193
194         ieee802154_remove_interfaces(local);
195
196         rtnl_unlock();
197
198         wpan_phy_unregister(local->phy);
199 }
200 EXPORT_SYMBOL(ieee802154_unregister_hw);
201
202 static int __init ieee802154_init(void)
203 {
204         return ieee802154_iface_init();
205 }
206
207 static void __exit ieee802154_exit(void)
208 {
209         ieee802154_iface_exit();
210
211         rcu_barrier();
212 }
213
214 subsys_initcall(ieee802154_init);
215 module_exit(ieee802154_exit);
216
217 MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
218 MODULE_LICENSE("GPL v2");