0001
0002
0003
0004
0005
0006
0007 #ifndef _PTP_PRIVATE_H_
0008 #define _PTP_PRIVATE_H_
0009
0010 #include <linux/cdev.h>
0011 #include <linux/device.h>
0012 #include <linux/kthread.h>
0013 #include <linux/mutex.h>
0014 #include <linux/posix-clock.h>
0015 #include <linux/ptp_clock.h>
0016 #include <linux/ptp_clock_kernel.h>
0017 #include <linux/time.h>
0018
0019 #define PTP_MAX_TIMESTAMPS 128
0020 #define PTP_BUF_TIMESTAMPS 30
0021 #define PTP_DEFAULT_MAX_VCLOCKS 20
0022
0023 struct timestamp_event_queue {
0024 struct ptp_extts_event buf[PTP_MAX_TIMESTAMPS];
0025 int head;
0026 int tail;
0027 spinlock_t lock;
0028 };
0029
0030 struct ptp_clock {
0031 struct posix_clock clock;
0032 struct device dev;
0033 struct ptp_clock_info *info;
0034 dev_t devid;
0035 int index;
0036 struct pps_device *pps_source;
0037 long dialed_frequency;
0038 struct timestamp_event_queue tsevq;
0039 struct mutex tsevq_mux;
0040 struct mutex pincfg_mux;
0041 wait_queue_head_t tsev_wq;
0042 int defunct;
0043 struct device_attribute *pin_dev_attr;
0044 struct attribute **pin_attr;
0045 struct attribute_group pin_attr_group;
0046
0047 const struct attribute_group *pin_attr_groups[2];
0048 struct kthread_worker *kworker;
0049 struct kthread_delayed_work aux_work;
0050 unsigned int max_vclocks;
0051 unsigned int n_vclocks;
0052 int *vclock_index;
0053 struct mutex n_vclocks_mux;
0054 bool is_virtual_clock;
0055 bool has_cycles;
0056 };
0057
0058 #define info_to_vclock(d) container_of((d), struct ptp_vclock, info)
0059 #define cc_to_vclock(d) container_of((d), struct ptp_vclock, cc)
0060 #define dw_to_vclock(d) container_of((d), struct ptp_vclock, refresh_work)
0061
0062 struct ptp_vclock {
0063 struct ptp_clock *pclock;
0064 struct ptp_clock_info info;
0065 struct ptp_clock *clock;
0066 struct hlist_node vclock_hash_node;
0067 struct cyclecounter cc;
0068 struct timecounter tc;
0069 spinlock_t lock;
0070 };
0071
0072
0073
0074
0075
0076
0077
0078
0079 static inline int queue_cnt(struct timestamp_event_queue *q)
0080 {
0081 int cnt = q->tail - q->head;
0082 return cnt < 0 ? PTP_MAX_TIMESTAMPS + cnt : cnt;
0083 }
0084
0085
0086 static inline bool ptp_vclock_in_use(struct ptp_clock *ptp)
0087 {
0088 bool in_use = false;
0089
0090 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
0091 return true;
0092
0093 if (!ptp->is_virtual_clock && ptp->n_vclocks)
0094 in_use = true;
0095
0096 mutex_unlock(&ptp->n_vclocks_mux);
0097
0098 return in_use;
0099 }
0100
0101
0102 static inline bool ptp_clock_freerun(struct ptp_clock *ptp)
0103 {
0104 if (ptp->has_cycles)
0105 return false;
0106
0107 return ptp_vclock_in_use(ptp);
0108 }
0109
0110 extern struct class *ptp_class;
0111
0112
0113
0114
0115
0116
0117 int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
0118 enum ptp_pin_function func, unsigned int chan);
0119
0120 long ptp_ioctl(struct posix_clock *pc,
0121 unsigned int cmd, unsigned long arg);
0122
0123 int ptp_open(struct posix_clock *pc, fmode_t fmode);
0124
0125 ssize_t ptp_read(struct posix_clock *pc,
0126 uint flags, char __user *buf, size_t cnt);
0127
0128 __poll_t ptp_poll(struct posix_clock *pc,
0129 struct file *fp, poll_table *wait);
0130
0131
0132
0133
0134
0135 extern const struct attribute_group *ptp_groups[];
0136
0137 int ptp_populate_pin_groups(struct ptp_clock *ptp);
0138 void ptp_cleanup_pin_groups(struct ptp_clock *ptp);
0139
0140 struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock);
0141 void ptp_vclock_unregister(struct ptp_vclock *vclock);
0142 #endif