2 * Copyright (C) 2007-2012 Siemens AG
5 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
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.
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.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/netdevice.h>
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>
28 #include "ieee802154_i.h"
31 static void ieee802154_tasklet_handler(unsigned long data
)
33 struct ieee802154_local
*local
= (struct ieee802154_local
*)data
;
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
43 ieee802154_rx(&local
->hw
, skb
);
46 WARN(1, "mac802154: Packet is of unknown type %d\n",
54 struct ieee802154_hw
*
55 ieee802154_alloc_hw(size_t priv_data_len
, const struct ieee802154_ops
*ops
)
58 struct ieee802154_local
*local
;
61 if (!ops
|| !(ops
->xmit_async
|| ops
->xmit_sync
) || !ops
->ed
||
62 !ops
->start
|| !ops
->stop
|| !ops
->set_channel
) {
63 pr_err("undefined IEEE802.15.4 device operations\n");
67 /* Ensure 32-byte alignment of our private data and hw private data.
68 * We use the wpan_phy priv data for both our ieee802154_local and for
69 * the driver's private data
71 * in memory it'll be like this:
73 * +-------------------------+
75 * +-------------------------+
76 * | struct ieee802154_local |
77 * +-------------------------+
78 * | driver's private data |
79 * +-------------------------+
81 * Due to ieee802154 layer isn't aware of driver and MAC structures,
82 * so lets align them here.
85 priv_size
= ALIGN(sizeof(*local
), NETDEV_ALIGN
) + priv_data_len
;
87 phy
= wpan_phy_new(&mac802154_config_ops
, priv_size
);
89 pr_err("failure to allocate master IEEE802.15.4 device\n");
93 phy
->privid
= mac802154_wpan_phy_privid
;
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 static void ieee802154_setup_wpan_phy_pib(struct wpan_phy
*wpan_phy
)
128 /* TODO warn on empty symbol_duration
129 * Should be done when all drivers sets this value.
132 wpan_phy
->lifs_period
= IEEE802154_LIFS_PERIOD
*
133 wpan_phy
->symbol_duration
;
134 wpan_phy
->sifs_period
= IEEE802154_SIFS_PERIOD
*
135 wpan_phy
->symbol_duration
;
138 int ieee802154_register_hw(struct ieee802154_hw
*hw
)
140 struct ieee802154_local
*local
= hw_to_local(hw
);
141 struct net_device
*dev
;
145 create_singlethread_workqueue(wpan_phy_name(local
->phy
));
146 if (!local
->workqueue
) {
151 hrtimer_init(&local
->ifs_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
152 local
->ifs_timer
.function
= ieee802154_xmit_ifs_timer
;
154 wpan_phy_set_dev(local
->phy
, local
->hw
.parent
);
156 ieee802154_setup_wpan_phy_pib(local
->phy
);
158 rc
= wpan_phy_register(local
->phy
);
164 dev
= ieee802154_if_add(local
, "wpan%d", NET_NAME_ENUM
,
165 NL802154_IFTYPE_NODE
,
166 cpu_to_le64(0x0000000000000000ULL
));
178 wpan_phy_unregister(local
->phy
);
180 destroy_workqueue(local
->workqueue
);
184 EXPORT_SYMBOL(ieee802154_register_hw
);
186 void ieee802154_unregister_hw(struct ieee802154_hw
*hw
)
188 struct ieee802154_local
*local
= hw_to_local(hw
);
190 tasklet_kill(&local
->tasklet
);
191 flush_workqueue(local
->workqueue
);
192 destroy_workqueue(local
->workqueue
);
196 ieee802154_remove_interfaces(local
);
200 wpan_phy_unregister(local
->phy
);
202 EXPORT_SYMBOL(ieee802154_unregister_hw
);
204 static int __init
ieee802154_init(void)
206 return ieee802154_iface_init();
209 static void __exit
ieee802154_exit(void)
211 ieee802154_iface_exit();
216 subsys_initcall(ieee802154_init
);
217 module_exit(ieee802154_exit
);
219 MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
220 MODULE_LICENSE("GPL v2");