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