timestamping.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * PTP 1588 clock support - support for timestamping in PHY devices
  3. *
  4. * Copyright (C) 2010 OMICRON electronics GmbH
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  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.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/errqueue.h>
  21. #include <linux/phy.h>
  22. #include <linux/ptp_classify.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/export.h>
  25. static unsigned int classify(const struct sk_buff *skb)
  26. {
  27. if (likely(skb->dev && skb->dev->phydev &&
  28. skb->dev->phydev->drv))
  29. return ptp_classify_raw(skb);
  30. else
  31. return PTP_CLASS_NONE;
  32. }
  33. void skb_clone_tx_timestamp(struct sk_buff *skb)
  34. {
  35. struct phy_device *phydev;
  36. struct sk_buff *clone;
  37. unsigned int type;
  38. if (!skb->sk)
  39. return;
  40. type = classify(skb);
  41. if (type == PTP_CLASS_NONE)
  42. return;
  43. phydev = skb->dev->phydev;
  44. if (likely(phydev->drv->txtstamp)) {
  45. clone = skb_clone_sk(skb);
  46. if (!clone)
  47. return;
  48. phydev->drv->txtstamp(phydev, clone, type);
  49. }
  50. }
  51. EXPORT_SYMBOL_GPL(skb_clone_tx_timestamp);
  52. bool skb_defer_rx_timestamp(struct sk_buff *skb)
  53. {
  54. struct phy_device *phydev;
  55. unsigned int type;
  56. if (!skb->dev || !skb->dev->phydev || !skb->dev->phydev->drv)
  57. return false;
  58. if (skb_headroom(skb) < ETH_HLEN)
  59. return false;
  60. __skb_push(skb, ETH_HLEN);
  61. type = ptp_classify_raw(skb);
  62. __skb_pull(skb, ETH_HLEN);
  63. if (type == PTP_CLASS_NONE)
  64. return false;
  65. phydev = skb->dev->phydev;
  66. if (likely(phydev->drv->rxtstamp))
  67. return phydev->drv->rxtstamp(phydev, skb, type);
  68. return false;
  69. }
  70. EXPORT_SYMBOL_GPL(skb_defer_rx_timestamp);