xref: /linux/drivers/ptp/ptp_private.h (revision 6f19b2c136d98a84d79030b53e23d405edfdc783)
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 
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>
18 #include <linux/list.h>
19 #include <linux/bitmap.h>
20 #include <linux/debugfs.h>
21 
22 #define PTP_MAX_TIMESTAMPS 128
23 #define PTP_BUF_TIMESTAMPS 30
24 #define PTP_DEFAULT_MAX_VCLOCKS 20
25 #define PTP_MAX_CHANNELS 2048
26 
27 struct timestamp_event_queue {
28 	struct ptp_extts_event buf[PTP_MAX_TIMESTAMPS];
29 	int head;
30 	int tail;
31 	spinlock_t lock;
32 	struct list_head qlist;
33 	unsigned long *mask;
34 	struct dentry *debugfs_instance;
35 	struct debugfs_u32_array dfs_bitmap;
36 };
37 
38 struct ptp_clock {
39 	struct posix_clock clock;
40 	struct device dev;
41 	struct ptp_clock_info *info;
42 	dev_t devid;
43 	int index; /* index into clocks.map */
44 	struct pps_device *pps_source;
45 	long dialed_frequency; /* remembers the frequency adjustment */
46 	struct list_head tsevqs; /* timestamp fifo list */
47 	struct mutex pincfg_mux; /* protect concurrent info->pin_config access */
48 	wait_queue_head_t tsev_wq;
49 	int defunct; /* tells readers to go away when clock is being removed */
50 	struct device_attribute *pin_dev_attr;
51 	struct attribute **pin_attr;
52 	struct attribute_group pin_attr_group;
53 	/* 1st entry is a pointer to the real group, 2nd is NULL terminator */
54 	const struct attribute_group *pin_attr_groups[2];
55 	struct kthread_worker *kworker;
56 	struct kthread_delayed_work aux_work;
57 	unsigned int max_vclocks;
58 	unsigned int n_vclocks;
59 	int *vclock_index;
60 	struct mutex n_vclocks_mux; /* protect concurrent n_vclocks access */
61 	bool is_virtual_clock;
62 	bool has_cycles;
63 	struct dentry *debugfs_root;
64 };
65 
66 #define info_to_vclock(d) container_of((d), struct ptp_vclock, info)
67 #define cc_to_vclock(d) container_of((d), struct ptp_vclock, cc)
68 #define dw_to_vclock(d) container_of((d), struct ptp_vclock, refresh_work)
69 
70 struct ptp_vclock {
71 	struct ptp_clock *pclock;
72 	struct ptp_clock_info info;
73 	struct ptp_clock *clock;
74 	struct hlist_node vclock_hash_node;
75 	struct cyclecounter cc;
76 	struct timecounter tc;
77 	struct mutex lock;	/* protects tc/cc */
78 };
79 
80 /*
81  * The function queue_cnt() is safe for readers to call without
82  * holding q->lock. Readers use this function to verify that the queue
83  * is nonempty before proceeding with a dequeue operation. The fact
84  * that a writer might concurrently increment the tail does not
85  * matter, since the queue remains nonempty nonetheless.
86  */
87 static inline int queue_cnt(struct timestamp_event_queue *q)
88 {
89 	int cnt = q->tail - q->head;
90 	return cnt < 0 ? PTP_MAX_TIMESTAMPS + cnt : cnt;
91 }
92 
93 /* Check if ptp virtual clock is in use */
94 static inline bool ptp_vclock_in_use(struct ptp_clock *ptp)
95 {
96 	bool in_use = false;
97 
98 	if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
99 		return true;
100 
101 	if (!ptp->is_virtual_clock && ptp->n_vclocks)
102 		in_use = true;
103 
104 	mutex_unlock(&ptp->n_vclocks_mux);
105 
106 	return in_use;
107 }
108 
109 /* Check if ptp clock shall be free running */
110 static inline bool ptp_clock_freerun(struct ptp_clock *ptp)
111 {
112 	if (ptp->has_cycles)
113 		return false;
114 
115 	return ptp_vclock_in_use(ptp);
116 }
117 
118 extern struct class *ptp_class;
119 
120 /*
121  * see ptp_chardev.c
122  */
123 
124 /* caller must hold pincfg_mux */
125 int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
126 		    enum ptp_pin_function func, unsigned int chan);
127 
128 long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd,
129 	       unsigned long arg);
130 
131 int ptp_open(struct posix_clock_context *pccontext, fmode_t fmode);
132 
133 int ptp_release(struct posix_clock_context *pccontext);
134 
135 ssize_t ptp_read(struct posix_clock_context *pccontext, uint flags, char __user *buf,
136 		 size_t cnt);
137 
138 __poll_t ptp_poll(struct posix_clock_context *pccontext, struct file *fp,
139 		  poll_table *wait);
140 
141 /*
142  * see ptp_sysfs.c
143  */
144 
145 extern const struct attribute_group *ptp_groups[];
146 
147 int ptp_populate_pin_groups(struct ptp_clock *ptp);
148 void ptp_cleanup_pin_groups(struct ptp_clock *ptp);
149 
150 struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock);
151 void ptp_vclock_unregister(struct ptp_vclock *vclock);
152 #endif
153