alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2012 Siemens AG |
| 3 | * |
| 4 | * Written by: |
| 5 | * Alexander Smirnov <alex.bluesman.smirnov@gmail.com> |
| 6 | * |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 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. |
| 10 | * |
| 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. |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/netdevice.h> |
| 20 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 21 | #include <net/netlink.h> |
Alexander Aring | 944742a | 2014-11-17 08:20:49 +0100 | [diff] [blame] | 22 | #include <net/nl802154.h> |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 23 | #include <net/mac802154.h> |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 24 | #include <net/ieee802154_netdev.h> |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 25 | #include <net/route.h> |
Alexander Aring | 5ad60d3 | 2014-10-25 09:41:02 +0200 | [diff] [blame] | 26 | #include <net/cfg802154.h> |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 27 | |
Alexander Aring | 0f1556b | 2014-10-25 09:41:00 +0200 | [diff] [blame] | 28 | #include "ieee802154_i.h" |
Alexander Aring | 1201cd2 | 2014-11-02 04:18:36 +0100 | [diff] [blame] | 29 | #include "cfg.h" |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 30 | |
Alexander Aring | c5c47e6 | 2014-10-27 17:13:30 +0100 | [diff] [blame] | 31 | static void ieee802154_tasklet_handler(unsigned long data) |
| 32 | { |
| 33 | struct ieee802154_local *local = (struct ieee802154_local *)data; |
| 34 | struct sk_buff *skb; |
| 35 | |
| 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 |
| 40 | * netstack. |
| 41 | */ |
| 42 | skb->pkt_type = 0; |
| 43 | ieee802154_rx(&local->hw, skb); |
| 44 | break; |
| 45 | default: |
| 46 | WARN(1, "mac802154: Packet is of unknown type %d\n", |
| 47 | skb->pkt_type); |
| 48 | kfree_skb(skb); |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 54 | struct ieee802154_hw * |
Alexander Aring | 1630186 | 2014-10-28 18:21:18 +0100 | [diff] [blame] | 55 | ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops) |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 56 | { |
| 57 | struct wpan_phy *phy; |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 58 | struct ieee802154_local *local; |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 59 | size_t priv_size; |
| 60 | |
Alexander Aring | ed0a5dc | 2014-10-26 09:37:08 +0100 | [diff] [blame] | 61 | if (!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed || |
| 62 | !ops->start || !ops->stop || !ops->set_channel) { |
Chen Weilong | 83a1a7c | 2013-10-30 15:28:07 +0800 | [diff] [blame] | 63 | pr_err("undefined IEEE802.15.4 device operations\n"); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 64 | return NULL; |
| 65 | } |
| 66 | |
| 67 | /* Ensure 32-byte alignment of our private data and hw private data. |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 68 | * We use the wpan_phy priv data for both our ieee802154_local and for |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 69 | * the driver's private data |
| 70 | * |
| 71 | * in memory it'll be like this: |
| 72 | * |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 73 | * +-------------------------+ |
| 74 | * | struct wpan_phy | |
| 75 | * +-------------------------+ |
| 76 | * | struct ieee802154_local | |
| 77 | * +-------------------------+ |
| 78 | * | driver's private data | |
| 79 | * +-------------------------+ |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 80 | * |
| 81 | * Due to ieee802154 layer isn't aware of driver and MAC structures, |
Alexander Aring | 139f14a | 2014-10-25 05:25:08 +0200 | [diff] [blame] | 82 | * so lets align them here. |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 83 | */ |
| 84 | |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 85 | priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len; |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 86 | |
Alexander Aring | f601379 | 2014-11-09 08:36:47 +0100 | [diff] [blame] | 87 | phy = wpan_phy_new(&mac802154_config_ops, priv_size); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 88 | if (!phy) { |
Chen Weilong | 83a1a7c | 2013-10-30 15:28:07 +0800 | [diff] [blame] | 89 | pr_err("failure to allocate master IEEE802.15.4 device\n"); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 90 | return NULL; |
| 91 | } |
| 92 | |
Alexander Aring | 6322d50 | 2014-11-12 03:36:51 +0100 | [diff] [blame] | 93 | phy->privid = mac802154_wpan_phy_privid; |
| 94 | |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 95 | 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.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 100 | |
Alexander Aring | d98be45 | 2014-10-25 17:16:38 +0200 | [diff] [blame] | 101 | INIT_LIST_HEAD(&local->interfaces); |
| 102 | mutex_init(&local->iflist_mtx); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 103 | |
Alexander Aring | c5c47e6 | 2014-10-27 17:13:30 +0100 | [diff] [blame] | 104 | tasklet_init(&local->tasklet, |
| 105 | ieee802154_tasklet_handler, |
| 106 | (unsigned long)local); |
| 107 | |
| 108 | skb_queue_head_init(&local->skb_queue); |
| 109 | |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 110 | return &local->hw; |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 111 | } |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 112 | EXPORT_SYMBOL(ieee802154_alloc_hw); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 113 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 114 | void ieee802154_free_hw(struct ieee802154_hw *hw) |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 115 | { |
Alexander Aring | 6074136 | 2014-10-25 17:16:39 +0200 | [diff] [blame] | 116 | struct ieee802154_local *local = hw_to_local(hw); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 117 | |
Alexander Aring | d98be45 | 2014-10-25 17:16:38 +0200 | [diff] [blame] | 118 | BUG_ON(!list_empty(&local->interfaces)); |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 119 | |
Alexander Aring | d98be45 | 2014-10-25 17:16:38 +0200 | [diff] [blame] | 120 | mutex_destroy(&local->iflist_mtx); |
Konstantin Khlebnikov | 1e9f954 | 2012-12-14 01:03:03 +0000 | [diff] [blame] | 121 | |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 122 | wpan_phy_free(local->phy); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 123 | } |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 124 | EXPORT_SYMBOL(ieee802154_free_hw); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 125 | |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 126 | static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy) |
| 127 | { |
| 128 | /* TODO warn on empty symbol_duration |
| 129 | * Should be done when all drivers sets this value. |
| 130 | */ |
| 131 | |
| 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; |
| 136 | } |
| 137 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 138 | int ieee802154_register_hw(struct ieee802154_hw *hw) |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 139 | { |
Alexander Aring | 6074136 | 2014-10-25 17:16:39 +0200 | [diff] [blame] | 140 | struct ieee802154_local *local = hw_to_local(hw); |
Alexander Aring | e4962a1 | 2014-11-05 20:51:19 +0100 | [diff] [blame] | 141 | struct net_device *dev; |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 142 | int rc = -ENOSYS; |
| 143 | |
Alexander Aring | f773054 | 2014-10-25 17:16:41 +0200 | [diff] [blame] | 144 | local->workqueue = |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 145 | create_singlethread_workqueue(wpan_phy_name(local->phy)); |
Alexander Aring | f773054 | 2014-10-25 17:16:41 +0200 | [diff] [blame] | 146 | if (!local->workqueue) { |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 147 | rc = -ENOMEM; |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 148 | goto out; |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 149 | } |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 150 | |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 151 | hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 152 | local->ifs_timer.function = ieee802154_xmit_ifs_timer; |
| 153 | |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 154 | wpan_phy_set_dev(local->phy, local->hw.parent); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 155 | |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 156 | ieee802154_setup_wpan_phy_pib(local->phy); |
| 157 | |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 158 | rc = wpan_phy_register(local->phy); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 159 | if (rc < 0) |
| 160 | goto out_wq; |
| 161 | |
Alexander Aring | e4962a1 | 2014-11-05 20:51:19 +0100 | [diff] [blame] | 162 | rtnl_lock(); |
| 163 | |
Varka Bhadram | 5b4a1039 | 2015-04-30 17:44:57 +0200 | [diff] [blame^] | 164 | dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM, |
| 165 | NL802154_IFTYPE_NODE, |
Alexander Aring | 0e57547 | 2014-11-17 08:20:52 +0100 | [diff] [blame] | 166 | cpu_to_le64(0x0000000000000000ULL)); |
Alexander Aring | e4962a1 | 2014-11-05 20:51:19 +0100 | [diff] [blame] | 167 | if (IS_ERR(dev)) { |
| 168 | rtnl_unlock(); |
| 169 | rc = PTR_ERR(dev); |
| 170 | goto out_wq; |
| 171 | } |
| 172 | |
| 173 | rtnl_unlock(); |
| 174 | |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 175 | return 0; |
| 176 | |
| 177 | out_wq: |
Alexander Aring | f773054 | 2014-10-25 17:16:41 +0200 | [diff] [blame] | 178 | destroy_workqueue(local->workqueue); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 179 | out: |
| 180 | return rc; |
| 181 | } |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 182 | EXPORT_SYMBOL(ieee802154_register_hw); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 183 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 184 | void ieee802154_unregister_hw(struct ieee802154_hw *hw) |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 185 | { |
Alexander Aring | 6074136 | 2014-10-25 17:16:39 +0200 | [diff] [blame] | 186 | struct ieee802154_local *local = hw_to_local(hw); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 187 | |
Alexander Aring | c5c47e6 | 2014-10-27 17:13:30 +0100 | [diff] [blame] | 188 | tasklet_kill(&local->tasklet); |
Alexander Aring | f773054 | 2014-10-25 17:16:41 +0200 | [diff] [blame] | 189 | flush_workqueue(local->workqueue); |
| 190 | destroy_workqueue(local->workqueue); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 191 | |
| 192 | rtnl_lock(); |
| 193 | |
Alexander Aring | 592dfbf | 2014-11-12 03:36:48 +0100 | [diff] [blame] | 194 | ieee802154_remove_interfaces(local); |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 195 | |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 196 | rtnl_unlock(); |
| 197 | |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 198 | wpan_phy_unregister(local->phy); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 199 | } |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 200 | EXPORT_SYMBOL(ieee802154_unregister_hw); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 201 | |
Alexander Aring | be4fd8e | 2014-11-12 03:36:53 +0100 | [diff] [blame] | 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 | |
Alexander Aring | 912f67a | 2014-11-12 03:36:52 +0100 | [diff] [blame] | 217 | MODULE_DESCRIPTION("IEEE 802.15.4 subsystem"); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 218 | MODULE_LICENSE("GPL v2"); |