ptp_private.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * PTP 1588 clock support - private declarations for the core module.
  4. *
  5. * Copyright (C) 2010 OMICRON electronics GmbH
  6. */
  7. #ifndef _PTP_PRIVATE_H_
  8. #define _PTP_PRIVATE_H_
  9. #include <linux/cdev.h>
  10. #include <linux/device.h>
  11. #include <linux/kthread.h>
  12. #include <linux/mutex.h>
  13. #include <linux/posix-clock.h>
  14. #include <linux/ptp_clock.h>
  15. #include <linux/ptp_clock_kernel.h>
  16. #include <linux/time.h>
  17. #define PTP_MAX_TIMESTAMPS 128
  18. #define PTP_BUF_TIMESTAMPS 30
  19. #define PTP_DEFAULT_MAX_VCLOCKS 20
  20. struct timestamp_event_queue {
  21. struct ptp_extts_event buf[PTP_MAX_TIMESTAMPS];
  22. int head;
  23. int tail;
  24. spinlock_t lock;
  25. };
  26. struct ptp_clock {
  27. struct posix_clock clock;
  28. struct device dev;
  29. struct ptp_clock_info *info;
  30. dev_t devid;
  31. int index; /* index into clocks.map */
  32. struct pps_device *pps_source;
  33. long dialed_frequency; /* remembers the frequency adjustment */
  34. struct timestamp_event_queue tsevq; /* simple fifo for time stamps */
  35. struct mutex tsevq_mux; /* one process at a time reading the fifo */
  36. struct mutex pincfg_mux; /* protect concurrent info->pin_config access */
  37. wait_queue_head_t tsev_wq;
  38. int defunct; /* tells readers to go away when clock is being removed */
  39. struct device_attribute *pin_dev_attr;
  40. struct attribute **pin_attr;
  41. struct attribute_group pin_attr_group;
  42. /* 1st entry is a pointer to the real group, 2nd is NULL terminator */
  43. const struct attribute_group *pin_attr_groups[2];
  44. struct kthread_worker *kworker;
  45. struct kthread_delayed_work aux_work;
  46. unsigned int max_vclocks;
  47. unsigned int n_vclocks;
  48. int *vclock_index;
  49. struct mutex n_vclocks_mux; /* protect concurrent n_vclocks access */
  50. bool is_virtual_clock;
  51. bool has_cycles;
  52. };
  53. #define info_to_vclock(d) container_of((d), struct ptp_vclock, info)
  54. #define cc_to_vclock(d) container_of((d), struct ptp_vclock, cc)
  55. #define dw_to_vclock(d) container_of((d), struct ptp_vclock, refresh_work)
  56. struct ptp_vclock {
  57. struct ptp_clock *pclock;
  58. struct ptp_clock_info info;
  59. struct ptp_clock *clock;
  60. struct hlist_node vclock_hash_node;
  61. struct cyclecounter cc;
  62. struct timecounter tc;
  63. struct mutex lock; /* protects tc/cc */
  64. };
  65. /*
  66. * The function queue_cnt() is safe for readers to call without
  67. * holding q->lock. Readers use this function to verify that the queue
  68. * is nonempty before proceeding with a dequeue operation. The fact
  69. * that a writer might concurrently increment the tail does not
  70. * matter, since the queue remains nonempty nonetheless.
  71. */
  72. static inline int queue_cnt(const struct timestamp_event_queue *q)
  73. {
  74. /*
  75. * Paired with WRITE_ONCE() in enqueue_external_timestamp(),
  76. * ptp_read(), extts_fifo_show().
  77. */
  78. int cnt = READ_ONCE(q->tail) - READ_ONCE(q->head);
  79. return cnt < 0 ? PTP_MAX_TIMESTAMPS + cnt : cnt;
  80. }
  81. /* Check if ptp virtual clock is in use */
  82. static inline bool ptp_vclock_in_use(struct ptp_clock *ptp)
  83. {
  84. bool in_use = false;
  85. if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
  86. return true;
  87. if (!ptp->is_virtual_clock && ptp->n_vclocks)
  88. in_use = true;
  89. mutex_unlock(&ptp->n_vclocks_mux);
  90. return in_use;
  91. }
  92. /* Check if ptp clock shall be free running */
  93. static inline bool ptp_clock_freerun(struct ptp_clock *ptp)
  94. {
  95. if (ptp->has_cycles)
  96. return false;
  97. return ptp_vclock_in_use(ptp);
  98. }
  99. extern struct class *ptp_class;
  100. /*
  101. * see ptp_chardev.c
  102. */
  103. /* caller must hold pincfg_mux */
  104. int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
  105. enum ptp_pin_function func, unsigned int chan);
  106. long ptp_ioctl(struct posix_clock *pc,
  107. unsigned int cmd, unsigned long arg);
  108. int ptp_open(struct posix_clock *pc, fmode_t fmode);
  109. ssize_t ptp_read(struct posix_clock *pc,
  110. uint flags, char __user *buf, size_t cnt);
  111. __poll_t ptp_poll(struct posix_clock *pc,
  112. struct file *fp, poll_table *wait);
  113. /*
  114. * see ptp_sysfs.c
  115. */
  116. extern const struct attribute_group *ptp_groups[];
  117. int ptp_populate_pin_groups(struct ptp_clock *ptp);
  118. void ptp_cleanup_pin_groups(struct ptp_clock *ptp);
  119. struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock);
  120. void ptp_vclock_unregister(struct ptp_vclock *vclock);
  121. #endif