tx.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/mac802154.h>
  27. #include <net/wpan-phy.h>
  28. #include "mac802154.h"
  29. /* IEEE 802.15.4 transceivers can sleep during the xmit session, so process
  30. * packets through the workqueue.
  31. */
  32. struct xmit_work {
  33. struct sk_buff *skb;
  34. struct work_struct work;
  35. struct mac802154_priv *priv;
  36. u8 chan;
  37. u8 page;
  38. u8 xmit_attempts;
  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. int res;
  44. mutex_lock(&xw->priv->phy->pib_lock);
  45. if (xw->priv->phy->current_channel != xw->chan ||
  46. xw->priv->phy->current_page != xw->page) {
  47. res = xw->priv->ops->set_channel(&xw->priv->hw,
  48. xw->page,
  49. xw->chan);
  50. if (res) {
  51. pr_debug("set_channel failed\n");
  52. goto out;
  53. }
  54. }
  55. res = xw->priv->ops->xmit(&xw->priv->hw, xw->skb);
  56. out:
  57. mutex_unlock(&xw->priv->phy->pib_lock);
  58. if (res) {
  59. if (xw->xmit_attempts++ < MAC802154_MAX_XMIT_ATTEMPTS) {
  60. queue_work(xw->priv->dev_workqueue, &xw->work);
  61. return;
  62. } else
  63. pr_debug("transmission failed for %d times",
  64. MAC802154_MAX_XMIT_ATTEMPTS);
  65. }
  66. dev_kfree_skb(xw->skb);
  67. kfree(xw);
  68. }
  69. netdev_tx_t mac802154_tx(struct mac802154_priv *priv, struct sk_buff *skb,
  70. u8 page, u8 chan)
  71. {
  72. struct xmit_work *work;
  73. if (!(priv->phy->channels_supported[page] & (1 << chan))) {
  74. WARN_ON(1);
  75. kfree_skb(skb);
  76. return NETDEV_TX_OK;
  77. }
  78. mac802154_monitors_rx(mac802154_to_priv(&priv->hw), skb);
  79. if (!(priv->hw.flags & IEEE802154_HW_OMIT_CKSUM)) {
  80. u16 crc = crc_ccitt(0, skb->data, skb->len);
  81. u8 *data = skb_put(skb, 2);
  82. data[0] = crc & 0xff;
  83. data[1] = crc >> 8;
  84. }
  85. if (skb_cow_head(skb, priv->hw.extra_tx_headroom)) {
  86. kfree_skb(skb);
  87. return NETDEV_TX_OK;
  88. }
  89. work = kzalloc(sizeof(struct xmit_work), GFP_ATOMIC);
  90. if (!work) {
  91. kfree_skb(skb);
  92. return NETDEV_TX_BUSY;
  93. }
  94. INIT_WORK(&work->work, mac802154_xmit_worker);
  95. work->skb = skb;
  96. work->priv = priv;
  97. work->page = page;
  98. work->chan = chan;
  99. work->xmit_attempts = 0;
  100. queue_work(priv->dev_workqueue, &work->work);
  101. return NETDEV_TX_OK;
  102. }