1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * PTP 1588 clock support - private declarations for the core module.
5 * Copyright (C) 2010 OMICRON electronics GmbH
7 #ifndef _PTP_PRIVATE_H_
8 #define _PTP_PRIVATE_H_
10 #include <linux/cdev.h>
11 #include <linux/device.h>
12 #include <linux/kthread.h>
13 #include <linux/mutex.h>
14 #include <linux/posix-clock.h>
15 #include <linux/ptp_clock.h>
16 #include <linux/ptp_clock_kernel.h>
17 #include <linux/time.h>
19 #define PTP_MAX_TIMESTAMPS 128
20 #define PTP_BUF_TIMESTAMPS 30
22 struct timestamp_event_queue {
23 struct ptp_extts_event buf[PTP_MAX_TIMESTAMPS];
30 struct posix_clock clock;
32 struct ptp_clock_info *info;
34 int index; /* index into clocks.map */
35 struct pps_device *pps_source;
36 long dialed_frequency; /* remembers the frequency adjustment */
37 struct timestamp_event_queue tsevq; /* simple fifo for time stamps */
38 struct mutex tsevq_mux; /* one process at a time reading the fifo */
39 struct mutex pincfg_mux; /* protect concurrent info->pin_config access */
40 wait_queue_head_t tsev_wq;
41 int defunct; /* tells readers to go away when clock is being removed */
42 struct device_attribute *pin_dev_attr;
43 struct attribute **pin_attr;
44 struct attribute_group pin_attr_group;
45 /* 1st entry is a pointer to the real group, 2nd is NULL terminator */
46 const struct attribute_group *pin_attr_groups[2];
47 struct kthread_worker *kworker;
48 struct kthread_delayed_work aux_work;
52 * The function queue_cnt() is safe for readers to call without
53 * holding q->lock. Readers use this function to verify that the queue
54 * is nonempty before proceeding with a dequeue operation. The fact
55 * that a writer might concurrently increment the tail does not
56 * matter, since the queue remains nonempty nonetheless.
58 static inline int queue_cnt(const struct timestamp_event_queue *q)
61 * Paired with WRITE_ONCE() in enqueue_external_timestamp(),
62 * ptp_read(), extts_fifo_show().
64 int cnt = READ_ONCE(q->tail) - READ_ONCE(q->head);
65 return cnt < 0 ? PTP_MAX_TIMESTAMPS + cnt : cnt;
72 /* caller must hold pincfg_mux */
73 int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
74 enum ptp_pin_function func, unsigned int chan);
76 long ptp_ioctl(struct posix_clock *pc,
77 unsigned int cmd, unsigned long arg);
79 int ptp_open(struct posix_clock *pc, fmode_t fmode);
81 ssize_t ptp_read(struct posix_clock *pc,
82 uint flags, char __user *buf, size_t cnt);
84 __poll_t ptp_poll(struct posix_clock *pc,
85 struct file *fp, poll_table *wait);
91 extern const struct attribute_group *ptp_groups[];
93 int ptp_populate_pin_groups(struct ptp_clock *ptp);
94 void ptp_cleanup_pin_groups(struct ptp_clock *ptp);