tx.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2007-2012 Siemens AG
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Written by:
  18. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  19. * Sergey Lapin <slapin@ossfans.org>
  20. * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
  21. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  22. */
  23. #include <linux/netdevice.h>
  24. #include <linux/if_arp.h>
  25. #include <linux/crc-ccitt.h>
  26. #include <net/ieee802154_netdev.h>
  27. #include <net/mac802154.h>
  28. #include <net/wpan-phy.h>
  29. #include "mac802154.h"
  30. /* IEEE 802.15.4 transceivers can sleep during the xmit session, so process
  31. * packets through the workqueue.
  32. */
  33. struct xmit_work {
  34. struct sk_buff *skb;
  35. struct work_struct work;
  36. struct mac802154_priv *priv;
  37. u8 chan;
  38. u8 page;
  39. };
  40. static void mac802154_xmit_worker(struct work_struct *work)
  41. {
  42. struct xmit_work *xw = container_of(work, struct xmit_work, work);
  43. struct mac802154_sub_if_data *sdata;
  44. int res;
  45. mutex_lock(&xw->priv->phy->pib_lock);
  46. if (xw->priv->phy->current_channel != xw->chan ||
  47. xw->priv->phy->current_page != xw->page) {
  48. res = xw->priv->ops->set_channel(&xw->priv->hw,
  49. xw->page,
  50. xw->chan);
  51. if (res) {
  52. pr_debug("set_channel failed\n");
  53. goto out;
  54. }
  55. xw->priv->phy->current_channel = xw->chan;
  56. xw->priv->phy->current_page = xw->page;
  57. }
  58. res = xw->priv->ops->xmit(&xw->priv->hw, xw->skb);
  59. if (res)
  60. pr_debug("transmission failed\n");
  61. out:
  62. mutex_unlock(&xw->priv->phy->pib_lock);
  63. /* Restart the netif queue on each sub_if_data object. */
  64. rcu_read_lock();
  65. list_for_each_entry_rcu(sdata, &xw->priv->slaves, list)
  66. netif_wake_queue(sdata->dev);
  67. rcu_read_unlock();
  68. dev_kfree_skb(xw->skb);
  69. kfree(xw);
  70. }
  71. netdev_tx_t mac802154_tx(struct mac802154_priv *priv, struct sk_buff *skb,
  72. u8 page, u8 chan)
  73. {
  74. struct xmit_work *work;
  75. struct mac802154_sub_if_data *sdata;
  76. if (!(priv->phy->channels_supported[page] & (1 << chan))) {
  77. WARN_ON(1);
  78. kfree_skb(skb);
  79. return NETDEV_TX_OK;
  80. }
  81. mac802154_monitors_rx(mac802154_to_priv(&priv->hw), skb);
  82. if (!(priv->hw.flags & IEEE802154_HW_OMIT_CKSUM)) {
  83. u16 crc = crc_ccitt(0, skb->data, skb->len);
  84. u8 *data = skb_put(skb, 2);
  85. data[0] = crc & 0xff;
  86. data[1] = crc >> 8;
  87. }
  88. if (skb_cow_head(skb, priv->hw.extra_tx_headroom)) {
  89. kfree_skb(skb);
  90. return NETDEV_TX_OK;
  91. }
  92. work = kzalloc(sizeof(struct xmit_work), GFP_ATOMIC);
  93. if (!work) {
  94. kfree_skb(skb);
  95. return NETDEV_TX_BUSY;
  96. }
  97. /* Stop the netif queue on each sub_if_data object. */
  98. rcu_read_lock();
  99. list_for_each_entry_rcu(sdata, &priv->slaves, list)
  100. netif_stop_queue(sdata->dev);
  101. rcu_read_unlock();
  102. INIT_WORK(&work->work, mac802154_xmit_worker);
  103. work->skb = skb;
  104. work->priv = priv;
  105. work->page = page;
  106. work->chan = chan;
  107. queue_work(priv->dev_workqueue, &work->work);
  108. return NETDEV_TX_OK;
  109. }